Shell脚本快速部署Kubernetes集群系统

简介:

  本文紧跟上节所讲的手动部署Kubernetes管理Docker篇所写,本篇主要内容利用Shell脚本完成快速部署Kubernetes集群。上节博文看过的朋友也能感觉到部署过程相对比较简单,那么,出于简化工作流程,推进运维自动化角度来说,于是花了2/3天时间写这个部署Kubernetes脚本。

  运维工作中,常常会遇到部署各种各样的服务,建议:常规部署都应该尽量使用脚本完成,一方面提高自身脚本编写能力,另一方面推进运维自动化。

详细部署说明文档http://lizhenliang.blog.51cto.com/7876557/1736572

提醒:即使按照本篇文章一步一步做,也不一定部署成功。原因你懂得!如果失败,也建议你仔细看看脚本内容,从中寻找解决办法。同时,相信你也会从脚本中获取到其他有价值的信息。

实验环境:

操作系统:Ubuntu14.04_x64

master:192.168.1.150

minion01 : 192.168.1.151  容器网段:172.17.1.0/24

minion02 : 192.168.1.152  容器网段:172.17.2.0/24

安装包下载:

etcd:http://pan.baidu.com/s/1c1wITMw

kubernetes:http://pan.baidu.com/s/1kUoxgYb

相关脚本下载:http://pan.baidu.com/s/1o7nEaca

脚本说明:

config_ssh_root_remote.sh     #配置root SSH登录(默认ubuntu系统禁止root SSH登录)

ssh_keypair_auth.sh         #配置master主机与minion主机SSH免交互认证

kubernetes-install.sh        #安装kubernetes的master端与minion端

config_gre_channel.sh        #配置两台Docker主机容器实现跨主机访问(OVS)

安装步骤(请按照步骤做):

1)在minion主机root权限开启root允许SSH远程登录

操作命令:$ sudo bash config_ssh_root_remote.sh

脚本内容:$ cat config_ssh_root_remote.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
#!/bin/bash
# Description: configuration root account ssh remote login
if  [ $USER !=  "root"  ];  then
     echo  "Please use root account operation or sudo!"
     exit  1
fi
function  color_echo() {
     if  [ $1 ==  "green"  ];  then
         echo  -e  "\033[32;40m$2\033[0m"
     elif  [ $1 ==  "red"  ];  then
         echo  -e  "\033[31;40m$2\033[0m"
     fi
}
function  check_pkg() {
     if  ! $(dpkg -l $PKG_NAME > /dev/null  2>&1);  then
         echo  no
     else
         echo  yes
     fi
}
function  install_pkg() {
     local  PKG_NAME=$1
     if  [ $(check_pkg $PKG_NAME) ==  "no"  ];  then
         apt-get  install  $PKG_NAME -y
         if  [ $(check_pkg $PKG_NAME) ==  "no"  ];  then
             color_echo green  "$PKG_NAME installation failure! Try to install again."
             apt-get autoremove && apt-get update
             apt-get  install  $PKG_NAME --force- yes  -y
             [ $(check_pkg $PKG_NAME) ==  "no"  ] && color_echo red  "The $PKG_NAME installation failure!"  &&  exit  1
         fi
     fi
}
install_pkg expect
# modify ssh config file
sed  -r -i  's/(PermitRootLogin).*/\1 yes/'  /etc/ssh/sshd_config
service  ssh  restart > /dev/null
# set root account password
echo  "------------------------------------------------------>"
while  true do
     read  -p  "Please enter you want to set the root account password: "  ROOT_PASS
     if  [ -n  "$ROOT_PASS"  ];  then
         break
     else
         color_echo red  "Password cannot be empty!"
         continue
     fi
done
expect -c "
     spawn  passwd  root
     expect {
         \"Enter new UNIX password:\" {send \"$ROOT_PASS\r\"; exp_continue}
         \"Retype new UNIX password:\" {send \"$ROOT_PASS\r\"}
     }
     expect eof
" > /dev/null
color_echo green  "The root account password is: $ROOT_PASS"

2)在master主机切换到root用户执行脚本与minion主机root用户建立SSH免交互登录

操作命令:# bash ssh_keypair_auth.sh root@192.168.1.151-152@123

脚本内容:# cat ssh_keypair_auth.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
#!/bin/bash
# Description: configuration local host and remote host ssh keypair authentication, Support Ubuntu and CentOS operation system.
function  color_echo() {
     if  [ $1 ==  "green"  ];  then
         echo  -e  "\033[32;40m$2\033[0m"
     elif  [ $1 ==  "red"  ];  then
         echo  -e  "\033[31;40m$2\033[0m"
     fi
}
function  os_version() {
     local  OS_V=$( cat  /etc/issue  | awk  'NR==1{print $1}' )
     if  [ $OS_V ==  "\S"  -o $OS_V ==  "CentOS"  ];  then
         echo  "CentOS"
     elif  [ $OS_V ==  "Ubuntu"  ];  then
         echo  "Ubuntu"
     fi
}
function  check_ssh_auth() {
     if  $( grep  "Permission denied"  $EXP_TMP_FILE > /dev/null );  then
         color_echo red  "Host $IP SSH authentication failure! Login password error."
         exit  1
     elif  $( ssh  $INFO  'echo yes >/dev/null' );  then
         color_echo green  "Host $IP SSH authentication successfully."
     fi
     rm  $EXP_TMP_FILE > /dev/null
}
function  check_pkg() {
     local  PKG_NAME=$1
     if  [ $(os_version) ==  "CentOS"  ];  then
         if  ! $(rpm -ql $PKG_NAME > /dev/null  2>&1);  then
             echo  no
         else
             echo  yes
         fi
     elif  [ $(os_version) ==  "Ubuntu"  ];  then
         if  ! $(dpkg -l $PKG_NAME > /dev/null  2>&1);  then
             echo  no
         else
             echo  yes
         fi
     fi
}
function  install_pkg() {
     local  PKG_NAME=$1
     if  [ $(os_version) ==  "CentOS"  ];  then
         if  [ $(check_pkg $PKG_NAME) ==  "no"  ];  then
             yum  install  $PKG_NAME -y
             if  [ $(check_pkg $PKG_NAME) ==  "no"  ];  then
                 color_echo green  "The $PKG_NAME installation failure! Try to install again."
                 yum makecache
                 yum  install  $PKG_NAME -y
                 [ $(check_pkg $PKG_NAME) ==  "no"  ] && color_echo red  "The $PKG_NAME installation failure!"  &&  exit  1
             fi
         fi
     elif  [ $(os_version) ==  "Ubuntu"  ];  then
         if  [ $(check_pkg $PKG_NAME) ==  "no"  ];  then
             apt-get  install  $PKG_NAME -y
             if  [ $(check_pkg $PKG_NAME) ==  "no"  ];  then
                 color_echo green  "$PKG_NAME installation failure! Try to install again."
                 apt-get autoremove && apt-get update
                 apt-get  install  $PKG_NAME --force- yes  -y
                 [ $(check_pkg $PKG_NAME) ==  "no"  ] && color_echo red  "The $PKG_NAME installation failure!"  &&  exit  1
             fi
         fi
     fi
}
function  generate_keypair() {
     if  [ ! -e ~/. ssh /id_rsa .pub ];  then
         color_echo green  "The public/private rsa key pair not exist, start Generating..."
         expect -c "
             spawn  ssh -keygen
             expect {
                 \" ssh /id_rsa ):\" {send \"\r\";exp_continue}
                 \"passphrase):\" {send \"\r\";exp_continue}
                 \"again:\" {send \"\r\";exp_continue}
             }
         " > /dev/null  2>&1
         if  [ -e ~/. ssh /id_rsa .pub ];  then
             color_echo green  "Generating public/private rsa key pair successfully."
         else
             color_echo red  "Generating public/private rsa key pair failure!"
             exit  1
         fi
     fi
}
EXP_TMP_FILE= /tmp/expect_ssh .tmp
if  [[ $1 =~ ^[a-z]+@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}@.* ]];  then
     install_pkg expect ; generate_keypair
     for  in  $@;  do
         USER=$( echo  $i| cut  -d@ -f1)
         IP=$( echo  $i| cut  -d@ -f2)
         PASS=$( echo  $i| cut  -d@ -f3)
         INFO=$USER@$IP
         expect -c "
             spawn  ssh -copy- id  $INFO
             expect {
                 \"( yes /no )?\" {send \" yes \r\";exp_continue}
                 \"password:\" {send \"$PASS\r\";exp_continue}
             }
         " > $EXP_TMP_FILE   # if login failed, login error info append temp file
         check_ssh_auth
     done
elif  [[ $1 =~ ^[a-z]+@[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}-[0-9]{1,3}@.* ]];  then
     install_pkg expect ; generate_keypair
     START_IP_NUM=$( echo  $1| sed  -r  's/.*\.(.*)-(.*)@.*/\1/' )
     END_IP_NUM=$( echo  $1| sed  -r  's/.*\.(.*)-(.*)@.*/\2/' )
     for  ((i=$START_IP_NUM;i<=$END_IP_NUM;i++));  do
         USER=$( echo  $1| cut  -d@ -f1)
         PASS=$( echo  $1| cut  -d@ -f3)
         IP_RANGE=$( echo  $1| sed  -r  's/.*@(.*\.).*/\1/' )
         IP=$IP_RANGE$i
         INFO=$USER@$IP_RANGE$i
         expect -c "
             spawn  ssh -copy- id  $INFO
             expect {
                 \"( yes /no )?\" {send \" yes \r\";exp_continue}
                 \"password:\" {send \"$PASS\r\";exp_continue}
             }
         " > $EXP_TMP_FILE
         check_ssh_auth
     done
else
     echo  "Example1: $0 <root@192.168.1.10-15@password>"
     echo  "Example2: $0 <root@192.168.1.10@password>"
     echo  "Example3: $0 [root@192.168.1.10@password root@192.168.1.11@password root@192.168.1.12@password ...]"
fi

3)在master主机root权限执行脚本安装master端服务

操作命令:$ sudo bash kubernetes-install.sh master

脚本内容:$ cat kubernetes-install.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