peter (admin)   -  



 

Service Accounts in Kubernetes

Here we will learn how service accounts work in Kubernetes and how we assign a service account to a pod so it can securely communicate with the Kubernetes API and perform only the actions that are allowed to it.

A ServiceAccount in Kubernetes is an identity that is used by pods instead of human users. Applications running inside pods use this identity to communicate with the Kubernetes API.

When we say identity, it means who is making the request in Kubernetes. Every action must come from someone, either a human user or a pod, and Kubernetes needs a way to recognize that source.

A ServiceAccount gives that identity to a pod so Kubernetes can know which application is sending the request.

For example, if a pod wants to read pod data from the cluster, it sends the request using its ServiceAccount. Kubernetes identifies the ServiceAccount and then applies the relevant RBAC rules.

In simple terms, identity means a name or label that Kubernetes uses to recognize who is trying to access the system.

Purpose of Service Accounts

The purpose of a ServiceAccount is to provide controlled and secure access for applications so they can perform only the actions they are allowed to perform in Kubernetes.

This is important because applications often need to read or interact with cluster resources such as pods or ConfigMaps, but we do not want to give them full access.

ServiceAccounts allow us to apply RBAC rules to applications so each pod gets limited permissions based on its role.

This makes the system more secure because even if a pod is compromised, it cannot automatically access everything in the cluster.

Service Accounts and RBAC

ServiceAccounts and RBAC work together, but they are not the same thing.

A ServiceAccount is the identity used by a pod, while RBAC is the permission system that decides what that identity can do.

A ServiceAccount tells Kubernetes who is making the request, while RBAC tells Kubernetes what that user or ServiceAccount is allowed to do.

For example, when a pod wants to communicate with the Kubernetes API, it uses a ServiceAccount as its identity. RBAC rules then check that identity and decide whether it can read pods, create resources, or perform no action.

In simple terms:

  • ServiceAccount: The identity used by the pod.

  • RBAC: The rules that define what that identity can access.

Create the Namespace

Let's understand this concept practically.

For this, we are already logged into both the master node and the worker node.

To verify the cluster, run:

kubectl get nodes

Both the master and worker should be in the Ready state, which means the cluster is up and both nodes are properly connected and working.

Now create a namespace named deep:

kubectl create namespace deep

This allows us to create and manage ServiceAccounts and their permissions in one place.

Create the ServiceAccount

Create a ServiceAccount named app-sa in the deep namespace:

kubectl create serviceaccount app-sa -n deep

We create this ServiceAccount because we want to give a pod its own identity so it can securely communicate with the Kubernetes API and perform only the actions allowed through RBAC rules.

Verify the ServiceAccount:

kubectl get serviceaccounts -n deep

The app-sa ServiceAccount should be listed along with the default ServiceAccount.

This confirms that our new identity for the pod is ready to be used.

Create the ServiceAccount Role

Now create a YAML file named sa-role.yaml.

This file defines what permissions the ServiceAccount will have, allowing us to control what actions a pod using this ServiceAccount can perform when communicating with the Kubernetes API.

The configuration is:

apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: sa-reader
  namespace: deep
rules:
  - apiGroups: [""]
    resources: ["pods"]
    verbs: ["get", "list"]

The apiVersion shows that we are using RBAC authorization.

The kind defines that we are creating a Role, meaning the permissions apply only inside the deep namespace.

Inside metadata, we define the namespace as deep and give the role the name sa-reader.

In the rules section, we define the permissions.

The apiGroups field is empty because pods belong to the core API group. In resources, we specify pods, meaning this role applies only to pod resources.

The verbs are get and list.

This means the ServiceAccount can view pod details and list pods, but it cannot create, update, or delete pods.

The permission is therefore read-only access to pods.

Save the file and exit.

Apply the Role

Apply the role:

kubectl apply -f sa-role.yaml

Kubernetes will create the role containing the read-only permissions for pods.

Create the RoleBinding

Next, create a RoleBinding that connects the sa-reader role with the app-sa ServiceAccount inside the deep namespace.

The configuration is:

apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: sa-reader-binding
  namespace: deep
subjects:
  - kind: ServiceAccount
    name: app-sa
    namespace: deep
roleRef:
  kind: Role
  name: sa-reader
  apiGroup: rbac.authorization.k8s.io

Apply the RoleBinding:

kubectl apply -f sa-role-binding.yaml

This connects the sa-reader role with the app-sa ServiceAccount.

The ServiceAccount now has read-only permissions on pods.

Any pod using this ServiceAccount can get and list pods but cannot create, update, or delete them.

Create a Pod Using the ServiceAccount

Now create a YAML file named sa-pod.yaml.

We want to create a pod and assign the ServiceAccount to it so we can test how the pod uses this identity to access the Kubernetes API.

The configuration is:

apiVersion: v1
kind: Pod
metadata:
  name: sa-demo
  namespace: deep
spec:
  serviceAccountName: app-sa
  containers:
    - name: nginx
      image: nginx

Inside metadata, we give the pod the name sa-demo and define the namespace as deep.

This means the pod runs in the same namespace where our ServiceAccount and role are defined.

The important part is:

serviceAccountName: app-sa

This assigns the app-sa ServiceAccount to the pod.

The pod will therefore use this identity when communicating with the Kubernetes API.

The container uses the NGINX image so it can run.

This setup means the pod is not using the default identity. Instead, it is using the ServiceAccount we created.

Because of this, it follows the RBAC permissions assigned earlier, which provide read-only access to pods.

Save the file and exit.

Apply the Pod Configuration

Apply the configuration:

kubectl apply -f sa-pod.yaml

Verify the pod:

kubectl get pods -n deep

The sa-demo pod should be in the Running state.

This means it is ready to use the ServiceAccount to communicate with the Kubernetes API according to its assigned permissions.

View the ServiceAccount Credentials

Now go inside the pod so we can check how the ServiceAccount is working:

kubectl exec -it sa-demo -n deep -- /bin/bash

Inside the pod, move to:

/var/run/secrets/kubernetes.io/serviceaccount

Kubernetes automatically creates this directory and attaches ServiceAccount credentials to the pod.

You can see files such as:

ca.crt
namespace
token

These files contain information used by the pod to communicate with the Kubernetes API.

The token file is used by the pod to authenticate with the Kubernetes API.

The ca.crt file is used to verify secure communication.

The namespace file shows which namespace the pod is running in.

This means the pod does not need a manual login. It automatically uses the ServiceAccount identity to communicate securely with the cluster.

Whenever this pod makes a request to the Kubernetes API, it uses the ServiceAccount credentials and Kubernetes checks the RBAC permissions assigned to that ServiceAccount before allowing the action.

Use the ServiceAccount Token

Now create a request to test whether the pod can communicate with the Kubernetes API using the ServiceAccount and its permissions.

First, store the ServiceAccount token in a variable:

TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)

The token comes from the ServiceAccount attached to the pod. Kubernetes automatically creates and mounts this token inside the pod at:

/var/run/secrets/kubernetes.io/serviceaccount/token

We are simply reading it from that location and storing it in a variable so it can be used for authentication when making a request to the Kubernetes API.

Next, store the CA certificate path:

CACERT=/var/run/secrets/kubernetes.io/serviceaccount/ca.crt

The CA certificate is used to verify secure communication with the Kubernetes API.

This allows the pod to verify that it is communicating with the correct API server.

Send a Request to the Kubernetes API

Now send a request from inside the pod to the Kubernetes API server to get the list of pods in the deep namespace.

The request uses the CA certificate to establish a secure and trusted connection and includes an authorization header containing the ServiceAccount token.

The request can be made using:

curl --cacert "$CACERT" 
  -H "Authorization: Bearer $TOKEN" 
  https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/api/v1/namespaces/deep/pods

The API server address uses environment variables that are automatically available inside the pod. These point to the Kubernetes service host and port.

The API path specifies that we want the API server to return the list of pods in the deep namespace.

This demonstrates how a pod uses its ServiceAccount token to securely communicate with the Kubernetes API and request information based on its assigned permissions.

Verify the API Response

After sending the request, the API should return a response successfully.

This means the request sent using curl reached the Kubernetes API and was accepted.

The ServiceAccount token was valid, and the API server recognized the identity of the pod.

Because the request did not return a Forbidden or other authorization error, it confirms that the assigned permission is working correctly.

The pod was able to send a request using the ServiceAccount token, the API server verified the identity and permissions, and the API returned the requested information.

This demonstrates that authentication using the ServiceAccount and authorization using RBAC are working together successfully.

 


Comments (0)

There are no comments yet. Be the first to comment.

Log in or register to leave a comment. all comments are subject to admin approval