Service

Service documentation:

Pods may live where ever they want, and replicate or go down at any time during the deployment lifetime. New IPs are assigned, and wil lead to problems when trying to connect to other pods in the cluster.

To remedy this we have a resource in kubernetes called Service. Services maintain a stable IP and defines a set of logical Pods and how to access them.

text

Services defines which pods to connect by utilizing the labels on the pods.

For instance: To create a service accessible on port 80, that connects to a pod named [pod-name] on port 8080 (the pod’s port), run: kubectl expose pod [pod-name] --port=80 --target-port=8080

Inspecting a service reveals that the service type is ClusterIP

There are 3 main types of services: NodePort, LoadBalancer and ClusterIP

  • ClusterIp Pods accessed within the cluster only.
  • NodePort Pods are exposed on all the hosts on a random port.
  • LoadBalancer The cloud provider automatically provisions a LB for your service, creating an external IP for the service.

Task 1

Expose your frontend deployment with a NodePort service on port 80, on target-port 8080.

Expose your backend deployment with a ClusterIP service on port 80, on target-port 8080.

Find out the NodePort port of your new frontend service.

Then, get the IP of any of the nodes in our cluster, and then open that IP with the frontend service’s NodePort port in a browser. This is where the frontend service can be accessed publicly on the Internet.

Solution

Solution 1: Exposing a pod with service

Frontend:

  • kubectl expose deployment ez-frontend --port 80 --target-port 8080 --type NodePort
  • kubectl get svc # Note the second part of the Port 80:34567
  • kubectl get nodes -o wide
  • Paste one of the node IPs into a browser followed by the generated port number. http://1.2.3.4:34567

Backend:

  • kubectl expose deployment workshop-api-deployment --port 80 --target-port 8080


Improve this page