|
| 1 | +## Kubernetes DNS example |
| 2 | + |
| 3 | +This is a toy example demonstrating how to use kubernetes DNS. |
| 4 | + |
| 5 | +### Step Zero: Prerequisites |
| 6 | + |
| 7 | +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](https://github.com/kubernetes/dns). |
| 8 | + |
| 9 | +```sh |
| 10 | +$ cd kubernetes |
| 11 | +$ hack/dev-build-and-up.sh |
| 12 | +``` |
| 13 | + |
| 14 | +### Step One: Create two namespaces |
| 15 | + |
| 16 | +We'll see how cluster DNS works across multiple [namespaces](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/), first we need to create two namespaces: |
| 17 | + |
| 18 | +```sh |
| 19 | +$ kubectl create -f examples/cluster-dns/namespace-dev.yaml |
| 20 | +$ kubectl create -f examples/cluster-dns/namespace-prod.yaml |
| 21 | +``` |
| 22 | + |
| 23 | +Now list all namespaces: |
| 24 | + |
| 25 | +```sh |
| 26 | +$ kubectl get namespaces |
| 27 | +NAME LABELS STATUS |
| 28 | +default <none> Active |
| 29 | +development name=development Active |
| 30 | +production name=production Active |
| 31 | +``` |
| 32 | + |
| 33 | +For kubectl client to work with each namespace, we define two contexts: |
| 34 | + |
| 35 | +```sh |
| 36 | +$ kubectl config set-context dev --namespace=development --cluster=${CLUSTER_NAME} --user=${USER_NAME} |
| 37 | +$ kubectl config set-context prod --namespace=production --cluster=${CLUSTER_NAME} --user=${USER_NAME} |
| 38 | +``` |
| 39 | + |
| 40 | +You can view your cluster name and user name in kubernetes config at ~/.kube/config. |
| 41 | + |
| 42 | +### Step Two: Create backend replication controller in each namespace |
| 43 | + |
| 44 | +Use the file [`examples/cluster-dns/dns-backend-rc.yaml`](dns-backend-rc.yaml) to create a backend server [replication controller](https://kubernetes.io/docs/concepts/workloads/controllers/replicationcontroller/) in each namespace. |
| 45 | + |
| 46 | +```sh |
| 47 | +$ kubectl config use-context dev |
| 48 | +$ kubectl create -f examples/cluster-dns/dns-backend-rc.yaml |
| 49 | +``` |
| 50 | + |
| 51 | +Once that's up you can list the pod in the cluster: |
| 52 | + |
| 53 | +```sh |
| 54 | +$ kubectl get rc |
| 55 | +CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS |
| 56 | +dns-backend dns-backend ddysher/dns-backend name=dns-backend 1 |
| 57 | +``` |
| 58 | + |
| 59 | +Now repeat the above commands to create a replication controller in prod namespace: |
| 60 | + |
| 61 | +```sh |
| 62 | +$ kubectl config use-context prod |
| 63 | +$ kubectl create -f examples/cluster-dns/dns-backend-rc.yaml |
| 64 | +$ kubectl get rc |
| 65 | +CONTROLLER CONTAINER(S) IMAGE(S) SELECTOR REPLICAS |
| 66 | +dns-backend dns-backend ddysher/dns-backend name=dns-backend 1 |
| 67 | +``` |
| 68 | + |
| 69 | +### Step Three: Create backend service |
| 70 | + |
| 71 | +Use the file [`examples/cluster-dns/dns-backend-service.yaml`](dns-backend-service.yaml) to create |
| 72 | +a [service](https://kubernetes.io/docs/concepts/services-networking/service/) for the backend server. |
| 73 | + |
| 74 | +```sh |
| 75 | +$ kubectl config use-context dev |
| 76 | +$ kubectl create -f examples/cluster-dns/dns-backend-service.yaml |
| 77 | +``` |
| 78 | + |
| 79 | +Once that's up you can list the service in the cluster: |
| 80 | + |
| 81 | +```sh |
| 82 | +$ kubectl get service dns-backend |
| 83 | +NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE |
| 84 | +dns-backend 10.0.2.3 <none> 8000/TCP name=dns-backend 1d |
| 85 | +``` |
| 86 | + |
| 87 | +Again, repeat the same process for prod namespace: |
| 88 | + |
| 89 | +```sh |
| 90 | +$ kubectl config use-context prod |
| 91 | +$ kubectl create -f examples/cluster-dns/dns-backend-service.yaml |
| 92 | +$ kubectl get service dns-backend |
| 93 | +NAME CLUSTER_IP EXTERNAL_IP PORT(S) SELECTOR AGE |
| 94 | +dns-backend 10.0.2.4 <none> 8000/TCP name=dns-backend 1d |
| 95 | +``` |
| 96 | + |
| 97 | +### Step Four: Create client pod in one namespace |
| 98 | + |
| 99 | +Use the file [`examples/cluster-dns/dns-frontend-pod.yaml`](dns-frontend-pod.yaml) to create a client [pod](https://kubernetes.io/docs/concepts/workloads/pods/pod/) 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`. |
| 100 | + |
| 101 | +```sh |
| 102 | +$ kubectl config use-context dev |
| 103 | +$ kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml |
| 104 | +``` |
| 105 | + |
| 106 | +Once that's up you can list the pod in the cluster: |
| 107 | + |
| 108 | +```sh |
| 109 | +$ kubectl get pods dns-frontend |
| 110 | +NAME READY STATUS RESTARTS AGE |
| 111 | +dns-frontend 0/1 ExitCode:0 0 1m |
| 112 | +``` |
| 113 | + |
| 114 | +Wait until the pod succeeds, then we can see the output from the client pod: |
| 115 | + |
| 116 | +```sh |
| 117 | +$ kubectl logs dns-frontend |
| 118 | +2015-05-07T20:13:54.147664936Z 10.0.236.129 |
| 119 | +2015-05-07T20:13:54.147721290Z Send request to: http://dns-backend.development.cluster.local:8000 |
| 120 | +2015-05-07T20:13:54.147733438Z <Response [200]> |
| 121 | +2015-05-07T20:13:54.147738295Z Hello World! |
| 122 | +``` |
| 123 | + |
| 124 | +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. |
| 125 | + |
| 126 | +If we switch to prod namespace with the same pod config, we'll see the same result, i.e. dns will resolve across namespace. |
| 127 | + |
| 128 | +```sh |
| 129 | +$ kubectl config use-context prod |
| 130 | +$ kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml |
| 131 | +$ kubectl logs dns-frontend |
| 132 | +2015-05-07T20:13:54.147664936Z 10.0.236.129 |
| 133 | +2015-05-07T20:13:54.147721290Z Send request to: http://dns-backend.development.cluster.local:8000 |
| 134 | +2015-05-07T20:13:54.147733438Z <Response [200]> |
| 135 | +2015-05-07T20:13:54.147738295Z Hello World! |
| 136 | +``` |
| 137 | + |
| 138 | + |
| 139 | +#### Note about default namespace |
| 140 | + |
| 141 | +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` |
| 142 | + |
| 143 | + |
| 144 | +### tl; dr; |
| 145 | + |
| 146 | +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`. |
| 147 | + |
| 148 | +```sh |
| 149 | +# create dev and prod namespaces |
| 150 | +kubectl create -f examples/cluster-dns/namespace-dev.yaml |
| 151 | +kubectl create -f examples/cluster-dns/namespace-prod.yaml |
| 152 | + |
| 153 | +# create two contexts |
| 154 | +kubectl config set-context dev --namespace=development --cluster=${CLUSTER_NAME} --user=${USER_NAME} |
| 155 | +kubectl config set-context prod --namespace=production --cluster=${CLUSTER_NAME} --user=${USER_NAME} |
| 156 | + |
| 157 | +# create two backend replication controllers |
| 158 | +kubectl config use-context dev |
| 159 | +kubectl create -f examples/cluster-dns/dns-backend-rc.yaml |
| 160 | +kubectl config use-context prod |
| 161 | +kubectl create -f examples/cluster-dns/dns-backend-rc.yaml |
| 162 | + |
| 163 | +# create backend services |
| 164 | +kubectl config use-context dev |
| 165 | +kubectl create -f examples/cluster-dns/dns-backend-service.yaml |
| 166 | +kubectl config use-context prod |
| 167 | +kubectl create -f examples/cluster-dns/dns-backend-service.yaml |
| 168 | + |
| 169 | +# create a pod in each namespace and get its output |
| 170 | +kubectl config use-context dev |
| 171 | +kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml |
| 172 | +kubectl logs dns-frontend |
| 173 | + |
| 174 | +kubectl config use-context prod |
| 175 | +kubectl create -f examples/cluster-dns/dns-frontend-pod.yaml |
| 176 | +kubectl logs dns-frontend |
| 177 | +``` |
| 178 | + |
| 179 | + |
| 180 | +<!-- BEGIN MUNGE: GENERATED_ANALYTICS --> |
| 181 | +[]() |
| 182 | +<!-- END MUNGE: GENERATED_ANALYTICS --> |
0 commit comments