How to Configure a Kubernetes Pod with a ServiceAccount
The DevOpscube team is deploying a microservice that needs to interact with Kubernetes API resources. The application must run using a dedicated ServiceAccount rather than the default identity.
Your task is to create a ServiceAccount named devopscube-sa in the cluster.
Create a Pod named devopscube-pod using the nginx:alpine-perl image and expose container port 80. Configure the Pod to use the devopscube-sa ServiceAccount.
Verify that:
- The ServiceAccount exists.
devopscube-podis in the Running state.- The Pod is using
devopscube-saas its ServiceAccount. - The Pod exposes port
80.
Service Account,pods refer to the official Kubernetes documentation.Create the ServiceAccount
A ServiceAccount provides an identity that Pods can use to authenticate with the Kubernetes API.
Create a ServiceAccount named devopscube-sa:
$ kubectl create serviceaccount devopscube-sa
serviceaccount/devopscube-sa createdCreate the Pod Manifest
Generate the Pod manifest without creating the Pod:
kubectl run devopscube-pod \
--image=nginx:alpine-perl \
--port=80 \
--dry-run=client -o yaml > pod.yamlEdit pod.yaml and add the ServiceAccount under the Pod's spec:
This configures the Pod to run using the devopscube-sa ServiceAccount.
devopscube-sa.Apply the Pod
Apply the manifest to create the Pod:
$ kubectl apply -f pod.yaml
pod/devopscube-pod createdCheck the Pod status:
$ kubectl get pod devopscube-pod
NAME READY STATUS RESTARTS AGE
devopscube-pod 1/1 Running 0 36sThe Pod should reach the Running state and use the devopscube-sa ServiceAccount.
Verify the ServiceAccount
Verify that the Pod is using the devopscube-sa ServiceAccount:
$ kubectl get pod devopscube-pod \
-o jsonpath='{.spec.serviceAccountName}{"\n"}'
devopscube-saYou can also use:
$ kubectl describe pod devopscube-pod | grep -i "Service Account"
Service Account: devopscube-saThis confirms that the Pod is associated with the correct ServiceAccount.
Why use serviceAccountName?
It tells Kubernetes which ServiceAccount identity the Pod should use. A ServiceAccount by itself does not grant permissions; RBAC Roles and RoleBindings are used when the application needs specific API permissions.
Delete the Pod and ServiceAccount
After completing the verification, delete the Pod:
kubectl delete pod devopscube-podDelete the ServiceAccount:
kubectl delete serviceaccount devopscube-saVerify that the ServiceAccount no longer exists:
kubectl get serviceaccount devopscube-saYou should see a NotFound error, confirming that the ServiceAccount has been deleted.
CKA Exam Tips
These tips will help you quickly identify the important Kubernetes concepts and avoid common mistakes during the exam.
1. Remember serviceAccountName
A Pod uses a specific ServiceAccount through the serviceAccountName field:
spec:
serviceAccountName: devopscube-sa
2. Create a ServiceAccount Quickly
You can create a ServiceAccount with:
kubectl create serviceaccount devopscube-sa
3. Verify the Association with JSONPath
Use JSONPath to quickly check which ServiceAccount the Pod uses:
kubectl get pod techiescamp-pod \
-o jsonpath='{.spec.serviceAccountName}{"\n"}'
4. Don't Confuse ServiceAccounts with Permissions
A ServiceAccount provides an identity for a Pod, but it does not automatically grant permissions. Access to Kubernetes resources is typically configured using RBAC.