# 半自动化安装K8s集群脚本

作者:Odboy (opens new window)

本站地址:https://blog.odboy.cn (opens new window)

适合刚入坑的运维小伙伴学习

我直接打包了,不太好描述,应该可以看懂,看不懂请私信我邮箱tianjun@odboy.cn,或者微信i_odboy
1

======== 点击此处下载 ========

# K8s Master节点

带有master的脚本意思是在master节点上执行的, 以下为脚本内容

#!/bin/bash
# Kubernetes部署环境要求
#(1)一台或多台机器,操作系统CentOS 7.x-86_x64
#(2)硬件配置:内存2GB或2G+,CPU 2核或CPU 2核+
#(3)集群内各个机器之间能相互通信
#(4)集群内各个机器可以访问外网,需要拉取镜像
#(5)禁止swap分区

# 安装步骤
#1. 安装docker
#1.1 如果没有安装docker,则安装docker。会附带安装一个docker-compose
#
#2. 安装k8s
#2.1 初始化环境
#2.2 添加安装源
#2.3 安装kubelet、kubectl、kubeadmin
#2.4 安装master
#2.5 安装网络插件

# 如果一个命令返回一个非0退出状态值(失败)就退出
set -e

# 安装日志
install_log=/var/log/install_k8s.log
install_time=$(date +'%Y%m%d %T')

# k8s相关
k8s_version=1.19.4
k8s_service_cidr=10.96.0.0/12
## 这个与component中, kube-flannel.yml文件中的, net-conf.json配置项中的Network节点一致
k8s_pod_network_cidr=10.244.0.0/16

function log_info(){
# ${1}表示传入info的第一个参数
echo -e "\x1b[0;32m[$install_time] [Info] ${1}\x1b[0m"
}

function run_cmd(){
sh -c "$1 | $(tee -a "$install_log")"
}

function run_function(){
$1 | tee -a "$install_log"
}

function install_init() {
# yum source alibaba
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak
curl http://mirrors.aliyun.com/repo/Centos-7.repo > /etc/yum.repos.d/CentOS-Base.repo
curl http://mirrors.aliyun.com/repo/epel-7.repo > /etc/yum.repos.d/epel.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/epel.repo

# 安装常用软件包
yum install wget -y

yum clean all
yum makecache
yum -y update
}

function install_docker(){
log_info "1.安装docker..."
yum install yum-utils device-mapper-persistent-data lvm2 -y
#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum install docker-ce -y

log_info "2.启动 Docker CE..."
sudo systemctl enable docker
sudo systemctl start docker

log_info "3.添加镜像加速器..."
if [ ! -f "/etc/docker/daemon.json" ];then
  touch /etc/docker/daemon.json
fi
cat <<EOF > /etc/docker/daemon.json
{
  	"registry-mirrors": [
  		"http://hub-mirror.c.163.com",
  		"https://docker.mirrors.ustc.edu.cn",
  		"https://registry.docker-cn.com"
  	]
}
EOF

log_info "4.重新启动服务..."
sudo systemctl daemon-reload
sudo systemctl restart docker

log_info "5.测试 Docker 是否安装正确..."
docker run hello-world

#log_info "6.安装docker-compose..."
#sudo curl -L "https://mirror.ghproxy.com/https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#sudo chmod a+x /usr/local/bin/docker-compose

# 8.验证是否安装成功
#log_info "7.验证docker-compose是否安装成功..."
#docker-compose -v
}

function install_k8s() {
log_info "初始化k8s部署环境..."
init_env

log_info "添加k8s安装源..."
add_aliyun_repo

log_info "安装kubelet kubeadmin kubectl..."
install_kubelet_kubeadmin_kubectl

log_info "安装kubernetes master..."
yum -y install net-tools
if [[ ! "$(ps aux | grep 'kubernetes' | grep -v 'grep')" ]];then
  kubeadmin_init
else
  log_info "kubernetes master已经安装..."
fi

log_info "安装网络插件flannel..."
install_flannel

log_info "去污点..."
kubectl taint nodes --all node-role.kubernetes.io/master-
}

function init_env() {
log_info "初始化部署环境..."
log_info "关闭防火墙..."
systemctl stop firewalld
systemctl disable firewalld

log_info "关闭selinux..."
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/g' /etc/selinux/config
source /etc/selinux/config

log_info "关闭swap(k8s禁止虚拟内存以提高性能)..."
swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab

log_info "设置网桥参数..."
cat <<-EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system  #生效
sysctl -w net.ipv4.ip_forward=1

log_info "时间同步..."
yum install ntpdate -y
ntpdate ntp.aliyun.com
}

function add_aliyun_repo() {
log_info "添加Kubernetes Aliyun安装源..."
cat > /etc/yum.repos.d/kubernetes.repo <<- EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
}

function install_kubelet_kubeadmin_kubectl() {
log_info "安装kubernetes..."
yum install kubelet-$k8s_version kubeadm-$k8s_version kubectl-$k8s_version -y
systemctl enable kubelet.service

log_info "确认kubelet kubeadmin kubectl是否安装成功..."
yum list installed | grep kubelet
yum list installed | grep kubeadm
yum list installed | grep kubectl
kubelet --version
}

function kubeadmin_init() {
local_ip=$(ip a| grep "scope global"|grep -v "docker"| cut -d " " -f 6| cut -d "/" -f 1)
kubeadm init --apiserver-advertise-address="${local_ip}" --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v$k8s_version --service-cidr=$k8s_service_cidr --pod-network-cidr=$k8s_pod_network_cidr
mkdir -p "$HOME"/.kube
sudo cp -i /etc/kubernetes/admin.conf "$HOME"/.kube/config
sudo chown "$(id -u)":"$(id -g)" "$HOME"/.kube/config
}

function install_flannel() {
kubectl apply -f ./component/kube-flannel.yml
}

function gen_never_expire_token() {
log_info "生成永不过期的Token,并且展示node节点加入子节点的命令"
kubeadm token create --ttl 0 --print-join-command
}

run_function "install_docker"
run_function "install_k8s"
run_function "gen_never_expire_token"
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

# K8s Node节点

带有node的脚本意思是在node节点上执行的, 以下为脚本内容

#!/bin/bash
# Kubernetes部署环境要求:
#(1)一台或多台机器,操作系统CentOS 7.x-86_x64
#(2)硬件配置:内存2GB或2G+,CPU 2核或CPU 2核+
#(3)集群内各个机器之间能相互通信
#(4)集群内各个机器可以访问外网,需要拉取镜像
#(5)禁止swap分区

# 安装步骤
#1. 安装docker
#1.1 如果没有安装docker,则安装docker。会附带安装一个docker-compose
#
#2. 安装k8s
#2.1 初始化环境
#2.2 添加安装源
#2.3 安装kubelet、kubectl、kubeadmin
#2.4 安装master
#2.5 安装网络插件

# 如果一个命令返回一个非0退出状态值(失败)就退出
set -e

# 安装日志
install_log=/var/log/install_k8s.log
install_time=$(date +'%Y%m%d %T')

# k8s相关
k8s_version=1.19.4
k8s_service_cidr=10.96.0.0/12
# 这个与component中, kube-flannel.yml文件中的, net-conf.json配置项中的Network节点一致
k8s_pod_network_cidr=10.244.0.0/16
# 这个地址需要手动修改哦,别忘了
k8s_master_ipaddr=192.168.234.100

function log_info(){
# ${1}表示传入info的第一个参数
echo -e "\x1b[0;32m[$install_time] [Info] ${1}\x1b[0m"
}

function run_cmd(){
sh -c "$1 | $(tee -a "$install_log")"
}

function run_function(){
$1 | tee -a "$install_log"
}

function install_init() {
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo_bak
curl http://mirrors.aliyun.com/repo/Centos-7.repo > /etc/yum.repos.d/CentOS-Base.repo
curl http://mirrors.aliyun.com/repo/epel-7.repo > /etc/yum.repos.d/epel.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/epel.repo
yum clean all
yum makecache
yum -y update
}

function install_docker(){
log_info "1.安装docker..."
yum install yum-utils device-mapper-persistent-data lvm2 -y
#yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum makecache fast
yum install docker-ce -y

log_info "2.启动 Docker CE..."
sudo systemctl enable docker
sudo systemctl start docker

log_info "3.添加镜像加速器..."
if [ ! -f "/etc/docker/daemon.json" ];then
  touch /etc/docker/daemon.json
fi
cat <<EOF > /etc/docker/daemon.json
{
  	"registry-mirrors": [
  		"http://hub-mirror.c.163.com",
  		"https://docker.mirrors.ustc.edu.cn",
  		"https://registry.docker-cn.com"
  	]
}
EOF

log_info "4.重新启动服务..."
sudo systemctl daemon-reload
sudo systemctl restart docker

log_info "5.测试 Docker 是否安装正确..."
docker run hello-world

#log_info "6.安装docker-compose..."
#sudo curl -L "https://mirror.ghproxy.com/https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
#sudo chmod a+x /usr/local/bin/docker-compose

# 8.验证是否安装成功
#log_info "7.验证docker-compose是否安装成功..."
#docker-compose -v
}

function install_k8s() {
info "初始化k8s部署环境..."
init_env

info "添加k8s安装源..."
add_aliyun_repo

info "安装kubelet kubeadmin kubectl..."
install_kubelet_kubeadmin_kubectl
}

# 初始化部署环境
function init_env() {
info "关闭防火墙..."
systemctl stop firewalld
systemctl disable firewalld

info "关闭selinux..."
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/g' /etc/selinux/config
source /etc/selinux/config

log_info "关闭swap(k8s禁止虚拟内存以提高性能)..."
swapoff -a
sed -i '/swap/s/^\(.*\)$/#\1/g' /etc/fstab

info "设置网桥参数"
cat <<-EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system  #生效
sysctl -w net.ipv4.ip_forward=1

log_info "时间同步..."
yum install ntpdate -y
ntpdate ntp.aliyun.com
}

# 添加aliyun安装源
function add_aliyun_repo() {
cat > /etc/yum.repos.d/kubernetes.repo <<- EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
}

function install_kubelet_kubeadmin_kubectl() {
log_info "安装kubernetes..."
yum install kubelet-$k8s_version kubeadm-$k8s_version kubectl-$k8s_version -y
systemctl enable kubelet.service

log_info "确认kubelet kubeadmin kubectl是否安装成功..."
yum list installed | grep kubelet
yum list installed | grep kubeadm
yum list installed | grep kubectl
kubelet --version
}

function kubeadmin_init() {
  kubeadm init --apiserver-advertise-address="${k8s_master_ipaddr}" --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v$k8s_version --service-cidr=$k8s_service_cidr --pod-network-cidr=$k8s_pod_network_cidr
  mkdir -p "$HOME"/.kube
  sudo cp -i /etc/kubernetes/admin.conf "$HOME"/.kube/config
  sudo chown "$(id -u)":"$(id -g)" "$HOME"/.kube/config
}

function install_flannel() {
  kubectl apply -f ./component/kube-flannel.yml
}

run_function "install_docker"
run_function "install_k8s"
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
最近更新: 2024-10-11
半自动化安装K8s集群脚本

2017 - 武林秘籍   |