Proxmox LXC設定

LXC是輕量級的容器,Proxmox 直接提供了這功能,等於是可以拿 LXC 來當 VM,這樣可以更壓榨硬體的效能出來。

這邊主要是紀錄用的時候遇到的一些狀況:

  1. Tailscale
  2. podman

Tailscale

因為我 Proxmox 網路設定是用NAT,再加上沒有第二張網卡,所以就簡單用 Tailscale 讓外部可以連到 Proxmox 裡面的 LXC 跟 VM。VM 用 tailscale 沒什麼問題,但 LXC 就遇到不能用的情況。

這邊要在 lxc 容器設定 (Proxmox 主機裡的 /etc/pve/lxc/<id>.conf 裡加上以下內容

lxc.cgroup2.devices.allow: c 10:200 rwm
lxc.mount.entry: /dev/net/tun dev/net/tun none bind,create=file

podman

podman 本身就容器,等於是容器裡的容器,這邊要處理 rootless 的情況,一樣,在 lxc 容器設定 (Proxmox 主機裡的 /etc/pve/lxc/<id>.conf 加上以下內容

lxc.idmap: u 0 100000 165536
lxc.idmap: g 0 100000 165536

還有 /etc/subuid

root:100000:200000

/etc/subgid

root:100000:200000

參考資料

使用systemctl 啟用podman出現Failed to connect to bus

在使用 sudo/su 切換身份以後,用以下指令去啟用/啟動 podman 服務,會出現以下錯誤

Failed to connect to bus: No such file or directory

查了以後,才知道是少了 DBUS 環境變數的問題:https://stackoverflow.com/questions/73814619/permission-denied-trying-to-use-rootless-podman-docker-compose-traefik-with

這個時候,只要查到 DBUS 的 socket 路徑,再去 export DBUS 環境變數即可

export DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
$ systemctl --user enable --now podman

啟用以後,會有 podman.sock 檔案,這個檔案的位置跟 root 身份的位置不一樣,會是在

/run/user/$UID/podman/podman.sock

這邊是要特別注意的。

如何使用podman volume掛載nfs分享

資料來源:Mount an NFS Share Using a Rootful Podman Volume (oracle.com)

在建立 volume 時,就可以指定 nfs 的資訊

sudo podman volume create --opt type=nfs --opt o=rw --opt device=10.0.0.150:/nfs-share nfsvol

在建立完成後,podman 會去掛載 nfs 分享,這部份可以使用 mount 查看到掛載到哪個資料夾。接下來 podman 就可以使用 -v 參數,去指定要使用剛剛建立的 volume ,容器啟動後,就可以使用了。

例如

sudo podman run -v nfsvol:/foo -it --rm oraclelinux:9 ls -al /foo

這就表示把建立好的 nfsvol ,掛載到容器裡面的 /foo ,應用程式就可以存取到了。

hadolint

名字裡有 lint 的,通常表示這是個用來檢查語法的工具。所以 hadolint 是個檢查用的工具,主要是用來檢查 Dockerfile 。

安裝方法

hadolint 的原始碼放在 github 上,可以直接透過這個網址去找最新的編譯好的版本來下載:https://github.com/hadolint/hadolint/tags

下載以後,變更權限,然後搬到 /usr/local/bin ,就可以使用了。

chmod +x hadolint-Linux-x86_64
mv hadolint-Linux-x86_64 /usr/local/bin/hadolint

使用

使用很簡單,帶入 Dockerfile 即可。

hadolint Dockerfile

執行以後,會出現 hadolint 覺得有問題的地方,這時候就可以參考 hadolint rules (要往下捲,找到 Rules) 的說明去修改 Dockerfile。修改完可以再重新執行檢查。

其他使用方式

除了可以下載執行檔以外,也可以透過 container 方式來運行,簡單的說就是 hadolint 工具被包在 Container image 裡,那用 docker run 或 podman run 就可以檢查。

docker pull hadolint/hadolint
docker pull hadolint/hadolint:v2.12.0-debian
docker pull hadolint/hadolint:v2.12.0-alpine

docker run --rm -i hadolint/hadolint < Dockerfile
docker run --rm -i hadolint/hadolint:v2.12.0-debian < Dockerfile
docker run --rm -i hadolint/hadolint:v2.12.0-alpine < Dockerfile

最後一種方式,是搭配 Visual Studio Code (vscode) 的 extension,如果你剛好是把 Visual Studio Code 當作主力的話,這很方便。從 Visual Studio Code 的 marketplace 上找 hadolint ,就可以安裝。安裝以後,在編輯 Dockerfile 時,下方就會出現檢查結果,邊修改邊檢查。

結論

Dockerfile 的檢查可以導入 CI 的流程裡,一方面是可以讓 Dockerfile 標準化,一方面透過這些規則,也可以縮小 Container image 的大小,加快 docker pull 的速度。

參考資料

podman如何使用registry mirror

之前弄好 registry mirror,因為是用 docker,所以需要調整的設定檔是 /etc/docker/daemon.json

那在 podman 裡,該怎麼設定呢?

這部份可以參考 /etc/containers/registries.conf 裡面的說明,然後在 /etc/containers/registries.conf.d 資料夾裏面加上新的設定檔就可以。

例如可以在 /etc/containers/registries.conf.d/my-mirror.conf 加入以下內容

[[registry]]
location = "docker.io"

[[registry.mirror]]
location = "192.168.11.2:6000"
insecure = true

依照行號說明如下:

  1. [[registry]] 必須要加
  2. location = "docker.io" 是表示,遇到 docker.io 時,就要參考這裡的設定。
  3. [[registry.mirror]] 這是表示下面要寫 registry mirror 設定了。
  4. location = "192.168.11.2:6000" 是表示 registry mirror 的位置。
  5. insecure = true 是表示不要檢查這座 registry mirror 的 SSL 憑證。

設定完以後,在 podman pull docker.io/<image> 的時候,實際就會到 registry mirror 拉取 容器映像 ,由 registry mirror 再去決定是否到外面的 docker.io 拉取容器映像。

參考資料