1. kubernetes集群规划
集群各节点资源规划分配
主机名 | IP地址 | 角色 | 操作系统 | 硬件配置 |
master01 | 192.168.204.110 | 管理节点 | Rocky Linux 9.4 | 2core/4G内存/50G |
node01 | 192.168.204.111 | 工作节点 | Rocky Linux 9.4 | 2core/4G内存/50G |
node02 | 192.168.204.112 | 工作节点 | Rocky Linux 9.4 | 2core/4G内存/50G |
2. 操作系统准备工作
2.1. 配置主机名称
2.1.1. Master01节点运行
[root@localhost ~]# hostnamectl hostname master01
2.1.2. Node01节点运行
[root@localhost ~]# hostnamectl hostname node01
2.1.3. Node02节点运行
[root@localhost ~]# hostnamectl hostname node02
2.2. 配置系统hosts文件(全部节点)
分别在所有节点运行
[root@localhost ~]# echo "192.168.204.110 master01" >> /etc/hosts
[root@localhost ~]# echo "192.168.204.111 node01" >> /etc/hosts
[root@localhost ~]# echo "192.168.204.112 node02" >> /etc/hosts
2.3. 关闭防火墙和selinux(全部节点)
分别在所有节点运行
[root@localhost ~]# systemctl stop firewalld.service
[root@localhost ~]# systemctl disable firewalld.service
[root@localhost ~]# setenforce 0
[root@localhost ~]# sed -i 's#SELINUX=enforcing#SELINUX=disabled#' /etc/selinux/config
2.4. 配置国内系统镜像源和安装epel源
2.4.1. 配置阿里云系统源(全部节点)。阿里云网上的文件名是Rocky.repo,系统上的是rocky-*.repo。
注意修改查看
[root@master01 ~]# sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
-i.bak \
/etc/yum.repos.d/rocky-*.repo
[root@master01 ~]# yum makecache
2.4.2. 配置epel源(全部节点)
文件内容取自我的华为云主机如下
[root@master01 ~]# vi /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux $releasever - $basearch
# It is much more secure to use the #metalink, but if you wish to use a local mirror
# place its address here.
baseurl=https://repo.huaweicloud.com/epel/$releasever/Everything/$basearch/
#metalink=https://mirrors.fedoraproject.org/#metalink?repo=epel-$releasever&arch=$basearch&infra=$infra&content=$contentdir
enabled=1
gpgcheck=1
countme=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-$releasever
[epel-debuginfo]
name=Extra Packages for Enterprise Linux $releasever - $basearch - Debug
# It is much more secure to use the #metalink, but if you wish to use a local mirror
# place its address here.
baseurl=https://repo.huaweicloud.com/epel/$releasever/Everything/$basearch/debug/
#metalink=https://mirrors.fedoraproject.org/#metalink?repo=epel-debug-$releasever&arch=$basearch&infra=$infra&content=$contentdir
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-$releasever
gpgcheck=1
[epel-source]
name=Extra Packages for Enterprise Linux $releasever - $basearch - Source
# It is much more secure to use the #metalink, but if you wish to use a local mirror
# place its address here.
baseurl=https://repo.huaweicloud.com/epel/$releasever/Everything/source/tree/
#metalink=https://mirrors.fedoraproject.org/#metalink?repo=epel-source-$releasever&arch=$basearch&infra=$infra&content=$contentdir
enabled=0
[root@master01 ~]# yum makecache
2.5. 时区和时间(全部节点)
检查时区和时间,由于我装机时候已经配置好,无需操作。
[root@master01 ~]# timedatectl
Local time: Fri 2024-05-31 18:26:23 CST
Universal time: Fri 2024-05-31 10:26:23 UTC
RTC time: Fri 2024-05-31 10:26:23
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
[root@master01 ~]# date
Fri May 31 06:27:40 PM CST 2024
如果时区和时间不对,需要手动设置时区和设置时间同步
本次使用系统自带的时间同步chrony工具。
2.6. 修改系统最大打开文件数(全部节点)
[root@master01 ~]# vi /etc/security/limits.conf
编辑文件,在文件后面添加一下两行数据
* soft nofile 65535
* hard nofile 65535
2.7. 修改内核参数(全部节点)
[root@master01 ~]# vi /etc/sysctl.conf
打开文件,在文件后面填入以下几行。
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_tw_buckets = 20480
net.ipv4.tcp_max_syn_backlog = 20480
net.core.netdev_max_backlog = 262144
net.ipv4.tcp_fin_timeout = 20
并运行一下命令使得配置的sysctl.conf文件生效
[root@master01 ~]# sysctl -p
2.8. 关闭swap(全部节点)
[root@master01 ~]# swapoff -a
[root@master01 ~]# sed -ri 's/.*swap.*/#&/' /etc/fstab
2.9. 安装系统性能分析工具和其他(全部节点)
[root@master01 ~]# yum install -y gcc autoconf sysstat
2.10. 开启bridge网桥过滤(全部节点)
[root@master01 ~]# vi /etc/sysctl.d/k8s.conf
编辑文件写入以下几行
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
加载br_netfilter模块,并查看
[root@master01 ~]# modprobe br_netfilter && lsmod | grep br_netfilter
加载配置文件,使其生效
[root@master01 ~]# sysctl -p /etc/sysctl.d/k8s.conf
3. Docker环境准备
3.1. 配置阿里云docker源(全部节点)
[root@master01 ~]# yum install -y yum-utils
[root@master01 ~]# yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3.2. 查看可用的docker版本(全部节点)
[root@master01 ~]# yum list docker-ce.x86_64 --showduplicates | sort -r
3.3. 安装docker,指定安装25.0.5-1.el9版本(全部节点)
[root@master01 ~]# yum -y install docker-ce-25.0.5-1.el9
3.4. 配置Docker Cgroup控制组(全部节点)
[root@master01 ~]# vi /etc/docker/daemon.json
编辑文件写入以下行
{
"exec-opts": ["native.cgroupdriver=systemd"]
}
3.5. 启动Docker服务并设置随机自启(全部节点)
[root@master01 ~]# systemctl enable docker --now
4. 配置cri-docker
4.1. 下载cri-docker
[root@master01 ~]# wget -c https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.9/cri-dockerd-0.3.9.amd64.tgz
4.2. 解压cri-docker
[root@master01 ~]#tar -xvf cri-dockerd-0.3.9.amd64.tgz --strip-components=1 -C /usr/local/bin/
4.3. 下载cri-docker service文件
[root@master01 ~]# wget -O /etc/systemd/system/cri-docker.service https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.service
[root@master01 ~]# wget -O /etc/systemd/system/cri-docker.socket https://raw.githubusercontent.com/Mirantis/cri-dockerd/master/packaging/systemd/cri-docker.socket
4.4. 编辑cri-docker.server
修改ExecStart行内容为
ExecStart=/usr/local/bin/cri-dockerd --pod-infra-container-image=registry.aliyuncs.com/google_containers/pause:3.9 --container-runtime-endpoint=unix:///var/run/cri-dockerd.sock --cri-dockerd-root-directory=/var/lib/dockershim --cri-dockerd-root-directory=/var/lib/docker
4.5. 编辑cri-docker.socket
修改ListenStream行内容为
ListenStream=/var/run/cri-dockerd.sock
4.6. 复制cri-dockerd-0.3.9.amd64.tgz到其他节点
[root@master01 ~]# scp cri-dockerd-0.3.9.amd64.tgz root@192.168.204.111:/root/
[root@master01 ~]# scp cri-dockerd-0.3.9.amd64.tgz root@192.168.204.112:/root/
4.7. 节点解压cri-docker
[root@node01 ~]# tar -xvf cri-dockerd-0.3.9.amd64.tgz --strip-components=1 -C /usr/local/bin/
[root@node02 ~]# tar -xvf cri-dockerd-0.3.9.amd64.tgz --strip-components=1 -C /usr/local/bin/
4.8. 复制修改好的service文件到其他节点
[root@master01 ~]# scp /etc/systemd/system/cri-docker.s* root@192.168.204.111:/etc/systemd/system/
[root@master01 ~]# scp /etc/systemd/system/cri-docker.s* root@192.168.204.112:/etc/systemd/system/
4.9. 启动并设置自启动
[root@master01 ~]# systemctl daemon-reload
[root@master01 ~]# systemctl enable cri-docker.service --now
5. kubeadm部署kubernetes集群
5.1. 配置阿里云k8s源(全部节点)
编辑文件/etc/yum.repos.d/k8s.repo
[root@master01 ~]# vi /etc/yum.repos.d/k8s.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/rpm/repodata/repomd.xml.key
5.2. 安装集群所需软件包kubelet kubeadm kubectl(全部节点)
[root@master01 ~]# yum install -y kubelet kubeadm kubectl
5.3. 配置k8s Cgoup控制组(全部节点)
[root@master01 ~]# vi /etc/sysconfig/kubelet
打开文件写入以下行
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"
5.4. 配置kubelet自启动(全部节点)
[root@master01 ~]# systemctl enable kubelet.service
5.5. 初始化集群(master节点运行)
5.5.1. 打印master节点所需的镜像文件(master节点运行)
[root@master01 ~]# kubeadm config images list
5.5.2. 打印集群初始化配置文件(master节点运行)
主机名ot@master01 ~]# kubeadm config print init-defaults > kubeadm-config.yaml
5.5.3. 修改参数(master节点)
[root@master01 ~]# vi kubeadm-config.yaml
…
localAPIEndpoint:
advertiseAddress: 192.168.204.110 #修改,集群初始化的主节点IP
bindPort: 6443
nodeRegistration:
criSocket: unix:///var/run/cri-dockerd.sock #修改使用docker
imagePullPolicy: IfNotPresent
name: master01 #修改节点名称
taints: null
…
…
…
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers #修改使用阿里云镜像仓库
…
…
注意:修改以上标记的位置即可
5.5.4. 使用配置文件初始化(master节点运行)
[root@master01 ~]# kubeadm init --config kubeadm-config.yaml --upload-certs
--upload-certs参数是将集群密钥添加到etcd数据库
5.5.5. 配置环境变量(master节点运行)
根据初始化完成提示运行下面行
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
5.5.6. 工作节点加入集群(工作节点运行)
根据初始化完成提示将工作节点添加入集群
kubeadm join 192.168.204.110:6443 --token abcdef.0123456789abcdef --discovery-token-ca-cert-hash sha256:fedc160482922f0c5c4077121fb45ed13ca9e169f937628eda2cbfcbba99015e --cri-socket=unix:///var/run/cri-dockerd.sock
注意:--cri-socket=unix:///var/run/cri-dockerd.sock参数是指定使用docker作为容器管理引擎。
5.6. 下载calico文件(master节点运行)
Calico是为集群中的 Pod 提供网络功能。
[root@master01 ~]# wget https://raw.githubusercontent.com/projectcalico/calico/v3.24.1/manifests/calico.yaml
5.7. 创建calico网络(master节点运行)
[root@master01 ~]# kubectl apply -f calico.yaml
5.8. 查看集群各节点状态
[root@master01 ~]# kubectl get nodes
NAME STATUS ROLES AGE VERSION
master01 Ready control-plane 16h v1.28.10
node01 Ready <none> 16h v1.28.10
node02 Ready <none> 16h v1.28.10
5.9. 查看k8s集群的各组件
[root@master01 ~]# kubectl get pod -n kube-system
NAME READY STATUS RESTARTS AGE
calico-kube-controllers-9d57d8f49-dkkkn 1/1 Running 1 (15h ago) 16h
calico-node-jllq5 1/1 Running 1 (15h ago) 16h
calico-node-q8fhr 1/1 Running 1 (15h ago) 16h
calico-node-tbdqk 1/1 Running 1 (15h ago) 16h
coredns-6554b8b87f-55k4c 1/1 Running 1 (15h ago) 16h
coredns-6554b8b87f-c6hrv 1/1 Running 1 (15h ago) 16h
etcd-master01 1/1 Running 1 (15h ago) 16h
kube-apiserver-master01 1/1 Running 1 (15h ago) 16h
kube-controller-manager-master01 1/1 Running 1 (15h ago) 16h
kube-proxy-7xl56 1/1 Running 1 (15h ago) 16h
kube-proxy-9x4dp 1/1 Running 1 (15h ago) 16h
kube-proxy-trgk8 1/1 Running 1 (15h ago) 16h
kube-scheduler-master01 1/1 Running 1 (15h ago) 16h
6. 安装ingress-nginx(不用域名访问可不装)
由于镜像拉不下来我就取消部署了,本来想时候手动导入镜像的。后来觉得不是用这个直接用IP访问也可以。就没有部署ingress-nginx。
6.1. 下载ingress-nginx部署文件
[root@master01 ~]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/main/deploy/static/provider/baremetal/deploy.yaml
6.2. 部署ingress-nginx到k8s集群上
[root@master01 ~]# kubectl apply -f deploy.yaml
来源:zhuanlan.zhihu.com/p/701199706