Install Kubernetes on Ubuntu 20.04 with CRI-O
This tutorial will show you how to install Kubernetes on Ubuntu Linux 20.04 using CRI-O as the container runtime and Calico for the Pod network add-on.
Overview of steps
First we will prepare the Ubuntu server for Kubernetes by disabling Swap, configuring the network and installing CRI-O. Then we will install kubeadm and Kubernetes. Finally, we will use kubeadm to create a single node cluster and deploy the Calico Pod network add-on.
Software versions:
- Kubernetes v1.23.4
- CRI-0 v1.23.1
- Calico
Step 1: Disable Swap
As of writing, Kubernetes requires that Swap is disabled on the Linux server so that the kubelet process can reliably schedule memory to the pods. To disable swap, run the following command:
sudo swapoff -a
To make the change permanent, you will need to edit the fstab
file.
Open /etc/fstab
, remove the line containing swap and then save the file.
/swap.img none swap sw 0 0
This will prevent swap from being enabled the next time the system boots.
Step 2: Configure the Network
- Assign a static IP address (I’ve used 10.1.1.11 in this guide) - Tutorial
- Assign a hostname (I’ve used k8s-master in this guide) - Tutorial
- Configure iptables to see bridged traffic.
# Create the .conf file to load the modules at bootup
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# Set up required sysctl params, these persist across reboots.
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sudo sysctl --system
Step 3: Install CRI-O
Add the Kubic repository because it contains the binary packages of CRI-O.
OS=xUbuntu_20.04
VERSION=1.23
# Add Kubic Repo
echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/ /" | \
sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list
# Import Public Key
curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/Release.key | \
sudo apt-key add -
# Add CRI Repo
echo "deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$VERSION/$OS/ /" | \
sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:$VERSION.list
# Import Public Key
curl -L https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$VERSION/$OS/Release.key | \
sudo apt-key add -
Update the package list and then install CRI-O.
sudo apt update
sudo apt install cri-o cri-o-runc cri-tools -y
Enable the CRI-O service so that it starts when Ubuntu does.
sudo systemctl enable crio.service
Start the CRI-O service now.
sudo systemctl start crio.service
You can check to see if the container runtime is ready by running.
sudo crictl info
{
"status": {
"conditions": [
{
"type": "RuntimeReady",
"status": true,
"reason": "",
"message": ""
},
{
"type": "NetworkReady",
"status": false,
"reason": "NetworkPluginNotReady",
"message": "Network plugin returns error: No CNI configuration file in /etc/cni/net.d/. Has your network provider started?"
}
]
}
}
Step 4: Install Kubernetes
Download the public signing key.
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://packages.cloud.google.com/apt/doc/apt-key.gpg
Add the Kubernetes apt repository:
echo "deb [signed-by=/usr/share/keyrings/kubernetes-archive-keyring.gpg] https://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
Update apt package index, install kubelet, kubeadm and kubectl, and pin their version:
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
Step 5: Create Cluster
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
Make kubectl work for current user.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Remove the taints from this node so that it can be used as a worker node.
kubectl taint nodes --all node-role.kubernetes.io/master-
Step 6: Install Calico Pod network add-on
Run the following commands to deploy the Calico Pod network add-on:
kubectl apply -f https://projectcalico.docs.tigera.io/manifests/calico.yaml
You can check to see if the pods have started with the following watch command:
watch kubectl get pods -n kube-system
Wait until each pod has the STATUS
of Running
then terminate the watch utility.
Check to see if the node is ready with the following command:
kubectl get nodes -o wide
You should see an output like the following:
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
k8s-master Ready control-plane,master 3m55s v1.23.4 10.1.1.11 <none> Ubuntu 20.04.4 LTS 5.4.0-100-generic cri-o://1.23.1
Conclusion
Congrats! You now have a single node Kubernetes cluster running on Ubuntu 20.04. We used CRI-O as a lightweight alternative to the Docker container runtime and we used Calico for the Pod network. You’re now ready to deploy some pods or join some worker nodes to the cluster.