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

Options:

  • 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

shows:

IP: 172.17.0.9
 Port: 8080/TCP

and

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

Expose port with:

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

then

kubectl describe svc hello-svc

Name:                     hello-svc
Namespace:                default
Labels:                   app=hello-world
Annotations:              <none>
Selector:                 app=hello-world
Type:                     NodePort
IP:                       10.111.196.170
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  31094/TCP
Endpoints:                172.17.0.10:8080,172.17.0.11:8080,172.17.0.12:8080 + 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
metadata:
    name: hello-svc
    labels:
        app: hello-world
spec:
    type: NodePort
    ports:
    - port: 8080 
      protocol: TCP
    selector:
        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 *