Adding load balancer to kubernetes

A load balancer routes request to clusters to optimize performance and ensure the reliability of our application. With a load balancer, the demands on our application are shared equally across clusters so that all available resources are utilized and no single cluster is overburdened


Create the yaml file in the editor of your choice which will be used to deploy the nginx pod

# vi nginx_pod.yaml

In this example:

•A Deployment named nginx is created, indicated by the metadata: name field.

•The Deployment creates two replicated Pods, indicated by the replicas field.

•The selector field defines how the Deployment finds which Pods to manage. In this case, we simply select one label defined in the Pod template (app: nginx). However, more sophisticated selection rules are possible, as long as the Pod template itself satisfies the rule.

•The Pod template’s specification, or template: spec field, indicates that the Pods run one container, nginx, which runs the nginx Docker Hub image at version 1.7.9.

•The Deployment opens port 80 for use by the Pods.

Create the nginx pod using kubectl

# kubectl create -f nginx_pod.yaml

replicationcontroller "nginx" created

In the above pod creation process, we have created two replicas of the nginx pod and its details can be listed as follows

# kubectl create -f nginx_pod.yaml

replicationcontroller "nginx" created

# kubectl get pods

NAME READY STATUS RESTARTS AGE

nginx-hzltb 0/1 ContainerCreating 0 1m

nginx-q79kx 0/1 ContainerCreating 0 1m

# kubectl get pods

NAME READY STATUS RESTARTS AGE

nginx-hzltb 1/1 Running 0 3m

nginx-q79kx 1/1 Running 0 3m

# kubectl get rc

NAME DESIRED CURRENT READY AGE

nginx 2 2 2 6m

This we have checked with our minions

#docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

b31e70918579 docker.io/nginx@sha256:285b49d42c703fdf257d1e2422765c4ba9d3e37768d6ea83d7fe2043dad6e63d "nginx -g 'daemon off" 2 minutes ago Up 2 minutes k8s_nginx_nginx-hzltb_default_f07a393a-0821-11e8-a4cf-080027dc1113_0

7a5318eec122 gcr.io/google_containers/pause-amd64:3.0 "/pause" 3 minutes ago Up 3 minutes k8s_POD_nginx-hzltb_default_f07a393a-0821-11e8-a4cf-080027dc1113_0

Deploy the nginx service using yaml file in order to expose the nginx pod on the host port “82”

# cat nginx_service.yaml

apiVersion: v1

kind: Service

metadata:

labels:

name: nginxservice

name: nginxservice

spec:

externalIPs:

- 10.9.54.100

ports:

# The port that this service should serve on.

- port: 82

# Label keys and values that must match to receive traffic for this service.

selector:

app: nginx

type: LoadBalancer

Created a Service which targets TCP port 82 on any Pod with the run: nginx label, and expose it on an abstracted Service port (targetPort: is the port the container accepts traffic on, port: is the abstracted Service port, which can be any port other pods use to access the Service). View service API object to see the list of supported fields in service definition and also we have provided external Ip for load balancing check

Create the nginx service using kubectl

# kubectl create -f nginx_service.yaml

service "nginxservice" created

The nginx service can be listed as follows

# kubectl get services

NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE

kubernetes ClusterIP 10.96.0.1 443/TCP 7d

nginxservice LoadBalancer 10.108.4.144 10.9.54.100 82:32371/TCP 2m

# kubectl describe service nginxservice

Name: nginxservice

Namespace: default

Labels: name=nginxservice

Annotations:

Selector: app=nginx

Type: LoadBalancer

IP: 10.108.4.144

External IPs: 10.9.54.101

Port: 82/TCP

TargetPort: 82/TCP

NodePort: 32371/TCP

Endpoints: 10.36.0.3:82,10.44.0.5:82

Session Affinity: None

External Traffic Policy: Cluster

Events:

Now the nginx server test page can be accessed on the following URL; HTTP::

Now we will delete the nginx pod and service

# kubectl delete service nginxservice

service "nginxservice" deleted

# kubectl delete rc nginx

Recent Comments

No comments

Leave a Comment