2015-04-03 20:58:08 +00:00
## Kubernetes DNS example
This is a toy example demonstrating how to use kubernetes DNS.
### Step Zero: Prerequisites
2016-12-14 00:03:06 +00:00
This example assumes that you have forked the repository and [turned up a Kubernetes cluster ](../../docs/getting-started-guides/ ). Make sure DNS is enabled in your setup, see [DNS doc ](../../build/kube-dns/ ).
2015-04-03 20:58:08 +00:00
2015-07-20 04:38:53 +00:00
```sh
2015-04-03 20:58:08 +00:00
$ cd kubernetes
$ hack/dev-build-and-up.sh
```
### Step One: Create two namespaces
2015-07-14 16:37:37 +00:00
We'll see how cluster DNS works across multiple [namespaces ](../../docs/user-guide/namespaces.md ), first we need to create two namespaces:
2015-04-03 20:58:08 +00:00
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl create -f examples/cluster-dns/namespace-dev.yaml
$ kubectl create -f examples/cluster-dns/namespace-prod.yaml
2015-04-03 20:58:08 +00:00
```
Now list all namespaces:
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl get namespaces
2015-04-03 20:58:08 +00:00
NAME LABELS STATUS
default < none > Active
development name=development Active
production name=production Active
```
For kubectl client to work with each namespace, we define two contexts:
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl config set-context dev --namespace=development --cluster=${CLUSTER_NAME} --user=${USER_NAME}
$ kubectl config set-context prod --namespace=production --cluster=${CLUSTER_NAME} --user=${USER_NAME}
2015-04-03 20:58:08 +00:00
```
2015-06-10 20:58:43 +00:00
You can view your cluster name and user name in kubernetes config at ~/.kube/config.
2015-04-03 20:58:08 +00:00
### Step Two: Create backend replication controller in each namespace
2015-07-14 16:37:37 +00:00
Use the file [`examples/cluster-dns/dns-backend-rc.yaml` ](dns-backend-rc.yaml ) to create a backend server [replication controller ](../../docs/user-guide/replication-controller.md ) in each namespace.
2015-04-03 20:58:08 +00:00
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl config use-context dev
$ kubectl create -f examples/cluster-dns/dns-backend-rc.yaml
2015-04-03 20:58:08 +00:00
```
Once that's up you can list the pod in the cluster:
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl get rc
2015-04-03 20:58:08 +00:00
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
dns-backend dns-backend ddysher/dns-backend name=dns-backend 1
```
Now repeat the above commands to create a replication controller in prod namespace:
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl config use-context prod
$ kubectl create -f examples/cluster-dns/dns-backend-rc.yaml
$ kubectl get rc
2015-04-03 20:58:08 +00:00
CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS
dns-backend dns-backend ddysher/dns-backend name=dns-backend 1
```
### Step Three: Create backend service
2015-05-24 07:15:58 +00:00
Use the file [`examples/cluster-dns/dns-backend-service.yaml` ](dns-backend-service.yaml ) to create
2015-07-14 16:37:37 +00:00
a [service ](../../docs/user-guide/services.md ) for the backend server.
2015-04-03 20:58:08 +00:00
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl config use-context dev
$ kubectl create -f examples/cluster-dns/dns-backend-service.yaml
2015-04-03 20:58:08 +00:00
```
Once that's up you can list the service in the cluster:
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl get service dns-backend
2015-08-08 04:08:43 +00:00
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE
dns-backend 10.0.2.3 < none > 8000/TCP name=dns-backend 1d
2015-04-03 20:58:08 +00:00
```
Again, repeat the same process for prod namespace:
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl config use-context prod
$ kubectl create -f examples/cluster-dns/dns-backend-service.yaml
$ kubectl get service dns-backend
2015-08-08 04:08:43 +00:00
NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE
dns-backend 10.0.2.4 < none > 8000/TCP name=dns-backend 1d
2015-04-03 20:58:08 +00:00
```
### Step Four: Create client pod in one namespace
2015-07-14 16:37:37 +00:00
Use the file [`examples/cluster-dns/dns-frontend-pod.yaml` ](dns-frontend-pod.yaml ) to create a client [pod ](../../docs/user-guide/pods.md ) in dev namespace. The client pod will make a connection to backend and exit. Specifically, it tries to connect to address `http://dns-backend.development.cluster.local:8000` .
2015-04-03 20:58:08 +00:00
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl config use-context dev
$ kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml
2015-04-03 20:58:08 +00:00
```
Once that's up you can list the pod in the cluster:
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl get pods dns-frontend
2015-07-07 01:20:28 +00:00
NAME READY STATUS RESTARTS AGE
dns-frontend 0/1 ExitCode:0 0 1m
2015-04-03 20:58:08 +00:00
```
Wait until the pod succeeds, then we can see the output from the client pod:
2015-07-20 04:38:53 +00:00
```sh
2015-06-25 08:53:45 +00:00
$ kubectl logs dns-frontend
2015-04-03 20:58:08 +00:00
2015-05-07T20:13:54.147664936Z 10.0.236.129
2015-06-10 20:58:43 +00:00
2015-05-07T20:13:54.147721290Z Send request to: http://dns-backend.development.cluster.local:8000
2015-04-03 20:58:08 +00:00
2015-05-07T20:13:54.147733438Z < Response [ 200 ] >
2015-05-07T20:13:54.147738295Z Hello World!
```
2015-07-14 00:13:09 +00:00
Please refer to the [source code ](images/frontend/client.py ) about the log. First line prints out the ip address associated with the service in dev namespace; remaining lines print out our request and server response.
2015-06-10 20:58:43 +00:00
If we switch to prod namespace with the same pod config, we'll see the same result, i.e. dns will resolve across namespace.
2015-04-03 20:58:08 +00:00
2015-07-20 04:38:53 +00:00
```sh
2015-06-05 21:50:11 +00:00
$ kubectl config use-context prod
$ kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml
2015-06-25 08:53:45 +00:00
$ kubectl logs dns-frontend
2015-04-03 20:58:08 +00:00
2015-05-07T20:13:54.147664936Z 10.0.236.129
2015-06-10 20:58:43 +00:00
2015-05-07T20:13:54.147721290Z Send request to: http://dns-backend.development.cluster.local:8000
2015-04-03 20:58:08 +00:00
2015-05-07T20:13:54.147733438Z < Response [ 200 ] >
2015-05-07T20:13:54.147738295Z Hello World!
```
#### Note about default namespace
2015-07-23 17:13:57 +00:00
If you prefer not using namespace, then all your services can be addressed using `default` namespace, e.g. `http://dns-backend.default.svc.cluster.local:8000` , or shorthand version `http://dns-backend:8000`
2015-05-14 22:12:45 +00:00
2015-06-25 08:53:45 +00:00
### tl; dr;
2015-07-17 22:35:41 +00:00
2015-06-25 08:53:45 +00:00
For those of you who are impatient, here is the summary of the commands we ran in this tutorial. Remember to set first `$CLUSTER_NAME` and `$USER_NAME` to the values found in `~/.kube/config` .
```sh
# create dev and prod namespaces
kubectl create -f examples/cluster-dns/namespace-dev.yaml
kubectl create -f examples/cluster-dns/namespace-prod.yaml
# create two contexts
kubectl config set-context dev --namespace=development --cluster=${CLUSTER_NAME} --user=${USER_NAME}
kubectl config set-context prod --namespace=production --cluster=${CLUSTER_NAME} --user=${USER_NAME}
# create two backend replication controllers
kubectl config use-context dev
kubectl create -f examples/cluster-dns/dns-backend-rc.yaml
kubectl config use-context prod
kubectl create -f examples/cluster-dns/dns-backend-rc.yaml
# create backend services
kubectl config use-context dev
kubectl create -f examples/cluster-dns/dns-backend-service.yaml
kubectl config use-context prod
kubectl create -f examples/cluster-dns/dns-backend-service.yaml
# create a pod in each namespace and get its output
kubectl config use-context dev
kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml
kubectl logs dns-frontend
kubectl config use-context prod
kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml
kubectl logs dns-frontend
```
2015-07-14 00:13:09 +00:00
<!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
2015-05-14 22:12:45 +00:00
[![Analytics ](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/examples/cluster-dns/README.md?pixel )]()
2015-07-14 00:13:09 +00:00
<!-- END MUNGE: GENERATED_ANALYTICS -->