Kubernetes Expose Command

KubernetesKubernetesBeginner
Practice Now

Introduction

In Kubernetes, the expose command is used to create a service that exposes the Kubernetes service on a specific port to the outside world. The expose command creates a new Kubernetes service and also creates a new Kubernetes endpoint object that binds the service to the pods running in the Kubernetes cluster.

In this lab, we will go through the steps required to use the Kubernetes expose command to create a new service.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL kubernetes(("`Kubernetes`")) -.-> kubernetes/BasicCommandsGroup(["`Basic Commands`"]) kubernetes/BasicCommandsGroup -.-> kubernetes/expose("`Expose`") kubernetes/BasicCommandsGroup -.-> kubernetes/get("`Get`") subgraph Lab Skills kubernetes/expose -.-> lab-8452{{"`Kubernetes Expose Command`"}} kubernetes/get -.-> lab-8452{{"`Kubernetes Expose Command`"}} end

Expose a Deployment

In this step, we will expose a deployment using the expose command. We will create a deployment with one replica, and then expose it as a service.

  1. Create a new deployment named hello-world with the nginx image and one replica:

    kubectl create deployment hello-world --image=nginx --replicas=1
  2. Expose the hello-world deployment as a service named hello-service on port 80:

    kubectl expose deployment hello-world --name=hello-service --port=80 --target-port=80 --type=NodePort

    The --target-port flag specifies the port that the container is listening on, while the --port flag specifies the port that the service will be available on. The --type flag specifies the type of service, in this case, a NodePort service.

    The NodePort type of service allows access to the service from a specific port on each node in the cluster. The port is assigned automatically by Kubernetes from a range of ports configured in the cluster.

  3. Get the details of the hello-service service:

    kubectl get services hello-service

    This command will show the details of the hello-service service, including the PORT(S) on which the service is listening.

Access the Exposed Service

In this step, we will access the service that we exposed in the previous step using curl.

  1. Get the IP address of any node in the cluster:

    kubectl get nodes -o wide
  2. Use curl to access the hello-service service on the IP address and NodePort that Kubernetes assigned to the service:

    curl <NODE_IP>:<NODE_PORT>

    Replace <NODE_IP> and <NODE_PORT> with the IP address and NodePort that Kubernetes assigned to the hello-service service.

    If everything is configured correctly, you should see the default nginx welcome page.

Summary

In this lab, we have gone through the steps required to use the Kubernetes expose command to create a new service. We created a deployment with one replica and exposed it as a NodePort service. We then accessed the service using curl and verified that it was working correctly.

Other Kubernetes Tutorials you may like