2016-12-08 22:22:13 +00:00
|
|
|
---
|
2017-06-08 19:12:52 +00:00
|
|
|
title: Distribute Credentials Securely Using Secrets
|
2018-05-05 16:00:51 +00:00
|
|
|
content_template: templates/task
|
2018-05-17 23:46:49 +00:00
|
|
|
weight: 50
|
2016-12-08 22:22:13 +00:00
|
|
|
---
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture overview %}}
|
2016-12-09 22:07:15 +00:00
|
|
|
This page shows how to securely inject sensitive data, such as passwords and
|
|
|
|
encryption keys, into Pods.
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture prerequisites %}}
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture steps %}}
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2017-06-08 19:12:52 +00:00
|
|
|
## Convert your secret data to a base-64 representation
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
Suppose you want to have two pieces of secret data: a username `my-app` and a password
|
|
|
|
`39528$vdg7Jb`. First, use [Base64 encoding](https://www.base64encode.org/) to
|
|
|
|
convert your username and password to a base-64 representation. Here's a Linux
|
|
|
|
example:
|
|
|
|
|
2017-02-27 05:09:27 +00:00
|
|
|
echo -n 'my-app' | base64
|
|
|
|
echo -n '39528$vdg7Jb' | base64
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2017-02-27 05:09:27 +00:00
|
|
|
The output shows that the base-64 representation of your username is `bXktYXBw`,
|
|
|
|
and the base-64 representation of your password is `Mzk1MjgkdmRnN0pi`.
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2017-06-08 19:12:52 +00:00
|
|
|
## Create a Secret
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
Here is a configuration file you can use to create a Secret that holds your
|
|
|
|
username and password:
|
|
|
|
|
2018-07-03 18:50:19 +00:00
|
|
|
{{< codenew file="pods/inject/secret.yaml" >}}
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
1. Create the Secret
|
|
|
|
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
kubectl create -f https://k8s.io/docs/tasks/inject-data-application/secret.yaml
|
|
|
|
```
|
2018-11-06 19:33:04 +00:00
|
|
|
{{< note >}}
|
|
|
|
If you want to skip the Base64 encoding step, you can create a Secret by using the `kubectl create secret` command:
|
|
|
|
{{< /note >}}
|
|
|
|
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
kubectl create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb'
|
|
|
|
```
|
2016-12-09 22:07:15 +00:00
|
|
|
|
2016-12-08 22:22:13 +00:00
|
|
|
1. View information about the Secret:
|
|
|
|
|
2017-08-06 04:22:56 +00:00
|
|
|
kubectl get secret test-secret
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
Output:
|
|
|
|
|
|
|
|
NAME TYPE DATA AGE
|
|
|
|
test-secret Opaque 2 1m
|
|
|
|
|
|
|
|
|
|
|
|
1. View more detailed information about the Secret:
|
|
|
|
|
2017-08-06 04:22:56 +00:00
|
|
|
kubectl describe secret test-secret
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
Output:
|
|
|
|
|
|
|
|
Name: test-secret
|
|
|
|
Namespace: default
|
|
|
|
Labels: <none>
|
|
|
|
Annotations: <none>
|
|
|
|
|
|
|
|
Type: Opaque
|
|
|
|
|
|
|
|
Data
|
|
|
|
====
|
2017-03-30 11:12:07 +00:00
|
|
|
password: 13 bytes
|
|
|
|
username: 7 bytes
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2018-07-03 18:50:19 +00:00
|
|
|
|
|
|
|
{{< note >}}
|
2018-11-06 19:33:04 +00:00
|
|
|
If you want to skip the Base64 encoding step, you can create a Secret
|
2018-07-03 18:50:19 +00:00
|
|
|
by using the `kubectl create secret` command:
|
|
|
|
{{< /note >}}
|
|
|
|
|
|
|
|
```shell
|
|
|
|
kubectl create secret generic test-secret --from-literal=username='my-app' --from-literal=password='39528$vdg7Jb'
|
|
|
|
```
|
|
|
|
|
2017-06-08 19:12:52 +00:00
|
|
|
## Create a Pod that has access to the secret data through a Volume
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
Here is a configuration file you can use to create a Pod:
|
|
|
|
|
2018-07-03 18:50:19 +00:00
|
|
|
{{< codenew file="pods/inject/secret-pod.yaml" >}}
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
1. Create the Pod:
|
|
|
|
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
kubectl create -f https://k8s.io/docs/tasks/inject-data-application/secret-pod.yaml
|
|
|
|
```
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
1. Verify that your Pod is running:
|
|
|
|
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
kubectl get pod secret-test-pod
|
|
|
|
```
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
Output:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
NAME READY STATUS RESTARTS AGE
|
|
|
|
secret-test-pod 1/1 Running 0 42m
|
|
|
|
```
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
1. Get a shell into the Container that is running in your Pod:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
kubectl exec -it secret-test-pod -- /bin/bash
|
|
|
|
```
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2016-12-09 22:07:15 +00:00
|
|
|
1. The secret data is exposed to the Container through a Volume mounted under
|
|
|
|
`/etc/secret-volume`. In your shell, go to the directory where the secret data
|
|
|
|
is exposed:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
root@secret-test-pod:/# cd /etc/secret-volume
|
|
|
|
```
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
1. In your shell, list the files in the `/etc/secret-volume` directory:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
root@secret-test-pod:/etc/secret-volume# ls
|
|
|
|
```
|
2016-12-08 22:22:13 +00:00
|
|
|
The output shows two files, one for each piece of secret data:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
password username
|
|
|
|
```
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
1. In your shell, display the contents of the `username` and `password` files:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
root@secret-test-pod:/etc/secret-volume# cat username; echo; cat password; echo
|
|
|
|
```
|
2016-12-08 22:22:13 +00:00
|
|
|
The output is your username and password:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
my-app
|
|
|
|
39528$vdg7Jb
|
|
|
|
```
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2017-06-08 19:12:52 +00:00
|
|
|
## Create a Pod that has access to the secret data through environment variables
|
2016-12-09 22:07:15 +00:00
|
|
|
|
|
|
|
Here is a configuration file you can use to create a Pod:
|
|
|
|
|
2018-07-03 18:50:19 +00:00
|
|
|
{{< codenew file="pods/inject/secret-envars-pod.yaml" >}}
|
2016-12-09 22:07:15 +00:00
|
|
|
|
|
|
|
1. Create the Pod:
|
|
|
|
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
kubectl create -f https://k8s.io/docs/tasks/inject-data-application/secret-envars-pod.yaml
|
|
|
|
```
|
2016-12-09 22:07:15 +00:00
|
|
|
|
|
|
|
1. Verify that your Pod is running:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
kubectl get pod secret-envars-test-pod
|
|
|
|
```
|
2016-12-09 22:07:15 +00:00
|
|
|
|
|
|
|
Output:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
NAME READY STATUS RESTARTS AGE
|
|
|
|
secret-envars-test-pod 1/1 Running 0 4m
|
|
|
|
```
|
2016-12-09 22:07:15 +00:00
|
|
|
|
|
|
|
1. Get a shell into the Container that is running in your Pod:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
kubectl exec -it secret-envars-test-pod -- /bin/bash
|
|
|
|
```
|
2016-12-09 22:07:15 +00:00
|
|
|
|
|
|
|
1. In your shell, display the environment variables:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
root@secret-envars-test-pod:/# printenv
|
|
|
|
```
|
2016-12-09 22:07:15 +00:00
|
|
|
|
|
|
|
The output includes your username and password:
|
2018-08-21 19:13:09 +00:00
|
|
|
```shell
|
|
|
|
...
|
|
|
|
SECRET_USERNAME=my-app
|
|
|
|
...
|
|
|
|
SECRET_PASSWORD=39528$vdg7Jb
|
|
|
|
```
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture whatsnext %}}
|
2016-12-08 22:22:13 +00:00
|
|
|
|
2017-04-19 17:56:47 +00:00
|
|
|
* Learn more about [Secrets](/docs/concepts/configuration/secret/).
|
2017-03-20 22:35:51 +00:00
|
|
|
* Learn about [Volumes](/docs/concepts/storage/volumes/).
|
2016-12-09 22:07:15 +00:00
|
|
|
|
2017-01-18 18:18:37 +00:00
|
|
|
### Reference
|
2016-12-09 22:07:15 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
* [Secret](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#secret-v1-core)
|
|
|
|
* [Volume](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#volume-v1-core)
|
|
|
|
* [Pod](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#pod-v1-core)
|
|
|
|
|
|
|
|
{{% /capture %}}
|
2016-12-08 22:22:13 +00:00
|
|
|
|
|
|
|
|