一、规划

  • VMware NAT:192.168.10.10/24
  • infra VM(192.168.10.10)
  • 基础虚拟机,Rocky9.6
  • 1C2G

二、配置

  1. 配置网络
1
2
3
4
5
6
sudo nmcli con mod ens160 ipv4.method manual \
ipv4.addresses 192.168.10.10/24 \
ipv4.gateway 192.168.10.2 \
ipv4.dns 192.168.10.2
sudo nmcli con up ens160
ping -c2 mirrors.rockylinux.org # 验证能联网
  1. 装 dnsmasq 和排错工具
1
sudo dnf install -y dnsmasq bind-utils
  1. 写配置
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
sudo tee /etc/dnsmasq.d/qx-lab.conf > /dev/null <<'EOF'
# 不用 /etc/resolv.conf 的上游,自己指定
no-resolv
# 上游 DNS:VMware NAT 网关(继承宿主机解析,国内外都通)
server=192.168.10.2

# 只监听本机和本网卡
listen-address=127.0.0.1,192.168.10.10
bind-interfaces

# 泛解析:所有 *.qx.lab 默认指向集群入口(后面 MetalLB 的 IP)
address=/qx.lab/192.168.10.60

# 更精确的会自动覆盖上面(dnsmasq 按最长匹配)
address=/harbor.qx.lab/192.168.10.20
address=/infra.qx.lab/192.168.10.10
address=/k8s231.qx.lab/192.168.10.231
address=/k8s232.qx.lab/192.168.10.232
address=/k8s233.qx.lab/192.168.10.233
address=/rancher.qx.lab/192.168.10.40
address=/gitlab.qx.lab/192.168.10.51

cache-size=1000
EOF
  1. 确保没有占用 53 端口
1
sudo ss -lnup | grep ':53'     # 应该没输出(Rocky 9 默认不开 systemd-resolved)
1
2
echo 'DNSStubListener=no' | sudo tee -a /etc/systemd/resolved.conf
sudo systemctl restart systemd-resolved 2>/dev/null
  1. 防火墙
1
2
sudo firewall-cmd --permanent --add-service=dns
sudo firewall-cmd --reload
  1. 起服务
1
2
sudo systemctl enable --now dnsmasq
sudo systemctl status dnsmasq # 看是否 active (running)
  1. 让 infra VM 用自己当 DNS
1
2
sudo nmcli con mod ens160 ipv4.dns 127.0.0.1
sudo nmcli con up ens160

三、验证

  1. 在 infra 上自测
1
2
3
dig +short harbor.qx.lab @127.0.0.1     # 期望 192.168.10.20
dig +short grafana.qx.lab @127.0.0.1 # 期望 192.168.10.60(通配命中)
dig +short www.baidu.com @127.0.0.1 # 期望 真实公网 IP(上游转发正常)

四、其它机器配置

  1. 虚拟机
1
2
3
4
sudo nmcli con mod ens160 ipv4.dns 192.168.10.10
sudo nmcli con mod ens160 ipv4.ignore-auto-dns yes # 忽略 DHCP 下发的 DNS
sudo nmcli con up ens160
dig +short harbor.qx.lab # 验证
  1. 物理机,Windows,管理员
1
Add-DnsClientNrptRule -Namespace ".qx.lab" -NameServers "192.168.10.10"

五、自签名证书

搞一套根证书,中间证书,来签发,占用资源不多,公用一个机器。

5.1 安装

1
2
3
4
5
6
7
8
9
10
11
12
sudo tee /etc/yum.repos.d/smallstep.repo > /dev/null <<'EOT'
[smallstep]
name=Smallstep
baseurl=https://packages.smallstep.com/stable/fedora/
enabled=1
repo_gpgcheck=0
gpgcheck=1
gpgkey=https://packages.smallstep.com/keys/smallstep-0x889B19391F774443.gpg
EOT
sudo dnf makecache
sudo dnf install -y step-cli
step version

5.2 建立根

1
2
3
4
5
6
7
8
9
10
mkdir -p ~/qx-ca && cd ~/qx-ca

# 根 CA:根证书将来要装到所有机器,有效期给长一点(10年)
step certificate create "QX Lab Root CA" root_ca.crt root_ca.key \
--profile root-ca --not-after 262800h --no-password --insecure

# 中间 CA:日常用它签叶子(5年)
step certificate create "QX Lab Intermediate CA" intermediate_ca.crt intermediate_ca.key \
--profile intermediate-ca --ca root_ca.crt --ca-key root_ca.key \
--not-after 43800h --no-password --insecure

5.3 签 Harbor 的叶子证书

1
2
3
4
5
step certificate create "harbor.qx.lab" harbor.qx.lab.crt harbor.qx.lab.key \
--profile leaf \
--ca intermediate_ca.crt --ca-key intermediate_ca.key \
--san harbor.qx.lab --san 192.168.10.20 \
--not-after 8760h --no-password --insecure --bundle

5.4 安装证书

1
2
ssh root@192.168.10.20 'mkdir -p /data/cert'
scp harbor.qx.lab.crt harbor.qx.lab.key root@192.168.10.20:/data/cert/

5.5 信任证书

1
2
3
4
5
6
7
# 系统信任(curl/浏览器等都认)
sudo cp root_ca.crt /etc/pki/ca-trust/source/anchors/qx-root-ca.crt
sudo update-ca-trust

# docker 拉取/登录单独认这个目录
sudo mkdir -p /etc/docker/certs.d/harbor.qx.lab
sudo cp root_ca.crt /etc/docker/certs.d/harbor.qx.lab/ca.crt