- 准备工作
- VMWare安装ubuntu系统
- 更新源
- 修改ip为静态地址
- 主机名设置hostname
- 主机名与IP地址解析
- 时间同步
- 配置内核转发及网桥过滤
- 安装ipset及ipvsadm及脚本自动执行
- 关闭swap分区
- 关闭防火墙
- 安装容器containerd
- 添加crictl.yaml文件
- 添加k8s源
- 配置集群
- 初始化集群
- 初始化失败解决
- CNI插件calico安装
- 检查 Kubernetes 集群状态
1. 准备工作
ubuntu 24.04.3 iso + VMWare虚拟机
-
系统要求: ubuntu 24.04.3
- VMWare虚拟机
2. VMWare安装ubuntu系统
# 默认用户登录,切换root登录
$ sudo -i
$ passwd root
$ 输入两次密码设置root密码
# 配置root登录
$ vim /etc/ssh/sshd_config
# 修改
PermitRootLogin yes
esc+:wq 保存退出
# 重启ssh
$ systemctl restart ssh
3. 更新源
$ apt update & apt upgrade -y
4. 修改ip为静态地址
# 获取当前网关信息
$ ip route show
#获取当前ip地址信息
$ ip addr show
# 查看当前网卡名称信息
$ ls /etc/netplan/
# 配置DHCP动态分配ip为静态ip
$ vim /etc/netplan/xx.yaml
# 根据当前主机信息配置
network:
version: 2
ethernets:
ens18:
dhcp4: no
addresses: [192.168.60.213/20] # 当前ip
routes:
- to: default
via: 192.168.50.1 # 网关
nameservers:
addresses: [114.114.114.114, 119.29.29.29, 8.8.8.8] #DNS解析
# 保存退出后应用网络信息
$ netplan apply
# 尝试解析一下(baidu.com),会返回本地DNS解析
$ dig -t a baidu.com
# DNS配置
$ cat /etc/resolv.conf
# 查看配置详细信息显示本地解析,未使用DNS解析
$ ll /etc/resolv.conf
# 查看链接信息
$ ls -l /run/systemd/resolve/
# 删除重新绑定后nslookup查看server会返回自定义DNS域名解析服务器
$ rm -f /etc/resolv.conf
$ ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
5. 主机名设置hostname
# master 分支
$ hostnamectl set-hostname k8s-master
# node节点设置
$ hostnamectl set-hostname k8s-node1
6. 主机名与IP地址解析
# 配置hosts文件
$ cat >> /etc/hosts << EOF
192.168.155.140 k8s-master
192.168.155.141 k8s-node01
192.168.155.142 k8s-node02
EOF
7. 时间同步
# 当前时间/时区
$ date
# 同步时间为当前时区时间
$ timedatectl set-timezone Asia/Shanghai
# 阿里云时间同步(可选)
$ ntpdate ntp.aliyun.com
# 计划任务同步时间
$ crontab -e
# 选择 2 后插入时间格式
$ 59 23 * * * ntpdate ntp.aliyun.com
# 查看
$ crontab -l
8. 配置内核转发及网桥过滤
# 网桥过滤
$ cat << EOF | tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF
# 应用,手动执行
$ modprobe overlay
$ modprobe br_netfilter
# 查看
$ lsmod | grep overlay
$ lsmod | grep br_netfilter
# 内核转发配置
$ cat << EOF | sudo tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF
# 加载内核参数
$ sysctl --system
9. 安装ipset及ipvsadm及脚本自动执行
# 安装ipset及ipvsadm
$ apt install ipset ipvsadm -y
# 配置ipvsadm模块加载, 添加需要加载的模块
$ cat << EOF | tee /etc/modules-load.d/ipvs.conf
ip_vs
ip_vs_rr
ip_vs_wrr
ip_vs_sh
nf_conntrack
EOF
# 创建加载模块脚本文件
$ cat << EOF | tee ipvs.sh
#!/bin/sh
modprobe -- ip_vs
modprobe -- ip_vs_rr
modprobe -- ip_vs_wrr
modprobe -- ip_vs_sh
modprobe -- nf_conntrack
EOF
# 查看脚本
$ ls
# 执行脚本文件
$ bash ipvs.sh
10. 关闭swap分区
# 查看分区
$ free -mh
# 手动关闭,重启后后自动开启
$ swapoff -a
# 永久关闭,注释swap分区开启
$ vim /etc/fstab
11. 关闭防火墙
# 查看防火墙状态
$ ufw status
# 关闭防火墙
$ ufw disable
# 系统关闭
$ systemctl disable ufw
12. 安装容器containerd
# 更新源
$ apt update $ apt upgrade -y
# 安装containerd
$ apt install containerd -y
# 生成配置文件
$ mkdir -p /etc/containerd/
$ containerd config default > /etc/containerd/config.toml
# 配置 systemd cgroup 驱动
echo "配置 systemd cgroup 驱动..."
$ sed -i 's/SystemdCgroup = false/SystemdCgroup = true/' /etc/containerd/config.toml
# 中国大陆环境拉取 pause 失败时,改用可达镜像
$ sed -i 's#sandbox_image = "registry\.k8s\.io/pause:.*"#sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.10"#' /etc/containerd/config.toml
or
# 编辑config.toml文件,修改配置
[plugins."io.containerd.grpc.v1.cri"]
sandbox_image = "registry.aliyuncs.com/google_containers/pause:3.10"
[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup = true
13. 添加crictl.yaml文件
$ cat<<EOF | sudo tee /etc/crictl.yaml
runtime-endpoint: unix:///run/containerd/containerd.sock
image-endpoint: unix:///run/containerd/containerd.sock
timeout: 5
debug: false
pull-image-on-create: false
EOF
$ systemctl daemon-reload
$ systemctl enable containerd
$ systemctl restart containerd
$ systemctl status containerd
$ apt-get update && apt-get install -y apt-transport-https
$ curl -fsSL https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.33/deb/Release.key | gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
$ echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.33/deb/ /" | tee /etc/apt/sources.list.d/kubernetes.list
$ apt list kubeadm kubelet kubectl
# 安装kubeadm kubelet kubectl
$ apt-get install -y kubelet kubeadm kubectl
# 安装指定版本号
$ apt-get install -y kubelet=1.33.3-1.1 kubeadm=1.33.3-1.1 kubectl=1.33.3-1.1
# 锁定版本,避免错误升级
$ apt-mark hold kubelet kubeadm kubectl
15. 配置集群
# 查看所需镜像文件
$ kubeadm config images list
# 避免超时,可下载到本地(可选)
$ kubeadm config images pull
16. 初始化集群
# 初始化集群
$ kubeadm init --kubernetes-version=1.33.3 --control-plane-endpoint=k8s-master:6443 --image-repository=registry.aliyuncs.com/google_containers --pod-network-cidr=192.168.0.0/16 --upload-certs --v=9
or
# 创建日志打印生成模板并编辑
$ kubeadm config print init-defaults > /etc/kubernetes/kubeadm-config.yaml
# 获取版本
$ kubelet --version
# 编辑kubeadm-config.yaml文件并修改配置
advertiseAddress: 192.168.60.183(master ip) # 当前ip
name: colin-k8s-master # 节点名称(可选)
kubernetesVersion: 1.32.3 # 版本
podSubnet: 10.244.0.0/16 # 固定格式
# 初始化集群
kubeadm init --config /etc/kubernetes/kubeadm-config.yaml --upload-certs --v=9
# 获取所有节点
$ kubectl get node
17. 初始化失败解决
# 重置集群配置
$ kubeadm reset -f
# 清理网络配置:
$ ipvsadm --clear
# 删除配置文件:
$ rm -rf ~/.kube
$ rm -rf /etc/kubernetes/manifests/*
# 检查并终止占用进程/查看端口占用情况
$ netstat -tlnp | grep -E '(10250|10257|10259)'
or
$ sudo kubeadm reset -f
$ sudo systemctl stop kubelet
$ sudo rm -rf /etc/kubernetes/
$ sudo rm -rf ~/.kube/
# node删除配置(无法加入)
$ rm -f /etc/kubernetes/kubelet.conf /etc/kubernetes/pki/ca.crt
# 生成新的node加入命令
$ kubeadm token create --print-join-command
# 安装 Tigera Operator 和自定义资源定义
$ kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.31.0/manifests/tigera-operator.yaml
# 通过创建必要的自定义资源来安装 Calico
$ kubectl create -f https://raw.githubusercontent.com/projectcalico/calico/v3.31.0/manifests/custom-resources.yaml
# 查看Calico Pod运行情况
$ kubectl get pods -n calico-system
19. 检查 Kubernetes 集群状态
# 查看所有pods状态
$ kubectl get pod -A
# 显示Kubernetes集群的基本信息/检查节点状态
$ kubectl cluster-info & kubectl get nodes -o wide
# 显示配置信息
$ kubectl config view