Creating and Managing Service Accounts
Creating Service Accounts
You can create a new Service Account using the Kubernetes command-line interface (kubectl) or by defining a Service Account resource in a YAML file.
To create a Service Account using kubectl, run the following command:
kubectl create serviceaccount my-service-account
This will create a new Service Account named "my-service-account" in the current namespace.
Alternatively, you can create a Service Account by defining it in a YAML file:
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-service-account
Save this file (e.g., service-account.yaml
) and apply it to your Kubernetes cluster using the following command:
kubectl apply -f service-account.yaml
Managing Service Accounts
Once you have created a Service Account, you can manage its associated secrets, tokens, and other properties using kubectl commands.
To list all the Service Accounts in a namespace, run:
kubectl get serviceaccounts
To describe a specific Service Account, run:
kubectl describe serviceaccount my-service-account
This will show you the details of the Service Account, including the associated secrets and tokens.
To delete a Service Account, run:
kubectl delete serviceaccount my-service-account
This will remove the Service Account and all its associated resources from the Kubernetes cluster.
Automounting Service Account Credentials
By default, Kubernetes automatically mounts the Service Account token into the container's file system, allowing the processes running within the pod to access the Kubernetes API. You can disable this behavior by setting the automountServiceAccountToken
field to false
in the Pod or ServiceAccount specification.
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
serviceAccountName: my-service-account
automountServiceAccountToken: false
containers:
- name: my-container
image: my-image
This will prevent the Service Account token from being automatically mounted into the container, and you will need to manually manage the token if your application requires access to the Kubernetes API.