DEV Community

Madhavam Saxena
Madhavam Saxena

Posted on • Updated on

Exposing a deployment in kubernetes

Exposing a deployment:

kubectl expose deployment <name_of_deployment> --type=LoadBalancer/NodePort/ClusterIP(Default) --port=<port_no_at_which_deployment_needs_to_be_exposed> 

Enter fullscreen mode Exit fullscreen mode

type= LoadBalancer is only available for those environments that support load balancer like AWS.
Now via above command what we are doing is, we are creating a service that will be exposed on the assigned port number.
Now, What is service?

Service:
Service basically help us to reach the pods and container running in a pod. Another k8s object responsible for exposing pod within cluster and outside it.
If you’ll see, Pods do have an IP address but we can’t access them from outside of cluster Via this IP, also, whenever the pod gets terminated and another pod spinnup’s, the IP address of the pod will change.
Service, basically groups these pods together and assign a particular IP address to that group of pods. Benefit of this is, pods might get terminated but the service will never terminate, and hence, pods inside the service will be reachable. Also, when on exposing this service, we make pods reachable from outside of cluster. Without service, it becomes very difficult to reach out these pods and hence we need service.

--type=NodePort :
Actually means that this deployment should be exposed with the help of worker/slave node on which its running, which makes this deployment accessible from outside, but a more better approach is loadbalancer.

--type=LoadBalancer :
This not only helps to assign IP to the service, but also, routes the load in the balanced way to the service.

---type=ClusterIP is default type and lets you expose the deployment via cluster IP.

To see the running services

kubectl get services
Enter fullscreen mode Exit fullscreen mode

This is basically an imperative approach of running or basically creating a service.
For declarative approach, stay tunned.

Top comments (0)