Monday, October 4, 2021

Types of Service on Kubernetes

Introduction

There are 4 types of service by which we can expose our deployments on Kubernetes. I am using docker-desktop as Kubernetes local cluster.

The application running in pods has their own IP address, they are given a single DNS name for a set od pods. Managing the connection of these pods to external world is not easy, we use K8 services resources to overcome this challenge. Services are an abstract way to expose an application running on a set of pods.

LoadBalancer

We are using the https://github.com/GoogleCloudPlatform/kubernetes-engine-samples/tree/main/hello-app app for deployment. It's already available publicly as docker image.

Load balancer type of service exposes the pod to external world.




I have created a deployment using below command:
$ kubectl create deployment hello-server --image=us-docker.pkg.dev/google-samples/containers/gke/hello-app:1.0
deployment.apps/hello-server created
Check the status using below command:
$ kubectl get all
NAME                                READY   STATUS    RESTARTS   AGE
pod/hello-server-5bd6b6875f-8p2c2   1/1     Running   0          18s

NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.96.0.1    <none>        443/TCP   47m

NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-server   1/1     1            1           18s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-server-5bd6b6875f   1         1         1       18s
We will expose the above application running in pods using the load balancer type of service using below command:
$ kubectl expose deployment hello-server --type LoadBalancer --port 80 --target-port 8080
service/hello-server exposed
Check the status using below command:
$ kubectl get all
NAME                                READY   STATUS    RESTARTS   AGE
pod/hello-server-5bd6b6875f-8p2c2   1/1     Running   0          65s

NAME                   TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
service/hello-server   LoadBalancer   10.111.116.63   localhost     80:30770/TCP   5s
service/kubernetes     ClusterIP      10.96.0.1       <none>        443/TCP        47m

NAME                           READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/hello-server   1/1     1            1           65s

NAME                                      DESIRED   CURRENT   READY   AGE
replicaset.apps/hello-server-5bd6b6875f   1         1         1       65s
If you were running it on GCP then the service was accessible on External-Ip of the service. But here we are running on minikube so you need a tunnel. 

Run minikube tunnel  in a new terminal: 

$  minikube tunnel
🏃  Starting tunnel for service hello-server.
Open the browser  http://localhost/ :



To delete the service:

$ kubectl delete svc hello-server
service "hello-server" deleted

NodePort

For exposing the NodePort Service for a set of pods where each port listens on `targetPort` and maps it to `port` :
$ kubectl expose deployment hello-server --type NodePort --port 8080
service/hello-server exposed
Use port-forward to see the response in the browser:
$ kubectl port-forward service/hello-server 7080:8080
Forwarding from 127.0.0.1:7080 -> 8080
Forwarding from [::1]:7080 -> 8080

Open the browser  http://localhost:7080/ :

To delete the service:

$ kubectl delete svc hello-server
service "hello-server" deleted

ClusterIP


It is the default type. To configure service, use below command:

$ kubectl expose deployment hello-server --type=ClusterIP --port=80 --target-port 8080
service/hello-server exposed

Check the status of the service:

$ kubectl get svc
NAME           TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
hello-server   ClusterIP   10.98.172.12   <none>        80/TCP    15s
We can access the service via browser by ingress manifest. But you can check via entering the pod.

$ kubectl get po
NAME                            READY   STATUS    RESTARTS   AGE
hello-server-5bd6b6875f-8p2c2   1/1     Running   0          37m
Use the pod name, to login into the pod:
$ kubectl exec -it hello-server-5bd6b6875f-8p2c2 -- sh
/ #
There is no curl in the container. Install curl:
$ apk add --no-cache curl
/ # apk add --no-cache curl
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/main/x86_64/APKINDEX.tar.gz
fetch https://dl-cdn.alpinelinux.org/alpine/v3.14/community/x86_64/APKINDEX.tar.gz
(1/5) Installing ca-certificates (20191127-r5)
(2/5) Installing brotli-libs (1.0.9-r5)
(3/5) Installing nghttp2-libs (1.43.0-r0)
(4/5) Installing libcurl (7.79.1-r0)
(5/5) Installing curl (7.79.1-r0)
Executing busybox-1.33.1-r3.trigger
Executing ca-certificates-20191127-r5.trigger
OK: 8 MiB in 19 packages
In the container, make a request to your Service by using your cluster IP address and port 80. Notice that 80 is the value of the port field of your Service. This is the port that you use as a client of the Service.

$ kubectl exec -it hello-server-5bd6b6875f-8p2c2 -- sh
/ # curl http://10.98.172.12/
Hello, world!
Version: 1.0.0
Hostname: hello-server-5bd6b6875f-8p2c2
To delete the service:

$ kubectl delete svc hello-server
service "hello-server" deleted

You can create an ingress and access it.

HeadLess

I will soon



Happy Coding !!!




No comments:

Post a Comment