软件环境:
操作系统 AlmaLinux 9
容器引擎 Containerd
Kubernetes 1.25
服务器规划:
k8s-master 192.168.200.11
k8s-node1 192.168.200.12
k8s-node2 192.168.200.13
查看系统内核
#查看当前系统的版本:
[root@k8s-master ~]# cat /etc/redhat-release
AlmaLinux release 9.4 (Seafoam Ocelot)
#查看当前系统的内核:
[root@k8s-master ~]# uname -sr
Linux 5.14.0-362.24.2.el9_3.x86_64
#更新系统组件以及安装开发工具:
dnf -y update \
&& dnf -y groupinstall "Development Tools" \
&& dnf -y install epel-release
#AlmaLinux 9 的内核版本不是很低,所以无需升级。
#设置主机名
hostnamectl set-hostname
#主机名解析
cat >> /etc/hosts << EOF
127.0.0.1 $(hostname)
192.168.100.10 k8s-master
192.168.100.20 k8s-node1
192.168.100.30 k8s-node2
EOF
#关闭防火墙
systemctl stop firewalld &&systemctl disable firewalld
#时间同步(公有云跳过)
#Kubernetes 要求集群中的节点时间必须精确一致,所以在每个节点上添加时间同步:
dnf -y install chrony
systemctl enable chronyd --now
chronyc sources
#关闭 SELinux
getenforce
setenforce 0
sed -i 's/enforcing/disabled/' /etc/selinux/config
#关闭 swap 分区
free -h
swapoff -a
sed -ri 's/.*swap.*/#&/' /etc/fstab
#将桥接的 IPv4 流量传递到 iptables 的链
#加载模块:
# 开机自动启动
cat <> /etc/sysctl.d/k8s.conf
echo "net.bridge.bridge-nf-call-ip6tables = 1" >> /etc/sysctl.d/k8s.conf
echo "net.bridge.bridge-nf-call-iptables = 1" >> /etc/sysctl.d/k8s.conf
echo "net.ipv6.conf.all.disable_ipv6 = 1" >> /etc/sysctl.d/k8s.conf
echo "net.ipv6.conf.default.disable_ipv6 = 1" >> /etc/sysctl.d/k8s.conf
echo "net.ipv6.conf.lo.disable_ipv6 = 1" >> /etc/sysctl.d/k8s.conf
echo "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.d/k8s.conf
#应用而不重新启动:
sysctl --system
#通过运行以下指令确认 br_netfilter 和 overlay 模块被加载:
lsmod | grep br_netfilter \
&& lsmod | grep overlay
#通过运行以下指令确认 net.bridge.bridge-nf-call-iptables、
#net.bridge.bridge-nf-call-ip6tables 和
#net.ipv4.ip_forward 系统变量的 sysctl 配置中被设置为 1:
sysctl net.bridge.bridge-nf-call-iptables \
net.bridge.bridge-nf-call-ip6tables \
net.ipv4.ip_forward
dnf config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
dnf -y install containerd.io \
--allowerasing # 此参数的意思是将冲突的依赖删除
cat << EOF | sudo tee /etc/crictl.yaml
runtime-endpoint: "unix:///run/containerd/containerd.sock"
image-endpoint: "unix:///run/containerd/containerd.sock"
debug: false
pull-image-on-create: true
disable-pull-on-run: false
EOF
systemctl status containerd &&systemctl stop containerd
cp /etc/containerd/config.toml /etc/containerd/config.toml.bak
containerd config default > /etc/containerd/config.toml
# 修改 /etc/containerd/config.toml 文件后,要将 docker、containerd 停止后,再启动
sed -i "s#registry.k8s.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g" /etc/containerd/config.toml
# 配置 containerd cgroup 驱动程序 systemd
sudo sed -i "s#SystemdCgroup = false#SystemdCgroup = true#g" /etc/containerd/config.toml \
&& grep SystemdCgroup /etc/containerd/config.toml
vim /etc/containerd/config.toml
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
# 增加如下的两行
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://b9pmyelo.mirror.aliyuncs.com", "https://registry-1.docker.io"]
systemctl enable --now containerd
添加阿里云的 Kubernetes 的 YUM 源
由于 Kubernetes 的镜像源在国外,非常慢,这里切换成国内的阿里云镜像源(三台机器均需执行下面命令):
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
dnf install -y kubelet-1.25.7 kubeadm-1.25.7 kubectl-1.25.7 \
--disableexcludes=kubernetes
systemctl enable --now kubelet
kubeadm init \
--apiserver-advertise-address=192.168.100.10 \
--image-repository=registry.cn-hangzhou.aliyuncs.com/google_containers \
--kubernetes-version=v1.25.7 \
--service-cidr=10.96.0.0/16 \
--pod-network-cidr=10.244.0.0/16 \
--token-ttl=0 # 如果不设置为 0 ,默认的 token 有效期是 24 小时
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
export KUBECONFIG=/etc/kubernetes/admin.conf
kubeadm token create --print-join-command
# 生成一个永不过期的 token
kubeadm token create --ttl 0 --print-join-command
#接着使用该token 命令在node节点执行,就可以让节点加入集群了
配置 NetworManager 在有 NetworkManager 的系统上需要配置 NetworkManager 不接管 calico 的网卡:
cat <> /etc/NetworkManager/conf.d/calico.conf
[keyfile]
unmanaged-devices=interface-name:cali*;interface-name:tunl*;interface-name:vxlan.calico
EOF
wget https://projectcalico.docs.tigera.io/v3.25/manifests/calico.yaml
下载完后还需要修改里面定义Pod网络( _CIDR),与前面kubeadm init指定的一样
修改完后应用清单:
$ kubectl apply -f calico.yaml
$ kubectl get pods -n kube-system