Difference between revisions of "Kubeadm kubernetes cluster Ubuntu 24.04"

From UVOO Tech Wiki
Jump to navigation Jump to search
Line 5: Line 5:
 
#!/bin/bash
 
#!/bin/bash
 
set -eu
 
set -eu
 +
 +
k8s_minor_version=1.31
 +
 
kubeadm_init(){
 
kubeadm_init(){
 
   sudo apt update && sudo apt upgrade -y
 
   sudo apt update && sudo apt upgrade -y
Line 15: Line 18:
 
   sudo systemctl restart containerd
 
   sudo systemctl restart containerd
  
   curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.30/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
+
   curl -fsSL https://pkgs.k8s.io/core:/stable:/v${k8s_minor_version}/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.30/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
+
   echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v${k8s_minor_version}/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
 
   sudo apt update
 
   sudo apt update
 
   sudo apt install -y kubelet kubeadm kubectl
 
   sudo apt install -y kubelet kubeadm kubectl
Line 42: Line 45:
  
 
kubeadm_init
 
kubeadm_init
 +
```
 +
 +
## Upgrade node Kubernetes version
 +
```
 +
#!/bin/bash
 +
set -eu
 +
 +
k8s_minor_version=1.31
 +
 +
update_kubeadm_version(){
 +
  k8s_minor_version=1.31
 +
  k8s_keyring_file="/etc/apt/keyrings/kubernetes-apt-keyring.gpg"
 +
  if [ ! -f "${k8s_keyring_file}" ]; then
 +
    curl -fsSL https://pkgs.k8s.io/core:/stable:/v${k8s_minor_version}/deb/Release.key 2>/dev/null | sudo gpg --dearmor -o ${k8s_keyring_file}
 +
  fi
 +
  echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v${k8s_minor_version}/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
 +
  sudo apt-get update
 +
 +
  sudo apt-mark unhold kubeadm kubectl kubelet && \
 +
  sudo apt-get install -y kubelet=${k8s_minor_version}.* kubeadm=${k8s_minor_version}.* kubectl=${k8s_minor_version}.* && \
 +
  sudo apt-mark hold kubeadm kubectl kubelet && \
 +
  sudo systemctl restart kubelet
 +
  sudo apt-get -y dist-upgrade
 +
  kubeadm version -o short
 +
  echo "Reboot in 10 seconds"; sleep 10
 +
  sudo reboot
 +
}
 +
 
```
 
```

Revision as of 18:44, 27 December 2024

Kubeadm Cluster Scripts

Init

#!/bin/bash
set -eu

k8s_minor_version=1.31

kubeadm_init(){
  sudo apt update && sudo apt upgrade -y
  sudo apt install apt-transport-https curl -y

  sudo apt install containerd -y
  sudo mkdir -p /etc/containerd
  containerd config default | sudo tee /etc/containerd/config.toml > /dev/null
  sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
  sudo systemctl restart containerd

  curl -fsSL https://pkgs.k8s.io/core:/stable:/v${k8s_minor_version}/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:/v${k8s_minor_version}/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
  sudo apt update
  sudo apt install -y kubelet kubeadm kubectl
  sudo apt-mark hold kubelet kubeadm kubectl

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

  sudo modprobe overlay
  sudo modprobe br_netfilter
  echo -e "br_netfilter\noverlay" | sudo tee /etc/modules-load.d/kubernetes-cni.conf
  lsmod | grep overlay
  lsmod | grep br_netfilter

  cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

  sysctl --system
  echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bashrc
  reboot
}

kubeadm_init

Upgrade node Kubernetes version

#!/bin/bash
set -eu

k8s_minor_version=1.31

update_kubeadm_version(){
  k8s_minor_version=1.31
  k8s_keyring_file="/etc/apt/keyrings/kubernetes-apt-keyring.gpg"
  if [ ! -f "${k8s_keyring_file}" ]; then
    curl -fsSL https://pkgs.k8s.io/core:/stable:/v${k8s_minor_version}/deb/Release.key 2>/dev/null | sudo gpg --dearmor -o ${k8s_keyring_file}
  fi
  echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v${k8s_minor_version}/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
  sudo apt-get update

  sudo apt-mark unhold kubeadm kubectl kubelet && \
  sudo apt-get install -y kubelet=${k8s_minor_version}.* kubeadm=${k8s_minor_version}.* kubectl=${k8s_minor_version}.* && \
  sudo apt-mark hold kubeadm kubectl kubelet && \
  sudo systemctl restart kubelet
  sudo apt-get -y dist-upgrade
  kubeadm version -o short
  echo "Reboot in 10 seconds"; sleep 10
  sudo reboot
}