腾讯云248上海 安装PVE 7.1-5 成功, NAT小鸡批量端口转发也搞定

服务器 sunny2178 1年前 (2021-11-30) 6551次浏览

开了 4 个小鸡测试, 分别是 Alpine 3.14 Debian 11 Centos 8 ubuntu 21.10

LXC 容器小鸡的模版 竟然是 debian 的最大。

LXC 容器 CT 模版从官方不能下载,是从中科大镜像站下载

https://mirrors.ustc.edu.cn/proxmox/images/system/

母鸡系统,使用 netboot.xyz 手工安装的 Debian 11

$ vim /etc/hosts #修改主机名

127.0.0.1 localhost
188.188.188.188 debian.proxmox.com debian
# 这里填母鸡公网 IP
# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

安装 Proxmox VE PVE 7.1-5

添加 Proxmox VE 存储库:

echo "deb [arch=amd64] http://download.proxmox.com/debian/pve bullseye pve-no-subscription" \
  > /etc/apt/sources.list.d/pve-install-repo.list

以 root 身份添加 Proxmox VE 存储库密钥(或使用 sudo):

wget https://enterprise.proxmox.com/debian/proxmox-release-bullseye.gpg \
  -O /etc/apt/trusted.gpg.d/proxmox-release-bullseye.gpg

通过运行更新您的存储库和系统:

apt update && apt full-upgrade

安装 Proxmox VE 软件包,因为母鸡国内上海,安装这部使用了 http 代理提高速度

apt install proxmox-ve postfix open-iscsi

PVE 源可以使用清华的 清华镜像:https://mirrors.tuna.tsinghua.edu.cn/help/proxmox/

安装完成,使用 https://ip:8006 使用 WebUI 管理

安装 proxmox-ve 的时候,可以先把网络防火墙,按图示先设施

网络设置: WebGUI 管理里添加 vmbr0 ,选 linux 网桥,再修改网络设置

cat /etc/network/interfaces

source /etc/network/interfaces.d/*

auto lo
iface lo inet loopback

auto ens5
iface ens5 inet dhcp

auto vmbr0
#private sub network
iface vmbr0 inet static
address 10.10.10.1/24
bridge-ports none
bridge-stp off
bridge-fd 0

post-up echo 1 > /proc/sys/net/ipv4/ip_forward
post-up iptables -t nat -A POSTROUTING -s '10.10.10.0/24' -o ens5 -j MASQUERADE
post-down iptables -t nat -D POSTROUTING -s '10.10.10.0/24' -o ens5 -j MASQUERADE

ens5 是腾讯云实际网卡名字,10.10.10.0/24 网段是给 NAT 小鸡分配的网段

设置参考文章: https://pve.proxmox.com/wiki/Network_Configuration

  • Masquerading (NAT) with iptables 使用 iptables 伪装 (NAT)
  • 伪装允许只有一个私有 IP 地址的访客使用主机 IP 地址来访问网络,用于传出流量。 每个传出数据包都由 iptables 重写以显示为源自主机,并且相应地重写响应以路由到原始发送方。

小鸡网络设置

IP 地址: 使用 10.10.10.0 网段静态

10.10.10.101/24 使用机器编号做 IP 地址

# 小鸡的  ssh 22 和 80 端口 准备端口转发  
IP 101:      10122    10180
IP 102:      10222    10280
IP 103:      10322    10380
IP 104:      10422    10480

NAT 小鸡端口转发 Iptables 调试搞定,批量只要修改下面的语句很容易搞定

iptables -t nat -A PREROUTING -p tcp -m tcp --dport 10180 \
    -j DNAT --to-destination 10.10.10.101:80

PVE NAT 批量端口转发设置脚本,使用命令 nat.sh $(lxc-ls) 自动调用 lxc 容器 id

# Usage: bash nat.sh $(lxc-ls)

# 小鸡的端口转发规则 ssh 22 http 80 10<N>00 : 10<N>99
# ID 对应 IP 101: 10122 10180 10100 : 10199
#-------------------------------------------------------------#
input_id()
{
id=101
echo -e "测试默认 ID: \033[41;37m ${id} \033[0m 可以修改设置其他 ID; "
read -p "请输入 NAT 小鸡的 ID 号(按回车不修改): " -t 30 new
if [[ ! -z "${new}" ]]; then
id="${new}"
fi
nat_port
iptables -t nat -nvL PREROUTING
echo -e ":: PVE NAT 批量端口转发设置脚本: \033[41;37m bash nat.sh $(lxc-ls) \033[0m \n 使用参考: https://262235.xyz/index.php/archives/714/"
}
# 以 id 为 ip 设置端口转发
nat_port()
{
iptables -t nat -I PREROUTING -p tcp -m tcp --dport ${id}22 -j DNAT --to-destination 10.10.10.${id}:22
# iptables -t nat -I PREROUTING -p tcp -m tcp --dport ${id}80 -j DNAT --to-destination 10.10.10.${id}:80
iptables -t nat -A PREROUTING -p tcp -m multiport --dport ${id}00:${id}99 -j DNAT --to-destination 10.10.10.${id}
}

# 手工输入 id,input_id 调用 nat 端口转发
if [ $# -eq 0 ];
then
input_id
exit
fi

# 遍历参数 批量设置 nat 端口转发
for arg in $*
do
id=$arg
nat_port
done

# 查看 nat PREROUTING 端口映射规则
iptables -t nat -nvL PREROUTING

# 清空 nat PREROUTING 端口映射规则
# iptables -t nat -F PREROUTING

乐趣公园 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:腾讯云 248 上海 安装 PVE 7.1-5 成功, NAT 小鸡批量端口转发也搞定
喜欢 (1)