github.com/qsunny/k8s@v0.0.0-20220101153623-e6dca256d5bf/examples-master/staging/cluster-dns/README.md (about)

     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](https://kubernetes.io/docs/setup/pick-right-solution/). 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/staging/cluster-dns/namespace-dev.yaml
    20  $ kubectl create -f examples/staging/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 using our current context as a base:
    34  
    35  ```sh
    36  $ CURRENT_CONTEXT=$(kubectl config view -o jsonpath='{.current-context}')
    37  $ USER_NAME=$(kubectl config view -o jsonpath='{.contexts[?(@.name == "'"${CURRENT_CONTEXT}"'")].context.user}')
    38  $ CLUSTER_NAME=$(kubectl config view -o jsonpath='{.contexts[?(@.name == "'"${CURRENT_CONTEXT}"'")].context.cluster}')
    39  $ kubectl config set-context dev --namespace=development --cluster=${CLUSTER_NAME} --user=${USER_NAME}
    40  $ kubectl config set-context prod --namespace=production --cluster=${CLUSTER_NAME} --user=${USER_NAME}
    41  ```
    42  
    43  ### Step Two: Create backend replication controller in each namespace
    44  
    45  Use the file [`examples/staging/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.
    46  
    47  ```sh
    48  $ kubectl config use-context dev
    49  $ kubectl create -f examples/staging/cluster-dns/dns-backend-rc.yaml
    50  ```
    51  
    52  Once that's up you can list the pod in the cluster:
    53  
    54  ```sh
    55  $ kubectl get rc
    56  CONTROLLER    CONTAINER(S)   IMAGE(S)              SELECTOR           REPLICAS
    57  dns-backend   dns-backend    ddysher/dns-backend   name=dns-backend   1
    58  ```
    59  
    60  Now repeat the above commands to create a replication controller in prod namespace:
    61  
    62  ```sh
    63  $ kubectl config use-context prod
    64  $ kubectl create -f examples/staging/cluster-dns/dns-backend-rc.yaml
    65  $ kubectl get rc
    66  CONTROLLER    CONTAINER(S)   IMAGE(S)              SELECTOR           REPLICAS
    67  dns-backend   dns-backend    ddysher/dns-backend   name=dns-backend   1
    68  ```
    69  
    70  ### Step Three: Create backend service
    71  
    72  Use the file [`examples/staging/cluster-dns/dns-backend-service.yaml`](dns-backend-service.yaml) to create
    73  a [service](https://kubernetes.io/docs/concepts/services-networking/service/) for the backend server.
    74  
    75  ```sh
    76  $ kubectl config use-context dev
    77  $ kubectl create -f examples/staging/cluster-dns/dns-backend-service.yaml
    78  ```
    79  
    80  Once that's up you can list the service in the cluster:
    81  
    82  ```sh
    83  $ kubectl get service dns-backend
    84  NAME         CLUSTER_IP       EXTERNAL_IP       PORT(S)                SELECTOR          AGE
    85  dns-backend  10.0.2.3         <none>            8000/TCP               name=dns-backend  1d
    86  ```
    87  
    88  Again, repeat the same process for prod namespace:
    89  
    90  ```sh
    91  $ kubectl config use-context prod
    92  $ kubectl create -f examples/staging/cluster-dns/dns-backend-service.yaml
    93  $ kubectl get service dns-backend
    94  NAME         CLUSTER_IP       EXTERNAL_IP       PORT(S)                SELECTOR          AGE
    95  dns-backend  10.0.2.4         <none>            8000/TCP               name=dns-backend  1d
    96  ```
    97  
    98  ### Step Four: Create client pod in one namespace
    99  
   100  Use the file [`examples/staging/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.svc.cluster.local:8000`.
   101  
   102  ```sh
   103  $ kubectl config use-context dev
   104  $ kubectl create -f examples/staging/cluster-dns/dns-frontend-pod.yaml
   105  ```
   106  
   107  Once that's up you can list the pod in the cluster:
   108  
   109  ```sh
   110  $ kubectl get pods dns-frontend
   111  NAME           READY     STATUS       RESTARTS   AGE
   112  dns-frontend   0/1       ExitCode:0   0          1m
   113  ```
   114  
   115  Wait until the pod succeeds, then we can see the output from the client pod:
   116  
   117  ```sh
   118  $ kubectl logs dns-frontend
   119  2015-05-07T20:13:54.147664936Z 10.0.236.129
   120  2015-05-07T20:13:54.147721290Z Send request to: http://dns-backend.development.svc.cluster.local:8000
   121  2015-05-07T20:13:54.147733438Z <Response [200]>
   122  2015-05-07T20:13:54.147738295Z Hello World!
   123  ```
   124  
   125  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.
   126  
   127  If we switch to prod namespace with the same pod config, we'll see the same result, i.e. dns will resolve across namespace.
   128  
   129  ```sh
   130  $ kubectl config use-context prod
   131  $ kubectl create -f examples/staging/cluster-dns/dns-frontend-pod.yaml
   132  $ kubectl logs dns-frontend
   133  2015-05-07T20:13:54.147664936Z 10.0.236.129
   134  2015-05-07T20:13:54.147721290Z Send request to: http://dns-backend.development.svc.cluster.local:8000
   135  2015-05-07T20:13:54.147733438Z <Response [200]>
   136  2015-05-07T20:13:54.147738295Z Hello World!
   137  ```
   138  
   139  
   140  #### Note about default namespace
   141  
   142  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`
   143  
   144  
   145  ### tl; dr;
   146  
   147  For those of you who are impatient, here is the summary of the commands we ran in this tutorial. Remember the values of `$CLUSTER_NAME` and `$USER_NAME` are based on current context found in `~/.kube/config`.
   148  
   149  ```sh
   150  # create dev and prod namespaces
   151  kubectl create -f examples/staging/cluster-dns/namespace-dev.yaml
   152  kubectl create -f examples/staging/cluster-dns/namespace-prod.yaml
   153  
   154  # create two contexts
   155  CURRENT_CONTEXT=$(kubectl config view -o jsonpath='{.current-context}')
   156  USER_NAME=$(kubectl config view -o jsonpath='{.contexts[?(@.name == "'"${CURRENT_CONTEXT}"'")].context.user}')
   157  CLUSTER_NAME=$(kubectl config view -o jsonpath='{.contexts[?(@.name == "'"${CURRENT_CONTEXT}"'")].context.cluster}')
   158  kubectl config set-context dev --namespace=development --cluster=${CLUSTER_NAME} --user=${USER_NAME}
   159  kubectl config set-context prod --namespace=production --cluster=${CLUSTER_NAME} --user=${USER_NAME}
   160  
   161  # create two backend replication controllers
   162  kubectl config use-context dev
   163  kubectl create -f examples/staging/cluster-dns/dns-backend-rc.yaml
   164  kubectl config use-context prod
   165  kubectl create -f examples/staging/cluster-dns/dns-backend-rc.yaml
   166  
   167  # create backend services
   168  kubectl config use-context dev
   169  kubectl create -f examples/staging/cluster-dns/dns-backend-service.yaml
   170  kubectl config use-context prod
   171  kubectl create -f examples/staging/cluster-dns/dns-backend-service.yaml
   172  
   173  # create a pod in each namespace and get its output
   174  kubectl config use-context dev
   175  kubectl create -f examples/staging/cluster-dns/dns-frontend-pod.yaml
   176  kubectl logs dns-frontend
   177  
   178  kubectl config use-context prod
   179  kubectl create -f examples/staging/cluster-dns/dns-frontend-pod.yaml
   180  kubectl logs dns-frontend
   181  ```
   182  
   183  
   184  <!-- BEGIN MUNGE: GENERATED_ANALYTICS -->
   185  [![Analytics](https://kubernetes-site.appspot.com/UA-36037335-10/GitHub/examples/staging/cluster-dns/README.md?pixel)]()
   186  <!-- END MUNGE: GENERATED_ANALYTICS -->