1、准备环境
ip | 角色 | 系统 | 主机名 | cpu | mem |
192.168.40.129 | master | centos7.9 | k8smaster | 4 | 8 |
192.168.40.130 | node1 | centos7.9 | k8snode1 | 4 | 8 |
192.168.40.131 | node2 | centos7.9 | k8snode2 | 4 | 8 |
192.168.40.132 | node3 | centos7.9 | k8snode3 | 4 | 8 |
2、系统配置(所有节点)
重要:首先将各主机ip配置为静态、主机名修改、时间同步 等
2.1 修改仓库源
#修改仓库源、安装必要工具
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum install -y conntrack ntpdate ntp ipvsadm ipset jq iptables curl sysstat libseccomp wget vim net-tools git
2.2 关闭防火墙
#关闭firewalld配置iptabes
systemctl stop firewalld && systemctl disable firewalld
yum -y install iptables-services && systemctl start iptables && systemctl enable iptables && iptables -F && iptables-save > /etc/sysconfig/iptables
2.3 关闭swap 和selinux
#关闭swap:
swapoff -a && sed -ri 's/.*swap.*/#&/' /etc/fstab
#关闭selinux:
setenforce 0 && sed -i 's/enforcing/disabled/' /etc/selinux/config
2.4 配置节点hosts
cat >> /etc/hosts <<EOF
192.168.40.129 k8smaster
192.168.40.130 k8snode1
192.168.40.131 k8snode2
192.168.40.132 k8snode3
EOF
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.40.129 k8smaster
192.168.40.130 k8snode1
192.168.40.131 k8snode2
192.168.40.132 k8snode3
2.5 设置系统时区为 中国/上海
timedatectl set-timezone Asia/Shanghai
#将当前的 UTC 时间写入硬件时钟
timedatectl set-local-rtc 0
#重启依赖于系统时间的服务
systemctl restart rsyslog
systemctl restart crond
2.6 关闭postfix
#关闭postfix
systemctl stop postfix && systemctl disable postfix
3、安装docker(所有节点)
3.1 安装docker
参考:《【CentOS7】Linux安装Docker教程(保姆篇)_linux centos7 安装docker-CSDN博客》
3.2 daemon.json配置
cat > /etc/docker/daemon.json <<EOF
{
#设置驱动
"exec-opts": ["native.cgroupdriver=systemd"],
#设置阿里云镜像服务器地址(启用了https)
"registry-mirrors": ["https://exsnkv91.mirror.aliyuncs.com"],
#设置是有镜像服务器地址(未启用https)
"insecure-registries":["192.168.40.129:5000"]
}
EOF
systemctl daemon-reload
systemctl restart docker
4、配置cri-docker(所有节点)
配置cri-docker使kubernetes以docker作为运行时 自 1.24 版起,Dockershim 已从 Kubernetes 项目中移除。因为历史问题docker却不支持kubernetes主推的CRI(容器运行时接口)标准,所以docker不能再作为kubernetes的容器运行时了,即从kubernetesv1.24开始不再使用docker了。 但是如果想继续使用docker的话,可以在kubelet和docker之间加上一个中间层cri-docker。cri-docker是一个支持CRI标准的shim(垫片)。一头通过CRI跟kubelet交互,另一头跟docker api交互,从而间接的实现了kubernetes以docker作为容器运行时。
4.1 下载cri-docker
到下面的链接下载最新版cri-docker
https://github.com/Mirantis/cri-dockerd/tags
4.2 解压安装
tar zxf cri-dockerd-0.2.1.amd64.tgz
cp cri-dockerd/cri-dockerd /usr/bin/
解压后,将文件复制到每个节点对应的目录
scp /usr/bin/cri-dockerd 192.168.40.130:/usr/bin/
scp /usr/bin/cri-dockerd 192.168.40.131:/usr/bin/
scp /usr/bin/cri-dockerd 192.168.40.132:/usr/bin/
4.3 配置启动文件
创建cri-docker启动文件 启动文件从下面链接找到。
https://github.com/Mirantis/cri-dockerd/tree/master/packaging/systemd
这里/usr/bin/cri-dockerd一定要加上参数 ,如下
vi /usr/lib/systemd/system/cri-docker.service
[Unit]
Description=CRI Interface for Docker Application Container Engine
Documentation=https://docs.mirantis.com
After=network-online.target firewalld.service docker.service
Wants=network-online.target
Requires=cri-docker.socket
[Service]
Type=notify
ExecStart=/usr/bin/cri-dockerd --network-plugin=cni --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9
ExecReload=/bin/kill -s HUP $MAINPID
TimeoutSec=0
RestartSec=2
Restart=always
StartLimitBurst=3
StartLimitInterval=60s
LimitNOFILE=infinity
LimitNPROC=infinity
LimitCORE=infinity
TasksMax=infinity
Delegate=yes
KillMode=process
[Install]
WantedBy=multi-user.target
创建启动文件 文件内容
/usr/lib/systemd/system/cri-docker.socket
vi /usr/lib/systemd/system/cri-docker.socket
[Unit]
Description=CRI Docker Socket for the API
PartOf=cri-docker.service
[Socket]
ListenStream=%t/cri-dockerd.sock
SocketMode=0660
SocketUser=root
SocketGroup=docker
[Install]
WantedBy=sockets.target
同步到每个节点
scp /usr/lib/systemd/system/cri-docker.service /usr/lib/systemd/system/cri-docker.socket root@192.168.40.130:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/cri-docker.service /usr/lib/systemd/system/cri-docker.socket root@192.168.40.131:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/cri-docker.service /usr/lib/systemd/system/cri-docker.socket root@192.168.40.132:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/cri-docker.socket/usr/lib/systemd/system/cri-docker.socket root@192.168.40.130:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/cri-docker.socket/usr/lib/systemd/system/cri-docker.socket root@192.168.40.131:/usr/lib/systemd/system/
scp /usr/lib/systemd/system/cri-docker.socket/usr/lib/systemd/system/cri-docker.socket root@192.168.40.132:/usr/lib/systemd/system/
启动cri-docker并设置开机自动启动
systemctl daemon-reload ; systemctl enable cri-docker --now
#查看状态
systemctl is-active cri-docker
5、安装kubeadm(所有节点)
5.1 配置k8s阿里云源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[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
5.2 安装kubeadm kubelet kubectl指定版本
#查看可安装的k8s版本
yum list kubeadm --showduplicates | sort -r
#安装1.28版本的k8s
yum install -y kubelet-1.28.2-0 kubeadm-1.28.2-0 kubectl-1.28.2-0
5.3 设置kubelet开机自启
systemctl enable --now kubelet.service
6、初始化(master节点)
kubeadm init --image-repository registry.aliyuncs.com/google_containers --apiserver-advertise-address=192.168.40.129 --kubernetes-version=v1.28.2 --service-cidr=10.96.0.0/12 --pod-network-cidr=10.244.0.0/16 --upload-certs --cri-socket /var/run/cri-dockerd.sock
注意:其中 --apiserver-advertise-address=192.168.40.129 是根据你自己的master节点定。
按照提示创建kubeconfig文件
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
7、安装网络插件(master节点)
下载flannel的yml文件并创建,这里flannel.yml文件里镜像拉取地址为外网地址,构建pod时会拉取超时,这里单独下载了镜像上传到了阿里云ACR上,需要修改yml文件里的image地址。
wget https://github.com/flannel-io/flannel/releases/download/v0.24.3/kube-flannel.yml
sed -i '/ image:/s#docker.io/flannel#registry.cn-hangzhou.aliyuncs.com/luoyuxin#' kube-flannel.yml
构建flannel
kubectl create -f kube-flannel.yml
kubectl get pod -n kube-flannel
8、加入集群(各子节点)
可根据提示加入其他node,在node节点上执行下面语句,注意后面加上 --cri-socket /var/run/cri-dockerd.sock 参数
kubeadm join 192.168.40.129:6443 --cri-socket /var/run/cri-dockerd.sock --token pr06h4.9uqcgg6aoej4uq4k \
--discovery-token-ca-cert-hash sha256:2b4b622220812a7230b96c600ba595640eee991f47e64c5c11e20e71fe195f6e
检查子节点是否已加入
kubectl get nodes
这种是命令刚执行,节点加入还在执行中,等30秒钟再看
kubectl get nodes -o wide