Getting Started with Kubernetes on Linux

Update 01-24-21: there’s new blog to succeed this article here.

1. Install Kubectl

Redhat-based commands:

# Add a new repo and use Yellowdog Updater Modified (yum) install
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
yum install -y kubectl

Debian-based commands:

# Obtain the kubectl public key for Apt. Pipe key to main then append output to apt sources directory 
sudo apt-get update && sudo apt-get install -y apt-transport-https
curl -s | sudo apt-key add -
echo "deb kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list
sudo apt-get update
sudo apt-get install -y kubectl

2. Install Minikube

Although Minikube is useful for testing and development purposes, labs and production would necessitate multi-node Kubernetes systems as illustrated here.

For purposes of this article, Minikube is an implementation of Kubernetes cluster onto localhost using kvm2 driver or VirtualBox (prerequisites). VT-x/AMD-v virtualization must also be enabled in system BIOS for this to work. It’s a recommended step to test this simple deployment prior to applying more advanced deployments. Minikube works by starting a single node kubernetes cluster locally. It conveniently packages and configures a Linux VM, Docker and all Kubernetes components, optimized for local environment, a la carte.

First, install the virtual environment dependency. Minikube requires either VirtualBox or KVM. Since the latter is more robust and production-grade, here are some commands for various popular linux flavors.

Debian-based systems:

sudo apt -y install libvirt-clients libvirt-daemon-system qemu-kvm libvirt-bin virt-top libosinfo-bin libguestfs-tools virtinst bridge-utils
sudo modprobe vhost_net
sudo lsmod | grep vhost
echo "vhost_net" | sudo tee -a /etc/modules

Redhat-based systems:

sudo yum install libvirt-daemon-kvm qemu-kvm
sudo systemctl enable libvirtd.service
sudo systemctl start libvirtd.service

# Add current login to libvirt group
sudo usermod -a -G libvirt $(whoami)

# Join group
newgrp libvirt

# Install driver
curl -LO \
&& sudo install docker-machine-driver-kvm2 /usr/local/bin/

Second, Minikube is then ready to be plugged into the system. Here’s a manual method:

curl -LO && sudo install minikube-linux-amd64 /usr/local/bin/minikube

Minikube Commands:

# Set kvm2 as default driver for minikube, instead of VirtualBox
minikube config set vm-driver kvm2
# Initiate minikube
minikube start
# Deploy a hello-world container
kubectl run hello-minikube --port=8080
# Expose deployment service to the external network zone (NodePort)
kubectl expose deployment hello-minikube --type=NotePort
# List all pods of current deployment
kubectl get pod
# Check exposed hello-minikube html contents
curl $(minikube service hello-minikube --url)
# Delete the deployment
kubectl delete deployment hello-minikube
# Stop the local cluster
minikube stop
# Terminate minikube
minikube delete

Leave a Reply

Your email address will not be published. Required fields are marked *