From 68a0c5195b89281ae281cf7fb88c4fd2648b863b Mon Sep 17 00:00:00 2001 From: steveperry-53 Date: Tue, 18 Oct 2016 20:01:43 -0700 Subject: [PATCH] Write new task: Defining Environment Variables for a Container. --- _data/tasks.yml | 4 + _includes/task-tutorial-prereqs.md | 4 + .../define-environment-variable-container.md | 77 +++++++++++++++++++ .../tasks/configure-pod-container/envars.yaml | 13 ++++ 4 files changed, 98 insertions(+) create mode 100644 _includes/task-tutorial-prereqs.md create mode 100644 docs/tasks/configure-pod-container/define-environment-variable-container.md create mode 100644 docs/tasks/configure-pod-container/envars.yaml diff --git a/_data/tasks.yml b/_data/tasks.yml index 86209a52aa..91bbe1bcca 100644 --- a/_data/tasks.yml +++ b/_data/tasks.yml @@ -2,6 +2,10 @@ bigheader: "Tasks" toc: - title: Tasks path: /docs/tasks/ +- title: Configuring Pods and Containers + section: + - title: Defining Environment Variables for a Container + path: /docs/tasks/configure-pod-container/define-environment-variable-container/ - title: Accessing Applications in a Cluster section: - title: Using Port Forwarding to Access Applications in a Cluster diff --git a/_includes/task-tutorial-prereqs.md b/_includes/task-tutorial-prereqs.md new file mode 100644 index 0000000000..a9cf90d265 --- /dev/null +++ b/_includes/task-tutorial-prereqs.md @@ -0,0 +1,4 @@ +You need to have a Kubernetes cluster, and the kubectl command-line tool must +be configured to communicate with your cluster. If you do not already have a +cluster, you can create one by using +[Minikube](/docs/getting-started-guides/minikube). diff --git a/docs/tasks/configure-pod-container/define-environment-variable-container.md b/docs/tasks/configure-pod-container/define-environment-variable-container.md new file mode 100644 index 0000000000..2cba3c55f1 --- /dev/null +++ b/docs/tasks/configure-pod-container/define-environment-variable-container.md @@ -0,0 +1,77 @@ +--- +--- + +{% capture overview %} + +This page shows how to define environment variables when you run a container +in a Kubernetes Pod. + +{% endcapture %} + + +{% capture prerequisites %} + +{% include task-tutorial-prereqs.md %} + +{% endcapture %} + + +{% capture steps %} + +### Defining an environment variable for a container + +When you create a Pod, you can set environment variables for the containers +that run in the Pod. To set environment variables, include the `env` field in +the configuration file. + +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: + +{% include code.html language="yaml" file="envars.yaml" ghlink="/docs/tasks/configure-pod-container/envars.yaml" %} + +1. Create a Pod based on the YAML configuration file: + + export REPO=https://raw.githubusercontent.com/kubernetes/kubernetes.github.io/master + kubectl create -f $REPO/docs/tasks/configure-pod-container/envars.yaml + +1. List the running Pods: + + kubectl get pods + + 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: + + kubectl exec -it envar-demo -- /bin/bash + +1. In your shell, run the `printenv` command to list the environment variables. + + root@envar-demo:/# printenv + + 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 + +1. To exit the shell, enter `exit`. + +{% endcapture %} + +{% capture whatsnext %} + +* Learn more about [environment variables](/docs/user-guide/environment-guide/). +* Learn about [using secrets as environment variables](/docs/user-guide/secrets/#using-secrets-as-environment-variables). +* See [EnvVarSource](/docs/api-reference/v1/definitions/#_v1_envvarsource). + +{% endcapture %} + + +{% include templates/task.md %} diff --git a/docs/tasks/configure-pod-container/envars.yaml b/docs/tasks/configure-pod-container/envars.yaml new file mode 100644 index 0000000000..97b296ac52 --- /dev/null +++ b/docs/tasks/configure-pod-container/envars.yaml @@ -0,0 +1,13 @@ +apiVersion: v1 +kind: Pod +metadata: + name: envar-demo + labels: + purpose: demonstrate-envars +spec: + containers: + - name: envar-demo-container + image: gcr.io/google-samples/node-hello:1.0 + env: + - name: DEMO_GREETING + value: "Hello from the environment"