-
Notifications
You must be signed in to change notification settings - Fork 10
/
k8-install-master.sh
357 lines (259 loc) · 9.02 KB
/
k8-install-master.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
# check the OS
if [ -f /etc/redhat-release ]; then
# code for CentOS
echo "Running on CentOS"
cat << EOF > k8.sh
#!/bin/bash
echo "
#################################################################
# #
# This Script Install Kubernetes Master Node on CentOS #
# #
#################################################################
"
# Check for hardware prerequisites
mem_size=\$(cat /proc/meminfo | grep MemTotal | awk '{print \$2}')
echo "Minimum memory required : 2097152 KB"
echo "Available memory : \$mem_size KB "
if [[ \$mem_size -lt 2097152 ]]; then
echo "Error: Your system does not meet the minimum memory requirement of 2GB " >&2
exit 1
fi
num_cpus=\$(nproc)
echo "Minimum CPU cores required : 2 cores"
echo "Available CPU cores : \$num_cpus cores"
if [[ \$num_cpus -lt 2 ]]; then
echo "Error: Your system does not meet the minimum CPU requirement of 2 cores " >&2
exit 1
fi
# Confirm with the user before proceeding
read -p "Do you want to proceed with the installation ? (y/n) " -n 1 -r
echo
if [[ ! \$REPLY =~ ^[Yy]\$ ]]
then
exit 1
fi
# Check for software prerequisites
if ! [ -x "\$(command -v curl)" ]; then
echo 'Error: curl is not installed.' >&2
exit 1
fi
if ! [ -x "\$(command -v yum)" ]; then
echo 'Error: yum is not installed.' >&2
exit 1
fi
# Get the hostname
echo "Enter the hostname:"
read hostname
hostnamectl set-hostname \$hostname
echo "`ip route get 1 | awk '{print \$NF;exit}'` \$hostname" >> /etc/hosts
# Update the package list and upgrade all packages
yum update -y
### setup terminal
yum update
yum install -y bash-completion binutils
echo 'colorscheme ron' >> ~/.vimrc
echo 'set tabstop=2' >> ~/.vimrc
echo 'set shiftwidth=2' >> ~/.vimrc
echo 'set expandtab' >> ~/.vimrc
echo 'source <(kubectl completion bash)' >> ~/.bashrc
echo 'alias k=kubectl' >> ~/.bashrc
echo 'alias c=clear' >> ~/.bashrc
echo 'complete -F __start_kubectl k' >> ~/.bashrc
sed -i '1s/^/force_color_prompt=yes\n/' ~/.bashrc
yum remove -y docker.io kubelet kubeadm kubectl kubernetes-cni
yum autoremove -y
yum install -y etcd-client vim build-essential
# Install necessary packages
yum install -y yum-utils device-mapper-persistent-data lvm2
# Add Kubernetes repository
printf "
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
" > /etc/yum.repos.d/kubernetes.repo
#Install docker
sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
sudo yum -y update
sudo yum -y install docker-ce docker-ce-cli docker-compose-plugin --skip-broken
systemctl start docker
systemctl enable docker
# Remove containerd config file
yum install -y containerd.io
rm -f /etc/containerd/config.toml
# Restart containerd service
systemctl restart containerd
chkconfig --add containerd
# Set sysctl net.bridge.bridge-nf-call-iptables to 1
echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
sysctl --system
# Set SELinux in permissive mode
setenforce 0
sed -i 's/^SELINUX=enforcing\$/SELINUX=permissive/' /etc/selinux/config
# Add ports to firewall
sudo firewall-cmd --permanent --add-port=6443/tcp
sudo firewall-cmd --permanent --add-port=2379-2380/tcp
sudo firewall-cmd --permanent --add-port=10250/tcp
sudo firewall-cmd --permanent --add-port=10251/tcp
sudo firewall-cmd --permanent --add-port=10252/tcp
sudo firewall-cmd --permanent --add-port=10255/tcp
sudo firewall-cmd –reload
# Install Kubernetes components
yum install -y kubelet kubeadm kubectl
# Enable and start kubelet service
systemctl enable --now kubelet
# Initialize kubeadm
kubeadm init --ignore-preflight-errors=Firewalld
# Copy kubeconfig to home directory
mkdir -p \$HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf \$HOME/.kube/config
sudo chown \$(id -u):\$(id -g) \$HOME/.kube/config
#Deploy a pod network
kubectl apply -f https://docs.projectcalico.org/v3.25/manifests/calico.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.49.0/deploy/static/provider/baremetal/deploy.yaml
#Cluster join link
clear
echo " Installation Successful "
echo " Run below Token on worker node to join cluster "
kubeadm token create --print-join-command
EOF
chmod +x k8.sh
./k8.sh
elif [ -f /etc/lsb-release ]; then
# code for Ubuntu
echo "Running on Ubuntu"
cat << EOF > k8.sh
#!/bin/bash
# Redirect output to log file
exec > >(tee -a script.log)
# Redirect error to log file
exec 2> >(tee -a script.log >&2)
echo "
#################################################################
# #
# This Script Install Kubernetes Master Node on Ubuntu #
# #
#################################################################
"
# Check for hardware prerequisites
mem_size=\$(free -k | grep Mem | awk '{print \$2}')
echo "Minimum memory required : 2097152 KB"
echo "Available memory : \$mem_size KB "
if [[ \$mem_size -lt 2097152 ]]; then
echo "Error: Your system does not meet the minimum memory requirement of 2GB " >&2
exit 1
fi
num_cpus=\$(nproc)
echo "Minimum CPU cores required : 2 cores"
echo "Available CPU cores : \$num_cpus cores"
if [[ \$num_cpus -lt 2 ]]; then
echo "Error: Your system does not meet the minimum CPU requirement of 2 cores " >&2
exit 1
fi
# Confirm with the user before proceeding
read -p "Do you want to proceed with the installation ? (y/n) " -n 1 -r
echo
if [[ ! \$REPLY =~ ^[Yy]\$ ]]
then
exit 1
fi
# Check for software prerequisites
if ! [ -x "\$(command -v curl)" ]; then
echo 'Error: curl is not installed.' >&2
exit 1
fi
if ! [ -x "\$(command -v apt-get)" ]; then
echo 'Error: apt-get is not installed.' >&2
exit 1
fi
# Get the hostname
echo "Enter the hostname:"
read hostname
hostnamectl set-hostname \$hostname
echo "`ip route get 1 | awk '{print \$NF;exit}'` \$hostname" >> /etc/hosts
# Update the package list and upgrade all packages
apt-get update -y
apt-get -o upgrade -y
### setup terminal
apt-get update
apt-get install -y bash-completion binutils
echo 'colorscheme ron' >> ~/.vimrc
echo 'set tabstop=2' >> ~/.vimrc
echo 'set shiftwidth=2' >> ~/.vimrc
echo 'set expandtab' >> ~/.vimrc
echo 'source <(kubectl completion bash)' >> ~/.bashrc
echo 'alias k=kubectl' >> ~/.bashrc
echo 'alias c=clear' >> ~/.bashrc
echo 'complete -F __start_kubectl k' >> ~/.bashrc
sed -i '1s/^/force_color_prompt=yes\n/' ~/.bashrc
### install k8s and docker
apt-get remove -y docker.io kubelet kubeadm kubectl kubernetes-cni
apt-get autoremove -y
apt-get install -y etcd-client vim build-essential
# Install necessary packages
apt-get install -y apt-transport-https
apt-get update -y
# Add Kubernetes repository
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
echo "deb https://apt.kubernetes.io/ kubernetes-xenial main" | tee -a /etc/apt/sources.list.d/kubernetes.list
# Install docker
apt-get install -y apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository -y "deb [arch=amd64] https://download.docker.com/linux/ubuntu \$(lsb_release -cs) stable"
apt-get -y install docker.io
apt-get update -y
apt-get -y install docker-ce docker-ce-cli docker-compose-plugin --skip-broken
systemctl start docker
systemctl enable docker
# Remove containerd config file
apt-get install -y containerd.io
rm -f /etc/containerd/config.toml
# Restart containerd service
systemctl restart containerd
systemctl enable containerd
sudo swapoff -a
# Set sysctl net.bridge.bridge-nf-call-iptables to 1
echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.conf
sysctl -p
# Add ports to firewall
ufw allow 6443/tcp
ufw allow 2379:2380/tcp
ufw allow 10250/tcp
ufw allow 10251/tcp
ufw allow 10252/tcp
ufw allow 10255/tcp
ufw reload
# Install Kubernetes components
apt-get install -y kubelet kubeadm kubectl
# Enable and start kubelet service
systemctl enable --now kubelet
# Initialize kubeadm
kubeadm init --ignore-preflight-errors=all
# Copy kubeconfig to home directory
mkdir -p \$HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf \$HOME/.kube/config
sudo chown \$(id -u):\$(id -g) \$HOME/.kube/config
#Deploy a pod network
kubectl apply -f https://docs.projectcalico.org/v3.25/manifests/calico.yaml
kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v0.49.0/deploy/static/provider/baremetal/deploy.yaml
#Cluster join link
clear
echo " Installation Successful
type "bash" before proceed
"
echo " Run below Token on worker node to join cluster "
kubeadm token create --print-join-command
EOF
chmod +x k8.sh
./k8.sh
else
# code for other OS
echo "Not a supported OS"
exit
fi