CRI-O is a lightweight container runtime alternative to Docker for Kubernetes. It serves as an implementation of Kubernetes CRI (Container Runtime Interface) and complies with OCI runtime (Open Container initiative) standards. With CRI-O, Kubernetes can execute containers directly, without any additional tools or code adjustments. It supports multiple image formats, including Docker, and provides functionality for managing container images, overlay filesystems, container process lifecycle, monitoring, logging, and resource isolation.
In this tutorial, we will guide you through the installation of the CRI-O Container Runtime on an Ubuntu 22.04 server. We will also cover the setup of the CNI (Container Network Interface) plugin with CRI-O and the basic usage of “cri-tools” for managing Pods and containers.
Prerequisites
Before we begin, make sure you have the following requirements in place:
- An Ubuntu 22.04 server – For this guide, we will use the Ubuntu Server with the hostname “server-ubuntu” and the server IP address “192.168.5.10”.
- A non-root user with root/administrator privileges.
Installing CRI-O Container Runtime
There are different ways to install CRI-O, including using the APT command or building and installing it from the source. In this example, we will install the CRI-O Container Runtime through a third-party repository.
First, let’s set up the environment variables “$OS” and “$CRIO_VERSION” by running the following command:
export OS=xUbuntu_22.04 export CRIO_VERSION=1.24
Next, add the CRI-O repository for Ubuntu 22.04 server by executing the following commands:
echo "deb https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable.list echo "deb http://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable:/cri-o:/$CRIO_VERSION/$OS/ /" | sudo tee /etc/apt/sources.list.d/devel:kubic:libcontainers:stable:cri-o:$CRIO_VERSION.list
Now, add the GPG key for the CRI-O repository:
curl -L https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:cri-o:$CRIO_VERSION/$OS/Release.key | sudo apt-key add - curl -L https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/$OS/Release.key | sudo apt-key add -
Update your Ubuntu repository and refresh the package index:
sudo apt update
To check the CRI-O package version, run the following command:
sudo apt info cri-o
Install the CRI-O container runtime using the apt command:
sudo apt install cri-o cri-o-runc
After the installation is complete, start the CRI-O service and enable it to run at system boot:
sudo systemctl start crio sudo systemctl enable crio
Verify the CRI-O service by running the following command:
sudo systemctl status crio
Installing CNI (Container Network Interface) Plugin
The CNI (Container Network Interface) plugin is required for the CRI-O container runtime. It allows you to set up networking on containers and Pods. We will install the CNI plugin from the official Ubuntu repository and configure it with CRI-O.
To install the CNI plugin, run the following command:
sudo apt install containernetworking-plugins
Next, edit the CRI-O configuration file “/etc/crio/crio.conf” using the following command:
sudo nano /etc/crio/crio.conf
Uncomment the “networkdir” and “plugindirs” options in the “[crio.network]” section. Also, add the CNI plugin directory “/usr/lib/cni/” to the “plugin_dirs” option:
[crio.network] network_dir = "/etc/cni/net.d/" plugin_dirs = ["/opt/cni/bin/","/usr/lib/cni/"]
Save and close the file.
Remove the default bridge CNI configuration file “/etc/cni/net.d/100-crio-bridge.conf” and download the new bridge CNI configuration file “/etc/cni/net.d/11-crio-ipv4-bridge.conf” which enables only IPv4 for Pods and containers:
rm -f /etc/cni/net.d/100-crio-bridge.conf sudo curl -fsSLo /etc/cni/net.d/11-crio-ipv4-bridge.conf https://raw.githubusercontent.com/cri-o/cri-o/main/contrib/cni/11-crio-ipv4-bridge.conf
Restart the CRI-O service to apply the new changes to the CNI plugin settings:
sudo systemctl restart crio
Verify the CRI-O service once again:
sudo systemctl status crio
Installing the “cri-tools” Package
The “cri-tools” package includes the command-line utility “crictl” that allows interaction with the CRI-O container runtime. It can be used with various container runtimes, including CRI-O, containerd, dockershim, and cri-dockerd.
To install the “cri-tools” package, run the following command:
sudo apt install cri-tools
After the installation is complete, you can check the current runtime version by running the following command:
crictl version
To check the status of the current Container Runtime and CNI Network Plugin, run the following command:
crictl info
To enable auto-completion for the “crictl” command in your shell, generate the command-completion for the Bash shell by running the following command:
crictl completion > /etc/bash_completion.d/crictl source ~/.bashrc
Now, when you run the “crictl” command and press TAB, you will see all available command completions.
Creating Pods and Containers using crictl
With “cri-tools” installed, let’s create a Pod sandbox and a container using the “crictl” command. In this example, we will create a Pod for an Nginx container.
Create a new directory “~/demo” by running the following command:
mkdir ~/demo/
Create a new JSON configuration file to define the Pod sandbox for the container:
cat <<EOF | tee ~/demo/sandbox_nginx.json { "metadata": { "name": "nginx-sandbox", "namespace": "default", "attempt": 1, "uid": "hdishd83djaidwnduwk28bcsb" }, "linux": {}, "log_directory": "/tmp" } EOF
Run the following command to start the Pod sandbox:
sudo crictl runp ~/demo/sandbox_nginx.json
To check the running Pods, run the following command:
sudo crictl pods
To inspect the details of a Pod, use the following command:
sudo crictl inspectp --output table <pod_id>
Download the Nginx image by running the following command:
sudo crictl pull nginx
To check the list of available images, run the following command:
sudo crictl images
Create a new JSON file to define the Nginx container:
cat <<EOF | tee ~/demo/container_nginx.json { "metadata": { "name": "nginx" }, "image": { "image": "nginx" }, "log_path": "nginx.0.log", "linux": {} } EOF
Create a new container within the Pod sandbox:
sudo crictl create <pod_id> ~/demo/container_nginx.json ~/demo/sandbox_nginx.json
Start the Nginx container:
sudo crictl start<container_id>
To check the running containers, use the following command:
sudo crictl ps
Access the Nginx container via the IP address of the Pod sandbox by running the following command:
curl <pod_ip_address>
Conclusion
Congratulations! You have successfully installed and configured the CRI-O Container Runtime with the CNI Plugin on your Ubuntu 22.04 server. You can now use it as the container runtime for your Kubernetes cluster. Additionally, you have learned the basics of managing Pods and containers using the “crictl” command.
If you are looking for a reliable and scalable cloud hosting solution, consider Shape.host’s Cloud VPS services. With Shape.host, you can experience efficient and secure cloud hosting tailored to your specific needs.