1、基础环境准备
当前国内无法访问docker.hub因此需要提前准备可以访问的镜像仓库
1、设备IP地址规划
名称 | IP地址 | 系统 |
---|---|---|
Master | 192.168.110.133 | Centos stream8 |
Slave01 | 192.168.110.134 | Centos stream8 |
Slave02 | 192.168.110.135 | Centos stream8 |
2、操作系统要求
# 1、关闭防火墙/SELINUX
ufw status
ufw disabel
# 2、禁用selinux
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
setenforce 0
#3、关闭swap分区,K8S在使用CPU和内存为物理内存和CPU,Cgroup相关驱动无法对其进行有效管理
sed -ri 's/.*swap.*/#&/' /etc/fstab
swapoff -a # 查查是否关闭swap分区
# 4、设置主机名称
vim /etc/hosts
192.168.110.133 Master
192.168.110.134 Slave01
192.168.110.135 Slave02
# 5、同步时间
#查看时区,时间
date
#先查看时区是否正常,不正确则替换为上海时区
timedatectl set-timezone Asia/Shanghai
#安装chrony,联网同步时间
apt install chrony -y && systemctl enable --now chronyd
# 6、配置桥接的IPV4流量传递到iptables的链
cat >> /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward=1
vm.swappiness=0
EOF
sysctl --system
# 7、服务器之间设置免密登录
ssh-keygen -t rsa
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.110.131
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.110.132
2、使用Kubeadm安装K8s(所有主机)
1、配置内核转发以及网桥过滤
# 创建加载内核模块 (主机启动后自动加载)
cat << EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
# 手动执行,加载模块
modprobe overlay
modprobe br_netfilter
# 查看以及已经加载模块
lsmod | egrep "overlay"
lsmod | egrep "br_netfilter"
# 添加网桥过滤以及内核转发配置文件
cat >> /etc/sysctl.d/k8s.conf <<EOF
net.bridge.bridge-nf-call-ip6tables=1
net.bridge.bridge-nf-call-iptables=1
net.ipv4.ip_forward=1
vm.swappiness=0
EOF
# 加载内核
sysctl --system
2、安装ipset 以及 ipvsadm
apt install ipset ipvsadm -y
cat << EOF | tee /etc/modules-load.d/ipvs.conf
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
ip_vs_sh
nf_conntrack
EOF
# 创建模块加载脚本
cat << EOF | tee ipvs.sh
#!/bin/sh
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- ip_conntrack
EOF
# 执行脚本,加载模块
sh ipvs.sh
3、容器运行时与containerd(所有主机)
1、安装containerd(二进制安装)
# 1、安装containerd
wget https://github.com/containerd/containerd/releases/download/v1.7.14/containerd-1.7.14-linux-amd64.tar.gz
tar xvf containerd-1.7.14-linux-amd64.tar.gz
#解压出来一个bin目录,containerd可执行文件都在bin目录里面
mv bin/* /usr/local/bin/
wget https://github.com/containerd/containerd/releases/download/v1.7.22/cri-containerd-1.7.22-linux-amd64.tar.gz
tar xf cri-containerd-1.7.22-linux-amd64.tar.gz -C /
2、Containerd配置文件修改,并启动containerd
mkdir /etc/containerd
containerd config default > /etc/containerd/config.toml
# 修改配置文件,修改pause版本,1.29以后为3.9
vim /etc/containerd/config.toml
sandbox_image = "registry.k8s.io/pause:3.9"
# 修改镜像仓库地址
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.9"
# 修改指定内核驱动为Cgroup,139行,修改runc中的配置
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
#启动containerd
systemctl enabled --now containerd
4、K8S集群部署(所有主机)
1、下载安装文件
sudo apt-get update