@@ -4,93 +4,97 @@ assignees:
|
|||||||
title: Using Environment Variables
|
title: Using Environment Variables
|
||||||
---
|
---
|
||||||
|
|
||||||
This example demonstrates running pods, replication controllers, and
|
This example demonstrates running pods, replication controllers, and
|
||||||
services. It shows two types of pods: frontend and backend, with
|
services. It shows two types of pods: frontend and backend, with
|
||||||
services on top of both. Accessing the frontend pod will return
|
services on top of both. Accessing the frontend pod will return
|
||||||
environment information about itself, and a backend pod that it has
|
environment information about itself, and a backend pod that it has
|
||||||
accessed through the service. The goal is to illuminate the
|
accessed through the service. The goal is to illuminate the
|
||||||
environment metadata available to running containers inside the
|
environment metadata available to running containers inside the
|
||||||
Kubernetes cluster. The documentation for the Kubernetes environment
|
Kubernetes cluster. The documentation for the Kubernetes environment
|
||||||
is [here](/docs/user-guide/container-environment).
|
is [here](/docs/user-guide/container-environment).
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
## Prerequisites
|
## Prerequisites
|
||||||
|
|
||||||
This example assumes that you have a Kubernetes cluster installed and
|
This example assumes that you have a Kubernetes cluster installed and
|
||||||
running, and that you have installed the `kubectl` command line tool
|
running, and that you have installed the `kubectl` command line tool
|
||||||
somewhere in your path. Please see the [getting
|
somewhere in your path. Please see the [getting
|
||||||
started](/docs/getting-started-guides/) for installation instructions
|
started](/docs/getting-started-guides/) for installation instructions
|
||||||
for your platform.
|
for your platform.
|
||||||
|
|
||||||
## Optional: Build your own containers
|
## Optional: Build your own containers
|
||||||
|
|
||||||
The code for the containers is under
|
These are the configuration files for the containers:
|
||||||
[containers/](/docs/user-guide/containers/)
|
|
||||||
|
* [backend-rc.yaml](https://raw.githubusercontent.com/kubernetes/kubernetes.github.io/master/docs/user-guide/environment-guide/backend-rc.yaml)
|
||||||
## Get everything running
|
* [backend-srv.yaml](https://raw.githubusercontent.com/kubernetes/kubernetes.github.io/master/docs/user-guide/environment-guide/backend-srv.yaml)
|
||||||
|
* [show-rc.yaml](https://raw.githubusercontent.com/kubernetes/kubernetes.github.io/master/docs/user-guide/environment-guide/show-rc.yaml)
|
||||||
```shell
|
* [show-srv.yaml](https://raw.githubusercontent.com/kubernetes/kubernetes.github.io/master/docs/user-guide/environment-guide/show-srv.yaml)
|
||||||
kubectl create -f ./backend-rc.yaml
|
|
||||||
kubectl create -f ./backend-srv.yaml
|
## Get everything running
|
||||||
kubectl create -f ./show-rc.yaml
|
|
||||||
kubectl create -f ./show-srv.yaml
|
```shell
|
||||||
```
|
kubectl create -f ./backend-rc.yaml
|
||||||
|
kubectl create -f ./backend-srv.yaml
|
||||||
## Query the service
|
kubectl create -f ./show-rc.yaml
|
||||||
|
kubectl create -f ./show-srv.yaml
|
||||||
Use `kubectl describe service show-srv` to determine the public IP of
|
```
|
||||||
your service.
|
|
||||||
|
## Query the service
|
||||||
> Note: If your platform does not support external load balancers,
|
|
||||||
you'll need to open the proper port and direct traffic to the
|
Use `kubectl describe service show-srv` to determine the public IP of
|
||||||
internal IP shown for the frontend service with the above command
|
your service.
|
||||||
|
|
||||||
Run `curl <public ip>:80` to query the service. You should get
|
> Note: If your platform does not support external load balancers,
|
||||||
something like this back:
|
you'll need to open the proper port and direct traffic to the
|
||||||
|
internal IP shown for the frontend service with the above command
|
||||||
```shell
|
|
||||||
Pod Name: show-rc-xxu6i
|
Run `curl <public ip>:80` to query the service. You should get
|
||||||
Pod Namespace: default
|
something like this back:
|
||||||
USER_VAR: important information
|
|
||||||
|
```shell
|
||||||
Kubernetes environment variables
|
Pod Name: show-rc-xxu6i
|
||||||
BACKEND_SRV_SERVICE_HOST = 10.147.252.185
|
Pod Namespace: default
|
||||||
BACKEND_SRV_SERVICE_PORT = 5000
|
USER_VAR: important information
|
||||||
KUBERNETES_RO_SERVICE_HOST = 10.147.240.1
|
|
||||||
KUBERNETES_RO_SERVICE_PORT = 80
|
Kubernetes environment variables
|
||||||
KUBERNETES_SERVICE_HOST = 10.147.240.2
|
BACKEND_SRV_SERVICE_HOST = 10.147.252.185
|
||||||
KUBERNETES_SERVICE_PORT = 443
|
BACKEND_SRV_SERVICE_PORT = 5000
|
||||||
KUBE_DNS_SERVICE_HOST = 10.147.240.10
|
KUBERNETES_RO_SERVICE_HOST = 10.147.240.1
|
||||||
KUBE_DNS_SERVICE_PORT = 53
|
KUBERNETES_RO_SERVICE_PORT = 80
|
||||||
|
KUBERNETES_SERVICE_HOST = 10.147.240.2
|
||||||
Found backend ip: 10.147.252.185 port: 5000
|
KUBERNETES_SERVICE_PORT = 443
|
||||||
Response from backend
|
KUBE_DNS_SERVICE_HOST = 10.147.240.10
|
||||||
Backend Container
|
KUBE_DNS_SERVICE_PORT = 53
|
||||||
Backend Pod Name: backend-rc-6qiya
|
|
||||||
Backend Namespace: default
|
Found backend ip: 10.147.252.185 port: 5000
|
||||||
```
|
Response from backend
|
||||||
|
Backend Container
|
||||||
First the frontend pod's information is printed. The pod name and
|
Backend Pod Name: backend-rc-6qiya
|
||||||
[namespace](https://github.com/kubernetes/kubernetes/blob/{{page.githubbranch}}/docs/design/namespaces.md) are retrieved from the
|
Backend Namespace: default
|
||||||
[Downward API](/docs/user-guide/downward-api). Next, `USER_VAR` is the name of
|
```
|
||||||
an environment variable set in the [pod
|
|
||||||
definition](/docs/user-guide/environment-guide/show-rc.yaml). Then, the dynamic Kubernetes environment
|
First the frontend pod's information is printed. The pod name and
|
||||||
variables are scanned and printed. These are used to find the backend
|
[namespace](https://github.com/kubernetes/kubernetes/blob/{{page.githubbranch}}/docs/design/namespaces.md) are retrieved from the
|
||||||
service, named `backend-srv`. Finally, the frontend pod queries the
|
[Downward API](/docs/user-guide/downward-api). Next, `USER_VAR` is the name of
|
||||||
backend service and prints the information returned. Again the backend
|
an environment variable set in the [pod
|
||||||
pod returns its own pod name and namespace.
|
definition](/docs/user-guide/environment-guide/show-rc.yaml). Then, the dynamic Kubernetes environment
|
||||||
|
variables are scanned and printed. These are used to find the backend
|
||||||
Try running the `curl` command a few times, and notice what
|
service, named `backend-srv`. Finally, the frontend pod queries the
|
||||||
changes. Ex: `watch -n 1 curl -s <ip>` Firstly, the frontend service
|
backend service and prints the information returned. Again the backend
|
||||||
is directing your request to different frontend pods each time. The
|
pod returns its own pod name and namespace.
|
||||||
frontend pods are always contacting the backend through the backend
|
|
||||||
service. This results in a different backend pod servicing each
|
Try running the `curl` command a few times, and notice what
|
||||||
request as well.
|
changes. Ex: `watch -n 1 curl -s <ip>` Firstly, the frontend service
|
||||||
|
is directing your request to different frontend pods each time. The
|
||||||
## Cleanup
|
frontend pods are always contacting the backend through the backend
|
||||||
|
service. This results in a different backend pod servicing each
|
||||||
```shell
|
request as well.
|
||||||
kubectl delete rc,service -l type=show-type
|
|
||||||
kubectl delete rc,service -l type=backend-type
|
## Cleanup
|
||||||
```
|
|
||||||
|
```shell
|
||||||
|
kubectl delete rc,service -l type=show-type
|
||||||
|
kubectl delete rc,service -l type=backend-type
|
||||||
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user