淘先锋技术网

首页 1 2 3 4 5 6 7

一、架构图

如下图所示:
在这里插入图片描述


二、环境信息

1、部署规划

主机名 IP地址 操作系统 内核版本 软件 说明
etcd01 192.168.1.62 Ubuntu 20.04.5 LTS 5.15.0-69-generic etcd etcd节点
etcd02 192.168.1.63 Ubuntu 20.04.5 LTS 5.15.0-69-generic etcd etcd节点
etcd03 192.168.1.64 Ubuntu 20.04.5 LTS 5.15.0-69-generic etcd etcd节点
k8s-master-65 192.168.1.65 Ubuntu 20.04.5 LTS 5.15.0-69-generic cri-containerd-cni、kubectl、kube-apiserver、kube-controller-manager、kube-scheduler、kubelet、kube-proxy master节点
k8s-worker-66 192.168.1.66 Ubuntu 20.04.5 LTS 5.15.0-69-generic cri-containerd-cni、kubelet、kube-proxy worker节点

2、软件信息

软件 版本 说明
etcd v3.5.6 二进制文件
cri-containerd-cni 1.6.12 二进制文件
crictl 1.26.1 二进制文件
kubectl、kube-apiserver、kube-controller-manager、kube-scheduler、kubelet、kube-proxy 1.24.12 二级制文件
coredns v1.8.6 容器镜像
calico v3.25.0 容器镜像
pause 3.7 容器镜像
cfssl 1.6.3 二进制文件
cfssljson 1.6.3 二进制文件
cfssl-certinfo 1.6.3 二进制文件

3、集群网段

宿主机 集群Pod网段 集群Service网段
192.168.1.0/24 10.48.0.0/16 10.96.0.0/16

说明:定义10.96.0.10为CoreDNS的Service IP地址。

4、服务端口

服务名称 端口默认值 参数 协议 端口 必须开启 说明
kube-apiserver 6443 –secure-port TCP(HTTPS) 安全端口 -
kube-controller-manager 10257 –secure-port TCP(HTTPS) 安全端口 建议开启 认证与授权
kube-scheduler 10259 –secure-port TCP(HTTPS) 安全端口 建议开启 认证与授权
kubelet 10248 –healthz-port TCP(HTTP) 健康检测端口 建议开启 -
kubelet 10250 –port TCP(HTTPS) 安全端口 认证与授权
kubelet 10255 –read-only-port TCP (HTTP) 非安全端口 建议开启 -
kube-proxy 10249 –metrics-port TCP(HTTP) 指标端口 建议开启 -
kube-proxy 10256 –healthz-port TCP(HTTP) 健康检测端口 建议开启 -
NodePort 30000-36000 –service-node-port-range TCP/UDP - - -
etcd 2379 - TCP - - etcd客户端请求端口
etcd 2380 - TCP - - etcd节点之间通信端口

三、初始化环境

3.1、配置主机名

说明:分别在对应的节点IP上设置主机名。

root@lolaage-virtual-machine:~#  hostnamectl set-hostname etcd01
root@lolaage-virtual-machine:~#  hostnamectl set-hostname etcd02
root@lolaage-virtual-machine:~#  hostnamectl set-hostname etcd03
root@lolaage-virtual-machine:~#  hostnamectl set-hostname k8s-master-65
root@lolaage-virtual-machine:~#  hostnamectl set-hostname k8s-worker-66

3.2、配置主机名映射

说明:以下操作无论是etcd节点、master节点和worker节点均需要执行。

cat >> /etc/hosts <<EOF
192.168.1.62 etcd01
192.168.1.63 etcd02
192.168.1.64 etcd03
192.168.1.65 k8s-master-65
192.168.1.66 k8s-worker-66
EOF

3.3、关闭防火墙

说明:以下操作无论是etcd节点、master节点和worker节点均需要执行。

ufw status
ufw disable

3.4、设置ulimit

说明:以下操作无论是etcd节点、master节点、worker节点、Nginx负载均衡节点均需要执行。

ulimit -SHn 65535
cat >> /etc/security/limits.conf <<EOF
root soft nofile 65535
root hard nofile 65535
root soft nproc 65535
root hard nproc 65535
* soft nofile 65535
* hard nofile 65535
* soft nproc 65535
* hard nproc 65535
EOF

3.5、关闭selinux

说明:以下操作无论是etcd节点、master节点和worker节点均需要执行。

apt install selinux-utils
apt install policycoreutils
sed -i 's#SELINUX=permissive#SELINUX=disabled#g' /etc/selinux/config
sestatus -v

说明:如果selinux默认关闭则无需修改。


3.6、禁用虚拟内存

说明:以下操作无论是etcd节点、master节点和worker节点均需要执行。

swapoff -a
sed -i 's/^\/swapfile\(.*\)$/#\/swapfile \1/g' /etc/fstab

3.7、配置时间同步

说明:以下操作无论是etcd节点、master节点和worker节点均需要执行。

1、设置时区为Asia/Shanghai,如果已经是则请忽略

root@k8s-master-65:~# timedatectl
               Local time: 五 2023-03-31 14:11:36 CST
           Universal time: 五 2023-03-31 06:11:36 UTC
                 RTC time: 五 2023-03-31 06:11:36    
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes                       
              NTP service: active                    
          RTC in local TZ: no  

2、使用chrony同步时间

root@k8s-master-65:~# apt install chrony -y
root@k8s-master-65:~# vim /etc/chrony/chrony.conf
server ntp.aliyun.com minpoll 4 maxpoll 10 iburst
server ntp1.aliyun.com minpoll 4 maxpoll 10 iburst
#pool ntp.ubuntu.com        iburst maxsources 4
#pool 0.ubuntu.pool.ntp.org iburst maxsources 1
#pool 1.ubuntu.pool.ntp.org iburst maxsources 1
#pool 2.ubuntu.pool.ntp.org iburst maxsources 2

root@k8s-master-65:~# systemctl enable chronyd.service 
root@k8s-master-65:~# systemctl restart chronyd.service
root@k8s-master-65:~# systemctl status chronyd.service

阿里云NTP服务器地址列表,状态检测如下所示:
在这里插入图片描述

然后就是chrony客户端上的一些常用命令:

#查看可用的时间同步源
chronyc sources -v
 
#查看时间同步源的状态
chronyc sourcestats -v
 
#对客户端系统时间进行强制同步
chronyc -a makestep

3.8、配置内核参数

说明:以下操作只需要在master节点和worker节点执行。

说明:有一些ipv4的流量不能走iptables链,因为linux内核的一个过滤器,每个流量都会经过他,然后再匹配是否可进入当前应用进程去处理,所以会导致流量丢失。配置k8s.conf文件,如下所示:

root@k8s-master-65:~# cat <<EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

root@k8s-master-65:~# modprobe overlay
root@k8s-master-65:~# modprobe br_netfilter

# 设置所需的sysctl参数,参数在重新启动后保持不变
root@k8s-master-65:~# cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

# 应用sysctl参数而不重新启动
root@k8s-master-65:~# sysctl --system

3.9、启用ipvs模块

说明:以下操作只需在master节点和worker节点执行。

说明:ube-proxy开启ipvs的前提需要加载以下的内核模块

ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack_ipv4

注意:如果出现modprobe: FATAL: Module nf_conntrack_ipv4 not found in directory /lib/modules/5.15.0-69-generic错误,这是因为使用了高内核,当前内核版本为5.15.0-69-generic,在高版本内核已经把nf_conntrack_ipv4替换为nf_conntrack了。

# 1、安装ipvs
root@k8s-master-65:~#  apt -y install ipvsadm ipset sysstat conntrack

# 2、加载内核模块脚本
root@k8s-master-65:~# cat > /etc/profile.d/ipvs.modules <<EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF
root@k8s-master-65:~# chmod 755 /etc/profile.d/ipvs.modules

#3、执行加载模块脚本
root@k8s-master-65:~# bash /etc/profile.d/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4

四、生成证书和软件包下载

4.1、下载软件包

1、下载etcd

wget https://github.com/etcd-io/etcd/releases/download/v3.5.6/etcd-v3.5.6-linux-amd64.tar.gz

2、下载cri-containerd-cni

wget https://github.com/containerd/containerd/releases/download/v1.6.12/cri-containerd-1.6.12-linux-amd64.tar.gz

3、下载crictl客户端工具

wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.26.1/crictl-v1.26.1-linux-amd64.tar.gz

4、下载Kubernetes二进制包