Newsletter
TechAnV Blog
Get updates on security engineering, Rust, eBPF, and DevSecOps. No spam, unsubscribe anytime.
Check your inbox and click the confirmation link to complete your subscription.
In our journey through Kubernetes RBAC, we’ve covered Roles, RoleBindings, ClusterRoles, and ClusterRoleBindings. Today, we’re diving into another crucial component of Kubernetes security: Service Accounts.
What are Service Accounts?#
In Kubernetes, there are two types of accounts:
- User Accounts: Used by humans (admins, developers, operators)
- Service Accounts: Used by processes running in pods to interact with the Kubernetes API
Service Accounts are crucial for applications running within your cluster that need to interact with the Kubernetes API or other services.
Creating and Managing Service Accounts#
Let’s start with the basics:
Creating a Service Account#
1kubectl create serviceaccount my-app-saViewing Service Accounts#
1kubectl get serviceaccounts2# or3kubectl get saHands-on: Using Service Accounts with RBAC#
Let’s walk through a practical example of creating and using a Service Account with RBAC.
Step 1: Create a Service Account#
1kubectl create serviceaccount pod-reader-saStep 2: Create a Role#
1apiVersion: rbac.authorization.k8s.io/v12kind: Role3metadata:4 namespace: default5 name: pod-reader6rules:7 - apiGroups: [""]8 resources: ["pods"]9 verbs: ["get", "watch", "list"]Save this as pod-reader-role.yaml and apply:
1kubectl apply -f pod-reader-role.yamlStep 3: Create a RoleBinding#
1apiVersion: rbac.authorization.k8s.io/v12kind: RoleBinding3metadata:4 name: read-pods5 namespace: default6subjects:7 - kind: ServiceAccount8 name: pod-reader-sa9 namespace: default10roleRef:11 kind: Role12 name: pod-reader13 apiGroup: rbac.authorization.k8s.ioSave this as pod-reader-rolebinding.yaml and apply:
1kubectl apply -f pod-reader-rolebinding.yamlStep 4: Use the Service Account in a Pod#
Now, let’s create a pod that uses this Service Account:
1apiVersion: v12kind: Pod3metadata:4 name: pod-reader-pod5spec:6 serviceAccountName: pod-reader-sa7 containers:8 - name: my-container9 image: busybox10 command: ["sh", "-c", "while true; do kubectl get pods; sleep 10; done"]Save this as pod-reader-pod.yaml and apply:
1kubectl apply -f pod-reader-pod.yamlThis pod will use the pod-reader-sa Service Account, which has permissions to list pods in the default namespace.
Key Takeaways#
- Automation-Friendly: Service Accounts are ideal for automated processes and applications running in your cluster.
- Namespace Scoped: Unlike ClusterRoles, Service Accounts are namespace-specific.
- Default Service Account: Each namespace has a default Service Account, but it’s best practice to create specific ones for your applications.
- Token Authentication: Service Accounts use tokens for authentication, which are automatically mounted into pods.
- RBAC Integration: Service Accounts can be bound to Roles or ClusterRoles just like user accounts.
Best Practices#
- Least Privilege: Always grant the minimum necessary permissions to your Service Accounts.
- Unique Accounts: Create separate Service Accounts for different applications or components.
- Regular Audits: Periodically review your Service Accounts and their permissions.
- Avoid Using Default: Don’t rely on the default Service Account; create specific ones for your needs.
- Rotate Tokens: Regularly rotate Service Account tokens for enhanced security.
Conclusion#
Service Accounts are a fundamental part of Kubernetes security and RBAC. They allow you to grant specific permissions to processes running within your cluster, enabling secure interactions between your applications and the Kubernetes API. By understanding and properly implementing Service Accounts, you can significantly enhance the security posture of your Kubernetes deployments.
This blog post template provides a comprehensive look at Service Accounts in Kubernetes, based on the task you described. It includes practical examples, explanations of key concepts, and important takeaways. The content is structured to be both informative and hands-on, suitable for readers who want to understand and implement Service Accounts in their Kubernetes environments.