前言

Kubernetes1.29 集群部署文档记录,使用虚拟机部署。

主机环境

系统:Ubuntu20.04
Kubernetes:v1.29.2
Container Runtime:Docker CE 25.0.3 和 cri-dockerd v0.3.10

建议提前修改节点hostname,尽量不要重名

# master节点
hostname master
# 从节点
hostname worker01
hostname worker02

安装

1. 安装docker(所有节点)

清华源安装

2. 配置docker cgroup为systemd(所有节点)

# 1.查看docker的Cgroup驱动
docker info | grep Cgroup
 # Cgroup Driver: cgroupfs
 # Cgroup Version: 1

# 2.修改Docker 启动方式,如果文件不存在,就新建文件
vim /etc/docker/daemon.json
{
  "registry-mirrors": ["https://dpxn2pal.mirror.aliyuncs.com"],
  "exec-opts": [ "native.cgroupdriver=systemd" ]
}

# 3. 重启 Docker 服务
systemctl restart docker

# 5. 重启后查看 Docker Cgroup驱动
docker info | grep Cgroup
 # Cgroup Driver: systemd
 # Cgroup Version: 1

4. 时间同步(所有节点)

timedatectl set-timezone "Asia/Shanghai"
sudo apt install ntpdate
sudo ntpdate -u ntp.aliyun.com
# 查看时间
date

5. 禁用防火墙和iptable服务和禁用swap分区(所有节点)

# 防火墙
systemctl stop ufw
systemctl disable ufw
# iptable
systemctl stop iptables
systemctl disable iptables
# swap
vim /etc/fstab
# 注释/swap.img     none    swap    sw      0       0

6. 转发 IPv4 并让 iptables 看到桥接流量(所有节点)

执行下述指令:

cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

sudo modprobe overlay
sudo modprobe br_netfilter

# 设置所需的 sysctl 参数,参数在重新启动后保持不变
cat <<EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF

# 应用 sysctl 参数而不重新启动
sudo sysctl --system

通过运行以下指令确认 br_netfilteroverlay 模块被加载:

lsmod | grep br_netfilter
lsmod | grep overlay

通过运行以下指令确认 net.bridge.bridge-nf-call-iptablesnet.bridge.bridge-nf-call-ip6tablesnet.ipv4.ip_forward 系统变量在你的 sysctl 配置中被设置为 1:

sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward

7. 安装cni插件(所有节点)

参考-无包管理器的情况

CNI_PLUGINS_VERSION="v1.3.0"
ARCH="amd64"
DEST="/opt/cni/bin"
sudo mkdir -p "$DEST"
curl -L "https://mirror.ghproxy.com/https://github.com/containernetworking/plugins/releases/download/${CNI_PLUGINS_VERSION}/cni-plugins-linux-${ARCH}-${CNI_PLUGINS_VERSION}.tgz" | sudo tar -C "$DEST" -xz

8. 安装容器运行时——cri-docker(所有节点)

  • 安装cri-docker

    # 替换成你自己的操作系统对应的包
    curl -LO https://github.com/Mirantis/cri-dockerd/releases/download/v0.3.13/cri-dockerd_0.3.13.3-0.ubuntu-focal_amd64.deb
    apt install ./cri-dockerd_0.3.10.3-0.ubuntu-focal_amd64.deb -y
    
  • 配置network-plugin

    • 服务未启用修改: /usr/lib/systemd/system/cri-docker.service

    • 服务启用修改: /etc/systemd/system/multi-user.target.wants/cri-docker.service

    vim /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
    # 修改镜像地址为国内,否则kubelet拉取不了镜像导致启动失败
    # 网络插件设置为CNI
    ExecStart=/usr/bin/cri-dockerd --container-runtime-endpoint fd:// --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
    # 这里的参数 `--pod-infra-container-image`,这个值要根据实际情况调整,如果你的安装环境不能访问互联网,那你就需要配置为内部镜像仓库。
    
  • 创建一个 socket 文件以便 kubelet 与 cri-dockerd 通信

    cat <<"EOF" > /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
    EOF
    
  • 启动服务

    # 重启守护进程
    systemctl daemon-reload
    # 开机自启动
    systemctl enable cri-docker.socket
    systemctl enable cri-docker.service
    # 启动
    systemctl start cri-docker  cri-docker.socket
    

9.安装 crictl——kubeadm/kubelet 容器运行时接口(CRI)所需(所有节点)

参考-无包管理器的情况

DOWNLOAD_DIR="/usr/local/bin"
sudo mkdir -p "$DOWNLOAD_DIR"
CRICTL_VERSION="v1.28.0"
ARCH="amd64"
curl -L "https://mirror.ghproxy.com/https://github.com/kubernetes-sigs/cri-tools/releases/download/${CRICTL_VERSION}/crictl-${CRICTL_VERSION}-linux-${ARCH}.tar.gz" | sudo tar -C $DOWNLOAD_DIR -xz

10. 安装 kubeadm、kubelet 和 kubectl(worker节点可不用安装kubectl)

参考

提前下载好kubeadmkubeletkubectl 的二进制文件

  1. 上传kubeadmkubelet/usr/local/bin目录下
sudo chmod +x {kubeadm,kubelet}
  1. 配置服务
RELEASE_VERSION="v0.16.2"
curl -sSL "https://mirror.ghproxy.com/https://raw.githubusercontent.com/kubernetes/release/${RELEASE_VERSION}/cmd/krel/templates/latest/kubelet/kubelet.service" | sed "s:/usr/bin:${DOWNLOAD_DIR}:g" | sudo tee /etc/systemd/system/kubelet.service
sudo mkdir -p /etc/systemd/system/kubelet.service.d
curl -sSL "https://mirror.ghproxy.com/https://raw.githubusercontent.com/kubernetes/release/${RELEASE_VERSION}/cmd/krel/templates/latest/kubeadm/10-kubeadm.conf" | sed "s:/usr/bin:${DOWNLOAD_DIR}:g" | sudo tee /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl daemon-reload
systemctl enable kubelet.service
  1. 安装bubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl

校验和请看:
https://kubernetes.io/zh-cn/docs/tasks/tools/install-kubectl-linux/

11. 使用 kubeadm 创建集群(master节点)

kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.29.2 --token-ttl 0 --cri-socket unix:///run/cri-dockerd.sock

注意: 运行是的警告和错误提示缺少指定包,按操作执行即可

[WARNING Swap]: swap is supported for cgroup v2 only; the NodeSwap featu                                                                              re gate of the kubelet is beta but disabled by default
        [WARNING FileExisting-socat]: socat not found in system path
        [WARNING Service-Kubelet]: kubelet service is not enabled, please run 's                                                                              ystemctl enable kubelet.service'
error execution phase preflight: [preflight] Some fatal errors occurred:
        [ERROR FileExisting-conntrack]: conntrack not found in system path
[preflight] If you know what you are doing, you can make a check non-fatal with
...

12 使用kubectl命令行管理工具

mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# 环境变量方式 临时生效(退出当前窗口重连环境变量失效)
export KUBECONFIG=/etc/kubernetes/admin.conf
# 环境变量方式 永久生效(推荐)
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
source  ~/.bash_profile

13. 部署网络插件(master节点)

kubectl get node

image-20240225211520994

此时status状态为notReady

# 下载kube-flannel.yml
wget https://raw.githubusercontent.com/flannel-io/flannel/master/Documentation/kube-flannel.yml
# 通过yaml文件部署kube-flannel
kubectl apply -f kube-flannel.yml

# 离线状态需要提前准备好flannel镜像(私服。或本地,本地的话所有节点都需要)以及kube-flannel.yml
# 也可以使用其他网络插件

部署完成后status状态为Ready
image-20240225212410688

12 加入到master(worker节点)

join命令主节点init后会提示,只需要加上自己的容器运行时即可

kubeadm join 192.168.1.203:6443 --token a07hyg.mfqeiirhlqk677cp \
--discovery-token-ca-cert-hash sha256:729940595963011c3387d1601736f3f422d2ea60bebc078975f80bf85b76d951 \
# 自己的容器运行时
--cri-socket=unix:///var/run/cri-dockerd.sock \

13 检查集群状态(master节点)

kubectl get node

后记

自己安装时花费了一些时间,也算是熟悉了这个安装流程,如果遗漏或错误的地方还望指出。

参考链接

  1. 官方链接
  2. 博客
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐