-
Notifications
You must be signed in to change notification settings - Fork 0
/
InstallKubeMaster.sh
225 lines (143 loc) · 6.24 KB
/
InstallKubeMaster.sh
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#!/bin/bash
# Function to print a message in blue
print_blue() {
echo -e "\e[34m$1\e[0m"
}
# Function to print a message in green
print_green() {
echo -e "\e[32m$1\e[0m"
}
# Prompt for confirmation to execute the major step
confirm_step() {
print_blue "$1"
read -p "Would you like to continue with this step? (yes/no): " choice
case "$choice" in
yes|Yes|YES ) echo "Continuing...";;
no|No|NO ) echo "Cancellation."; exit 1;;
* ) echo "Please answer with 'yes' or 'no'."; confirm_step "$1";;
esac
}
# Start of the step: Enable iptables Bridged Traffic on all nodes
print_blue "Start of the step: Enable iptables Bridged Traffic on all nodes"
cat <<EOF | sudo tee /etc/modules-load.d/k8s.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/k8s.conf
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward = 1
EOF
# Apply sysctl params without reboot
sudo sysctl --system
print_green "Step completed: Enable iptables Bridged Traffic on all nodes"
# Start of the step: Disable swap on all the Nodes
print_blue "Start of the step: Disable swap on all the Nodes"
sudo swapoff -a
(crontab -l 2>/dev/null; echo "@reboot /sbin/swapoff -a") | crontab - || true
print_green "Step completed: Disable swap on all the Nodes"
# Start of the step: Install Docker
confirm_step "Start of the step: Install Docker"
print_blue "Start of the step: Install Docker"
# Add Docker repository to Apt sources
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
$(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo systemctl start docker
sudo systemctl enable docker
print_green "Step completed: Install Docker"
# Start of the step: Install Kubernetes
confirm_step "Start of the step: Install Kubernetes"
print_blue "Start of the step: Install Kubernetes"
# Install dependency packages
sudo apt-get install -y apt-transport-https ca-certificates curl
# Download and add GPG key
sudo curl -fsSLo /usr/share/keyrings/kubernetes-archive-keyring.gpg https://dl.k8s.io/apt/doc/apt-key.gpg
# Add the Kubernetes APT repository to your system
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
# Update package listings
sudo apt-get update -y
# Install Kubernetes packages
sudo apt-get install -y kubelet=1.24.0-00 kubeadm=1.24.0-00 kubectl=1.24.0-00
# Turn off automatic updates
sudo apt-mark hold kubelet kubeadm kubectl
# Verification
kubeadm version
# Add the node IP to KUBELET_EXTRA_ARGS
sudo apt-get install -y jq
local_ip="$(ip --json a s | jq -r '.[] | if .ifname == "eth1" then .addr_info[] | if .family == "inet" then .local else empty end else empty end')"
cat > /etc/default/kubelet << EOF
KUBELET_EXTRA_ARGS=--node-ip=$local_ip
EOF
print_green "Step completed: Install Kubernetes"
# Start of the step: Initialize the Kubernetes cluster
confirm_step "Start of the step: Initialize the Kubernetes cluster"
print_blue "Start of the step: Initialize the Kubernetes cluster"
# Install containerd
sudo apt-get update
sudo apt-get install -y containerd
# Start containerd
sudo systemctl start containerd
sudo systemctl enable containerd
# Check if installed
sudo ls /var/run/containerd/containerd.sock
# Start and enable Kubectl
sudo systemctl daemon-reload
sudo systemctl enable kubelet
sudo systemctl restart kubelet
sudo systemctl status kubelet
# Start of the step: Initialize Kubeadm on the master node to set up the control plane, Private IP mode
# Get hostname
# Set pod CIDR
# Get private IP address
NODENAME=$(hostname -s)
POD_CIDR="192.168.0.0/16"
IPADDR=$(ip addr show | grep 'inet ' | grep -v 127.0.0.1 | awk '{print $2}' | cut -d/ -f1 | head -n 1)
#IPADDR=$(curl ifconfig.me && echo "") # if public
# Print variables
echo "IPADDR=$IPADDR"
echo "NODENAME=$NODENAME"
echo "POD_CIDR=$POD_CIDR"
confirm_step "Start of the step: Initialize Kubeadm on the master node to set up the control plane, Private IP mode"
print_blue "Start of the step: Initialize Kubeadm on the master node to set up the control plane, Private IP mode"
# sudo kubeadm init --control-plane-endpoint=$IPADDR --apiserver-cert-extra-sans=$IPADDR --pod-network-cidr=$POD_CIDR --node-name $NODENAME --ignore-preflight-errors Swap
sudo kubeadm init --pod-network-cidr 192.168.0.0/16 --kubernetes-version 1.24.0
### To start using Cluster
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
## get Kube config
echo "Kube configuration : "
kubectl get po -n kube-system
# verify all the cluster component health statuses
kubectl get --raw='/readyz?verbose'
# get cluster infos
kubectl cluster-info
print_green "Step completed: Initialize Kubeadm on the master node to set up the control plane, Private IP mode"
# Start of the step: Install Calico Network Plugin for Pod Networking
confirm_step "Start of the step: Install Calico Network Plugin for Pod Networking"
print_blue "Start of the step: Install Calico Network Plugin for Pod Networking"
## install CALICO network plugin
kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/tigera-operator.yaml
curl https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/custom-resources.yaml -O
kubectl create -f custom-resources.yaml
print_green "Step completed: Install Calico Network Plugin for Pod Networking"
## Setup Kubernetes Metrics Server
# install the service :
kubectl apply -f https://raw.githubusercontent.com/techiescamp/kubeadm-scripts/main/manifests/metrics-server.yaml
# node and pod metrics
kubectl top nodes
# pod CPU and memory metrics
kubectl top pod -n kube-system
## Nodes list
kubectl get nodes -o wide