差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

下次修改
前次修改
tech:ubuntu_lxd [2025/11/13 18:23] – 建立 jonathantech:ubuntu_lxd [2025/11/14 11:56] (目前版本) – [3. 如何啟動 Web UI] jonathan
行 138: 行 138:
  
 ===== 2. 將 proxmox 的 ct 匯出 ===== ===== 2. 將 proxmox 的 ct 匯出 =====
-  Exp. 將 proxmox 的 ct-118 使用 vzdump 產生的 vzdump-lxc-118-2025_11_13-14_03_41.tar 複製到 Ubuntu VM 內<cli>+  Exp. 將 proxmox 的 ct-118 使用 vzdump 產生的 vzdump-lxc-118-2025_11_13-14_03_41.tar 複製到 Ubuntu VM 內<cli>
 vzdump 118 --dumpdir /tmp vzdump 118 --dumpdir /tmp
 scp /tmp/vzdump-lxc-118-2025_11_13-14_03_41.tar [email protected]:/tmp scp /tmp/vzdump-lxc-118-2025_11_13-14_03_41.tar [email protected]:/tmp
 </cli> </cli>
  
 +===== 3. 建立 LXD image =====
 +  * 使用 root 權限執行<cli>
 +sudo -i
 +mkdir test
 +mv /tmp/vzdump-lxc-118-2025_11_13-14_03_41.tar test/
 +cd test
 +tar -xvf vzdump-lxc-118-2025_11_13-14_03_41.tar
 +ls -la
 +# (會看到一堆檔案)
 +
 +# 查看系統版本
 +cat etc/os-release
 +</cli> 
 +  * 將當前目錄的所有系統檔案打包成 rootfs<cli>
 +# 排除原始備份檔
 +tar --numeric-owner -czf rootfs.tar.gz \
 +  --exclude='vzdump-lxc-118-2025_11_13-14_03_41.tar' \
 +  --exclude='rootfs.tar.gz' \
 +  .
 +# 檢查打包結果
 +ls -lh rootfs.tar.gz
 +</cli>
 +  * LXD 需要 metadata 檔案來識別映像<cli>
 +cat > metadata.yaml << 'EOF'
 +architecture: x86_64
 +creation_date: 1699862400
 +properties:
 +  description: CentOS 7 from Proxmox CT-118
 +  os: centos
 +  release: "7"
 +EOF
 +
 +# 打包 metadata
 +tar -czf metadata.tar.gz metadata.yaml
 + 
 +# 確認兩個檔案都存在
 +ls -lh metadata.tar.gz rootfs.tar.gz
 +</cli>
 +
 +===== 4. 建立與啟動 LXD 容器  =====
 +  * 導入為 LXD 映像 Exp. ct-linsport <cli>
 +lxc image import metadata.tar.gz rootfs.tar.gz --alias ct-linsport
 +</cli>
 +  * 查看導入的映像<cli>
 +lxc image list
 +root@lin-web-210:~/test# lxc image list
 ++-------------+--------------+--------+------------------------------+--------------+-----------+------------+------------------------------+
 +|    ALIAS    | FINGERPRINT  | PUBLIC |         DESCRIPTION          | ARCHITECTURE |   TYPE    |    SIZE    |         UPLOAD DATE          |
 ++-------------+--------------+--------+------------------------------+--------------+-----------+------------+------------------------------+
 +| ct-linsport | 62085b012d6b | no     | CentOS 7 from Proxmox CT-118 | x86_64       | CONTAINER | 1633.91MiB | Nov 13, 2025 at 6:20am (UTC) |
 ++-------------+--------------+--------+------------------------------+--------------+-----------+------------+------------------------------+
 +</cli>
 +  * 從映像創建容器 Exp. ct-118 <cli>
 +lxc init ct-linsport ct-118
 +</cli>
 +  * 設定權限配置參數檔 ct-118-raw.conf 與相關權限設定<cli>
 +cat > ct-118-raw.conf << 'EOF'
 +# 放寬安全限制
 +lxc.apparmor.profile=unconfined
 +lxc.cap.drop=
 +
 +# 自動 mount:proc rw、sys mixed、cgroup mixed(關鍵:自動處理 v1)
 +lxc.mount.auto=proc:rw sys:mixed cgroup:mixed
 +
 +# 手動 bind v1 控制器(僅 systemd 作為補充,optional)
 +lxc.mount.entry=/sys/fs/cgroup/systemd /sys/fs/cgroup/systemd none bind,create=dir,optional 0 0
 +EOF
 +
 +# 讀取並設定
 +lxc config set ct-118 raw.lxc "$(cat ct-118-raw.conf)"
 +
 +# 設定特權模式
 +lxc config set ct-118 security.privileged true
 +lxc config set ct-118 security.nesting true
 +</cli>
 +  * 設定容器可以對外連結虛擬網卡<cli>
 +# Override eth0 為獨立 nic type(移除 profile 影響)
 +lxc config device override ct-118 eth0 type=nic
 +# 移除 eth0(現在可移除,因為已 override)
 +lxc config device remove ct-118 eth0
 +# 新增全新 eth0 nic:macvlan 模式,綁 ens18
 +lxc config device add ct-118 eth0 nic nictype=bridged parent=br0 name=eth0
 +</cli>
 +  * 啟動容器<cli>
 +# 啟動容器
 +lxc start ct-118
 +
 +# 查看容器狀態
 +lxc list
 +</cli>
 +
 +===== 5. 進入容器測試  =====
 +  * <cli>
 +# 進入容器
 +lxc exec ct-118 -- bash
 +
 +# 檢查系統
 +cat /etc/redhat-release
 +hostname
 +ip addr
 +systemctl status
 +
 +# 退出
 +exit
 +</cli>
 +
 +===== FAQ =====
 +==== 1. 發現無法自動啟動服務 (systemctl 權限問題) ====
 +  * Ubuntu 24.04 host 要先設定好 mount-cgroup-v1.sh / umount-cgroup-v1.sh / cgroup-v1-legacy.service 這些環境配置
 +  * 重建容器與設定正確權限<cli>
 +# 停止容器(如果卡住)
 +lxc stop ct-118 --force || true
 +
 +# 刪除容器 (保留映像)
 +lxc delete ct-118
 +
 +# 重新創建
 +lxc init ct-linsport ct-118
 +
 +# 創建設定檔案(完整的 systemd 支援配置)
 +cat > ct-118-raw.conf << 'EOF'
 +# 放寬安全限制
 +lxc.apparmor.profile=unconfined
 +lxc.cap.drop=
 +
 +# 自動 mount:proc rw、sys mixed、cgroup mixed(關鍵:自動處理 v1)
 +lxc.mount.auto=proc:rw sys:mixed cgroup:mixed
 +
 +# 手動 bind v1 控制器(僅 systemd 作為補充,optional)
 +lxc.mount.entry=/sys/fs/cgroup/systemd /sys/fs/cgroup/systemd none bind,create=dir,optional 0 0
 +EOF
 +
 +# 讀取並設定
 +lxc config set ct-118 raw.lxc "$(cat ct-118-raw.conf)"
 +
 +# 設定特權模式
 +lxc config set ct-118 security.privileged true
 +lxc config set ct-118 security.nesting true
 +
 +# 啟動容器
 +lxc start ct-118
 +
 +# 檢查容器狀態
 +lxc info ct-118
 +
 +# 查看啟動日誌
 +lxc info ct-118 --show-log | tail -50
 +
 +# 等待幾秒讓系統啟動
 +sleep 5
 +
 +# 檢查狀態
 +lxc info ct-118 | grep Processes
 +
 +# 查看容器狀態
 +lxc list
 +</cli>
 +
 +==== 2. 發現容器無法對外連結 =====
 +  * 要在 Ubuntu 24.04 Host 建立可以對外網卡的 Bridge Exp. br0
 +  * 將原本容器的網卡刪除更換成這 br0 Exp.<cli>
 +# Override eth0 為獨立 nic type(移除 profile 影響)
 +lxc config device override ct-118 eth0 type=nic
 +# 移除 eth0(現在可移除,因為已 override)
 +lxc config device remove ct-118 eth0
 +# 新增全新 eth0 nic:macvlan 模式,綁 ens18
 +lxc config device add ct-118 eth0 nic nictype=bridged parent=br0 name=eth0
 +</cli>
 +
 +==== 3. 如何啟動 Web UI =====
 +  * 設定 config <cli>
 +root@lin-web-210:~# lxd --version
 +5.21.4 LTS
 +root@lin-web-210:~# lxc config set core.https_address [::]
 +root@lin-web-210:~# lxc config set core.https_address 0.0.0.0:8443
 +root@lin-web-210:~# lxc config show
 +config:
 +  core.https_address: 0.0.0.0:8443
 +root@lin-web-210:~# lxc config trust list
 ++------+------+-------------+-------------+------------+-------------+
 +| TYPE | NAME | COMMON NAME | FINGERPRINT | ISSUE DATE | EXPIRY DATE |
 ++------+------+-------------+-------------+------------+-------------+
 +</cli>
 +  * 因為 LXD Web UI 是採用雙向 SSL 認證方式, 所以第一次進入網址要產生與下載憑證 Exp. https://192.168.1.210:8443
 +    - 瀏覽器會彈出選擇憑證, 如果沒有憑證或取消就會進入 Step 1. TLS login 設定畫面 \\ {{:tech:螢幕擷取畫面_2025-11-14_103428.png?1000|}}
 +    - 下載憑證( Exp. lxd-ui-192.168.1.210.pfx )後匯入讓瀏覽器可以使用
 +    - 開啟瀏覽器無痕模式, 彈出選擇憑證時, 選擇剛剛匯入的憑證 Exp. \\ {{:tech:螢幕擷取畫面_2025-11-14_104711.png|}}
 +    - 看到網頁 Step 2. 設定畫面
 +    - 回到 LXD Server 執行 <cli>lxc auth identity create tls/lxd-ui --group admins</cli>產生 identity token Exp. <cli>
 +root@lin-web-210:~# lxc auth identity create tls/lxd-ui --group admins
 +TLS identity "tls/lxd-ui" (4fd2xxxx-xxxx-xxxx-xxxx-xxxxxxxx88bc) pending identity token:
 +eyJjbGllbnxxxxxxxxxxxxxxxxxxxxIsImZpbmdlcnByaW5xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxmI2NSIsImFkZHJlc3NlcyI6WyIxOTIuMTY4LjExLxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx0aWZpY2F0ZSJ9
 +</cli>
 +    - 將產生的 identity token 貼上 Step 2. \\ {{:tech:螢幕擷取畫面_2025-11-14_104936.png?1000|}}
 +    - 點下 Connet 後就會進入 Web UI \\ {{:tech:螢幕擷取畫面_2025-11-14_105240.png?1000|}} \\ {{:tech:螢幕擷取畫面_2025-11-14_105422.png?1000|}}
 +    * 如果需要設定透過反向代理 Exp. NPM 原本雙向 SSL 認證就無法順利傳遞, 必須要增加設定 OIDC (Exp. Google oAuth 認證) 方式來達成, 可參考 - [[tech/ubuntu_lxd/oidc]]
 +
 +{{tag>pve ct lxd}}
  • tech/ubuntu_lxd.1763029391.txt.gz
  • 上一次變更: 2025/11/13 18:23
  • jonathan