Post Image

Kubernetes Microk8s First Deployment with Load Balancer

In this post I will guide you through your first deployment using a LoadBalancer as a service to expose your deployment. I provide a yaml file that you can use and walk you through deploying it and validating it is functioning. The container image I deploy is the basic nginx image available that hosts the default nginx web page.

 

Prerequisites

You will need to have MetalLB installed and configured or some sort of load balancer. In this example I have MetalLB configured and a range of IP addresses to pick from to assign to my service allowing me to provide external access to the service into my cluster.

 

 

Deployment YAML

Below I have the YAML file that will create a service named nginx that exposes port 80 externally to port 80 of any pod named 'nginx' and use a load balancer IP address of 10.204.255.50. This will forward any traffic that hits that address on port 80 to one of my pods in the nginx deployment.

 

Next I have the nginx deployment which spins up a pod with the nginx container image listening on port 80.

apiVersion: v1
kind: Service
metadata:
  name: nginx
  annotations:
spec:
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: nginx
  type: LoadBalancer
  loadBalancerIP: 10.204.255.50
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

 

Deploy it

Below is the command to deploy.

$ microk8s kubectl apply -f /path/to/your/yamlfile.yaml

 

Verify

You can verify it is running by running the following command and verifying that you have a pod, service, deployment, and replicaset similar to below. Your IP addresses should be different but everything should be running and in its desired state.

$ microk8s kubectl get all
NAME                                    READY   STATUS    RESTARTS   AGE
pod/nginx-deployment-7fb96c846b-6hcmh   1/1     Running   0          13s

NAME                 TYPE           CLUSTER-IP       EXTERNAL-IP     PORT(S)        AGE
service/nginx        LoadBalancer   10.152.183.150   10.204.255.50   80:32483/TCP   14s

NAME                               READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx-deployment   1/1     1            1           14s

NAME                                          DESIRED   CURRENT   READY   AGE
replicaset.apps/nginx-deployment-7fb96c846b   1         1         1       13s
$

 

Now from your workstation open a browser and enter the external IP address that is listed and you should be presented with the default nginx web page.

 

 



Comments (0)
Leave a Comment