n/vpsu/James · 2025-01-02
注册 登录
n/vps · 2025-01-02 · 542 次浏览
u/James💯传奇

使用 PVE 实现自动开小鸡历程

以前没玩过PVE,一直用的VirtFusion;前几天决定抛弃 ISPconfig,不再提供免费主机了,遂决定研究PVE,把之前的几台机器重新利用起来给大家提供免费小鸡。

### Step 1

第一步安装 PVE,这是很简单的事情。

https://pve.proxmox.com/wiki/Install_Proxmox_VE_on_Debian_12_Bookworm

按以上流程都可以很快速的安装。

### Step 2

配置集群。

因为NL的财务系统使用的是 Paymenter,一款开源的财务产品,虽然有点简陋,bug也多,也凑活通用。

如果要一个财务系统管理多台 pve 主要,那就需要配置 Cluster 了,这玩意困扰了我两天,一是有些机器是 NAT 转发机,本身没有公网ip。 在配置PVE的Cluster的时候,会有各种各样的问题。

另外,即使有公网ip,比如 ZLIDC的机器,就是加入不到集群中,到现在也没搞明白什么原因。 所以目前集群中有3台机器,一台是 skywolf,一台是niihost,还有一台是 cmivps 的。

Image description~~

### Step 3

配置网络。

首先,母鸡增加两个桥接,一个vmbr0,一个vmbr1。 vmbr0是桥接网卡的,vmbr1是桥接小鸡的。

母鸡转发小鸡对外请求

```
iptables -t nat -A POSTROUTING -s 10.0.0.0/24 -o vmbr0 -j MASQUERADE
```

母鸡转发外网对小鸡请求

```
iptables -t nat -A PREROUTING -i vmbr0 -p tcp --dport 10222 -j DNAT --to-destination 10.0.0.2:22
iptables -t nat -A PREROUTING -i vmbr0 -p udp --dport 10222 -j DNAT --to-destination 10.0.0.2:22
```

这里是把 ~~</s>10222<e> 转发到了 </s>10.0.0.2<e> 这台小鸡的 '22` 端口。

###~~ Step 4

配置资源池。(可不做)

### Step 5

配置Storage(可不做)

### Step 6

Paymenter 接口对接 ,见回复代码。

0
18
登录后参与讨论。

18 条回复

排序
  1. kwxos 白银 2025-01-02

    支持,(前排)2楼表示,直接送我得了,不用踩了:ac01:

    0
  2. lusky 荣誉会员 铂金 圣贤 2025-01-02

    @"James"#p200897 刚看了视频,一看时间太晚,我准备明天再试试

    0
  3. anderbowie 白银 2025-01-02

    听不懂,既然是给大家谋福利肯定支持。我怎么插入不了表情我靠。

    0
  4. James 💯 传奇 楼主 MANAGER 2025-01-02

    卡在这里了,在Paymenter后台扩展里边获取不到pve里边的信息

    Image description~~

    0
  5. hko46 黄金 不离不弃 2025-01-02

    搞不懂这玩意,还是你们厉害:yct02:

    0
  6. James 💯 传奇 楼主 MANAGER 2025-01-02

    卡在api创建虚拟机不成功问题上了,不知道是哪里不行、也不报错,明日再战。

    0
  7. hkvip8 白银 2025-01-02

    坐等更新,财务系统能直接对接pve的,whmcs插件贵

    0
  8. licheng527 白银 2025-01-02

    站长辛苦了,可惜我帮不上忙

    0
  9. newbeeplus8910 黄金 不离不弃 2025-01-02

    劝退

    0
  10. laozhang 荣誉会员 王者 王者会员 2025-01-02

    快快整,期待小鸡呢。

    0
  11. James 💯 传奇 楼主 MANAGER 2025-01-02

    Image description~~

    接近了接近了

    0
  12. James 💯 传奇 楼主 MANAGER 2025-01-02

    Paymenter的 CreateServer有些问题,比如IP这里,会卡住。

    ```
    public function createServer($user, $parmas, $order, $product, $configurableOptions)
    {
    $node = isset($configurableOptions['node']) ? $configurableOptions['node'] : $parmas['node'];
    $storage = isset($configurableOptions['storage']) ? $configurableOptions['storage'] : $parmas['storage'];
    $pool = isset($configurableOptions['pool']) ? $configurableOptions['pool'] : $parmas['pool'];
    $cores = isset($configurableOptions['cores']) ? $configurableOptions['cores'] : $parmas['cores'];
    $memory = isset($configurableOptions['memory']) ? $configurableOptions['memory'] : $parmas['memory'];
    $disk = isset($configurableOptions['disk']) ? $configurableOptions['disk'] : $parmas['disk'];
    $swap = isset($configurableOptions['swap']) ? $configurableOptions['swap'] : $parmas['swap'];
    $network_limit = isset($configurableOptions['network_limit']) ? $configurableOptions['network_limit'] : ($parmas['network_limit'] ?? null);
    $cpu = isset($configurableOptions['cpu']) ? $configurableOptions['cpu'] : ($parmas['cpu'] ?? null);

        $vmid = $this-&gt;getRequest('/cluster/nextid')-&gt;json()['data'];
    
        // Assign it to the orderProduct for further use
        ExtensionHelper::setOrderProductConfig('vmid', $vmid, $product-&gt;id);
        $postData = [];
    
        $currentConfig = $product-&gt;product-&gt;settings;
        $postData = [];
        $vmType = $currentConfig-&gt;where('name', 'type')-&gt;first()-&gt;value;
        if ($currentConfig-&gt;where('name', 'clone')-&gt;first()-&gt;value == '1') {
            $postData = [
                'newid' =&gt; $vmid,
                'target' =&gt; $node,
                'full' =&gt; 1,
            ];
            isset($parmas['pool']) &amp;&amp; $postData['pool'] = $parmas['pool'];
            $response = $this-&gt;postRequest('/nodes/' . $node . '/' . $vmType . '/' . $parmas['vmId'] . '/clone', $postData);
            if (!$response-&gt;json()) throw new Exception('Unable to clone server');
    
            // Update hardware
            $postData = [
                'cores' =&gt; $cores,
                'memory' =&gt; $memory,
                'cipassword' =&gt; $parmas['config']['password'],
            ];
            $response = $this-&gt;putRequest('/nodes/' . $node . '/' . $vmType . '/' . $vmid . '/config', $postData);
            if (!$response-&gt;json()) throw new Exception('Unable to update hardware');
    
            // Get disk
            $disk = $this-&gt;getRequest('/nodes/' . $node . '/' . $vmType . '/' . $vmid . '/config')-&gt;json()['data'];
            $disk = explode('order=', $disk['boot'])[1];
            $disk = explode(',', $disk)[0];
            $postData = [
                'disk' =&gt; $disk,
                'size' =&gt; $parmas['disk'] . 'G',
            ];
            $response = $this-&gt;putRequest('/nodes/' . $node . '/' . $vmType . '/' . $vmid . '/resize', $postData);
            return true;
        } else if ($vmType == 'lxc') {
            $postData = [
                'vmid' =&gt; $vmid,
                'node' =&gt; $node,
                'storage' =&gt; $storage,
                'cores' =&gt; $cores,
                'memory' =&gt; $memory,
                'onboot' =&gt; 1,
                'ostemplate' =&gt; $parmas['template'],
                'ostype' =&gt; $parmas['ostypelxc'],
                'description' =&gt; $parmas['config']['hostname'],
                'hostname' =&gt; $parmas['config']['hostname'],
                'password' =&gt; $parmas['config']['password'],
                'swap' =&gt; $swap ?? 512,
                'unprivileged' =&gt; isset($parmas['unprivileged']) ? 1 : 0,
                'net0' =&gt; 'name=test' . ',bridge=' . $parmas['bridge'] . ',' . (isset($parmas['firewall']) ? 'firewall=1' : 'firewall=0') . (isset($network_limit) ? ',rate=' . $network_limit : ''),
            ];
            $ips = isset($configurableOptions['ips']) ? $configurableOptions['ips'] : ($parmas['ips'] ?? null);
            if (isset($ips)) {
                $ips = explode(',', $ips);
                // Get all ips which are not used
                $usedIps = OrderProduct::where('product_id', $product-&gt;product-&gt;id)-&gt;where('status', '!=', 'terminated')-&gt;with('config')-&gt;get();
                $usedIps = $usedIps-&gt;map(function ($orderProduct) {
                    return $orderProduct-&gt;config-&gt;where('key', 'ips')-&gt;first()-&gt;value ?? false;
                })-&gt;toArray();
                $ips = array_diff($ips, $usedIps);
                if (empty($ips)) {
                    throw new Exception('No more IPs available');
                }                // Only one
                // 获取第一个可用 IP
                $ips = array_values($ips); // 重建索引
                $ips = $ips[0];
                ExtensionHelper::setOrderProductConfig('ips', $ips, $product-&gt;id);
                $postData['net0'] .= ',ip=' . $ips . '/24';
            }
            $gateway = isset($configurableOptions['gateway']) ? $configurableOptions['gateway'] : ($parmas['gateway'] ?? null);
            isset($gateway) ? $postData['net0'] .= ',gw=' . $gateway : null;
            isset($pool) ? $postData['pool'] = $pool : null;
            $response = $this-&gt;postRequest('/nodes/' . $node . '/lxc', $postData);
        } else {
            $socket = isset($configurableOptions['sockets']) ? $configurableOptions['sockets'] : ($parmas['sockets'] ?? null);
            $vcpu = isset($configurableOptions['vcpu']) ? $configurableOptions['vcpu'] : ($parmas['vcpu'] ?? null);
            $postData = [
                'vmid' =&gt; $vmid,
                'node' =&gt; $node,
                'storage' =&gt; $storage,
                'cores' =&gt; $cores,
                'memory' =&gt; $memory,
                'onboot' =&gt; 1,
                'sockets' =&gt; $socket ?? 1,
                'agent' =&gt; 1,
                'ostype' =&gt; $parmas['ostype'],
                'name' =&gt; $parmas['config']['hostname'],
                'description' =&gt; $parmas['config']['hostname'],
                $parmas['storageType'] . '0' =&gt; $parmas['storage'] . ':' . $disk . ',format=' . $parmas['storageFormat'],
                'net0' =&gt; $parmas['model'] . ',bridge=' . $parmas['bridge'] . ',' . (isset($parmas['firewall']) ? 'firewall=1' : 'firewall=0'),
            ];
            isset($pool) ? $postData['pool'] = $pool : null;
            isset($parmas['cloudinit']) ? $postData[$parmas['storageType'] . '0'] = $parmas['storage'] . ':cloudinit' . ',format=' . $parmas['storageFormat'] : null;
            isset($cpu) ? $postData['cpu'] = $cpu : null;
            isset($vcpu) ? $postData['vcpus'] = $vcpu : null;
            if (isset($parmas['os']) &amp;&amp; $parmas['os'] == 'iso') {
                $postData['ide2'] = $parmas['iso'] . ',media=cdrom';
            }
            $response = $this-&gt;postRequest('/nodes/' . $node . '/qemu', $postData);
        }
        if (!$response-&gt;json()) throw new Exception('Unable to create server' . $response-&gt;body());
        return true;
    }<i>
    

    ```
    这是更新后的代码。

    0
  13. James 💯 传奇 楼主 MANAGER 2025-01-02

    有几个点要注意,虽然Bridge是写在QEMU里边,但也还是要配置一下,小鸡是使用的vmbr1,所以选一下就行。
    Clone那里哪怕不用,也要随便写一个ID,不然会报错。
    Image description~~

    0
  14. James 💯 传奇 楼主 MANAGER 2025-01-02

    IPs这里就是这么写,虽然挺傻,但没有办法 。 Storage开小鸡选local就完了。Resource Pool 可以不选。

    Image description~~

    0
  15. James 💯 传奇 楼主 MANAGER 2025-01-02

    目前只有3节点,有时间看看能不能把其它NAT IP的节点也加上,有点麻烦。

    Image description~~

    0
  16. awq 黄金 不离不弃 2025-01-02

    劝退,下回在学习

    0
  17. amdyes 白银 2025-01-18

    看看

    0
  18. undefined 白银 2025-01-19

    学习

    0