Kubernetesインストール(シングル) 2023年12月版

https://www.linuxtechi.com/install-kubernetes-on-ubuntu-22-04/


mmm161: Ubuntu22 Kubernetes master node
mmm162: Ubuntu22 Kubernetes worker node


前提:
CPU数=2
メモリ=8G
Disk=50G

hosts設定済み

※インストール後の作業はmaster nodeから一般ユーザでおこなう


-- 1. masterとworkerの共通作業

-- 1.1 Disable swap & Add kernel Parameters

sudo su -

cat /etc/fstab
sed -i '/swap/ s/^\(.*\)$/#\1/g' /etc/fstab
cat /etc/fstab
swapoff -a

 

tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF

modprobe overlay
modprobe br_netfilter

tee /etc/sysctl.d/kubernetes.conf <<EOT
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOT

sysctl --system

-- 1.2 Install Containerd Runtime

apt install -y curl gnupg2 software-properties-common apt-transport-https ca-certificates

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

apt update
apt install -y containerd.io

containerd config default | sudo tee /etc/containerd/config.toml >/dev/null 2>&1
sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml


systemctl restart containerd
systemctl enable containerd

-- 1.3 Add Apt Repository for Kubernetes

curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

-- 1.4 Install Kubectl, Kubeadm and Kubelet


apt update
apt install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl

 


-- 2. master nodeの作成

kubeadm init --control-plane-endpoint=mmm161

最後に表示される出力をコピーペーストして保存。あとでworker nodeで実行する


exit

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

kubectl cluster-info
kubectl get nodes


-- 3. worker nodeの作成

kubeadm join mmm161:6443 --token gezwcc.v5w4eef9dre1j64c \
        --discovery-token-ca-cert-hash sha256:eb342998e927e4812340a88b37d0d1bc52af7c4a745aeded09dc0c24ce1de3fc

 

-- 4. Install Calico Network Plugin
master nodeで実施

kubectl get nodes

kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.0/manifests/calico.yaml
kubectl get pods -n kube-system
kubectl get nodes

 

-- 5. Test Your Kubernetes Cluster Installation
master nodeで実施


kubectl run hello-world --image hello-world --restart=Never
kubectl get pod
kubectl logs pod/hello-world
kubectl delete pod/hello-world
kubectl get pod