VMware 虚拟机安装并初始化配置(K8S)

网站: https://releases.ubuntu.com/jammy/ (下载ubuntu-22.04.5-live-server-amd64.iso)

安装虚拟机,并设置快照,作为os-base机器。

从os-base虚拟机克隆一台master01,并做如下初始化配置:

# 临时关闭防火墙
sudo systemctl stop ufw
# 禁止开机自启
sudo systemctl disable ufw
# 验证状态(显示 inactive 即成功)
sudo systemctl status ufw

# 临时关闭 Swap
sudo swapoff -a
# 永久关闭(注释 Swap 分区行)
sudo sed -i '/swap/s/^/#/' /etc/fstab
# 验证(显示空行即成功)
sudo swapon --show

# 添加内核配置
cat <<EOF | sudo tee /etc/modules-load.d/k8s.conf
overlay
br_netfilter
EOF

# 加载模块
sudo modprobe overlay
sudo modprobe br_netfilter

# 设置网络转发参数
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

# 生效配置
sudo sysctl --system

# 导入阿里云 Docker 镜像的 GPG 密钥(国内服务器,不会超时)
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg

# 添加阿里云 Docker 软件源(替代官方源)
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null

# 安装 Containerd
sudo apt update && sudo apt install -y containerd.io

# 生成默认配置并修改
sudo mkdir -p /etc/containerd
containerd config default | sudo tee /etc/containerd/config.toml

# 修改 sandbox 镜像为国内源(加速)
sudo sed -i 's/registry.k8s.io/pause:3.8/registry.aliyuncs.com\/google_containers\/pause:3.8/g' /etc/containerd/config.toml
# 启用 SystemdCgroup(关键)
sudo sed -i 's/SystemdCgroup = false/SystemdCgroup = true/g' /etc/containerd/config.toml

# 重启并设置自启
sudo systemctl restart containerd
sudo systemctl enable containerd

# 添加阿里云 K8s 源
curl -fsSL https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/kubernetes.gpg
echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" | sudo tee /etc/apt/sources.list.d/kubernetes.list

# 更新源缓存
sudo apt update

# 安装常用工具
sudo apt install -y vim wget net-tools iputils-ping

# 配置检查
# 1. 防火墙状态(inactive)
sudo systemctl status ufw
# 2. Swap 状态(无输出)
swapon --show
# 3. 内核参数(均为 1)
sysctl net.bridge.bridge-nf-call-iptables net.bridge.bridge-nf-call-ip6tables net.ipv4.ip_forward
# 4. Containerd 状态(active)
sudo systemctl status containerd
# 5. 节点互通(Master 上 ping Worker,Worker 上 ping Master)
ping -c 3 k8s-worker  # Master 节点执行
ping -c 3 k8s-master  # Worker 节点执行

配置完毕,先关闭机器,设置快照k8s-base; 然后从master01 虚拟机克隆 worker01 虚拟机。

master01 配置

# 设置主机名
sudo hostnamectl set-hostname k8s-master

#配置为静态ip
vi /etc/netplan/00-installer-config.yaml

"""
    ens33:
      dhcp4: no
      addresses:
        - 192.168.38.201/24  # Master 节点 IP(/24 是子网掩码,无需改)
      gateway4: 192.168.38.2  # 替换为你的网关
      nameservers:
        addresses: [127.0.0.53, 114.114.114.114, 8.8.8.8]  # DNS 服务器
"""
sudo netplan apply

# 修改hosts文件 
sudo vi /etc/hosts

"""
192.168.38.201 k8s-master
192.168.38.202 k8s-worker
"""

worker01 配置

# 设置主机名
sudo hostnamectl set-hostname k8s-worker

#配置为静态ip
vi /etc/netplan/00-installer-config.yaml

"""
    ens33:
      dhcp4: no
      addresses:
        - 192.168.38.202/24  # Worker 节点 IP(/24 是子网掩码,无需改)
      gateway4: 192.168.38.2  # 替换为你的网关
      nameservers:
        addresses: [127.0.0.53, 114.114.114.114, 8.8.8.8]  # DNS 服务器
"""
sudo netplan apply

# 修改hosts文件 
sudo vi /etc/hosts

"""
192.168.38.201 k8s-master
192.168.38.202 k8s-worker
"""

posted @ 2026-02-08 16:29  武平宁  阅读(46)  评论(0)    收藏  举报