Kubernetes Part 3: Install Kubernetes on a Rasberry Pi 4

 









In my previous post. We have prepared the Rasperry Pi's with Ubuntu LTS 20.04 and Docker, so now we are going to install Kubernetes. One of the Raspberry Pi's (k8s-master) will function as a master node and the others will configerd as worker nodes. The master nodes requires some additional steps, which will be explained in this blog, so let's continue

Actions for Master and Worker nodes

- Add Kubernetes repository

sudo nano /etc/apt/sources.list.d/kubernetes.list

Add the following line:

deb http://apt.kubernetes.io/ kubernetes-xenial main

Add GPG key for security (to make sure you connect to the legit repository) with the following command

curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -

- Install required Kubernetes packages

sudo apt update
sudo apt install kubeadm kubectl kubelet

Note: If you get errors with the first command, wait a few minutes and try again.




Actions for the Master node only:

The following actions are only required for Raspberry Pi that will function as a master node:

-Initialize Kubernetes with following command

 sudo kubeadm init --pod-network-cidr=10.244.0.0/16

Once this runs, you will get some output that will include the join command, but don't join nodes yet. Copy this somewhere for later. It should look somthing like kubeadm join 192.168.1.204:6443 --token qt57zu.wuvqh64un13trr7x --discovery-token-ca-cert-hash sha256:5ad014cad868fdfe9388d5b33796cf40fc1e8c2b3dccaebff0b066a0532e9823

- Set up config directory

 mkdir -p ~.kube
 sudo cp /etc/kubernetes/admin.conf ~/.kube/config
 sudo chown $(id -u):$(id -g) $HOME/.kube/config

Go ahead and run those, but if it recommends different commands, run those instead.

- Install flannel network driver

 kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

Note: The lack of sudo is intentional

Once all of the pods have come up, run the join command on each worker node. 


Actions for all Workers nodes:

Log on the each worker node via SSH (Putty) and enter the command you had copied earlier. Like 

kubeadm join 192.168.1.204:6443 --token qt57zu.wuvqh64un13trr7x --discovery-token-ca-cert-hash sha256:5ad014cad868fdfe9388d5b33796cf40fc1e8c2b3dccaebff0b066a0532e9823

Remark: red are example values values. 

- Check status of nodes
See if the nodes have joined successfully, run the following command a few times until everything is ready:

 kubectl get nodes

You should see something similar like, then your kubernetes cluster is up and running





TIP: If you have only one Rasberry Pi to test Kubernetes and no worker nodes, you need to run the command. This allows standard pods running on the master node:

kubectl taint nodes --all node-role.kubernetes.io/master-

Remark: Only use the command above if you have no worker nodes installed.


- Create a test pod

You can create a test pod to see if kubernetes if working properly by creating a test pod. To create a test pod do the following:

nano pod.yml

Copy and paste to contact below and save the file (Ctrl-O, Ctrl-X)

 apiVersion: v1
 kind: Pod
 metadata:
   name: nginx-example
   labels:
     app: nginx
 spec:
   containers:
     - name: nginx
       image: linuxserver/nginx
       ports:
         - containerPort: 80
           name: "nginx-http"
---
 apiVersion: v1
 kind: Service
 metadata:
   name: nginx-example
 spec:
   type: NodePort
   ports:
     - name: http
       port: 80
       nodePort: 30080
       targetPort: nginx-http
   selector:
     app: nginx


bectl ap

Apply the pod yaml file via the command

 kubectl apply -f pod.yml


Check the status with:

kubectl get pods

Check the status with more info:

kubectl get pods  -o wide

Check the status of the service

kubectl get service

You should see something similar to this (yellow line)

Bijschrift toevoegen




You can access the nginx example via one the IP address you have given you Raspberry Pi via poort 30080.  (for example http://192.168.1.201:30080). You should the following website


 
If the test has run ok, you delete the test pod via the following command:

 kubectl delete -f pod.yml

In the Part 4 of the Kubernetes series the installation of the MetalLB load balancer will be explained.

Remark:
The blog is based on this wiki with some minor adjustmenst. If you run into issues don't hesitate to leave a comment. I would also recommend to watch this video video.

Comments

  1. Got an error, everything was going so well :)
    kubectl apply -f pod.yml
    error: error parsing pod.yml: error converting YAML to JSON: yaml: line 2: mapping values are not allowed in this context

    ReplyDelete
    Replies
    1. Thanks for the reply. It have tested it and had the same problem. Seems like there was a space somewhere in yaml file that not be. It should be working now, so you can try again.

      Delete
  2. I still had to remove the space from the start of each line, but it now works. Thank you!!

    ReplyDelete
    Replies
    1. I am thinking about placing these script on my Github account, so you can download them. Yaml files can be very picky on the syntax.

      Delete
    2. The example files now be download from Github at

      https://github.com/erik-de-bont/blog/tree/main/kubernetes

      Delete

Post a Comment