K8s集群v1.36版本部署(containerd)
本次使用系统Rocky Linux 9.3,采用一组两从模式。
没有特殊说明的均为所有节点都需要操作,单独操作的会写说明。
172.30.30.135 master
172.30.30.136 node1
172.30.30.137 node2
修改三台主机名
hostnamectl set-hostname master
hostnamectl set-hostname node1
hostnamectl set-hostname node2
设置时间同步
yum install chrony -y
vim /etc/chrony.conf
#将默认的服务器配置(pool 2.rocky.pool.ntp.org iburst),将其注释或删除,改为
server ntp.aliyun.com iburst
systemctl restart chronyd
chronyc tracking
配置初始环境
#关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
#关闭selinux
setenforce 0
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/sysconfig/selinux
sed -i 's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config
#关闭swap
swapoff -a && sysctl -w vm.swappiness=0
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab
# 加载内核模块(overlay、br_netfilter,K8s网络依赖)
cat <<EOF | tee /etc/modules-load.d/containerd.conf
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
#写入/etc/hosts文件
172.30.30.135 master
172.30.30.136 node1
172.30.30.137 node2
#安装所需工具
yum install -y wget vim bash-completion epel-release yum-utils
配置网络内核参数(开启IP转发、桥接过滤,避免网络不通)
cat <<EOF | tee /etc/sysctl.d/99-kubernetes-cri.conf
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
net.bridge.bridge-nf-call-ip6tables = 1
EOF
sysctl --system
添加仓库,下载containerd
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
yum install containerd.io -y
mkdir -p /etc/containerd
containerd config default | tee /etc/containerd/config.toml
# 配置containerd
sed -i 's#SystemdCgroup = false#SystemdCgroup = true#g' /etc/containerd/config.toml
sed -i 's#k8s.gcr.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g' /etc/containerd/config.toml
sed -i 's#registry.gcr.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g' /etc/containerd/config.toml
sed -i 's#registry.k8s.io/pause#registry.cn-hangzhou.aliyuncs.com/google_containers/pause#g' /etc/containerd/config.toml
# 重置containerd服务
systemctl daemon-reload
systemctl restart containerd
systemctl enable --now containerd
添加v1.36源
cat <<EOF | tee /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.36/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.36/rpm/repodata/repomd.xml.key
EOF
安装K8s 1.36 组件(kubeadm、kubelet、kubectl)
yum install kubeadm-1.36.* kubelet-1.36.* kubectl-1.36.* -y
systemctl enable --now kubelet
kubeadm config images pull \
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers --kubernetes-version 1.36.0
# 验证(输出版本号即成功)
echo "kubeadm: $(kubeadm version -o yaml | grep gitVersion | awk '{print $2}')"
echo "kubectl: $(kubectl version --client -o yaml | grep gitVersion | awk '{print $2}')"
echo "kubelet: $(kubelet --version | awk '{print $2}')"
初始化节点(仅Master节点)
kubeadm init --apiserver-advertise-address 172.30.30.135 \
--image-repository registry.cn-hangzhou.aliyuncs.com/google_containers \
--cri-socket "unix:///var/run/containerd/containerd.sock" --kubernetes-version 1.36.0
#根据给出的命令在node节点加入集群
kubeadm join 172.30.30.135:6443 --token 005uci.fwhfrlwwa7orxknb \
--discovery-token-ca-cert-hash sha256:cc73de0df4a384ed3fee54b4197aa55bf570499136b1eb39da9bb9bf434c6187
#Master节点创建以下文件
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
#如果节点加入的时候此token过期,如果需要重新打印的话运行
kubeadm token create --print-join-command
安装网络插件,选用calico(仅Master节点)
wget -O calico.yaml https://ghproxy.net/https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calico.yaml
sed -i 's|docker.io/calico/|docker.m.daocloud.io/calico/|g' calico.yaml
kubectl apply -f calico.yaml
安装crictl工具
在没有
crictl的情况下,仍然可以使用containerd自带的命令行工具ctr来查看和操作容器,但是不太方便,命令也略微繁琐,建议下载crictl工具。
#v1.36.0版本
https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.36.0/crictl-v1.36.0-linux-amd64.tar.gz
#使用工具将下载好的包传到三台机器。使用tar命令
tar zxvf crictl-v1.36.0-linux-amd64.tar.gz -C /usr/local/bin/
#创建crictl.yaml文件,若没有这个文件,会产生警告,警告是因为 crictl 没有找到配置文件,正在使用默认的端点列表。
tee /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF
#完成后就可以开始测试是否成功可用,命令和Docker类似
[root@master ~]# crictl version
Version: 0.1.0
RuntimeName: containerd
RuntimeVersion: v2.2.3
RuntimeApiVersion: v1
#查看容器和镜像命令
crictl ps
crictl images
命令补全(可选)
echo 'source <(crictl completion bash)' >> ~/.bashrc
echo 'source <(kubectl completion bash)' >>~/.bashrc
source ~/.bashrc
#也可设置别名,按k和按kubectl效果一致。
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -o default -F __start_kubectl k' >> ~/.bashrc
source ~/.bashrc
测试
#在Master查看集群状态,直到Ready即可
[root@master ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready control-plane 9m12s v1.36.1
node1 Ready <none> 6m45s v1.36.1
node2 Ready <none> 6m52s v1.36.1
#查看所有Pod状态(kube-system、kube-flannel命名空间下所有Pod均为Running)
kubectl get pods -A
#查看集群组件状态(均为Healthy)
kubectl get cs
此时集群安装完毕,功能完善。
2 条评论
发表评论需要 登录
还没有账号?立即注册