Kubernetes部署
# 集群环境部署规划
- 首先需要规划好K8S三个网络的网段。
- 节点网络就是节点们所在局域网的网络,例如:192.168.20.0/24
- Service网络:10.96.0.0/12
- Pod网络:10.1.0.0/16
# K8S部署方式
第一种方式是将K8s的各组件,运行为系统级的守护进程,该方式需要通过yum、编译等方式进行安装,过程会非常的繁琐。
第二种方式是使用kubeadm集群部署管理工具来进行部署,kubeadm用于快速的部署K8s的核心组件,同时会自动的生成各组件的CA证书。
- kubeadm的部署方式,会将K8s的各组件作为Pod以容器引擎为底层进行部署,每一个节点都需要通过kubeadm安装kubelet和其他需要的组件到容器引擎中。
# 部署Master节点
# 1. 环境准备
# 设置主机名
# 一般设为master01、master02之类的
hostnamectl set-hostname master01
# 添加主机名解析: 本机在节点网络中的地址
echo '192.168.20.11 master01' >> /etc/hosts
# 安装IPVS负载均衡管理工具 (CentOS使用yum安装)
apt install -y ipset ipvsadm
# 关闭防火墙和交换分区
# Ubuntu执行
ufw disable && systemctl disable ufw
swapoff -a && sed -ri 's@(.*? swap .*)@#\1@g' /etc/fstab
# CentOS执行
systemctl stop firewalld && systemctl disable firewalld
setenforce 0 && sed -i.bak "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
swapoff -a && sed -ri 's@(.*? swap .*)@#\1@g' /etc/fstab
# 设置时间同步 (CentOS使用yum安装)
apt install -y ntpdate
ntpdate ntp.aliyun.com
# 添加内核模块开机自启
cat>>/etc/rc.d/rc.local<<'EOF'
# netfilter
modprobe br_netfilter
# containerd.
modprobe overlay
# ipvs
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack
EOF
# 立即载入模块
chmod +x /etc/rc.d/rc.local && source /etc/rc.d/rc.local
# 内核相关参数调整
cat>>/etc/sysctl.conf<<'EOF'
net.ipv4.ip_forward = 1
vm.swappiness = 0
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
# 验证是否生效
sysctl -p /etc/sysctl.conf
lsmod | grep -e ip_vs -e nf_conntrack
# 重新加载系统配置文件
sysctl --system
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
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
# 2. 部署Containerd服务
- 下载Containerd二进制包
- https://github.com/containerd/containerd/releases
- 解压二进制包
tar xvf containerd*.tar.gz
mv ./bin/* /usr/bin/ && rm -rf ./bin
1
2
2
- 创建配置文件
# 创建配置文件
mkdir /etc/containerd
containerd config default > /etc/containerd/config.toml
# 修改配置文件
sed -ri "s#(.*? = \").*\.io/pause(.*)#\1registry.cn-hangzhou.aliyuncs.com/google_containers/pause\2#g" /etc/containerd/config.toml && \
sed -ri 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml && \
sed -ri 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml && \
sed -ri "/registry.mirrors]/a\ \ \ \ \ \ [plugins.\"io.containerd.grpc.v1.cri\".registry.mirrors.\"docker.io\"]\n endpoint = [\"http://hub-mirror.c.163.com\"]" /etc/containerd/config.toml
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 创建systemd管理文件
cat>/lib/systemd/system/containerd.service<<'EOF'
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target
[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/bin/containerd
Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
TasksMax=infinity
OOMScoreAdjust=-999
[Install]
WantedBy=multi-user.target
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- 安装RunC容器组件
# 可以按需选择最新版进行下载
curl -o /usr/bin/runc https://github.com/opencontainers/runc/releases/download/v1.1.10/runc.amd64
chmod +x /usr/bin/runc
1
2
3
2
3
- 启动containerd
systemctl daemon-reload && \
systemctl start containerd && \
systemctl enable containerd && \
systemctl status containerd && \
ctr --version
1
2
3
4
5
2
3
4
5
# 3. 安装kubeadm环境
# Ubuntu
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
echo 'deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main' >> /etc/apt/sources.list.d/kubernetes.list
apt-get update
# 查看kubeadm版本号
apt-cache madison kubeadm | head -n 8
# 安装kubelet/kubeadm/kubectl
apt-get -y install kubeadm=1.28.2-00 kubelet=1.28.2-00 kubectl=1.28.2-00 && \
apt-mark hold kubelet kubeadm kubectl
# 安装crictl工具并生成配置文件
apt install -y cri-tools
crictl config runtime-endpoint /run/containerd/containerd.sock
# 启动kubelet服务
systemctl start kubelet && systemctl enable kubelet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# CentOS
cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
enabled=1
EOF
# 查看kubeadm版本号
yum list kubeadm --showduplicates | tail -8
# 安装kubelet/kubeadm/kubectl
yum install -y kubelet-1.28.2-0 kubeadm-1.28.2-0 kubectl-1.28.2-0
# 安装crictl工具并生成配置文件
yum install -y cri-tools
crictl config runtime-endpoint /run/containerd/containerd.sock
# 启动kubelet服务
systemctl start kubelet && systemctl enable kubelet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 4. 初始化集群
# 执行初始化
kubeadm init --apiserver-advertise-address [内网网卡IP] \ # Master在节点网络中的IP, 例如:192.168.20.11
--apiserver-bind-port 6443 \
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \ # 指定组件镜像源
--kubernetes-version v1.28.2 \ # 集群版本号,保持和kubeadm的一致即可
--pod-network-cidr 10.1.0.0/16 \ # Pod网络
--service-cidr 10.96.0.0/12 \ # Service网络
--service-dns-domain cluster.local \
-v6 # 打印详细日志
--apiserver-cert-extra-sans [额外允许访问的外网IP] # 如果需要外网访问集群,则需要指定,否则跳过即可
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
初始化完成后,记录一下打印的join命令,类似如下:
# 该命令是Node节点用来加入集群用的
kubeadm join 192.168.20.11:6443 --token 2zpkxe.rzh83h888pyyl0il \
--discovery-token-ca-cert-hash sha256:062a273040581ad7f033f7c4157f7f756cd29c605c1d8a23a181809dd54ef943
1
2
3
2
3
然后再根据打印的提示配置kubectl认证信息,一般如下:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
1
2
3
2
3
# 5. 配置Calico网络插件
# 下载calico清单文件
curl -O https://raw.githubusercontent.com/projectcalico/calico/master/manifests/calico.yaml
# 修改calico.yaml中节点网络的IP网段, 同时需要去掉#注释
sed -i -e 's@# value: "192.168.0.0/16"@ value: "10.1.0.0/16"@g' -e "s@# - name: CALICO_IPV4POOL_CIDR@- name: CALICO_IPV4POOL_CIDR@g" calico.yaml
# 检查是否配置正确
cat calico.yaml |grep -A1 CALICO_IPV4POOL_CIDR
# 部署calico插件
kubectl apply -f calico.yaml
# 观察是否正常部署
kubectl get pod -n kube-system -w
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 6. 启用IPVS
# 编辑K8s配置, 将mode的值修改为"ipvs"
kubectl edit configmap kube-proxy -n kube-system
# 删除所有kube-proxy,让其重新拉起
kubectl delete pod -l k8s-app=kube-proxy -n kube-system
# 检查是否生效
# 有打印 "Using ipvs Proxier” 则表示已生效
kubectl logs -l k8s-app=kube-proxy -n kube-system --tail=100 | grep "Using ipvs Proxier"
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 7. 其他优化
以下是优化使用体验的操作,可以不执行
# 添加命令补全
apt install -y bash-completion
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)" >> ~/.bashrc
1
2
3
4
2
3
4
# 解除nodePort端口限制 (默认nodePort只能指定高位端口)
- 找到
kube-apiserver.yaml
文件,一般在 /etc/kubernetes/manifests/kube-apiserver.yaml - 在spec.containers.command下添加参数 --service-node-port-range=1-65535
- 然后等待apiserver自动重启即可
- 找到
spec:
containers:
- command:
- kube-apiserver
- --service-node-port-range=1-65535
1
2
3
4
5
2
3
4
5
# 去除Master不可调度污点 (可选,生产环境不要使用)
- 查看污点
kubectl describe nodes master | grep Taints
- 删除不可调度污点
kubectl taint nodes master node-role.kubernetes.io/master=:NoSchedule-
kubectl taint nodes master node-role.kubernetes.io/control-plane:NoSchedule-
- 查看污点
# 简化kubectl命令
- 给kubectl设置别名,命令操作更方便,缺点是不能命令补全。
alias k='kubectl'
echo "alias k='kubectl'" >> ~/.bashrc
1
2
2
# 部署Node节点
直到初始化为止,步骤都和Master节点差不多。
# 1. 环境准备
# 设置主机名
# 一般设为node01、node02之类的
hostnamectl set-hostname node01
# 添加主机名解析: 本机在节点网络中的地址
echo '192.168.20.12 node01' >> /etc/hosts
# 关闭交换分区
swapoff -a && sed -ri 's@(.*? swap .*)@#\1@g' /etc/fstab
# 设置时间同步 (CentOS使用yum安装)
apt install -y ntpdate
ntpdate ntp.aliyun.com
# 添加内核模块开机自启
cat>>/etc/rc.d/rc.local<<'EOF'
# netfilter
modprobe br_netfilter
# containerd.
modprobe overlay
# ipvs
modprobe ip_vs
modprobe ip_vs_rr
modprobe ip_vs_wrr
modprobe ip_vs_sh
modprobe nf_conntrack
EOF
# 立即载入模块
chmod +x /etc/rc.d/rc.local && source /etc/rc.d/rc.local
# 内核相关参数调整
cat>>/etc/sysctl.conf<<'EOF'
net.ipv4.ip_forward = 1
vm.swappiness = 0
net.bridge.bridge-nf-call-iptables = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
# 验证是否生效
sysctl -p /etc/sysctl.conf
lsmod | grep -e ip_vs -e nf_conntrack
# 重新加载系统配置文件
sysctl --system
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
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
# 2. 部署Containerd服务
- 下载Containerd二进制包
- https://github.com/containerd/containerd/releases
- 解压二进制包
tar xvf containerd*.tar.gz
mv ./bin/* /usr/bin/ && rm -rf ./bin
1
2
2
- 创建配置文件
# 创建配置文件
mkdir /etc/containerd
containerd config default > /etc/containerd/config.toml
# 修改配置文件
sed -ri "s#(.*? = \").*\.io/pause(.*)#\1registry.cn-hangzhou.aliyuncs.com/google_containers/pause\2#g" /etc/containerd/config.toml && \
sed -ri 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml && \
sed -ri 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml && \
sed -ri "/registry.mirrors]/a\ \ \ \ \ \ [plugins.\"io.containerd.grpc.v1.cri\".registry.mirrors.\"docker.io\"]\n endpoint = [\"http://hub-mirror.c.163.com\"]" /etc/containerd/config.toml
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
- 创建systemd管理文件
cat>/lib/systemd/system/containerd.service<<'EOF'
[Unit]
Description=containerd container runtime
Documentation=https://containerd.io
After=network.target local-fs.target
[Service]
ExecStartPre=-/sbin/modprobe overlay
ExecStart=/usr/bin/containerd
Type=notify
Delegate=yes
KillMode=process
Restart=always
RestartSec=5
LimitNPROC=infinity
LimitCORE=infinity
LimitNOFILE=infinity
TasksMax=infinity
OOMScoreAdjust=-999
[Install]
WantedBy=multi-user.target
EOF
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
- 安装RunC容器组件
# 可以按需选择最新版进行下载
curl -o /usr/bin/runc https://github.com/opencontainers/runc/releases/download/v1.1.10/runc.amd64
chmod +x /usr/bin/runc
1
2
3
2
3
- 启动containerd
systemctl daemon-reload && \
systemctl start containerd && \
systemctl enable containerd && \
systemctl status containerd && \
ctr --version
1
2
3
4
5
2
3
4
5
# 3. 安装kubeadm环境
# Ubuntu
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
echo 'deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main' >> /etc/apt/sources.list.d/kubernetes.list
apt-get update
# 安装kubelet/kubeadm,版本需要和集群版本一致
apt-get -y install kubeadm=1.28.2-00 kubelet=1.28.2-00 && \
apt-mark hold kubelet kubeadm
# 启动kubelet服务
systemctl start kubelet && systemctl enable kubelet
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# CentOS
cat > /etc/yum.repos.d/kubernetes.repo <<EOF
[kubernetes]
name=Kubernetes Repo
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
enabled=1
EOF
# 安装kubelet/kubeadm,版本需要和集群版本一致
yum install -y kubelet-1.28.2-0 kubeadm-1.28.2-0
# 启动kubelet服务
systemctl start kubelet && systemctl enable kubelet
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 4.加入集群
# 执行之前记录的kubeadm join命令即可
kubeadm join 192.168.20.11:6443 --token 2zpkxe.rzh83h888pyyl0il \
--discovery-token-ca-cert-hash sha256:062a273040581ad7f033f7c4157f7f756cd29c605c1d8a23a181809dd54ef943
# 等待两三分钟后去Master节点执行以下命令, 查看node状态是否为Ready正常即可
kubectl get node
1
2
3
4
5
6
2
3
4
5
6