Kubernetes: Services

Services are used to access a Pod inside and outside a cluster.

Service has a static IP, DNS, Port and load balances requests to the Pods.

Service uses Label selector to choose Pods.

Service Discovery


  • DNS based
  • Environment variables

Using the ReplicationController we built here: kubectl: create pod

we’ll make an app visible to the outside world.

Checking it’s not visible currently:

  • kubectl describe rc
  • kubectl describe pod/hello-rc-tp5vf


 Port: 8080/TCP


curl: (7) Failed to connect to port 8080: Connection refused

Expose port with:

kubectl expose rc hello-rc --name=hello-svc --target-port 8080 --type=NodePort
service/hello-svc exposed


kubectl describe svc hello-svc

Name:                     hello-svc
Namespace:                default
Labels:                   app=hello-world
Annotations:              <none>
Selector:                 app=hello-world
Type:                     NodePort
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  31094/TCP
Endpoints:      ,, + 17 more...
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

to show the service.

To delete pods, replicationcontroller or service use:

kubectl delete -f pod.yml

kubectl delete -f rc.yml

kubectl delete svc hello-svc


Service Types

ClusterIP: Stable internal cluster IP

NodePort: exposes the app outside of the cluster by adding a cluster-wide port on top of ClusterIP

LoadBalancer: integrates NodePort with cloud-based load balancers

Creating the Service above with a yml file looks like:

apiVersion: v1
kind: Service
    name: hello-svc
        app: hello-world
    type: NodePort
    - port: 8080 
      protocol: TCP
        app: hello-world

Note: port is what the app is using inside the container. This then gets mapped to NodePort.




Leave a Reply

Your email address will not be published. Required fields are marked *