Created
December 16, 2023 05:26
-
-
Save INDIAN2020/da391cf21c22b2b3747bfd17d3b9e537 to your computer and use it in GitHub Desktop.
Kubernetes Cluster for CKAD/CKA practice with Vagrant + VirtualBox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Directory structure: | |
kubernetes-lab | |
Vagrantfil | |
bootstrap.sh | |
master.sh | |
worker.sh | |
──────────────────────────────────────────────── cat Vagrantfile ──────────────────────────────────────────────── | |
# -*- mode: ruby -*- | |
# vi: set ft=ruby : | |
ENV['VAGRANT_NO_PARALLEL'] = 'yes' | |
Vagrant.configure(2) do |config| | |
config.vm.provision "shell", path: "bootstrap.sh" | |
config.vm.synced_folder ".", "/vagrant", type: "virtualbox" | |
# Kubernetes Master Server | |
config.vm.define "kmaster" do |node| | |
node.vm.box = "generic/ubuntu2004" | |
node.vm.box_check_update = false | |
node.vm.box_version = "3.3.0" | |
node.vm.hostname = "kmaster.k8s.com" | |
node.vm.network "private_network", ip: "192.168.56.100" | |
node.vm.provider :virtualbox do |v| | |
v.name = "kmaster" | |
v.memory = 2048 | |
v.cpus = 2 | |
end | |
node.vm.provision "shell", path: "master.sh" | |
end | |
# Kubernetes Worker Nodes | |
NodeCount = 2 | |
(1..NodeCount).each do |i| | |
config.vm.define "kworker#{i}" do |node| | |
node.vm.box = "generic/ubuntu2004" | |
node.vm.box_check_update = false | |
node.vm.box_version = "3.3.0" | |
node.vm.hostname = "kworker#{i}.k8s.com" | |
node.vm.network "private_network", ip: "192.168.56.10#{i}" | |
node.vm.provider :virtualbox do |v| | |
v.name = "kworker#{i}" | |
v.memory = 2048 | |
v.cpus = 2 | |
end | |
node.vm.provision "shell", path: "worker.sh" | |
end | |
end | |
end | |
─────────────────────────────────────────────── cat bootstrap.sh ──────────────────────────────────────────────── | |
#!/bin/bash | |
## !IMPORTANT ## | |
# | |
## This script is tested only in the generic/ubuntu2004 Vagrant box | |
## If you use a different version of Ubuntu or a different Ubuntu Vagrant box test this again | |
# | |
echo "[TASK 0] Setting TimeZone" | |
timedatectl set-timezone Asia/Shanghai | |
echo "[TASK 1] Setting DNS" | |
cat >/etc/systemd/resolved.conf <<EOF | |
[Resolve] | |
DNS=8.8.8.8 | |
FallbackDNS=223.5.5.5 | |
EOF | |
systemctl daemon-reload | |
systemctl restart systemd-resolved.service | |
mv /etc/resolv.conf /etc/resolv.conf.bak | |
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf | |
echo "[TASK 2] Setting Ubuntu System Mirrors" | |
cat >/etc/apt/sources.list<<EOF | |
deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse | |
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse | |
deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse | |
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse | |
deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse | |
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse | |
deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse | |
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse | |
deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse | |
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse | |
EOF | |
apt update -qq >/dev/null 2>&1 | |
echo "[TASK 3] Disable and turn off SWAP" | |
sed -i '/swap/d' /etc/fstab | |
swapoff -a | |
echo "[TASK 4] Stop and Disable firewall" | |
systemctl disable --now ufw >/dev/null 2>&1 | |
echo "[TASK 5] Enable and Load Kernel modules" | |
cat >>/etc/modules-load.d/containerd.conf<<EOF | |
overlay | |
br_netfilter | |
EOF | |
modprobe overlay | |
modprobe br_netfilter | |
echo "[TASK 6] Add Kernel settings" | |
cat >>/etc/sysctl.d/kubernetes.conf<<EOF | |
net.bridge.bridge-nf-call-ip6tables = 1 | |
net.bridge.bridge-nf-call-iptables = 1 | |
net.ipv4.ip_forward = 1 | |
EOF | |
sysctl --system >/dev/null 2>&1 | |
echo "[TASK 7] Install containerd runtime" | |
apt install -qq -y containerd apt-transport-https >/dev/null 2>&1 | |
mkdir /etc/containerd | |
containerd config default > /etc/containerd/config.toml | |
sed -i "s#k8s.gcr.io#registry.aliyuncs.com/k8sxio#g" /etc/containerd/config.toml | |
sed -i 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml | |
sed -i "s#https://registry-1.docker.io#https://registry.cn-hangzhou.aliyuncs.com#g" /etc/containerd/config.toml | |
sed -i '/\[plugins\.\"io\.containerd\.grpc\.v1\.cri\"\.registry\.mirrors\]/ a\\ \ \ \ \ \ \ \ [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]\n\ \ \ \ \ \ \ \ \ \ endpoint = ["https://bqr1dr1n.mirror.aliyuncs.com"]' /etc/containerd/config.toml | |
sed -i '/\[plugins\.\"io\.containerd\.grpc\.v1\.cri\"\.registry\.mirrors\]/ a\\ \ \ \ \ \ \ \ [plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"]\n\ \ \ \ \ \ \ \ \ \ endpoint = ["https://registry.aliyuncs.com/k8sxio"]' /etc/containerd/config.toml | |
systemctl daemon-reload | |
systemctl enable containerd --now >/dev/null 2>&1 | |
systemctl restart containerd | |
echo "[TASK 8] Add apt repo for kubernetes" | |
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - | |
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list | |
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main | |
EOF | |
apt update -qq >/dev/null 2>&1 | |
echo "[TASK 9] Install Kubernetes components (kubeadm, kubelet and kubectl)" | |
apt install -qq -y kubeadm=1.22.0-00 kubelet=1.22.0-00 kubectl=1.22.0-00 >/dev/null 2>&1 | |
crictl config runtime-endpoint /run/containerd/containerd.sock | |
crictl config image-endpoint /run/containerd/containerd.sock | |
echo "[TASK 10] Enable ssh password authentication" | |
sed -i 's/^PasswordAuthentication .*/PasswordAuthentication yes/' /etc/ssh/sshd_config | |
echo 'PermitRootLogin yes' >> /etc/ssh/sshd_config | |
systemctl reload sshd | |
echo "[TASK 11] Set root password" | |
echo -e "kubeadmin\nkubeadmin" | passwd root >/dev/null 2>&1 | |
echo "export TERM=xterm" >> /etc/bash.bashrc | |
echo "[TASK 12] Update /etc/hosts file" | |
cat >>/etc/hosts<<EOF | |
192.168.56.100 kmaster.k8s.com kmaster | |
192.168.56.101 kworker1.k8s.com kworker1 | |
192.168.56.102 kworker2.k8s.com kworker2 | |
EOF | |
───────────────────────────────────────────────── cat master.sh ───────────────────────────────────────────────── | |
#!/bin/bash | |
echo "[TASK 1] Pull required containers" | |
kubeadm config images list | grep -v 'coredns' | sed 's#k8s.gcr.io#ctr images pull registry.aliyuncs.com\/k8sxio#g' > images.sh | |
cat >> images.sh<<EOF | |
ctr -n k8s.io images pull docker.io/v5cn/coredns:v1.8.4 | |
ctr -n k8s.io images tag docker.io/v5cn/coredns:v1.8.4 registry.aliyuncs.com/k8sxio/coredns:v1.8.4 | |
EOF | |
chmod +x images.sh && ./images.sh >/dev/null 2>&1 | |
echo "[TASK 2] Initialize Kubernetes Cluster" | |
kubeadm init \ | |
--apiserver-advertise-address=192.168.56.100 \ | |
--control-plane-endpoint=kmaster.k8s.com \ | |
--kubernetes-version v1.22.0 \ | |
--image-repository registry.aliyuncs.com/k8sxio \ | |
--pod-network-cidr=192.168.0.0/16 > /root/kubeinit.log 2>/dev/null | |
echo "[TASK 3] Deploy Calico network" | |
kubectl --kubeconfig=/etc/kubernetes/admin.conf create -f https://docs.projectcalico.org/v3.18/manifests/calico.yaml >/dev/null 2>&1 | |
echo "[TASK 4] Generate and save cluster join command to /joincluster.sh" | |
kubeadm token create --print-join-command > /root/joincluster.sh 2>/dev/null | |
───────────────────────────────────────────────── cat worker.sh ───────────────────────────────────────────────── | |
#!/bin/bash | |
echo "[TASK 1] Join node to Kubernetes Cluster" | |
apt install -qq -y sshpass >/dev/null 2>&1 | |
sshpass -p "kubeadmin" scp -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no kmaster.k8s.com:/root/joincluster.sh /root/joincluster.sh 2>/dev/null | |
bash /root/joincluster.sh >/dev/null 2>&1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment