In kubernetes a service always enables its network access to a pod or set of pods

Services will select the pods based on their labels and when a network is made to those services it selects all Pods in the cluster matching the service’s selector and will choose one of them, and then will forwards the network request to it.

Image for post

source Thanks to (

Kubernetes Service vs Deployment

How can we differentiate a Deployment and a Service in K8s?

A deployment is responsible for keeping a set of pods running in a cluster ‘

A service is responsible for enabling network access to a set of pods in a cluster

We can use deployment without using service so we can keep a couple of same pods running is K8s cluster

The deployment could be scaled up and down and pods could be replicated.

In kubernetes a single pod can be accessed individually directly via network requests, hence to track pods is a bit difficult

We can always and also use a service type without a deployment part.Here if we do that we would be creating a single pod rather than creating all together like we do in deployment .Then we have alternative that our Service is capable of routing network requests to those pods by selecting on the basis of their labels allocated to them .

How can we discover a Kubernetes service

In Kubernetes, there are two ways to discover a service:

  • DNS type. In this specific part, the DNS server is added to the cluster in order to watch the Kubernetes API create DNS record sets for each new service.
  • “When DNS is enabled all over the cluster, all pods should be able to automatically perform name resolution of services.”
  • ENV Variables. In this discovery method, a pod runs on a node, so the kubelet adds environment variables for each active service.

How to create a service

To understand in a better way with simple example in the form of “Hello World” App with help of deployment kind .

As we see the app is deployed and running with Up status , we will NOW create service (ClusterIP) for accessing our application in kubernetes

Now , let’s create a deployment running

 “kubectl run hello-world –replicas=3 –labels=”run=load-balancer-example” – –port=8080”. 

Here this command creates a deployment with two replicas of our application in kubernetes


run “kubectl get deployment hello-world” so see that the deployment is running. Now we can check the replicaset and pods that the deployment created.

$ kubectl get deployments hello-world
hello-world   3        3        3           3          76s

So the app is running now , if you want to access the freshly created application ,we need to create ClusterIP type of Service

  • Create a YAML manifest for the service and apply it, or
  • Use the “kubectl expose” command, which is the easier option. This expose command creates a service without creating a YAML file.
$ kubectl expose deployment hello-world --type=ClusterIP --name=example-service
service "example-service" exposed

Here, we’ll create a service called example-service with type ClusterIP.

So now we will access our application :

run “kubectl get service example-service” to get our port number.

Then, we need to execute command port-forward. Because here our service type is Cluster IP, which can only be accessed within the cluster, we must access our application by forwarding the port to a local port in cluster .

We can use other types, like “LoadBalanacer”, which will create an LB in AWS or GCP, then we can access the app using the DNS address given to the LB with our port number.

$ kubectl get service example-service
NAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
example-service   ClusterIP   <none>        8080/TCP   1h

$ kubectl port-forward service/example-service 8080:8080
Forwarding from -> 8080

Now we can browse http://localhost:8080 from our workstation and we should see:

Hello Kubernetes!

#aws #docker #kubernetes

Service Types in Kubernetes
8.05 GEEK