What You Will Learn
Beginner
- What ServiceAccounts are
- How to create and use them
- RBAC with ServiceAccounts
A ServiceAccount is an identity for Pods. When a Pod needs to talk to the K8s API (e.g., to list other Pods), it uses a ServiceAccount.
Terminalbash
# Create a ServiceAccount
kubectl create serviceaccount my-app-sa
# Use in a Pod
spec:
serviceAccountName: my-app-sa
containers:
- name: app
image: my-appDefault ServiceAccount
Every Pod gets the 'default' ServiceAccount if none is specified. It has minimal permissions. For apps that need API access, create a dedicated ServiceAccount with specific RBAC.Practical Exercise
Create a ServiceAccount: kubectl create sa my-sa
Create a Pod with serviceAccountName: my-sa
Exec in: kubectl exec -it pod -- cat /var/run/secrets/kubernetes.io/serviceaccount/token
Create a Role + RoleBinding for this ServiceAccount
Test: the Pod can now access the API
Key Takeaways
- ServiceAccount = identity for Pods.
- Every Pod gets 'default' SA if not specified.
- Create dedicated SA for apps that access K8s API.
- Combine with RBAC (Role + RoleBinding).
- Token is mounted at /var/run/secrets/kubernetes.io/serviceaccount/.
Comments
Comments
Post a Comment