2015-07-12 04:04:52 +00:00
<!-- BEGIN MUNGE: UNVERSIONED_WARNING -->
<!-- BEGIN STRIP_FOR_RELEASE -->
2015-07-16 17:02:26 +00:00
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
< img src = "http://kubernetes.io/img/warning.png" alt = "WARNING"
width="25" height="25">
2015-07-13 22:15:35 +00:00
2015-07-16 17:02:26 +00:00
< h2 > PLEASE NOTE: This document applies to the HEAD of the source tree< / h2 >
2015-07-12 04:04:52 +00:00
2015-07-16 17:02:26 +00:00
If you are using a released version of Kubernetes, you should
refer to the docs that go with that version.
2015-07-12 04:04:52 +00:00
2015-12-14 18:37:38 +00:00
<!-- TAG RELEASE_LINK, added by the munger automatically -->
2015-07-16 17:02:26 +00:00
< strong >
2015-11-03 18:17:57 +00:00
The latest release of this document can be found
[here ](http://releases.k8s.io/release-1.1/docs/user-guide/secrets.md ).
2015-07-16 17:02:26 +00:00
Documentation for other releases can be found at
[releases.k8s.io ](http://releases.k8s.io ).
< / strong >
--
2015-07-13 22:15:35 +00:00
2015-07-12 04:04:52 +00:00
<!-- END STRIP_FOR_RELEASE -->
<!-- END MUNGE: UNVERSIONED_WARNING -->
2015-07-17 22:35:41 +00:00
2015-04-10 17:35:03 +00:00
# Secrets
2015-03-17 17:58:02 +00:00
Objects of type `secret` are intended to hold sensitive information, such as
passwords, OAuth tokens, and ssh keys. Putting this information in a `secret`
is safer and more flexible than putting it verbatim in a `pod` definition or in
2015-07-24 21:52:18 +00:00
a docker image. See [Secrets design document ](../design/secrets.md ) for more information.
2015-03-17 17:58:02 +00:00
2015-07-13 17:57:44 +00:00
**Table of Contents**
<!-- BEGIN MUNGE: GENERATED_TOC -->
2015-07-17 16:20:19 +00:00
2015-07-13 17:57:44 +00:00
- [Secrets ](#secrets )
- [Overview of Secrets ](#overview-of-secrets )
2015-08-14 01:25:46 +00:00
- [Service Accounts Automatically Create and Attach Secrets with API Credentials ](#service-accounts-automatically-create-and-attach-secrets-with-api-credentials )
2015-07-13 17:57:44 +00:00
- [Creating a Secret Manually ](#creating-a-secret-manually )
- [Manually specifying a Secret to be Mounted on a Pod ](#manually-specifying-a-secret-to-be-mounted-on-a-pod )
- [Manually specifying an imagePullSecret ](#manually-specifying-an-imagepullsecret )
2015-08-14 01:25:46 +00:00
- [Arranging for imagePullSecrets to be Automatically Attached ](#arranging-for-imagepullsecrets-to-be-automatically-attached )
- [Automatic Mounting of Manually Created Secrets ](#automatic-mounting-of-manually-created-secrets )
2015-07-13 17:57:44 +00:00
- [Details ](#details )
- [Restrictions ](#restrictions )
- [Consuming Secret Values ](#consuming-secret-values )
- [Secret and Pod Lifetime interaction ](#secret-and-pod-lifetime-interaction )
- [Use cases ](#use-cases )
2015-07-17 00:56:56 +00:00
- [Use-Case: Pod with ssh keys ](#use-case-pod-with-ssh-keys )
- [Use-Case: Pods with prod / test credentials ](#use-case-pods-with-prod--test-credentials )
- [Use-case: Secret visible to one container in a pod ](#use-case-secret-visible-to-one-container-in-a-pod )
2015-07-13 17:57:44 +00:00
- [Security Properties ](#security-properties )
- [Protections ](#protections )
- [Risks ](#risks )
<!-- END MUNGE: GENERATED_TOC -->
2015-05-21 20:25:20 +00:00
## Overview of Secrets
2015-03-17 17:58:02 +00:00
2015-08-14 01:25:46 +00:00
A Secret is an object that contains a small amount of sensitive data such as
a password, a token, or a key. Such information might otherwise be put in a
Pod specification or in an image; putting it in a Secret object allows for
more control over how it is used, and reduces the risk of accidental exposure.
2015-05-21 20:25:20 +00:00
2015-08-14 01:25:46 +00:00
Users can create secrets, and the system also creates some secrets.
2015-05-21 20:25:20 +00:00
2015-08-14 01:25:46 +00:00
To use a secret, a pod needs to reference the secret.
2015-08-25 14:47:58 +00:00
A secret can be used with a pod in two ways: either as files in a [volume ](volumes.md ) mounted on one or more of
2015-05-21 20:25:20 +00:00
its containers, or used by kubelet when pulling images for the pod.
2015-08-14 01:25:46 +00:00
### Service Accounts Automatically Create and Attach Secrets with API Credentials
2015-05-21 20:25:20 +00:00
Kubernetes automatically creates secrets which contain credentials for
accessing the API and it automatically modifies your pods to use this type of
secret.
The automatic creation and use of API credentials can be disabled or overridden
if desired. However, if all you need to do is securely access the apiserver,
this is the recommended workflow.
2015-07-15 05:07:44 +00:00
See the [Service Account ](service-accounts.md ) documentation for more
2015-05-21 20:25:20 +00:00
information on how Service Accounts work.
### Creating a Secret Manually
This is an example of a simple secret, in yaml format:
2015-07-17 02:01:02 +00:00
2015-05-21 20:25:20 +00:00
```yaml
apiVersion: v1
kind: Secret
metadata:
name: mysecret
type: Opaque
data:
password: dmFsdWUtMg0K
username: dmFsdWUtMQ0K
2015-03-17 17:58:02 +00:00
```
2015-06-09 15:24:14 +00:00
The data field is a map. Its keys must match
2015-08-19 11:01:50 +00:00
[`DNS_SUBDOMAIN` ](../design/identifiers.md ), except that leading dots are also
2015-06-17 12:58:54 +00:00
allowed. The values are arbitrary data, encoded using base64. The values of
username and password in the example above, before base64 encoding,
are `value-1` and `value-2` , respectively, with carriage return and newline characters at the end.
2015-03-17 17:58:02 +00:00
2015-07-14 16:37:37 +00:00
Create the secret using [`kubectl create` ](kubectl/kubectl_create.md ).
2015-05-21 20:25:20 +00:00
2015-08-14 01:25:46 +00:00
Once the secret is created, you can need to modify your pod to specify
that it should use the secret.
2015-05-21 20:25:20 +00:00
### Manually specifying a Secret to be Mounted on a Pod
This is an example of a pod that mounts a secret in a volume:
2015-07-17 02:01:02 +00:00
2015-03-17 17:58:02 +00:00
```json
{
2015-06-05 19:47:15 +00:00
"apiVersion": "v1",
2015-05-19 04:20:43 +00:00
"kind": "Pod",
2015-05-14 14:41:48 +00:00
"metadata": {
2015-05-19 04:20:43 +00:00
"name": "mypod",
"namespace": "myns"
2015-05-14 14:41:48 +00:00
},
2015-03-17 17:58:02 +00:00
"spec": {
2015-05-19 04:20:43 +00:00
"containers": [{
"name": "mypod",
"image": "redis",
"volumeMounts": [{
2015-03-17 17:58:02 +00:00
"name": "foo",
2015-05-19 04:20:43 +00:00
"mountPath": "/etc/foo",
"readOnly": true
2015-03-17 17:58:02 +00:00
}]
2015-05-19 04:20:43 +00:00
}],
"volumes": [{
"name": "foo",
"secret": {
"secretName": "mysecret"
}
}]
2015-03-17 17:58:02 +00:00
}
2015-05-19 04:20:43 +00:00
}
2015-03-17 17:58:02 +00:00
```
2015-05-21 20:25:20 +00:00
Each secret you want to use needs its own `spec.volumes` .
If there are multiple containers in the pod, then each container needs its
own `volumeMounts` block, but only one `spec.volumes` is needed per secret.
You can package many files into one secret, or use many secrets,
whichever is convenient.
2015-07-16 00:28:59 +00:00
See another example of creating a secret and a pod that consumes that secret in a volume [here ](secrets/ ).
2015-05-21 20:25:20 +00:00
### Manually specifying an imagePullSecret
2015-07-17 22:35:41 +00:00
2015-08-08 21:29:57 +00:00
Use of imagePullSecrets is described in the [images documentation ](images.md#specifying-imagepullsecrets-on-a-pod )
2015-07-17 22:35:41 +00:00
2015-08-14 01:25:46 +00:00
### Arranging for imagePullSecrets to be Automatically Attached
You can manually create an imagePullSecret, and reference it from
a serviceAccount. Any pods created with that serviceAccount
2015-09-26 04:42:10 +00:00
or that default to use that serviceAccount, will get their imagePullSecret
2015-08-14 01:25:46 +00:00
field set to that of the service account.
See [here ](service-accounts.md#adding-imagepullsecrets-to-a-service-account )
for a detailed explanation of that process.
2015-05-21 20:25:20 +00:00
2015-08-14 01:25:46 +00:00
### Automatic Mounting of Manually Created Secrets
2015-05-21 20:25:20 +00:00
2015-08-14 01:25:46 +00:00
We plan to extend the service account behavior so that manually created
secrets (e.g. one containing a token for accessing a github account)
can be automatically attached to pods based on their service account.
*This is not implemented yet. See [issue 9902 ](http://issue.k8s.io/9902 ).*
2015-05-21 20:25:20 +00:00
## Details
2015-07-17 22:35:41 +00:00
2015-03-17 17:58:02 +00:00
### Restrictions
2015-07-17 22:35:41 +00:00
2015-03-17 17:58:02 +00:00
Secret volume sources are validated to ensure that the specified object
reference actually points to an object of type `Secret` . Therefore, a secret
needs to be created before any pods that depend on it.
Secret API objects reside in a namespace. They can only be referenced by pods
in that same namespace.
Individual secrets are limited to 1MB in size. This is to discourage creation
of very large secrets which would exhaust apiserver and kubelet memory.
However, creation of many smaller secrets could also exhaust memory. More
comprehensive limits on memory usage due to secrets is a planned feature.
2015-05-29 00:21:32 +00:00
Kubelet only supports use of secrets for Pods it gets from the API server.
This includes any pods created using kubectl, or indirectly via a replication
controller. It does not include pods created via the kubelets
`--manifest-url` flag, its `--config` flag, or its REST API (these are
not common ways to create pods.)
2015-03-17 17:58:02 +00:00
### Consuming Secret Values
2015-06-17 12:58:54 +00:00
Inside the container that mounts a secret volume, the secret keys appear as
files and the secret values are base-64 decoded and stored inside these files.
This is the result of commands
executed inside the container from the example above:
2015-07-19 00:23:08 +00:00
```console
2015-06-17 12:58:54 +00:00
$ ls /etc/foo/
username
password
$ cat /etc/foo/username
value-1
$ cat /etc/foo/password
value-2
```
2015-03-17 17:58:02 +00:00
The program in a container is responsible for reading the secret(s) from the
files. Currently, if a program expects a secret to be stored in an environment
variable, then the user needs to modify the image to populate the environment
variable from the file as an step before running the main program. Future
versions of Kubernetes are expected to provide more automation for populating
2015-04-27 02:37:14 +00:00
environment variables from files.
2015-03-17 17:58:02 +00:00
2015-05-21 20:25:20 +00:00
### Secret and Pod Lifetime interaction
2015-03-17 17:58:02 +00:00
2015-05-29 00:02:54 +00:00
When a pod is created via the API, there is no check whether a referenced
secret exists. Once a pod is scheduled, the kubelet will try to fetch the
secret value. If the secret cannot be fetched because it does not exist or
because of a temporary lack of connection to the API server, kubelet will
periodically retry. It will report an event about the pod explaining the
reason it is not started yet. Once the a secret is fetched, the kubelet will
create and mount a volume containing it. None of the pod's containers will
start until all the pod's volumes are mounted.
Once the kubelet has started a pod's containers, its secret volumes will not
change, even if the secret resource is modified. To change the secret used,
the original pod must be deleted, and a new pod (perhaps with an identical
2015-06-16 21:48:51 +00:00
`PodSpec` ) must be created. Therefore, updating a secret follows the same
2015-05-29 00:02:54 +00:00
workflow as deploying a new container image. The `kubectl rolling-update`
2015-07-14 16:37:37 +00:00
command can be used ([man page](kubectl/kubectl_rolling-update.md)).
2015-03-17 17:58:02 +00:00
2015-07-15 05:07:44 +00:00
The [`resourceVersion` ](../devel/api-conventions.md#concurrency-control-and-consistency )
2015-06-17 12:58:54 +00:00
of the secret is not specified when it is referenced.
2015-03-17 17:58:02 +00:00
Therefore, if a secret is updated at about the same time as pods are starting,
then it is not defined which version of the secret will be used for the pod. It
is not possible currently to check what resource version of a secret object was
used when a pod was created. It is planned that pods will report this
2015-06-16 21:48:51 +00:00
information, so that a replication controller restarts ones using an old
`resourceVersion` . In the interim, if this is a concern, it is recommended to not
2015-03-17 17:58:02 +00:00
update the data of existing secrets, but to create new ones with distinct names.
## Use cases
### Use-Case: Pod with ssh keys
To create a pod that uses an ssh key stored as a secret, we first need to create a secret:
```json
{
"kind": "Secret",
2015-06-05 19:47:15 +00:00
"apiVersion": "v1",
2015-05-16 00:17:30 +00:00
"metadata": {
"name": "ssh-key-secret"
},
2015-03-17 17:58:02 +00:00
"data": {
2015-05-16 00:17:30 +00:00
"id-rsa": "dmFsdWUtMg0KDQo=",
"id-rsa.pub": "dmFsdWUtMQ0K"
2015-03-17 17:58:02 +00:00
}
}
```
**Note:** The serialized JSON and YAML values of secret data are encoded as
base64 strings. Newlines are not valid within these strings and must be
omitted.
Now we can create a pod which references the secret with the ssh key and
consumes it in a volume:
```json
{
"kind": "Pod",
2015-06-05 19:47:15 +00:00
"apiVersion": "v1",
2015-05-16 00:17:30 +00:00
"metadata": {
"name": "secret-test-pod",
"labels": {
"name": "secret-test"
}
2015-03-17 17:58:02 +00:00
},
2015-05-16 00:17:30 +00:00
"spec": {
"volumes": [
{
"name": "secret-volume",
"secret": {
"secretName": "ssh-key-secret"
}
}
],
"containers": [
{
2015-03-17 17:58:02 +00:00
"name": "ssh-test-container",
"image": "mySshImage",
2015-05-16 00:17:30 +00:00
"volumeMounts": [
{
"name": "secret-volume",
"readOnly": true,
"mountPath": "/etc/secret-volume"
2015-03-17 17:58:02 +00:00
}
2015-05-16 00:17:30 +00:00
]
}
]
2015-03-17 17:58:02 +00:00
}
}
```
When the container's command runs, the pieces of the key will be available in:
/etc/secret-volume/id-rsa.pub
/etc/secret-volume/id-rsa
The container is then free to use the secret data to establish an ssh connection.
### Use-Case: Pods with prod / test credentials
This example illustrates a pod which consumes a secret containing prod
credentials and another pod which consumes a secret with test environment
credentials.
The secrets:
```json
{
2015-06-05 19:47:15 +00:00
"apiVersion": "v1",
2015-05-17 22:22:25 +00:00
"kind": "List",
"items":
[{
"kind": "Secret",
2015-06-05 19:47:15 +00:00
"apiVersion": "v1",
2015-05-17 22:22:25 +00:00
"metadata": {
"name": "prod-db-secret"
},
"data": {
"password": "dmFsdWUtMg0KDQo=",
"username": "dmFsdWUtMQ0K"
}
2015-05-16 00:17:30 +00:00
},
2015-05-17 22:22:25 +00:00
{
"kind": "Secret",
2015-06-05 19:47:15 +00:00
"apiVersion": "v1",
2015-05-17 22:22:25 +00:00
"metadata": {
"name": "test-db-secret"
},
"data": {
"password": "dmFsdWUtMg0KDQo=",
"username": "dmFsdWUtMQ0K"
}
}]
}
2015-03-17 17:58:02 +00:00
```
The pods:
```json
2015-05-17 22:22:25 +00:00
{
2015-06-05 19:47:15 +00:00
"apiVersion": "v1",
2015-05-17 22:22:25 +00:00
"kind": "List",
"items":
[{
"kind": "Pod",
2015-06-05 19:47:15 +00:00
"apiVersion": "v1",
2015-05-17 22:22:25 +00:00
"metadata": {
"name": "prod-db-client-pod",
"labels": {
"name": "prod-db-client"
2015-05-16 00:17:30 +00:00
}
2015-05-17 22:22:25 +00:00
},
"spec": {
"volumes": [
{
"name": "secret-volume",
"secret": {
"secretName": "prod-db-secret"
2015-03-17 17:58:02 +00:00
}
2015-05-17 22:22:25 +00:00
}
],
"containers": [
{
"name": "db-client-container",
"image": "myClientImage",
"volumeMounts": [
{
"name": "secret-volume",
"readOnly": true,
"mountPath": "/etc/secret-volume"
}
]
}
]
2015-05-16 00:17:30 +00:00
}
2015-03-17 17:58:02 +00:00
},
2015-05-17 22:22:25 +00:00
{
"kind": "Pod",
2015-06-05 19:47:15 +00:00
"apiVersion": "v1",
2015-05-17 22:22:25 +00:00
"metadata": {
"name": "test-db-client-pod",
"labels": {
"name": "test-db-client"
2015-05-16 00:17:30 +00:00
}
2015-05-17 22:22:25 +00:00
},
"spec": {
"volumes": [
{
"name": "secret-volume",
"secret": {
"secretName": "test-db-secret"
2015-03-17 17:58:02 +00:00
}
2015-05-17 22:22:25 +00:00
}
],
"containers": [
{
"name": "db-client-container",
"image": "myClientImage",
"volumeMounts": [
{
"name": "secret-volume",
"readOnly": true,
"mountPath": "/etc/secret-volume"
}
]
}
]
}
}]
}
2015-03-17 17:58:02 +00:00
```
Both containers will have the following files present on their filesystems:
2015-07-17 02:01:02 +00:00
2015-07-19 00:23:08 +00:00
```console
2015-03-17 17:58:02 +00:00
/etc/secret-volume/username
/etc/secret-volume/password
```
Note how the specs for the two pods differ only in one field; this facilitates
creating pods with different capabilities from a common pod config template.
2015-08-19 11:01:50 +00:00
You could further simplify the base pod specification by using two Service Accounts:
2015-05-21 20:25:20 +00:00
one called, say, `prod-user` with the `prod-db-secret` , and one called, say,
`test-user` with the `test-db-secret` . Then, the pod spec can be shortened to, for example:
2015-07-17 02:01:02 +00:00
2015-05-21 20:25:20 +00:00
```json
{
"kind": "Pod",
"apiVersion": "v1",
"metadata": {
"name": "prod-db-client-pod",
"labels": {
"name": "prod-db-client"
}
},
"spec": {
"serviceAccount": "prod-db-client",
"containers": [
{
"name": "db-client-container",
"image": "myClientImage",
}
]
}
```
2015-03-17 17:58:02 +00:00
### Use-case: Secret visible to one container in a pod
2015-07-17 22:35:41 +00:00
2015-04-10 17:35:03 +00:00
< a name = "use-case-two-containers" > < / a >
2015-03-17 17:58:02 +00:00
Consider a program that needs to handle HTTP requests, do some complex business
logic, and then sign some messages with an HMAC. Because it has complex
application logic, there might be an unnoticed remote file reading exploit in
the server, which could expose the private key to an attacker.
This could be divided into two processes in two containers: a frontend container
which handles user interaction and business logic, but which cannot see the
private key; and a signer container that can see the private key, and responds
to simple signing requests from the frontend (e.g. over localhost networking).
With this partitioned approach, an attacker now has to trick the application
server into doing something rather arbitrary, which may be harder than getting
it to read a file.
2015-05-21 20:25:20 +00:00
<!-- TODO: explain how to do this while still using automation. -->
2015-03-17 17:58:02 +00:00
## Security Properties
### Protections
Because `secret` objects can be created independently of the `pods` that use
them, there is less risk of the secret being exposed during the workflow of
creating, viewing, and editing pods. The system can also take additional
precautions with `secret` objects, such as avoiding writing them to disk where
2015-04-10 17:35:03 +00:00
possible.
2015-03-17 17:58:02 +00:00
A secret is only sent to a node if a pod on that node requires it. It is not
written to disk. It is stored in a tmpfs. It is deleted once the pod that
depends on it is deleted.
2015-04-15 13:32:00 +00:00
On most Kubernetes-project-maintained distributions, communication between user
2015-03-17 17:58:02 +00:00
to the apiserver, and from apiserver to the kubelets, is protected by SSL/TLS.
Secrets are protected when transmitted over these channels.
2015-07-31 01:33:00 +00:00
Secret data on nodes is stored in tmpfs volumes and thus does not come to rest
on the node.
2015-03-17 17:58:02 +00:00
There may be secrets for several pods on the same node. However, only the
secrets that a pod requests are potentially visible within its containers.
2015-04-10 17:35:03 +00:00
Therefore, one Pod does not have access to the secrets of another pod.
2015-03-17 17:58:02 +00:00
There may be several containers in a pod. However, each container in a pod has
to request the secret volume in its `volumeMounts` for it to be visible within
the container. This can be used to construct useful [security partitions at the
Pod level](#use-case-two-containers).
### Risks
2015-07-31 01:33:00 +00:00
- In the API server secret data is stored as plaintext in etcd; therefore:
- Administrators should limit access to etcd to admin users
- Secret data in the API server is at rest on the disk that etcd uses; admins may want to wipe/shred disks
used by etcd when no longer in use
2015-03-17 17:58:02 +00:00
- Applications still need to protect the value of secret after reading it from the volume,
2015-04-10 17:35:03 +00:00
such as not accidentally logging it or transmitting it to an untrusted party.
2015-03-17 17:58:02 +00:00
- A user who can create a pod that uses a secret can also see the value of that secret. Even
if apiserver policy does not allow that user to read the secret object, the user could
run a pod which exposes the secret.
2015-07-31 01:33:00 +00:00
- If multiple replicas of etcd are run, then the secrets will be shared between them.
2015-03-17 17:58:02 +00:00
By default, etcd does not secure peer-to-peer communication with SSL/TLS, though this can be configured.
2015-07-20 20:45:36 +00:00
- It is not possible currently to control which users of a Kubernetes cluster can
2015-04-10 17:35:03 +00:00
access a secret. Support for this is planned.
2015-03-17 17:58:02 +00:00
- Currently, anyone with root on any node can read any secret from the apiserver,
by impersonating the kubelet. It is a planned feature to only send secrets to
nodes that actually require them, to restrict the impact of a root exploit on a
single node.
2015-05-14 22:12:45 +00:00
2015-07-14 00:13:09 +00:00
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
2015-07-14 16:37:37 +00:00
[![Analytics ](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/docs/user-guide/secrets.md?pixel )]()
2015-07-14 00:13:09 +00:00
<!-- END MUNGE: GENERATED_ANALYTICS -->