2016-03-31 19:55:32 +00:00
|
|
|
---
|
|
|
|
---
|
2016-02-26 11:54:48 +00:00
|
|
|
|
|
|
|
This example demonstrates running pods, replication controllers, and
|
|
|
|
services. It shows two types of pods: frontend and backend, with
|
|
|
|
services on top of both. Accessing the frontend pod will return
|
|
|
|
environment information about itself, and a backend pod that it has
|
|
|
|
accessed through the service. The goal is to illuminate the
|
|
|
|
environment metadata available to running containers inside the
|
|
|
|
Kubernetes cluster. The documentation for the Kubernetes environment
|
2016-02-29 23:17:22 +00:00
|
|
|
is [here](/docs/user-guide/container-environment).
|
2016-02-26 11:54:48 +00:00
|
|
|
|
|
|
|
![Diagram](/images/docs/diagram.png)
|
|
|
|
|
|
|
|
## Prerequisites
|
|
|
|
|
|
|
|
This example assumes that you have a Kubernetes cluster installed and
|
|
|
|
running, and that you have installed the `kubectl` command line tool
|
|
|
|
somewhere in your path. Please see the [getting
|
2016-02-29 23:17:22 +00:00
|
|
|
started](/docs/getting-started-guides/) for installation instructions
|
2016-02-26 11:54:48 +00:00
|
|
|
for your platform.
|
|
|
|
|
|
|
|
## Optional: Build your own containers
|
|
|
|
|
|
|
|
The code for the containers is under
|
2016-03-31 19:55:32 +00:00
|
|
|
[containers/](/docs/user-guide/containers/)
|
2016-02-26 11:54:48 +00:00
|
|
|
|
|
|
|
## Get everything running
|
|
|
|
|
2016-03-31 19:55:32 +00:00
|
|
|
```shell
|
|
|
|
kubectl create -f ./backend-rc.yaml
|
|
|
|
kubectl create -f ./backend-srv.yaml
|
|
|
|
kubectl create -f ./show-rc.yaml
|
|
|
|
kubectl create -f ./show-srv.yaml
|
2016-02-26 11:54:48 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
## Query the service
|
|
|
|
|
|
|
|
Use `kubectl describe service show-srv` to determine the public IP of
|
|
|
|
your service.
|
|
|
|
|
|
|
|
> Note: If your platform does not support external load balancers,
|
2016-03-07 12:09:02 +00:00
|
|
|
you'll need to open the proper port and direct traffic to the
|
|
|
|
internal IP shown for the frontend service with the above command
|
2016-02-26 11:54:48 +00:00
|
|
|
|
|
|
|
Run `curl <public ip>:80` to query the service. You should get
|
|
|
|
something like this back:
|
|
|
|
|
2016-03-31 19:55:32 +00:00
|
|
|
```shell
|
2016-02-26 11:54:48 +00:00
|
|
|
Pod Name: show-rc-xxu6i
|
|
|
|
Pod Namespace: default
|
|
|
|
USER_VAR: important information
|
|
|
|
|
|
|
|
Kubernetes environment variables
|
|
|
|
BACKEND_SRV_SERVICE_HOST = 10.147.252.185
|
|
|
|
BACKEND_SRV_SERVICE_PORT = 5000
|
|
|
|
KUBERNETES_RO_SERVICE_HOST = 10.147.240.1
|
|
|
|
KUBERNETES_RO_SERVICE_PORT = 80
|
|
|
|
KUBERNETES_SERVICE_HOST = 10.147.240.2
|
|
|
|
KUBERNETES_SERVICE_PORT = 443
|
|
|
|
KUBE_DNS_SERVICE_HOST = 10.147.240.10
|
|
|
|
KUBE_DNS_SERVICE_PORT = 53
|
|
|
|
|
|
|
|
Found backend ip: 10.147.252.185 port: 5000
|
|
|
|
Response from backend
|
|
|
|
Backend Container
|
|
|
|
Backend Pod Name: backend-rc-6qiya
|
|
|
|
Backend Namespace: default
|
2016-03-31 19:55:32 +00:00
|
|
|
```
|
|
|
|
|
2016-02-26 11:54:48 +00:00
|
|
|
First the frontend pod's information is printed. The pod name and
|
|
|
|
[namespace](https://github.com/kubernetes/kubernetes/blob/{{page.githubbranch}}/docs/design/namespaces.md) are retrieved from the
|
2016-02-29 23:17:22 +00:00
|
|
|
[Downward API](/docs/user-guide/downward-api). Next, `USER_VAR` is the name of
|
2016-02-26 11:54:48 +00:00
|
|
|
an environment variable set in the [pod
|
2016-02-29 23:17:22 +00:00
|
|
|
definition](/docs/user-guide/environment-guide/show-rc.yaml). Then, the dynamic Kubernetes environment
|
2016-02-26 11:54:48 +00:00
|
|
|
variables are scanned and printed. These are used to find the backend
|
|
|
|
service, named `backend-srv`. Finally, the frontend pod queries the
|
|
|
|
backend service and prints the information returned. Again the backend
|
|
|
|
pod returns its own pod name and namespace.
|
|
|
|
|
|
|
|
Try running the `curl` command a few times, and notice what
|
|
|
|
changes. Ex: `watch -n 1 curl -s <ip>` Firstly, the frontend service
|
|
|
|
is directing your request to different frontend pods each time. The
|
|
|
|
frontend pods are always contacting the backend through the backend
|
|
|
|
service. This results in a different backend pod servicing each
|
|
|
|
request as well.
|
|
|
|
|
|
|
|
## Cleanup
|
2016-03-31 19:55:32 +00:00
|
|
|
|
2016-02-26 11:54:48 +00:00
|
|
|
```shell
|
|
|
|
kubectl delete rc,service -l type=show-type
|
2016-03-31 19:55:32 +00:00
|
|
|
kubectl delete rc,service -l type=backend-type
|
|
|
|
```
|