How to install kubernetes on ubuntu 20.04
Kubernetes is new linux os at nowadays. This is manually install kubernetes on ubuntu 20.04 with kubeadm.
I have 3 proxmox ve servers. then I plan to install 3 ubuntu virtual machines on eache proxmox ve server.
No. | Name | OS | IP ADDRESS |
---|
01 | ubuntu-k8s-master | ubuntu 20.04 LTS | 192.168.11.71 |
02 | ubuntu-k8s-node01 | ubuntu 20.04 LTS | 192.168.11.72 |
03 | ubuntu-k8s-node02 | ubuntu 20.04 LTS | 192.168.11.73 |
Here is my plan to install kubernetes:
- Create ubuntu 20.04 cloud image on 3 proxmox ve server
- Delpy 3 ubuntu 20.04 virtual machines on each 3 proxmox ve server.(1 for kubernetes master node, other 2 for kubernetes work nodes)
- Install kubeadm,kubelet,kubectl,containerd on each nodes(include master node and worker nodes)
- Initalize Kubernetes on master node and install calico network CNI
- Let worker nodes join kubernets cluster.
Provision 3 ubuntu server on 3 proxmox ve nodes
Create ubuntu 20.04 template on 3 proxmox ve nodes
Run following command on eache proxmox ve server to create ubuntu 20.04 server template with cloudinit enabled.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
| cat << 'EOF' | tee create_ubuntu_20.04_template.sh
#!/bin/bash
VMID=9003
IMAGE_NAME="focal-server-cloudimg-amd64.img"
VM_NAME="ubuntu-20.04TLS-template"
STORAGE="SSD"
STORAGE_FULL_PATH="/mnt/pve"
VCPUS=4
VM_MEMORY=4096
#STORAGE_FULL_PATH="/var/lib/vz"
# Download ubuntu 20.04 LTS cloud image
[ ! -f /var/lib/vz/template/qcow/focal-server-cloudimg-amd64.img ] && curl -fsSL https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img -o /var/lib/vz/template/qcow/focal-server-cloudimg-amd64.img
qm create $VMID --name ${VM_NAME} \
--memory ${VM_MEMORY} \
--net0 virtio,bridge=vmbr0 \
--cores ${VCPUS} \
--sockets 1 \
--cpu cputype=host \
--description "${VM_NAME}" \
--kvm 1 \
--machine q35
qm importdisk $VMID /var/lib/vz/template/qcow/${IMAGE_NAME} ${STORAGE}
qm set $VMID --scsihw virtio-scsi-pci --virtio0 ${STORAGE}:${VMID}/vm-$VMID-disk-0.raw
qm set $VMID --serial0 socket
qm set $VMID --boot c --bootdisk virtio0
qm set $VMID --agent 1
qm set $VMID --hotplug disk,network,usb
qm set $VMID --vcpus ${VCPUS}
qm set $VMID --vga qxl
qm set $VMID --name ${VM_NAME}
qm set $VMID --ide2 ${STORAGE}:cloudinit
qm set $VMID --serial0 socket --vga serial0
qm set $VMID --sshkey /etc/pve/pub_keys/pub_key.pub
qm set $VMID --ciuser ubuntu
qm set $VMID --cipassword MyPassW0rd
qm set $VMID --ipconfig0 ip=dhcp
qm resize $VMID virtio0 +50G
qemu-img convert -f raw ${STORAGE_FULL_PATH}/${STORAGE}/images/$VMID/vm-$VMID-disk-0.raw -O qcow2 ${STORAGE_FULL_PATH}/${STORAGE}/images/$VMID/vm-$VMID-disk-0.qcow2
sed -i 's/.raw/.qcow2/g' /etc/pve/qemu-server/${VMID}.conf
rm -rf ${STORAGE_FULL_PATH}/${STORAGE}/images/${VMID}/*.raw
qm set $VMID --template
EOF
|
Run the command on proxmxo ve server to create ubuntu server 20.04 template
1
2
| # Run script to create ubuntu 20.04 template
bash create_ubuntu_20.04_template.sh
|
Create 3 kubernetes nodes from template
Create ubuntu server 20.04 as kubernetes nodes from template
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # Run commands on proxmox ve server 01
qm clone 9003 113 --full --name ubuntu-k8s-master
qm set 113 --ipconfig0 ip=192.168.11.71/24,gw=192.168.11.1
qm set 113 --onboot 1
# Run commands on proxmox ve server 02
qm clone 9003 113 --full --name ubuntu-k8s-node01
qm set 113 --ipconfig0 ip=192.168.11.72/24,gw=192.168.11.1
qm set 113 --onboot 1
# Run commands on proxmox ve server 03
qm clone 9003 113 --full --name ubuntu-k8s-node02
qm set 113 --ipconfig0 ip=192.168.11.73/24,gw=192.168.11.1
qm set 113 --onboot 1
|
Install Prerequest packects
Change ubuntu sourcelist and update OS then reboot it if necessary.
Change ubuntu sourcelist and update packages
1
2
3
4
5
6
7
| source /etc/os-release
echo $VERSION_CODENAME
[ ! -f /etc/apt/sources.list.bak ] &&mv /etc/apt/sources.list{,.bak}
[ ! -f /etc/apt/sources.list ] &&curl -fsSL https://mirrors.ustc.edu.cn/repogen/conf/ubuntu-https-4-${VERSION_CODENAME} -o /etc/apt/sources.list
sudo apt-get update
sudo apt -y full-upgrade
[ -f /var/run/reboot-requried ] && sudo reboot -f
|
Forwarding IPv4 and letting iptables see bridged traffic
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| cat <<EOF | sudo tee /etc/modules-load.d/kubernetes.conf
overlay
br_netfilter
EOF
sudo modprobe overlay
sudo modprobe br_netfilter
# sysctl params required by setup, params persist across reboots
cat <<EOF | sudo tee /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# Reload sysctl
sudo sysctl --system
|
Install containerd runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| sudo apt-get update
#sudo apt-get install ca-certificates curl gnupg lsb-release
# Install required packages
sudo apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates lsb-release
# Add Docker's offical GPG key
sudo mkdir -p /etc/apt/keyrings
[ ! -f /etc/apt/keyrings/docker.gpg ] &&curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
[ ! -f /etc/apt/sources.list.d/docker.list ] &&echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
# Install
sudo apt update
sudo apt install -y containerd.io
|
Modify containerd configuration
1
2
3
4
5
6
7
8
| [ ! -f /etc/containerd ] && mkdir -p /etc/containerd
sudo rm -rf /etc/containerd/config.toml
sudo containerd config default | sudo tee /etc/containerd/config.toml
#set plugins.cri.systemd_cgroup = true in /etc/containerd/config.toml
sudo sed -i 's/SystemdCgroup\ =\ false/SystemdCgroup\ =\ true/g' /etc/containerd/config.toml
sudo systemctl restart containerd
sudo systemctl enable containerd
|
Disable SWAP
1
2
3
4
5
6
| # sudo sed -i '/swap/d' /etc/fstab
# Search for a swap line and add # (hashtag) sign in front of the line.
sudo sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab
sudo swapoff -a
sudo mount -a
free -h
|
Install kubectl,kubeadm, kubelet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
| sudo apt-get install -y ca-certificates curl apt-transport-https vim git curl wget
# if you use Debian 9(stretch) or earlier you would also need to install `apt-transport-https`
sudo apt-get install -y apt-transport-https
# Download the Google Cloud public signing key
[ ! -f /usr/share/keyrings/kubernetes-archive-keyring.gpg ]&&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
[ ! -f /etc/apt/sources.list.d/kubernetes.list ] && 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
apt update
# Chek kubeadm version for us to select the right version of kubernete to install
apt-cache madison kubeadm
#1.23.10-00
sudo apt-get install -y kubelet=1.23.10-00 kubeadm=1.23.10-00 kubectl=1.23.10-00
# Provent auto update new version
# Hold the current version
sudo apt-mark hold kubelet kubeadm kubectl
# Verify whether kubectl has been successfully installed by running the following command upon the completion of the previous steps:
kubectl version --client
kubeadm version
kubelet --version
|
Setup master node
Inital Kubernetes master node
1
2
3
4
5
6
7
| sudo kubeadm config images pull
kubeadm init --pod-network-cidr=10.244.0.0/16 --upload-certs
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
|
Install network CNI calico on master node
1
2
3
4
5
|
kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
# Monitor pod status
watch kubectl get pods -n calico-system
kubectl get pods -n calico-system -w
|
Option: Single kubernetes nodes to remove taint on master nodes
1
2
3
| # Single node k8s
#kubectl taint nodes --all node-role.kubernetes.io/master-
#kubectl taint nodes --all node-role.kubernetes.io/control-plane-
|
Option: query join command
1
2
| # if you forget the join information, you can query it with commands as show below
kubeadm token create --print-join-command
|
Option: Reset nodes and try it agaion
1
2
| sudo su
kubeadm reset -f
|
Setup Worker Nodes
1
2
3
4
| sudo kubeadm config images pull
kubeadm join 192.168.11.71:6443 --token akn012.rp0e7oxw0qn7b5o3 \
--discovery-token-ca-cert-hash sha256:f78548da4af356ea8b006531962673b5945dd1c36588e137c6ec44c99d4ad7e1
|
Check Node status
1
2
| kubectl get nodes
kubectl get pods -A -o wide
|
Reference
Deploy metrics-server
Deploy kubevirt
Kubernetes Kubevirt
DEPLOY A KUBERNETES CLUSTER USING ANSIBLE
Install Kubernetes Cluster on Ubuntu 20.04 with kubeadm