2016-10-19 03:01:43 +00:00
|
|
|
---
|
2017-06-08 19:12:52 +00:00
|
|
|
title: Define Environment Variables for a Container
|
2018-05-05 16:00:51 +00:00
|
|
|
content_template: templates/task
|
2018-05-17 23:46:49 +00:00
|
|
|
weight: 20
|
2016-10-19 03:01:43 +00:00
|
|
|
---
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture overview %}}
|
2016-10-19 03:01:43 +00:00
|
|
|
|
2018-05-06 23:23:50 +00:00
|
|
|
This page shows how to define environment variables for a container
|
2016-10-19 03:01:43 +00:00
|
|
|
in a Kubernetes Pod.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture prerequisites %}}
|
2016-10-19 03:01:43 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< include "task-tutorial-prereqs.md" >}} {{< version-check >}}
|
2016-10-19 03:01:43 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture steps %}}
|
2016-10-19 03:01:43 +00:00
|
|
|
|
2017-06-08 19:12:52 +00:00
|
|
|
## Define an environment variable for a container
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
When you create a Pod, you can set environment variables for the containers
|
2016-12-15 17:46:03 +00:00
|
|
|
that run in the Pod. To set environment variables, include the `env` or
|
|
|
|
`envFrom` field in the configuration file.
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
In this exercise, you create a Pod that runs one container. The configuration
|
|
|
|
file for the Pod defines an environment variable with name `DEMO_GREETING` and
|
|
|
|
value `"Hello from the environment"`. Here is the configuration file for the
|
|
|
|
Pod:
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< code file="envars.yaml" >}}
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
1. Create a Pod based on the YAML configuration file:
|
|
|
|
|
2017-08-06 04:22:56 +00:00
|
|
|
kubectl create -f https://k8s.io/docs/tasks/inject-data-application/envars.yaml
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
1. List the running Pods:
|
|
|
|
|
2017-08-06 04:22:56 +00:00
|
|
|
kubectl get pods -l purpose=demonstrate-envars
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
The output is similar to this:
|
|
|
|
|
|
|
|
NAME READY STATUS RESTARTS AGE
|
|
|
|
envar-demo 1/1 Running 0 9s
|
|
|
|
|
|
|
|
1. Get a shell to the container running in your Pod:
|
|
|
|
|
2017-08-06 04:22:56 +00:00
|
|
|
kubectl exec -it envar-demo -- /bin/bash
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
1. In your shell, run the `printenv` command to list the environment variables.
|
|
|
|
|
2017-08-18 01:21:59 +00:00
|
|
|
root@envar-demo:/# printenv
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
The output is similar to this:
|
|
|
|
|
|
|
|
NODE_VERSION=4.4.2
|
|
|
|
EXAMPLE_SERVICE_PORT_8080_TCP_ADDR=10.3.245.237
|
|
|
|
HOSTNAME=envar-demo
|
|
|
|
...
|
|
|
|
DEMO_GREETING=Hello from the environment
|
2018-01-02 08:46:49 +00:00
|
|
|
DEMO_FAREWELL=Such a sweet sorrow
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
1. To exit the shell, enter `exit`.
|
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< note >}}
|
2018-01-21 09:10:59 +00:00
|
|
|
**Note:** The environment variables set using the `env` or `envFrom` field
|
2018-01-21 19:41:25 +00:00
|
|
|
will override any environment variables specified in the container image.
|
2018-05-05 16:00:51 +00:00
|
|
|
{{< /note >}}
|
2018-01-21 09:10:59 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% /capture %}}
|
2016-10-19 03:01:43 +00:00
|
|
|
|
2018-05-05 16:00:51 +00:00
|
|
|
{{% capture whatsnext %}}
|
2016-10-19 03:01:43 +00:00
|
|
|
|
2017-04-19 17:56:47 +00:00
|
|
|
* Learn more about [environment variables](/docs/tasks/configure-pod-container/environment-variable-expose-pod-information/).
|
2016-10-19 03:01:43 +00:00
|
|
|
* Learn about [using secrets as environment variables](/docs/user-guide/secrets/#using-secrets-as-environment-variables).
|
2018-05-05 16:00:51 +00:00
|
|
|
* See [EnvVarSource](/docs/reference/generated/kubernetes-api/{{< param "version" >}}/#envvarsource-v1-core).
|
|
|
|
|
|
|
|
{{% /capture %}}
|
2016-10-19 03:01:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
|