2017-04-03 19:31:32 +00:00
---
2018-02-18 19:29:37 +00:00
reviewers:
2017-04-03 19:31:32 +00:00
- bprashanth
- liggitt
- thockin
2017-06-08 18:48:28 +00:00
title: Configure Service Accounts for Pods
2018-06-22 18:20:04 +00:00
content_template: templates/task
2018-05-20 04:43:52 +00:00
weight: 90
2017-04-03 19:31:32 +00:00
---
2018-06-22 18:20:04 +00:00
{{% capture overview %}}
2017-04-03 19:31:32 +00:00
A service account provides an identity for processes that run in a Pod.
*This is a user introduction to Service Accounts. See also the
2018-07-18 22:25:38 +00:00
[Cluster Admin Guide to Service Accounts ](/docs/reference/access-authn-authz/service-accounts-admin/ ).*
2017-04-03 19:31:32 +00:00
2018-05-05 16:00:51 +00:00
{{< note > }}
2017-08-17 23:19:24 +00:00
**Note:** This document describes how service accounts behave in a cluster set up
2017-04-03 19:31:32 +00:00
as recommended by the Kubernetes project. Your cluster administrator may have
customized the behavior in your cluster, in which case this documentation may
2017-08-17 23:19:24 +00:00
not apply.
2018-05-05 16:00:51 +00:00
{{< / note > }}
2017-04-03 19:31:32 +00:00
2017-10-13 08:23:07 +00:00
When you (a human) access the cluster (for example, using `kubectl` ), you are
2017-04-03 19:31:32 +00:00
authenticated by the apiserver as a particular User Account (currently this is
usually `admin` , unless your cluster administrator has customized your
cluster). Processes in containers inside pods can also contact the apiserver.
2017-10-13 08:23:07 +00:00
When they do, they are authenticated as a particular Service Account (for example,
2017-04-03 19:31:32 +00:00
`default` ).
2018-06-22 18:20:04 +00:00
{{% /capture %}}
{{< toc > }}
{{% capture prerequisites %}}
{{< include " task-tutorial-prereqs . md " > }} {{< version-check > }}
{{% /capture %}}
{{% capture steps %}}
2017-06-08 18:48:28 +00:00
## Use the Default Service Account to access the API server.
2017-04-03 19:31:32 +00:00
2017-04-04 20:24:15 +00:00
When you create a pod, if you do not specify a service account, it is
automatically assigned the `default` service account in the same namespace.
2017-10-13 08:23:07 +00:00
If you get the raw json or yaml for a pod you have created (for example, `kubectl get pods/podname -o yaml` ),
2017-04-04 20:24:15 +00:00
you can see the `spec.serviceAccountName` field has been
2017-04-03 19:31:32 +00:00
[automatically set ](/docs/user-guide/working-with-resources/#resources-are-automatically-modified ).
2017-04-04 20:24:15 +00:00
You can access the API from inside a pod using automatically mounted service account credentials,
2017-04-03 19:31:32 +00:00
as described in [Accessing the Cluster ](/docs/user-guide/accessing-the-cluster/#accessing-the-api-from-a-pod ).
2018-07-18 22:07:24 +00:00
The API permissions of the service account depend on the [authorization plugin and policy ](/docs/reference/access-authn-authz/authorization/#authorization-modules ) in use.
2017-04-04 20:24:15 +00:00
In version 1.6+, you can opt out of automounting API credentials for a service account by setting
`automountServiceAccountToken: false` on the service account:
```yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: build-robot
automountServiceAccountToken: false
...
```
In version 1.6+, you can also opt out of automounting API credentials for a particular pod:
```yaml
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
serviceAccountName: build-robot
automountServiceAccountToken: false
...
```
The pod spec takes precedence over the service account if both specify a `automountServiceAccountToken` value.
2017-04-03 19:31:32 +00:00
2017-06-08 18:48:28 +00:00
## Use Multiple Service Accounts.
2017-04-03 19:31:32 +00:00
Every namespace has a default service account resource called `default` .
You can list this and any other serviceAccount resources in the namespace with this command:
```shell
2018-07-20 23:04:57 +00:00
kubectl get serviceAccounts
2017-04-03 19:31:32 +00:00
NAME SECRETS AGE
default 1 1d
```
2017-04-04 20:24:15 +00:00
You can create additional ServiceAccount objects like this:
2017-04-03 19:31:32 +00:00
```shell
2018-07-20 23:04:57 +00:00
kubectl create -f - < < EOF
2017-04-03 19:31:32 +00:00
apiVersion: v1
kind: ServiceAccount
metadata:
name: build-robot
EOF
2018-08-30 05:48:18 +00:00
serviceaccount/build-robot created
2017-04-03 19:31:32 +00:00
```
If you get a complete dump of the service account object, like this:
```shell
2018-07-20 23:04:57 +00:00
kubectl get serviceaccounts/build-robot -o yaml
2017-04-03 19:31:32 +00:00
apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: 2015-06-16T00:12:59Z
name: build-robot
namespace: default
resourceVersion: "272500"
selfLink: /api/v1/namespaces/default/serviceaccounts/build-robot
uid: 721ab723-13bc-11e5-aec2-42010af0021e
secrets:
- name: build-robot-token-bvbk5
```
then you will see that a token has automatically been created and is referenced by the service account.
2018-07-27 19:12:56 +00:00
You may use authorization plugins to [set permissions on service accounts ](/docs/reference/access-authn-authz/rbac/#service-account-permissions ).
2017-04-03 19:31:32 +00:00
2017-04-04 20:24:15 +00:00
To use a non-default service account, simply set the `spec.serviceAccountName`
2017-04-03 19:31:32 +00:00
field of a pod to the name of the service account you wish to use.
The service account has to exist at the time the pod is created, or it will be rejected.
You cannot update the service account of an already created pod.
You can clean up the service account from this example like this:
```shell
2018-07-20 23:04:57 +00:00
kubectl delete serviceaccount/build-robot
2017-04-03 19:31:32 +00:00
```
## Manually create a service account API token.
Suppose we have an existing service account named "build-robot" as mentioned above, and we create
a new secret manually.
```shell
2018-07-20 23:04:57 +00:00
kubectl create -f - < < EOF
2017-04-03 19:31:32 +00:00
apiVersion: v1
kind: Secret
metadata:
name: build-robot-secret
2017-07-28 15:23:11 +00:00
annotations:
2017-04-03 19:31:32 +00:00
kubernetes.io/service-account.name: build-robot
type: kubernetes.io/service-account-token
EOF
2018-08-30 05:48:18 +00:00
secret/build-robot-secret created
2017-04-03 19:31:32 +00:00
```
Now you can confirm that the newly built secret is populated with an API token for the "build-robot" service account.
Any tokens for non-existent service accounts will be cleaned up by the token controller.
```shell
2018-07-20 23:04:57 +00:00
kubectl describe secrets/build-robot-secret
2017-09-26 18:40:39 +00:00
Name: build-robot-secret
Namespace: default
Labels: < none >
Annotations: kubernetes.io/service-account.name=build-robot
kubernetes.io/service-account.uid=da68f9c6-9d26-11e7-b84e-002dc52800da
2017-04-03 19:31:32 +00:00
2017-09-26 18:40:39 +00:00
Type: kubernetes.io/service-account-token
2017-04-03 19:31:32 +00:00
Data
====
2017-09-26 18:40:39 +00:00
ca.crt: 1338 bytes
namespace: 7 bytes
token: ...
2017-04-03 19:31:32 +00:00
```
2018-05-05 16:00:51 +00:00
{{< note > }}
2017-08-17 23:19:24 +00:00
**Note:** The content of `token` is elided here.
2018-05-05 16:00:51 +00:00
{{< / note > }}
2017-04-03 19:31:32 +00:00
2017-06-08 18:48:28 +00:00
## Add ImagePullSecrets to a service account
2017-04-03 19:31:32 +00:00
2017-08-16 09:26:05 +00:00
First, create an imagePullSecret, as described [here ](/docs/concepts/containers/images/#specifying-imagepullsecrets-on-a-pod ).
2017-04-03 19:31:32 +00:00
Next, verify it has been created. For example:
```shell
2018-07-20 23:04:57 +00:00
kubectl get secrets myregistrykey
2017-04-03 19:31:32 +00:00
NAME TYPE DATA AGE
myregistrykey kubernetes.io/.dockerconfigjson 1 1d
```
2017-06-14 14:26:47 +00:00
Next, modify the default service account for the namespace to use this secret as an imagePullSecret.
2017-04-03 19:31:32 +00:00
```shell
2018-09-20 11:53:38 +00:00
kubectl patch serviceaccount default -p '{"imagePullSecrets": [{"name": "myregistrykey"}]}'
2017-04-03 19:31:32 +00:00
```
Interactive version requiring manual edit:
2017-08-17 23:19:24 +00:00
2017-04-03 19:31:32 +00:00
```shell
2018-07-20 23:04:57 +00:00
kubectl get serviceaccounts default -o yaml > ./sa.yaml
cat sa.yaml
2017-04-03 19:31:32 +00:00
apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: 2015-08-07T22:02:39Z
name: default
namespace: default
resourceVersion: "243024"
selfLink: /api/v1/namespaces/default/serviceaccounts/default
uid: 052fb0f4-3d50-11e5-b066-42010af0d7b6
secrets:
- name: default-token-uudge
2018-07-20 23:04:57 +00:00
vi sa.yaml
2017-04-03 19:31:32 +00:00
[editor session not shown]
[delete line with key "resourceVersion"]
2018-04-10 15:59:10 +00:00
[add lines with "imagePullSecrets:"]
2018-07-20 23:04:57 +00:00
cat sa.yaml
2017-04-03 19:31:32 +00:00
apiVersion: v1
kind: ServiceAccount
metadata:
creationTimestamp: 2015-08-07T22:02:39Z
name: default
namespace: default
selfLink: /api/v1/namespaces/default/serviceaccounts/default
uid: 052fb0f4-3d50-11e5-b066-42010af0d7b6
secrets:
- name: default-token-uudge
imagePullSecrets:
- name: myregistrykey
2018-07-20 23:04:57 +00:00
kubectl replace serviceaccount default -f ./sa.yaml
2017-04-03 19:31:32 +00:00
serviceaccounts/default
```
Now, any new pods created in the current namespace will have this added to their spec:
```yaml
spec:
imagePullSecrets:
- name: myregistrykey
```
<!-- ## Adding Secrets to a service account.
TODO: Test and explain how to use additional non-K8s secrets with an existing service account.
2017-04-11 04:09:31 +00:00
-->
2018-06-22 18:20:04 +00:00
2018-06-29 01:48:20 +00:00
## Service Account Volume Projection
Kubernetes 1.11 and higher supports a new way to project a service account token into a Pod.
You can specify a token request with audiences, expirationSeconds. The service account token
becomes invalid when the Pod is deleted. A Projected Volume named
[ServiceAccountToken ](/docs/concepts/storage/volumes/#projected ) requests and stores the token.
{{% /capture %}}