一. 基础环境准备
基础环境配置是针对集群中的所有主机
1. 修改主机名
hostnamectl set-hostname <主机名>
2. 配置 /etc/hosts
172.19.0.2 master01
172.19.0.16 node01
3. 关闭 swap
关闭 swap 并验证
swapoff -a && sysctl -w vm.swappiness=0
sed -i '/swap/d' /etc/fstab
free
4. 关闭 selinux 与防火墙
sed -ri 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config //修改配置文件,下次重启时生效
setenforce 0
getenforce
systemctl stop iptables
systemctl disable iptables
systemctl stop firewalld
systemctl disable firewalld
firewall-cmd --state
5. 配置基础 repo 仓库
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all && yum makecache
## 软件安装
yum -y install net-tools telnet vim lsof wget lrzsz bind-utils traceroute ipset ipvsadm yum-utils device-mapper-persistent-data lvm2
6. 配置主机网桥过滤功能
6.1 添加网桥过滤及地址转发
cat > /etc/sysctl.d/k8s.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
vm.swappiness = 0
EOF
modprobe br_netfilter
lsmod | grep br_netfilter
sysctl -p /etc/sysctl.d/k8s.conf
6.2. 开启IPVS
cat > /etc/ipvs.modules << EOF
#!/bin/bash
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF
chmod +x /etc/ipvs.modules && bash /etc/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4
7. 部署 Containerd
- yum 方式部署
# step 1: 安装必要的一些系统工具
yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加软件源信息
yum-config-manager --add-repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3
sed -i 's+download.docker.com+mirrors.aliyun.com/docker-ce+' /etc/yum.repos.d/docker-ce.repo
# Step 4: 更新并安装 Containerd
yum makecache fast && yum -y install containerd
- 安装包方式部署(或)
wget https://github.com/containerd/containerd/releases/download/v1.6.7/cri-containerd-1.6.7-linux-amd64.tar.gz
## 直接将压缩包解压到系统的各个目录中
tar -C / -xzf cri-containerd-1.6.7-linux-amd64.tar.gz
echo "export PATH=$PATH:/usr/local/bin:/usr/local/sbin" >> ~/.bashrc
sourch ~/.bashrc
## 生成 containerd 的默认配置文件 /etc/containerd/config.toml
mkdir /etc/containerd
containerd config default > /etc/containerd/config.toml
## 启动 containerd
systemctl enable containerd --now
## 查看服务版本
containerd --version
- 配置修改
## 备份/etc/containerd/config.toml
mv /etc/containerd/config.toml /etc/containerd/config.toml.bak
## 生成默认配置文件
containerd config default | tee /etc/containerd/config.toml
vim /etc/containerd/config.toml
# 调整
# sandbox_image = "k8s.gcr.io/pause:3.6"
sandbox_image = "registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.8"
# 调整
# SystemdCgroup = false
SystemCgroup = true
[plugins."io.containerd.grpc.v1.cri".registry.mirrors]
# 添加
[plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
endpoint = ["https://f1361db2.m.daocloud.io"]
systemctl daemon-reload
systemctl restart containerd
- 部署工具
## nerdctl 是 containerd原生的命令行管理工具和 Docker 的命令行兼容。
wget https://github.com/containerd/nerdctl/releases/download/v0.22.2/nerdctl-0.22.2-linux-amd64.tar.gz
mkdir nerdctl && tar -xf nerdctl-0.22.2-linux-amd64.tar.gz -C nerdctl && cd ./nerdctl
cp -a nerdctl /usr/bin
nerdctl images # 验证
## crictl 是 Kubernetes 用于管理 Containerd 上的镜像和容器的一个命令行工具,主要用于 Debug
wget https://github.com/kubernetes-sigs/cri-tools/releases/download/v1.24.0/crictl-v1.24.0-linux-amd64.tar.gz
tar -zxvf crictl-v1.24.0-linux-amd64.tar.gz -C /usr/local/bin
cat > /etc/crictl.yaml <<EOF
runtime-endpoint: unix:///var/run/containerd/containerd.sock
image-endpoint: unix:///var/run/containerd/containerd.sock
timeout: 10
debug: false
pull-image-on-create: false
EOF
systemctl daemon-reload
crictl images # 验证
## 如果使用 nerdctl build 进行 Dockerfile 的镜像构建时,需要进行安装 buildkit
wget https://github.com/moby/buildkit/releases/download/v0.10.5/buildkit-v0.10.5.linux-amd64.tar.gz
tar xf buildkit-v0.10.5.linux-amd64.tar.gz
cp -a bin /usr/local
# 编写buildkitd的启动文件
cat > /etc/systemd/system/buildkit.service << EOF
[Unit]
Description=BuildKit
Documentation=https://github.com/moby/buildkit
[Service]
ExecStart=/usr/local/bin/buildkitd --oci-worker=false --containerd-worker=true
[Install]
WantedBy=multi-user.target
EOF
# 启动 buildkitd 服务端程序
systemctl enable buildkit --now
8. 部署集群相关组件
- 配置 repo 仓库 — aliyun 源
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=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
- 安装组件
yum list kubeadm.x86_64 --showduplicates | sort -r
yum install -y kubeadm-1.25.2-0 kubelet-1.25.2-0 kubectl.1.25.2-0
- 修改 kubelet 配置
cat > /etc/sysconfig/kubelet << EOF
KUBELET_EXTRA_ARGS="--cgroup-driver=systemd"
EOF
cat > /var/lib/kubelet/kubeadm-flags.env << EOF
KUBELET_KUBEADM_ARGS="--container-runtime=remote --container-runtime-endpoint=unix:///run/containerd/containerd.sock --pod-infra-container-image=registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.8"
EOF
# kubelet 设置为开机启动即可(不需要手动启动),集群初始化后会自动启动
systemctl enable kubelet
二. 集群部署
登录 master 节点
# 拉取集群镜像
kubeadm config images pull --kubernetes-version v1.25.2 --image-repository registry.aliyuncs.com/google_containers
# 初始化配置
kubeadm config print init-defaults > kubeadm-init.yaml
vim kubeadm-init.yaml
apiVersion: kubeadm.k8s.io/v1beta3
bootstrapTokens:
- groups:
- system:bootstrappers:kubeadm:default-node-token
token: abcdef.0123456789abcdef
ttl: 24h0m0s
usages:
- signing
- authentication
kind: InitConfiguration
localAPIEndpoint:
advertiseAddress: 10.0.0.6 # 当前主机 IP
bindPort: 6443 # api 端口,一般默认 6443
nodeRegistration:
criSocket: unix:///var/run/containerd/containerd.sock # 指定运行时
imagePullPolicy: IfNotPresent
name: master01 # 当前主机名称
taints: null
---
apiServer:
timeoutForControlPlane: 4m0s
apiVersion: kubeadm.k8s.io/v1beta3
certificatesDir: /etc/kubernetes/pki
clusterName: kubernetes
controllerManager: {}
dns: {}
etcd:
local:
dataDir: /var/lib/etcd
imageRepository: registry.aliyuncs.com/google_containers # 修改仓库地址
kind: ClusterConfiguration
kubernetesVersion: 1.25.2 # k8s 集群版本
networking:
dnsDomain: cluster.local
podSubnet: 10.244.0.0/16
serviceSubnet: 10.96.0.0/12
scheduler: {}
---
apiVersion: kubeproxy.config.k8s.io/v1alpha1
kind: KubeProxyConfiguration
mode: "ipvs"
# 集群初始化
# 根据提示信息,进行相关的配置及将 node 节点添加到集群
kubeadm init --config kubeadm-init.yaml --upload-certs
# 网络配置
curl -O https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
# 注意根据具体情况修改 Pod 网络
kubectl apply -f kube-flannel.yml
# 此时检测看 pod 是否都为running状态
kubectl get pod -A
# 检查节点状态
kubectl get node