All Linux HowTo's Kubernetes

Kubernetes (MicroK8s) Part 2 – Replica Sets and Scaling on Ubuntu 20.04

This article continues from Part 1 – Installation and configuration.

We can create a replica set (replicateset) or “rs” for short, so we can scale an application to meet demand. Create a file on the Kubernetes host called “my-rep-set.yaml” and populate it with the following:

Tip: This is just an example but it will work. Note the “replicas: 5” which means we’ll get 5 instances. We’ll change this later.

apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: nginx-proxy
  labels:
    app: nginx-proxy
    tier: frontend

spec:
  replicas: 5
  selector:
    matchLabels:
      tier: frontend
  template:
    metadata:
      labels:
        tier: frontend
    spec:
      containers:
      - name: nginx
        image: nginx

And then create the rs with this command:

sudo microk8s kubectl apply -f my-rep-set.yaml

You can see the state of your new rs using the following command:

sudo microk8s kubectl get replicaset
NAME          DESIRED   CURRENT   READY   AGE
nginx-proxy   5         5         5       2m29s

You can scale it up to 6 using the following command:

sudo microk8s kubectl scale -n default replicaset nginx-proxy --replicas=6

And confirm it:

sudo microk8s kubectl get replicaset
NAME          DESIRED   CURRENT   READY   AGE
nginx-proxy   6         6         6       5m11s

TIP: You can delete an rs using this command. Replace “nginx-proxy” with whatever rs you want to remove, and repalce “default” with the namespace you’re working in:

sudo microk8s kubectl delete -n default replicaset nginx-proxy

Now we can expose our new rs to the wider network:

sudo microk8s kubectl expose rs nginx-proxy --type=NodePort --port=80 --name=nginx-proxy

Get a summary of the pods, services and ports:

sudo microk8s kubectl get services
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
kubernetes    ClusterIP   10.152.183.1            443/TCP        7h56m
agix-nginx    NodePort    10.152.183.91           80:30045/TCP   5h53m
nginx-proxy   NodePort    10.152.183.48           80:32538/TCP   80s

The one we care about is the “nginx-proxy” which is on port “32538”.

Allow it thought the local firewall:

ufw allow 32538

And now you can browse to it:

http://example.com:32538

But as you’ve guessed, working with ports that are generated at random isn’t going to work in the real world. We’ll deal with that in Part 3.

Similar Posts:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.