-
Notifications
You must be signed in to change notification settings - Fork 0
/
k3s-deploy.sh
210 lines (168 loc) · 6.77 KB
/
k3s-deploy.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
#!/bin/bash
echo -e " \033[33;0m __ _ _ ___ \033[0m"
echo -e " \033[33;0m \ \(_)_ __ ___( )__ / _ \__ _ _ __ __ _ __ _ ___ \033[0m"
echo -e " \033[33;0m \ \ | '_ \` _ \/ __| / /_\/ _\` | '__/ _\` |/ _\` |/ _ \ \033[0m"
echo -e " \033[33;0m /\_/ / | | | | | \__ \ / /_\\ (_| | | | (_| | (_| | __/ \033[0m"
echo -e " \033[33;0m \___/|_|_| |_| |_|___/ \____/\__,_|_| \__,_|\__, |\___| \033[0m"
echo -e " \033[33;0m |___/ \033[0m"
echo -e " \033[36;0m _ _________ ___ _ _ _ \033[0m"
echo -e " \033[36;0m | |/ |__ / __| |_ _|_ _ __| |_ __ _| | | \033[0m"
echo -e " \033[36;0m | ' < |_ \__ \ | || ' \(_-| _/ _\` | | | \033[0m"
echo -e " \033[36;0m |_|\_|___|___/ |___|_||_/__/\__\__,_|_|_| \033[0m"
echo -e " \033[36;0m \033[0m"
echo -e " \033[32;0m https://youtube.com/@jims-garage \033[0m"
echo -e " \033[32;0m \033[0m"
#############################################
# YOU SHOULD ONLY NEED TO EDIT THIS SECTION #
#############################################
# Version of Kube-VIP to deploy
KVVERSION="v0.6.3"
# Set the IP addresses of the master and work nodes
master1=192.168.155.51
master2=192.168.155.52
master3=192.168.155.53
worker1=192.168.155.61
worker2=192.168.155.62
worker3=192.168.155.63
# User of remote machines
user=dankk
# Interface used on remotes
interface=eth0
# Set the virtual IP address (VIP)
vip=192.168.155.20
# Array of master nodes
masters=($master2 $master3)
# Array of worker nodes
workers=($worker1 $worker2 $worker3)
# Array of all
all=($master1 $master2 $master3 $worker1 $worker2 $worker3)
# Array of all minus master
allnomaster1=($master2 $master3 $worker1 $worker2 $worker3)
#Loadbalancer IP range
lbrange=192.168.155.30-192.168.155.50
#ssh certificate name variable
certName=id_rsa
# Redirect stdout (1) to both the screen and the log file using tee
LOG_FILE="/home/dankk/k3s-install.log"
exec > >(tee -a "$LOG_FILE") 2>&1
#############################################
# DO NOT EDIT BELOW #
#############################################
# For testing purposes - in case time is wrong due to VM snapshots
sudo timedatectl set-ntp off
sudo timedatectl set-ntp on
# Move SSH certs to ~/.ssh and change permissions
cp /home/$user/{$certName,$certName.pub} /home/$user/.ssh
chmod 600 /home/$user/.ssh/$certName
chmod 644 /home/$user/.ssh/$certName.pub
# Install k3sup to local machine if not already present
if ! command -v k3sup version &> /dev/null
then
echo -e " \033[31;5mk3sup not found, installing\033[0m"
curl -sLS https://get.k3sup.dev | sh
sudo install k3sup /usr/local/bin/
else
echo -e " \033[32;5mk3sup already installed\033[0m"
fi
# Install Kubectl if not already present
if ! command -v kubectl version &> /dev/null
then
echo -e " \033[31;5mKubectl not found, installing\033[0m"
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
else
echo -e " \033[32;5mKubectl already installed\033[0m"
fi
# Install Docker to generate manifest and daemonset if not already present
if ! command -v docker version &> /dev/null
then
echo -e " \033[31;5mDocker not found, installing\033[0m"
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh
wait $!
else
echo -e " \033[32;5mDocker already installed\033[0m"
fi
# Create SSH Config file to ignore checking (don't use in production!)
echo "StrictHostKeyChecking no" > ~/.ssh/config
#add ssh keys for all nodes
for node in "${all[@]}"; do
ssh-copy-id $user@$node
done
# Step 1: Bootstrap First k3s Node
mkdir ~/.kube
k3sup install \
--ip $master1 \
--user $user \
--tls-san $vip \
--cluster \
--k3s-channel stable \
--k3s-version v1.26.10+k3s2 \
--k3s-extra-args "--disable traefik --disable servicelb --flannel-iface=$interface --node-ip=$master1" \
--merge \
--sudo \
--local-path $HOME/.kube/config \
--ssh-key $HOME/.ssh/$certName \
--context k3s-ha
echo -e " \033[32;5mFirst Node bootstrapped successfully!\033[0m"
# Step 2: Install Kube-VIP for HA
kubectl k3s-ha
kubectl apply -f https://kube-vip.io/manifests/rbac.yaml
# Step 3: Generate Daemonset with Docker
sudo docker run --network host --rm ghcr.io/kube-vip/kube-vip:$KVVERSION manifest daemonset \
--interface $interface \
--address $vip \
--inCluster \
--taint \
--controlplane \
--services \
--arp \
--leaderElection | tee $HOME/kube-vip.yaml
# Step 4: Copy kube-vip.yaml to master1
scp -i ~/.ssh/$certName $HOME/kube-vip.yaml $user@$master1:~/kube-vip.yaml
# Step 5: Connect to Master1 and move kube-vip.yaml
ssh $user@$master1 -i ~/.ssh/$certName <<- EOF
sudo mkdir -p /var/lib/rancher/k3s/server/manifests
sudo mv kube-vip.yaml /var/lib/rancher/k3s/server/manifests/kube-vip.yaml
EOF
# Step 6: Add new master nodes (servers)
for newnode in "${masters[@]}"; do
k3sup join \
--ip $newnode \
--user $user \
--sudo \
--k3s-channel stable \
--k3s-version v1.26.10+k3s2 \
--server \
--server-ip $master1 \
--ssh-key $HOME/.ssh/$certName \
--k3s-extra-args "--disable traefik --disable servicelb --flannel-iface=$interface --node-ip=$newnode" \
--server-user $user
echo -e " \033[32;5mMaster node joined successfully!\033[0m"
done
for newagent in "${workers[@]}"; do
k3sup join \
--ip $newagent \
--user $user \
--sudo \
--k3s-channel stable \
--k3s-version v1.26.10+k3s2 \
--server-ip $master1 \
--ssh-key $HOME/.ssh/$certName
echo -e " \033[32;5mAgent node joined successfully!\033[0m"
done
# Step 7: Install kube-vip as network LoadBalancer - Install the kube-vip Cloud Provider
kubectl apply -f https://raw.githubusercontent.com/kube-vip/kube-vip-cloud-provider/main/manifest/kube-vip-cloud-controller.yaml
#IP range for loadbalancer services to use
kubectl create configmap -n kube-system kubevip --from-literal range-global=$lbrange
# Step 8: Test with Nginx
kubectl apply -f https://raw.githubusercontent.com/inlets/inlets-operator/master/contrib/nginx-sample-deployment.yaml -n default
kubectl expose deployment nginx-1 --port=80 --type=LoadBalancer -n default
echo -e " \033[32;5mWaiting 20s for K3S to sync and LoadBalancer to come online\033[0m"
while [[ $(kubectl get pods -l app=nginx -o 'jsonpath={..status.conditions[?(@.type=="Ready")].status}') != "True" ]]; do
sleep 1
done
kubectl get nodes
kubectl get svc
kubectl get pods --all-namespaces -o wide
echo -e " \033[32;5mHappy Kubing! Access Nginx at EXTERNAL-IP above\033[0m"