目录
1. 概述
1.1 架构说明
本教程将搭建一个7节点的Kubernetes高可用集群,包含:
- 2个Master节点:用于运行Kubernetes控制平面组件
- 3个Worker节点:用于运行实际工作负载
- 2个负载均衡节点:使用HAProxy + Keepalived实现Master节点的高可用访问
ps:有条件可以搭建三主节点
1.2 软件版本
- Kubernetes: 1.28.2
1.3 所需软件说明
Kubernetes核心组件
- kubelet: 运行在所有节点上的主要节点代理
- kubeadm: 用于初始化集群的命令行工具
- kubectl: 用于操作集群的命令行工具
容器运行时
- Containerd: 工业级标准的容器运行时,用于运行容器
负载均衡组件
- HAProxy: 提供TCP/HTTP负载均衡
- Keepalived: 实现VIP浮动,确保负载均衡的高可用性
网络插件
- Calico: 提供容器网络和网络策略
2. 环境准备
2.1 服务器配置要求
IP地址 | 主机名 | 角色 | 系统 | 配置 |
---|---|---|---|---|
192.168.145.143 | master01 | 管理节点 | CentOS 7 | 2C/4G/20G |
192.168.145.144 | master02 | 管理节点 | CentOS 7 | 2C/4G/20G |
192.168.145.145 | node01 | 工作节点 | CentOS 7 | 2C/4G/20G |
192.168.145.146 | node02 | 工作节点 | CentOS 7 | 2C/4G/20G |
192.168.145.147 | node03 | 工作节点 | CentOS 7 | 2C/4G/20G |
192.168.145.148 | k8s-ha1 | 主负载均衡 | CentOS 7 | 1C/1G/20G |
192.168.145.149 | k8s-ha2 | 备负载均衡 | CentOS 7 | 1C/1G/20G |
ps:有条件的小伙伴master节点可以增加到3个
3. 集群部署
3.1 所有节点基础配置
3.1.1 关闭防火墙和SELinux
# 关闭防火墙
systemctl stop firewalld
systemctl disable firewalld
# 关闭SELinux
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
3.1.2 配置主机名
# Master01节点
hostnamectl set-hostname master01
##其他节点配置对应主机名
3.1.3 配置hosts文件
cat >> /etc/hosts << EOF
192.168.145.143 master01
192.168.145.144 master02
192.168.145.145 node01
192.168.145.146 node02
192.168.145.147 node03
192.168.145.148 k8s-ha1
192.168.145.149 k8s-ha2
EOF
3.1.4 配置时间同步
# 安装chrony
yum install -y chrony
# 配置时间服务器
sed -i 's/server 0.centos.pool.ntp.org iburst/server ntp.aliyun.com iburst/g' /etc/chrony.conf
sed -i 's/server 1.centos.pool.ntp.org iburst/server ntp1.aliyun.com iburst/g' /etc/chrony.conf
sed -i 's/server 2.centos.pool.ntp.org iburst/server ntp2.aliyun.com iburst/g' /etc/chrony.conf
sed -i 's/server 3.centos.pool.ntp.org iburst/server ntp3.aliyun.com iburst/g' /etc/chrony.conf
# 启动服务
systemctl start chronyd
systemctl enable chronyd
3.1.5 配置内核参数
cat > /etc/sysctl.d/k8s.conf << EOF
net.bri