Skip to main content

Cluster Architecture, Installation & Configuration

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-pod is in the Running state.
  • The Pod is using devopscube-sa as its ServiceAccount.
  • The Pod exposes port 80.
📚
For more details about 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 created

Create 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.yaml

Edit pod.yaml and add the ServiceAccount under the Pod's spec:

pod.yaml
apiVersion: v1 kind: Pod metadata: name: devopscube-pod labels: run: devopscube-pod spec: serviceAccountName: devopscube-sa containers: - name: devopscube-pod image: nginx:alpine-perl ports: - containerPort: 80

This configures the Pod to run using the devopscube-sa ServiceAccount.

💡
Important: The ServiceAccount name must exactly match the ServiceAccount you created: devopscube-sa.

Apply the Pod

Apply the manifest to create the Pod:

$  kubectl apply -f pod.yaml

pod/devopscube-pod created

Check the Pod status:

$ kubectl get pod devopscube-pod
NAME             READY   STATUS    RESTARTS   AGE
devopscube-pod   1/1     Running   0          36s

The 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-sa

You can also use:

$ kubectl describe pod devopscube-pod | grep -i "Service Account"

Service Account:  devopscube-sa

This 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.

🤔
How to delete the Pod and the ServiceAccount.

Delete the Pod and ServiceAccount

After completing the verification, delete the Pod:

kubectl delete pod devopscube-pod

Delete the ServiceAccount:

kubectl delete serviceaccount devopscube-sa

Verify that the ServiceAccount no longer exists:

kubectl get serviceaccount devopscube-sa

You 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.