-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathk8s-deploy.sh
executable file
·377 lines (317 loc) · 9.32 KB
/
k8s-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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#!/bin/bash
set -x
set -e
HTTP_SERVER=192.168.56.1:8000
KUBE_HA=true
KUBE_REPO_PREFIX=gcr.io/google_containers
KUBE_ETCD_IMAGE=quay.io/coreos/etcd:v3.0.17
root=$(id -u)
if [ "$root" -ne 0 ] ;then
echo must run as root
exit 1
fi
kube::install_docker()
{
set +e
docker info> /dev/null 2>&1
i=$?
set -e
if [ $i -ne 0 ]; then
curl -L http://$HTTP_SERVER/rpms/docker.tar.gz > /tmp/docker.tar.gz
tar zxf /tmp/docker.tar.gz -C /tmp
yum localinstall -y /tmp/docker/*.rpm
systemctl enable docker.service && systemctl start docker.service
kube::config_docker
fi
echo docker has been installed
rm -rf /tmp/docker /tmp/docker.tar.gz
}
kube::config_docker()
{
setenforce 0 > /dev/null 2>&1 && sed -i -e 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
sysctl -w net.bridge.bridge-nf-call-iptables=1
sysctl -w net.bridge.bridge-nf-call-ip6tables=1
cat <<EOF >>/etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sed -i -e 's/DOCKER_STORAGE_OPTIONS=/DOCKER_STORAGE_OPTIONS="-s overlay --selinux-enabled=false"/g' /etc/sysconfig/docker-storage
systemctl daemon-reload && systemctl restart docker.service
}
kube::load_images()
{
mkdir -p /tmp/k8s
images=(
kube-apiserver-amd64_v1.6.2
kube-controller-manager-amd64_v1.6.2
kube-scheduler-amd64_v1.6.2
kube-proxy-amd64_v1.6.2
pause-amd64_3.0
k8s-dns-dnsmasq-nanny-amd64_1.14.1
k8s-dns-kube-dns-amd64_1.14.1
k8s-dns-sidecar-amd64_1.14.1
etcd_v3.0.17
flannel-amd64_v0.7.1
)
for i in "${!images[@]}"; do
ret=$(docker images | awk 'NR!=1{print $1"_"$2}'| grep $KUBE_REPO_PREFIX/${images[$i]} | wc -l)
if [ $ret -lt 1 ];then
curl -L http://$HTTP_SERVER/images/${images[$i]}.tar > /tmp/k8s/${images[$i]}.tar
docker load < /tmp/k8s/${images[$i]}.tar
fi
done
rm /tmp/k8s* -rf
}
kube::install_k8s()
{
set +e
which kubeadm > /dev/null 2>&1
i=$?
set -e
if [ $i -ne 0 ]; then
curl -L http://$HTTP_SERVER/rpms/k8s.tar.gz > /tmp/k8s.tar.gz
tar zxf /tmp/k8s.tar.gz -C /tmp
yum localinstall -y /tmp/k8s/*.rpm
rm -rf /tmp/k8s*
# 增加上限,每个node最多可以跑1024个pod
sed -i 's/--allow-privileged=true/--allow-privileged=true --max-pods=1024/g' /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl enable kubelet.service && systemctl daemon-reload && systemctl start kubelet.service && rm -rf /etc/kubernetes
fi
}
kube::config_firewalld()
{
systemctl disable firewalld && systemctl stop firewalld
# iptables -A IN_public_allow -p tcp -m tcp --dport 9898 -m conntrack --ctstate NEW -j ACCEPT
# iptables -A IN_public_allow -p tcp -m tcp --dport 6443 -m conntrack --ctstate NEW -j ACCEPT
# iptables -A IN_public_allow -p tcp -m tcp --dport 10250 -m conntrack --ctstate NEW -j ACCEPT
}
kube::get_env()
{
HA_STATE=$1
[ $HA_STATE == "MASTER" ] && HA_PRIORITY=200 || HA_PRIORITY=`expr 200 - ${RANDOM} / 1000 + 1`
KUBE_VIP=$(echo $2 |awk -F= '{print $2}')
VIP_PREFIX=$(echo ${KUBE_VIP} | cut -d . -f 1,2,3)
###dhcp和static地址的不同取法
VIP_INTERFACE=$(ip addr show | grep ${VIP_PREFIX} | awk -F 'dynamic' '{print $2}' | head -1)
[ -z ${VIP_INTERFACE} ] && VIP_INTERFACE=$(ip addr show | grep ${VIP_PREFIX} | awk -F 'global' '{print $2}' | head -1)
###
LOCAL_IP=$(ip addr show | grep ${VIP_PREFIX} | awk -F / '{print $1}' | awk -F ' ' '{print $2}' | head -1)
MASTER_NODES=$(echo $3 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}')
MASTER_NODES_NO_LOCAL_IP=$(echo "${MASTER_NODES}" | sed -e 's/'${LOCAL_IP}'//g')
}
kube::install_keepalived()
{
kube::get_env $@
set +e
which keepalived > /dev/null 2>&1
i=$?
set -e
if [ $i -ne 0 ]; then
ip addr add ${KUBE_VIP}/32 dev ${VIP_INTERFACE}
curl -L http://$HTTP_SERVER/rpms/keepalived.tar.gz > /tmp/keepalived.tar.gz
tar zxf /tmp/keepalived.tar.gz -C /tmp
yum localinstall -y /tmp/keepalived/*.rpm
rm -rf /tmp/keepalived*
systemctl enable keepalived.service && systemctl start keepalived.service
kube::config_keepalived
fi
}
kube::config_keepalived()
{
echo "gen keepalived configuration"
cat <<EOF >/etc/keepalived/keepalived.conf
global_defs {
router_id LVS_k8s
}
vrrp_script CheckK8sMaster {
script "curl -k https://127.0.0.1:6443/api"
interval 3
timeout 9
fall 2
rise 2
}
vrrp_instance VI_1 {
state ${HA_STATE}
interface ${VIP_INTERFACE}
virtual_router_id 61
priority ${HA_PRIORITY}
advert_int 1
mcast_src_ip ${LOCAL_IP}
nopreempt
authentication {
auth_type PASS
auth_pass 378378
}
unicast_peer {
${MASTER_NODES_NO_LOCAL_IP}
}
virtual_ipaddress {
${KUBE_VIP}
}
track_script {
CheckK8sMaster
}
}
EOF
modprobe ip_vs
systemctl daemon-reload && systemctl restart keepalived.service
}
kube::get_etcd_endpoint()
{
local var=$2
local temp=${var#*//}
etcd_endpoint=${temp%%:*}
}
kube::save_master_ip()
{
if [ ${KUBE_HA} == true ];then
kube::get_etcd_endpoint $@
set +e; ssh root@$etcd_endpoint "etcdctl mk ha_master ${LOCAL_IP}"; set -e
fi
}
kube::copy_master_config()
{
kube::get_etcd_endpoint $@
local master_ip=$(ssh root@$etcd_endpoint "etcdctl get /ha_master")
mkdir -p /etc/kubernetes
scp -r root@${master_ip}:/etc/kubernetes/* /etc/kubernetes/
systemctl daemon-reload && systemctl start kubelet
}
kube::set_label()
{
export KUBECONFIG=/etc/kubernetes/admin.conf
local hstnm=`hostname`
local lowhstnm=$(echo $hstnm | tr '[A-Z]' '[a-z]')
until kubectl get no | grep -i $lowhstnm; do sleep 1; done
kubectl label node $lowhstnm kubeadm.alpha.kubernetes.io/role=master
}
kube::config_kubeadm()
{
# kubeadm需要联网去找最新版本
echo $HTTP_SERVER storage.googleapis.com >> /etc/hosts
local endpoints=$2
local temp=${endpoints#*=}
local etcd0=$(echo $temp | awk -F ',' '{print $1}')
local etcd1=$(echo $temp | awk -F ',' '{print $2}')
local etcd2=$(echo $temp | awk -F ',' '{print $3}')
local advertiseAddress=$1
local advertiseAddress=${advertiseAddress#*=}
cat <<EOF >$HOME/kubeadm-config.yml
apiVersion: kubeadm.k8s.io/v1alpha1
kind: MasterConfiguration
api:
advertiseAddress: "$advertiseAddress"
# bindPort: <int>
etcd:
endpoints:
- "$etcd0"
- "$etcd1"
- "$etcd2"
# caFile: <path|string>
# certFile: <path|string>
# keyFile: <path|string>
kubernetesVersion: "v1.6.2"
networking:
# dnsDomain: <string>
# serviceSubnet: <cidr>
# 这里一定要带上--pod-network-cidr参数,不然后面的flannel网络会出问题
podSubnet: 12.240.0.0/12
EOF
}
kube::install_cni()
{
# install flannel network
export KUBECONFIG=/etc/kubernetes/admin.conf
kubectl apply -f http://$HTTP_SERVER/network/kube-flannel-rbac.yml
kubectl apply -f http://$HTTP_SERVER/network/kube-flannel.yml --namespace=kube-system
}
kube::master_up()
{
shift
kube::install_docker
kube::load_images
kube::install_k8s
[ ${KUBE_HA} == true ] && kube::install_keepalived "MASTER" $@
# 存储master ip, replica侧需要用这个信息来copy 配置
kube::save_master_ip $@
kube::config_kubeadm $@
kubeadm init --config=$HOME/kubeadm-config.yml
echo -e "\033[32m 赶紧找地方记录上面的token! \033[0m"
kube::install_cni
# 为了kubectl get no的时候可以显示master标识
kube::set_label
# show pods
kubectl get po --all-namespaces
}
kube::replica_up()
{
shift
kube::install_docker
kube::load_images
kube::install_k8s
kube::install_keepalived "BACKUP" $@
kube::copy_master_config $@
kube::set_label
}
kube::node_up()
{
shift
kube::install_docker
kube::load_images
kube::install_k8s
kube::config_firewalld
kubeadm join $@
}
kube::tear_down()
{
systemctl stop kubelet.service
docker ps -aq|xargs -I '{}' docker stop {}
docker ps -aq|xargs -I '{}' docker rm {}
df |grep /var/lib/kubelet|awk '{ print $6 }'|xargs -I '{}' umount {}
rm -rf /var/lib/kubelet && rm -rf /etc/kubernetes/ && rm -rf /var/lib/etcd
yum remove -y kubectl kubeadm kubelet kubernetes-cni
if [ ${KUBE_HA} == true ]
then
yum remove -y keepalived
rm -rf /etc/keepalived/keepalived.conf
fi
rm -rf /var/lib/cni
rm -rf /etc/systemd/system/docker.service.d/*
ip link del cni0
ip link del docker0
ip link del flannel.1
}
kube::test()
{
shift
kube::config_kubeadm $@
}
main()
{
case $1 in
"m" | "master" )
kube::master_up $@
;;
"r" | "replica" )
kube::replica_up $@
;;
"j" | "join" )
kube::node_up $@
;;
"d" | "down" )
kube::tear_down
;;
"t" | "test" )
kube::test $@
;;
*)
echo "usage: $0 m[master] | r[replica] | j[join] token | d[down] "
echo " $0 master to setup master "
echo " $0 replica to setup replica master "
echo " $0 join to join master with token "
echo " $0 down to tear all down ,inlude all data! so becarefull"
echo " unkown command $0 $@"
;;
esac
}
main $@