Creating a Service and Discovering DNS Names in Kubernetes

Hands-On Lab

 

Photo of Chad Crowell

Chad Crowell

DevOps Training Architect II in Content

Length

00:30:00

Difficulty

Intermediate

In this hands-on lab, you will be presented with a three-node cluster. You will be creating a simple nginx deployment, in which you must create a service from that deployment. In order to accomplish this, you will use the kubectl command line to create the deployment. Then, you will expose the deployment to create the service. When you have verified the service is up and running, you will create a busybox pod to test the DNS resolution to that service.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating a Service and Discovering DNS Names in Kubernetes

We have been given a three-node cluster. Within that cluster, we must perform the following tasks in order to create a service and resolve the DNS names for that service. We will create the necessary Kubernetes resources in order to perform this DNS query.

To adequately complete this hands-on lab, we must have a working deployment, a working service, and be able to record the DNS name of the service within our Kubernetes cluster.

Log in to the Kube Master server using the credentials on the lab page (either in your local terminal, using the Instant Terminal feature, or using the public IP), and work through the objectives listed.

Create an nginx deployment, and verify it was successful.

  1. Use this command to create an nginx deployment:

    kubectl run nginx --image=nginx
  • Use this command to verify deployment was successful:

    kubectl get deployments

Create a service, and verify the service was successful.

  1. Use this command to create a service:
    kubectl expose deployment nginx --port 80 --type NodePort
  • Use this command to verify the service was created:
    kubectl get services

Create a pod that will allow you to query DNS, and verify it’s been created.

  1. Using an editor of your choice (e.g., Vim and the command vim busybox.yaml), enter the following YAML to create the busybox pod spec:

    apiVersion: v1
    kind: Pod
    metadata:
      name: busybox
    spec:
      containers:
      - image: busybox:1.28.4
        command:
          - sleep
          - "3600"
        name: busybox
      restartPolicy: Always
  • Use the following command to create the busybox pod:

    kubectl create -f busybox.yaml
  • Use the following command to verify the pod was created successfully:

    kubectl get pods

Perform a DNS query to the service.

  1. Use the following command to query the DNS name of the nginx service:

    kubectl exec busybox -- nslookup nginx

Record the DNS name.

  1. Record the name of:

    <service-name>.default.svc.cluster.local