环境搭建
本次环境搭建需要安装三台Centos服务器(一主二从),k8s采用1.25.4版本
主机安装
安装虚拟机过程中注意下面选项的设置:
- 操作系统环境:CPU(2C) 内存(4G) 硬盘(50G)
- 语言选择:English
- 软件选择:基础设施服务器
- 分区选择:自动分区
- 网络配置:按照下面配置网路地址信息
- 主机名设置:按照下面信息设置主机名
192.168.11.105 k8s-master
192.168.11.106 k8s-node01
192.168.11.107 k8s-node02
环境初始化
1、检查操作系统的版本
# 此方式下安装kubernetes集群要求Centos版本要在7.9
[root@k8s-master ~]# cat /etc/centos-release
CentOS Linux release 7.9.2009 (Core)
2、域名解析
为了方便后面集群节点间的直接调用,在这配置一下主机名解析,企业中推荐使用内部DNS服务器
[root@k8s-master ~]# vim /etc/hosts +
[root@k8s-master ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.11.105 k8s-master
192.168.11.106 k8s-node01
192.168.11.107 k8s-node02
3、时间同步
kubernetes要求集群中的节点时间必须精确一致,这里直接使用chronyd服务从网络同步时间。
企业中建议配置内部的时间同步服务器
# 安装chrony
[root@k8s-master ~]# yum install -y chrony.x86_64
# 修改配置文件
[root@k8s-master ~]# vim /etc/chrony.conf
[root@k8s-master ~]# grep ^server /etc/chrony.conf
server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
# 添加阿里云服务器地址
server ntp1.aliyun.com iburst
# 重启并设置为开机自启动
[root@k8s-master ~]# systemctl enable --now chronyd
# chronyd服务启动稍等几秒钟,就可以使用date命令验证时间了
[root@k8s-master ~]# chronyc sources
[root@k8s-master ~]# date
4、禁用iptables和firewalld服务
kubernetes和docker在运行中会产生大量的iptables规则,为了不让系统规则跟它们混淆,直接关闭系统的规则
# 1 关闭firewalld服务
[root@master ~]# systemctl stop firewalld
[root@master ~]# systemctl disable firewalld
[root@master ~]# firewall-cmd --state
not running
# 2 关闭iptables服务
[root@master ~]# systemctl stop iptables
[root@master ~]# systemctl disable iptables
5、禁用selinux
selinux是linux系统下的一个安全服务,如果不关闭它,在安装集群中会产生各种各样的奇葩问题
# 编辑 /etc/selinux/config 文件,修改SELINUX的值为disabled
# 注意修改完毕之后需要重启linux服务
SELINUX=disabled
sed -i '/^SELINUX=/c SELINUX=disabled' /etc/selinux/config
setenfoce 0
[root@master ~]# sestatus
SELinux status: disabled
6、禁用swap分区
swap分区指的是虚拟内存分区,它的作用是在物理内存使用完之后,将磁盘空间虚拟成内存来使用启用swap设备会对系统的性能产生非常负面的影响,因此kubernetes要求每个节点都要禁用swap设备
但是如果因为某些原因确实不能关闭swap分区,就需要在集群安装过程中通过明确的参数进行配置说明
# 编辑分区配置文件/etc/fstab,注释掉swap分区一行
# 注意修改完毕之后需要重启linux服务
UUID=455cc753-7a60-4c17-a424-7741728c44a1 /boot xfs defaults 0 0
/dev/mapper/centos-home /home xfs defaults 0 0
# /dev/mapper/centos-swap swap swap defaults 0 0
# 临时关闭
swapoff -a
7、升级操作系统内核
# 导入elrepo gpg key
[root@master ~]# rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
# 安装elrepo YUM源仓库
[root@master ~]# yum -y install https://www.elrepo.org/elrepo-release-7.0-4.el7.elrepo.noarch.rpm
# 安装kernel-ml版本,ml为长期稳定版本,lt为长期维护版本
[root@master ~]# yum --enablerepo="elrepo-kernel" -y install kernel-ml.x86_64
# 设置grub2默认引导为0
[root@master ~]# grub2-set-default 0
# 重新生成grub2引导文件
[root@master ~]# grub2-mkconfig -o /boot/grub2/grub.cfg
# 更新后,需要重启,使用升级的内核生效。
[root@master ~]# reboot
# 重启后,需要验证内核是否为更新对应的版本
[root@k8s-master ~]# uname -r
6.3.8-1.el7.elrepo.x86_64
8、修改linux的内核参数
# 修改linux的内核参数,添加网桥过滤和地址转发功能
# 编辑/etc/sysctl.d/kubernetes.conf文件,添加如下配置:
[root@k8s-master ~]# vim /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness=0
[root@k8s-master ~]# sysctl -p
# 加载网桥过滤模块
[root@k8s-master ~]# modprobe br_netfilter
# 重新加载配置
[root@k8s-master ~]# sysctl -p /etc/sysctl.d/kubernetes.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness = 0
# 查看网桥过滤模块是否加载成功
[root@master ~]# lsmod | grep br_netfilter
9、配置ipvs功能
在kubernetes中service有两种代理模型,一种是基于iptables的,一种是基于ipvs的两者比较的话,ipvs的性能明显要高一些,但是如果要使用它,需要手动载入ipvs模块
# 1 安装ipset和ipvsadm
[root@k8s-master ~]# yum install ipset ipvsadm -y
# 2 添加需要加载的模块写入脚本文件
[root@k8s-master ~]# cat <<EOF > /etc/sysconfig/modules/ipvs.modules
> #!/bin/bash
> modprobe -- ip_vs
> modprobe -- ip_vs_rr
> modprobe -- ip_vs_wrr
> modprobe -- ip_vs_sh
> modprobe -- nf_conntrack
> EOF
# 3 为脚本文件添加执行权限
[root@k8s-master ~]# chmod +x /etc/sysconfig/modules/ipvs.modules
# 4 执行脚本文件
[root@k8s-master ~]# /bin/bash /etc/sysconfig/modules/ipvs.modules
# 5 查看对应的模块是否加载成功
[root@k8s-node02 ~]# lsmod | grep -e ip_vs -e nf_conntrack_ipv4
ip_vs_sh 16384 0
ip_vs_wrr 16384 0
ip_vs_rr 16384 0
ip_vs 200704 6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
nf_conntrack 180224 1 ip_vs
nf_defrag_ipv6 24576 2 nf_conntrack,ip_vs
libcrc32c 16384 3 nf_conntrack,xfs,ip_vs
基础环境
k8s基本环境准备
由于kubernetes的镜像源在国外,速度比较慢,这里切换成国内的镜像源,本文使用阿里云YUM源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
[root@k8s-master ~]# yum clean all && yum makecache
1、查看所有的可用版本
[root@k8s-master ~]# yum list kubeadm kubelet kubectl --showduplicates | sort -r
2、集群软件安装
安装kubeadm、kubelet和kubectl
[root@k8s-master ~]# yum install kubelet-1.25.4 kubectl-1.25.4 kubeadm-1.25.4
3、安装后查看版本
[root@k8s-master ~]# kubeadm version
kubeadm version: &version.Info{Major:"1", Minor:"25", GitVersion:"v1.25.4", GitCommit:"872a965c6c6526caa949f0c6ac028ef7aff3fb78", GitTreeState:"clean", BuildDate:"2022-11-09T13:35:06Z", GoVersion:"go1.19.3", Compiler:"gc", Platform:"linux/amd64"}
4、配置kubelet的cgroup
为了实现docker使用的cgroupdriver与kubelet使用的cgroup的一致性,建议修改如下文件内容。
[root@k8s-master ~]# vim /etc/sysconfig/kubelet
KUBE_PROXY_MODE="ipvs"
5、设置kubelet开机自启
[root@k8s-master ~]# systemctl enable kubelet
做完基础环境,建议创建快照,后续会分别使用三种不同方式创建集群
集群创建方式1:containerd
https://zhuanlan.zhihu.com/p/612051521
https://blog.csdn.net/Jailman/article/details/126504154
安装containerd,所有节点都安装
# 直接安装docker-ce源 https://developer.aliyun.com/mirror/docker-ce
# step 1: 安装必要的一些系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
sudo yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
sudo sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce
# Step 4: 开启Docker服务
sudo service docker start
# Step 5:安装软件包
过滤一下软件包:
[root@k8s-master ~]# yum list | grep containerd containerd.io.x86_64 1.6.21-3.1.el7 @docker-ce-stable
[root@k8s-master ~]# yum install -y containerd
# Step 6:初始化默认配置
[root@k8s-master ~]# containerd config default | tee /etc/containerd/config.toml
# Step 7:修改containerd配置更改cgroup
[root@k8s-master ~]# sed -i "s#SystemdCgroup\ \=\ false#SystemdCgroup\ \=\ true#g" /etc/containerd/config.toml
# Step 8:修改镜像源
[root@k8s-master ~]# sed -i "s#registry.k8s.io#registry.aliyuncs.com/google_containers#g" /etc/containerd/config.toml
# Step 9:配置crictl
[root@k8s-master ~]# cat <<EOF | tee /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
EOF
[root@k8s-master ~]# systemctl daemon-reload
[root@k8s-master ~]# systemctl restart containerd
[root@k8s-master ~]# systemctl enable containerd
# 验证是否可用
crictl pull nginx:alpine
crictl rmi nginx:alpine
crictl images
解决方法:
安装 CRI 客户端 crictl
[root@k8s-master ~]# wget -c https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.27.0/crictl-v1.27.0-linux-amd64.tar.gz
[root@k8s-master ~]# tar xf crictl-v1.27.0-linux-amd64.tar.gz -C /usr/local/bin/
[root@k8s-master ~]# vim /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 10
debug: false
#vim /etc/containerd/config.toml
修改runtime_type
runtime_type = "io.containerd.runtime.v1.linux"
[root@k8s-master docker]# systemctl daemon-reload
[root@k8s-master docker]# systemctl restart containerd.service
[root@k8s-master docker]# crictl pull busybox
集群创建方式2:cri-o
安装cri-o
所有节点安装配置cri-o
[root@k8s-master yum.repos.d]# VERSION=1.25
[root@k8s-master yum.repos.d]# curl -L -o /etc/yum.repos.d/devel:kubic:libcontainers:stable.repo \
> https://download.opensuse.org/repositories/devel:/kubic:/libcontainers:/stable/CentOS_7/devel:kubic:libcontainers:stable.repo
[root@k8s-master ~]# curl -L -o /etc/yum.repos.d/devel:kubic:libcontainers:stable:crio:${VERSION}.repo \
> https://download.opensuse.org/repositories/devel:kubic:libcontainers:stable:crio:${VERSION}/CentOS_7/devel:kubic:libcontainers:stable:cri-o:${VERSION}.repo
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 424 100 424 0 0 212 0 0:00:02 0:00:01 0:00:01 212
100 425 100 425 0 0 189 0 0:00:02 0:00:02 --:--:-- 189
100 426 100 426 0 0 171 0 0:00:02 0:00:02 --:--:-- 0
100 427 100 427 0 0 130 0 0:00:03 0:00:03 --:--:-- 130
100 428 100 428 0 0 121 0 0:00:03 0:00:03 --:--:-- 0
0 0 0 1083 0 0 288 0 --:--:-- 0:00:03 --:--:-- 288
[root@k8s-master ~]# yum install cri-o -y
# 修改crio配置文件
[root@master ~]# grep "pause_image =" /etc/crio/crio.conf
pause_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.7"
[root@master ~]# grep "insecure_registries =" -A 2 /etc/crio/crio.conf
insecure_registries = [
"docker.mirrors.ustc.edu.cn","dockerhub.azk8s.cn","hub-mirror.c.163.com"
] s
ystemctl daemon-reload
systemctl start crio
systemctl enable crio
修改/etc/sysconfig/kubelet
# more /etc/sysconfig/kubelet
KUBELET_EXTRA_ARGS="--container-runtime=remote --cgroup-driver=systemd --
container-runtime-endpoint='unix:///var/run/crio/crio.sock' --runtime-req
uest-timeout=5m"
# systemctl daemon-reload
# systemctl restart kubelet.service
集群创建方式3:docker
# 1 切换镜像源
[root@k8s-master ~]# wget -c https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo
[root@k8s-master ~]# sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# 2 查看当前镜像源中支持的docker版本
[root@k8s-master ~]# yum list docker-ce --showduplicates
# 3 安装特定版本的docker-ce
# 必须指定--setopt=obsoletes=0,否则yum会自动安装更高版本
[root@k8s-master ~]# yum install --setopt=obsoletes=0 docker-ce-18.06.3.ce-3.el7 -y
# 安装源里最新版本
$ yum install docker-ce
# 4 添加一个配置文件
# Docker在默认情况下使用的Cgroup Driver为cgroupfs,而kubernetes推荐使用systemd来代替
cgroupfs
[root@k8s-master ~]# mkdir /etc/docker
[root@k8s-master ~]# cat << EOF > /etc/docker/daemon.json
{
"exec-opts": ["native.cgroupdriver=systemd"],
"registry-mirrors": ["https://kn0t2bca.mirror.aliyuncs.com"]
}
EOF
# 5 启动docker
[root@k8s-master ~]# systemctl restart docker.service
[root@k8s-master ~]# systemctl enable docker.service
Created symlink from /etc/systemd/system/multi-user.target.wants/docker.service to /usr/lib/systemd/system/docker.service.
# 6 检查docker状态和版本
[root@k8s-master ~]# docker version
cri-dockerd安装
# 下载
[root@k8s-master ~]# wget -c https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.3/cri-dockerd-0.3.3-3.el7.x86_64.rpm
# 安装
https://github.com/Mirantis/cri-dockerd
[root@k8s-master ~]# rpm -ivh cri-dockerd-0.3.3-3.el7.x86_64.rpm
error: Failed dependencies:
containerd.io >= 1.2.2-3 is needed by cri-dockerd-3:0.3.3-3.el7.x86_64
这个问题是因为docker-ce缺少container-selinux和container.io这两个依赖包
[root@k8s-master ~]# yum install container-selinux -y
[root@k8s-master ~]# yum install -y containerd.io
[root@k8s-master ~]# wget -c http://github.com/Mirantis/cri-dockerd/releases/download/v0.2.3/cri-dockerd-0.2.3-3.el7.x86_64.rpm
[root@k8s-master ~]# rpm -ivh cri-dockerd-0.2.3-3.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:cri-dockerd-3:0.2.3-3.el7 ################################# [100%]
[root@k8s-master ~]# yum install cri-dockerd-0.2.6-3.el7.x86_64.rpm -y
# 修改启动文件 /usr/lib/systemd/system/cri-docker.service
ExecStart=/usr/bin/cri-dockerd --network-plugin=cni --pod-infra-containerimage=registry.aliyuncs.com/google_containers/pause:3.7
# 启动cri-docker
systemctl daemon-reload && systemctl enable --now cri-docker cri-docker.socket
错误排查
1、cri-docker服务起不来
[root@k8s-master lib]# systemctl status cri-docker.service
â— cri-docker.service - CRI Interface for Docker Application Container Engine
Loaded: loaded (/usr/lib/systemd/system/cri-docker.service; enabled; vendor preset: disabled)
Active: failed (Result: start-limit) since Sat 2023-06-17 23:30:29 CST; 3s ago
Docs: https://docs.mirantis.com
Process: 18729 ExecStart=/usr/bin/cri-dockerd --network-plugin=cni --pod-infra-containerimage=registry.aliyuncs.com/google_containers/pause:3.7 (code=exited, status=1/FAILURE)
Main PID: 18729 (code=exited, status=1/FAILURE)
Jun 17 23:30:26 k8s-master cri-dockerd[18729]: time="2023-06-17T23:30:26+08:00" level=fatal msg="unknown flag: --pod-infra-containerimage"
Jun 17 23:30:26 k8s-master systemd[1]: Failed to start CRI Interface for Docker Application Container Engine.
Jun 17 23:30:26 k8s-master systemd[1]: Unit cri-docker.service entered failed state.
Jun 17 23:30:26 k8s-master systemd[1]: cri-docker.service failed.
Jun 17 23:30:29 k8s-master systemd[1]: cri-docker.service holdoff time over, scheduling restart.
Jun 17 23:30:29 k8s-master systemd[1]: Stopped CRI Interface for Docker Application Container Engine.
Jun 17 23:30:29 k8s-master systemd[1]: start request repeated too quickly for cri-docker.service
Jun 17 23:30:29 k8s-master systemd[1]: Failed to start CRI Interface for Docker Application Container Engine.
Jun 17 23:30:29 k8s-master systemd[1]: Unit cri-docker.service entered failed state.
Jun 17 23:30:29 k8s-master systemd[1]: cri-docker.service failed.
尝试解决
1、进行了内核升级,将yum源更新
[root@k8s-master ~]# yum update -y
[root@k8s-master ~]# reboot
2、好家伙,swap分区没有禁,因为之前设置的临时关闭,开机又打开了
安装k8s
[root@k8s-master ~]# yum install -y kubelet-1.25.6 kubeadm-1.25.6 kubectl-1.25.6
[root@k8s-master ~]# systemctl enable --now kubelet
部署master节点
集群初始化
kubeadm init --kubernetes-version=v1.27.3 --pod-network-cidr=10.224.0.0/16 --
apiserver-advertise-address=192.168.11.105 --cri-socket unix:///var/run/cridockerd.sock --image-repository registry.aliyuncs.com/google_containers
添加 Worker 节点
在 Worker 节点执行
标签:--,CentOS7.9,集群,yum,master,docker,k8s,root From: https://www.cnblogs.com/sre-chan/p/17488230.html