Setting up Kubernetes on Ubuntu is a journey that transforms your infrastructure into a container orchestration powerhouse, enabling you to deploy, scale, and manage application containers across a cluster of machines. Kubernetes, also known as K8s, automates the deployment, scaling, and operation of application containers, making it easier for developers and sysadmins to manage applications.
This article provides a comprehensive guide to setting up Kubernetes on Ubuntu, covering everything from installation to operation. Whether you’re setting up a single-node cluster for development or a multi-node cluster for production, this guide will walk you through the necessary steps to get your Kubernetes cluster up and running.
Prerequisites
Before starting, ensure you have:
- One or more machines running Ubuntu 18.04 LTS or later, each with at least 2GB of RAM and 2 CPUs.
- A user account with sudo privileges on each machine.
- The machines are networked together, able to communicate with each other.
Step 1: Preparing Your Environment
- Update Your System: Start by updating your system to ensure all packages are up to date.
sudo apt-get update
sudo apt-get upgrade -y
- Install Docker: Kubernetes uses Docker as its container runtime. Install Docker on each machine.
sudo apt-get install docker.io -y
After installation, add your user to the Docker group to run Docker commands without sudo.
sudo usermod -aG docker $USER
- Disable Swap: Kubernetes requires you to disable swap on each machine.
sudo swapoff -a
To make this change permanent, comment out any swap entries in /etc/fstab.
Step 2: Install Kubernetes Components
- Add Kubernetes Repository: Add the Kubernetes package repository to each machine.
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add -
echo "deb http://apt.kubernetes.io/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list
- Install Kubernetes: Install kubeadm, kubelet, and kubectl on each machine.
sudo apt-get update
sudo apt-get install -y kubelet kubeadm kubectl
Pin their versions to prevent automatic updates.
sudo apt-mark hold kubelet kubeadm kubectl
Step 3: Initialize Your Cluster
On the master node, initialize the cluster using kubeadm.
sudo kubeadm init --pod-network-cidr=192.168.0.0/16
After initialization, follow the on-screen instructions to start using your cluster. This typically involves running the following commands as a regular user.
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
Step 4: Install a Pod Network
Install a pod network add-on on your cluster so that your pods can communicate with each other. Calico is a popular choice that can be installed with:
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
Step 5: Join Nodes to the Cluster
To add worker nodes to your cluster, use the kubeadm join command provided at the end of the master’s initialization process. Run this command on each worker node.
sudo kubeadm join <master-ip>:<port> --token <token> --discovery-token-ca-cert-hash sha256:<hash>
Step 6: Deploy Applications
With your cluster up and running, you’re ready to deploy applications. You can start by deploying a simple application using a deployment YAML file.
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
Apply this deployment using: kubectl apply -f <filename>.yaml
.
Step 7: Scale and Manage Your Cluster
Scale your applications by adjusting the number of replicas in your deployment, and manage your cluster resources using kubectl, Kubernetes’ command-line tool. Explore more complex deployments, set up Kubernetes Dashboard for a GUI, and consider installing Helm for package management.
Conclusion
Congratulations! You have successfully set up a Kubernetes cluster on Ubuntu. This cluster is now ready for you to deploy applications. Remember, Kubernetes is a powerful tool, and there’s much more to learn about its features and capabilities. Experiment with deploying different applications, scaling them, and exploring Kubernetes’ many features to get comfortable managing your containerized environments.