Install Kubernetes on Ubuntu
Hi all, in this post I would like to explain how to install kubernetes on ubuntu with default partitioning. I’ve decided to take the challenge of studying for my Kubernetes Administrator exam.
Installing Ubuntu
I have produced a simple video explaining how to Install Ubuntu 22.04.
Hosts Files
on each of your nodes you will need to specify the IP address and hostnames within the /etc/hosts file. Below is an example of my setup.
Installing Docker
If you watched my video above you should now have a clean install of Ubuntu 22.04, and are now ready to install Docker. Docker is a platform that allows developers to easily create, deploy, and run applications in containers. Containers are lightweight, portable, and self-sufficient, which makes them an ideal choice for building and deploying microservices. With Docker, developers can package their applications and all of their dependencies into a single container, which can then be run on any machine that supports Docker. This makes it easy to move applications between different environments and eliminates the need to worry about dependencies and configuration.
In time I will write some articles about Docker but for now more information can be seen at Docker.
enter the following in your shell:
1 |
wget -qO- https://get.docker.com/ | sh |
Once the process is complete, you’ll see output that gives you a tip regarding adding your local user to the docker group, which will allow you to manage containers without switching to the root account or using sudo.
1 |
sudo usermod -aG docker replace_with_you_username |
the next order or business is for us to create a new file, this fille allows us to tune the Docker deamon with additional parameters.
1 |
sudo nano /etc/docker/daemon.json |
add the following to the above file:
1 2 3 4 5 6 7 8 |
{ "exec-opts": ["native.cgroupdriver=systemd"], "log-driver": "json-file", "log-opts": { "max-size": "100m" }, "storage-driver": "overlay2" } |
in this file we are setting the cgroupdriver to systemd. cgroups is a built in feature of the Linux kernel that controls access to system resources (such as network, CPU, RAM, and so on) by processes that are running on our server.
Moving on we will need to edit another file /etc/sysctl.conf. inside the file you will find the following line
1 |
#net.ipv4.ip_forward=1 |
you need to uncomment this line, (remove the # at the beginning)
now reboot.
1 |
sudo reboot now |
Testing Docker
now lets make sure docker is working by running the “hello world” example.
1 |
sudo docker run hello-world |
if all being well you should see the below output as shown in the image:
Installing Kubernetes
Now that we have Docker installed it’s now time to install kubernetes on ubuntu. Kubernetes is an open-source container orchestration system that automates the deployment, scaling, and management of containerized applications. It allows you to manage a cluster of machines and schedule containers to run on those machines. With Kubernetes, you can easily scale your applications up or down to handle changes in traffic, automate rolling updates and rollbacks, and ensure that your applications are always running in a healthy state. Kubernetes also provides a number of other features such as automatic service discovery, load balancing, and storage orchestration. It is widely used in production environments for managing large scale and complex containerized workloads.
Disable swap & Add kernel Parameters
you will need to turn swapoff, and also edit the /etc/fstab file and comment out the line that mentions swap.
1 2 |
sudo swapoff -a sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab |
Load the following kernel modules on all the nodes,
1 2 3 4 5 6 |
sudo tee /etc/modules-load.d/containerd.conf <<EOF overlay br_netfilter EOF sudo modprobe overlay sudo modprobe br_netfilter |
Set the following Kernel parameters for Kubernetes
1 |
sudo nano /etc/sysctl.d/kubernetes.conf |
in the above newly created file add the following:
1 2 3 |
net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 net.ipv4.ip_forward = 1 |
Reload the above changes, run
1 |
sudo sysctl --system |
Containerd Setup
Next we need to configure containerd.
1 2 3 4 5 |
sudo mkdir -p /etc/containerd/ containerd config default | sudo tee /etc/containerd/config.toml sudo sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml |
then reboot again:
1 |
sudo reboot now |
Google Repository
Next we need to add google cloud repository:
1 |
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add |
This command retrieves the package signing key for the Google Cloud Platform package repositories, and adds it to the system’s list of trusted package signing keys.
The command uses “curl” to download the package signing key from the URL “https://packages.cloud.google.com/apt/doc/apt-key.gpg“. The “-s” flag tells curl to operate in silent mode, which means it will not output any progress information.
The output of the “curl” command is then piped (|) to the command “sudo apt-key add”, which adds the key to the system’s list of trusted package signing keys. This is necessary for the package manager to verify the authenticity of packages from that repository.
The package signing key is used by the package manager to check the integrity of the packages, and ensure that they haven’t been tampered with since they were signed by the key’s owner.
Adding the Kubernetes Repository
Now let’s add the Kubernetes repository:
1 |
sudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main" |
This command adds the Kubernetes package repository to the system’s list of package sources.
The “sudo apt-add-repository” command is used to add a new package repository to the system. It takes the repository URL as an argument.
The repository URL “deb http://apt.kubernetes.io/ kubernetes-xenial main” points to the Kubernetes package repository. The “deb” in the URL specifies that this is a Debian-based package repository. The rest of the URL specifies the location of the repository, in this case “http://apt.kubernetes.io/“, and the release or version of the package it contains “kubernetes-xenial” and the main branch of the packages in the repository.
After running this command, the package manager will be able to access the packages in the Kubernetes repository, and you will be able to install and update them using commands like “sudo apt update” and “sudo apt install”
Installing needed packages
Now lets install the needed kubernetes packages:
1 |
sudo apt install kubeadm kubelet kubectl |
This command installs the Kubernetes packages “kubeadm”, “kubelet”, “kubectl” and “kubernetes-cni” on an Ubuntu-based system using the package manager “apt”.
“kubeadm” is a command-line tool that helps to bootstrap a best-practice Kubernetes cluster on existing infrastructure. It’s used to create and manage the control plane nodes in a cluster.
“kubelet” is a process that runs on each node in the cluster, it is responsible for maintaining the desired state of the pods and containers running on that node.
“kubectl” is a command-line tool that allows you to interact with a Kubernetes cluster, it provides a way to manage and interact with the resources in the cluster, such as pods, services, and deployments.
Initialize the Cluster
now that Kubernetes is installed we can initialize the cluster on this node which will be the master node.
1 |
sudo kubeadm init --control-plane-endpoint=YOUR MASTER HOST NAME --pod-network-cidr=10.244.0.0/16 |
the above command will initialize the cluster, and also assign it a pod network. The Pod Network is an internal-only network that is not accessible from the outside.
after running the “sudo kubeadm init” command and if all being well after a few minutes you will see information on your screen as to what to do next, read though this information.
the next step is too perform the following:
1 2 3 |
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config |
we can now check if our cluster is running:
1 |
kubectl cluster-info |
Deploy Pod network
when you issue the following command:
1 |
kubectl get pods --all-namespaces |
you will see that the coredns pods are in the pending state. To resolve this lets deploy the Pod Network
1 |
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml |
after a minute or two if you issue the following command again, you will see that all pods are in a running state.
1 |
kubectl get pods --all-namespaces |
Conclusion
Congratulations you have now installed Kubernetes on Ubuntu 22.04.
Troubleshooting
if you get the following error when running “sudo kubeadm init”
[ERROR CRI]: container runtime is not running [Issue Encountered]
This is a common issue when you run the kubeadm init command while the CRI used is Containerd. In most cases, the issue is with the config.tomal file.
Fix the Error
To fix the error you can delete the config.tomal file and restart containerd then try the init command like below:
1 2 3 |
sudo rm /etc/containerd/config.toml sudo systemctl restart containerd sudo kubeadm init --pod-network-cidr=10.244.0.0/16 |