|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Flag variable to track valid input |
| 4 | +valid_input=false |
| 5 | + |
| 6 | +# Loop until valid input is received |
| 7 | +while [ "$valid_input" = false ]; do |
| 8 | + # Prompt the user to continue or stop |
| 9 | + echo "This script requires to be run as root and will erase and reconstruct a base cluster." |
| 10 | + read -p "Do you wish to continue ? (Yes/No) [Yes]: " |
| 11 | + |
| 12 | + # Convert user input to lowercase for case-insensitive comparison |
| 13 | + user_input_lower=$(printf "%s" "$REPLY" | tr '[:upper:]' '[:lower:]') |
| 14 | + |
| 15 | + # Check user input and take action accordingly |
| 16 | + case $user_input_lower in |
| 17 | + "" | "y" | "yes") |
| 18 | + valid_input=true |
| 19 | + ;; |
| 20 | + "n" | "no") |
| 21 | + echo "Stopping." |
| 22 | + exit 0 |
| 23 | + ;; |
| 24 | + *) |
| 25 | + echo "Invalid input." |
| 26 | + ;; |
| 27 | + esac |
| 28 | +done |
| 29 | + |
| 30 | +kubeadm reset -f |
| 31 | +systemctl stop containerd |
| 32 | +systemctl stop docker.socket |
| 33 | +systemctl stop docker.service |
| 34 | +systemctl stop $(systemctl list-units --all | awk '/kube/ {print $1}') |
| 35 | +systemctl stop kubelet |
| 36 | +dnf remove -y containerd.io docker-ce kubelet kubeadm kubectl |
| 37 | +rm -rf /etc/containerd |
| 38 | +rm -rf /etc/cni |
| 39 | +rm -rf /etc/kubernetes |
| 40 | +rm -rf /etc/docker |
| 41 | +rm -rf /var/lib/kubelet |
| 42 | +rm -rf /var/lib/cni |
| 43 | +rm -rf /var/lib/containerd |
| 44 | +rm -rf /var/lib/calico |
| 45 | +rm -rf /data |
| 46 | +ip link delete docker0 |
| 47 | +ipvsadm --clear |
| 48 | +iptables -P INPUT ACCEPT |
| 49 | +iptables -P OUTPUT ACCEPT |
| 50 | +iptables -P FORWARD ACCEPT |
| 51 | +iptables -F |
| 52 | +iptables -X |
| 53 | +iptables -t nat -F |
| 54 | +iptables -t nat -X |
| 55 | +iptables -t mangle -F |
| 56 | +iptables -t mangle -X |
| 57 | +dnf makecache |
| 58 | +dnf install -y containerd.io docker-ce kubelet kubeadm kubectl --disableexcludes=kubernetes |
| 59 | +mv /etc/containerd/config.toml /etc/containerd/config.toml.orig |
| 60 | +containerd config default > /etc/containerd/config.toml |
| 61 | +sudo sed -i 's/^ SystemdCgroup = false/ SystemdCgroup = true/' /etc/containerd/config.toml |
| 62 | +systemctl enable --now containerd.service |
| 63 | +systemctl enable --now kubelet.service |
| 64 | +systemctl start kubelet |
| 65 | +kubeadm config images pull |
| 66 | +kubeadm init |
| 67 | +export KUBECONFIG=/etc/kubernetes/admin.conf |
| 68 | +mkdir -p $HOME/.kube |
| 69 | +sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config |
| 70 | +sudo chown $(id -u):$(id -g) $HOME/.kube/config |
| 71 | +echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> /etc/profile.d/k8s.sh |
| 72 | +kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.25.0/manifests/calico.yaml |
| 73 | + |
| 74 | +# Taint node for later use |
| 75 | +kubectl taint nodes --all node-role.kubernetes.io/control-plane- |
| 76 | + |
| 77 | +# Create StorageClass, PersistentVolume and tag it as default |
| 78 | +cat > /root/sc.yaml << EOF |
| 79 | +apiVersion: storage.k8s.io/v1 |
| 80 | +kind: StorageClass |
| 81 | +metadata: |
| 82 | + name: local-storage |
| 83 | +provisioner: kubernetes.io/no-provisioner |
| 84 | +volumeBindingMode: WaitForFirstConsumer |
| 85 | +EOF |
| 86 | + |
| 87 | +cat > /root/pv.yaml << EOF |
| 88 | +apiVersion: v1 |
| 89 | +kind: PersistentVolume |
| 90 | +metadata: |
| 91 | + annotations: {} |
| 92 | + name: storage-pv |
| 93 | +spec: |
| 94 | + accessModes: |
| 95 | + - ReadWriteOnce |
| 96 | + capacity: |
| 97 | + storage: 10Gi |
| 98 | + local: |
| 99 | + path: /data |
| 100 | + nodeAffinity: |
| 101 | + required: |
| 102 | + nodeSelectorTerms: |
| 103 | + - matchExpressions: |
| 104 | + - key: kubernetes.io/hostname |
| 105 | + operator: In |
| 106 | + values: |
| 107 | + - kubernetes |
| 108 | + persistentVolumeReclaimPolicy: Retain |
| 109 | + storageClassName: local-storage |
| 110 | + volumeMode: Filesystem |
| 111 | +EOF |
| 112 | + |
| 113 | +mkdir /data |
| 114 | + |
| 115 | +kubectl apply -f /root/sc.yaml |
| 116 | +kubectl apply -f /root/pv.yaml |
| 117 | +kubectl patch storageclass local-storage -p '{"metadata": {"annotations":{"storageclass.kubernetes.io/is-default-class":"true"}}}' |
| 118 | + |
| 119 | +# Add portainer's repo and install it. |
| 120 | +helm repo add portainer https://portainer.github.io/k8s/ |
| 121 | +helm repo update |
| 122 | +helm upgrade --install --create-namespace -n portainer portainer portainer/portainer |
0 commit comments