在 Ubuntu 的 k3s 上安裝並登入 kubectl
在 Ubuntu 伺服器安裝 Zeabur Identity、設定 HTTPS 與管理員帳號,再從你的電腦登入 k3s 叢集。
在 Ubuntu 伺服器安裝 Zeabur Identity,設定 HTTPS 和第一個管理員帳號後,你就能從自己的電腦登入 k3s 叢集,執行 kubectl。伺服器使用 k3s 內建的 Traefik 處理 Ingress。
把叢集的管理員 kubeconfig 留在伺服器上,用來安裝元件,或在 Zeabur Identity 無法運作時救援。另外存一份加密副本,離線保管。你的電腦只取得叢集的 CA 憑證,以及使用 Zeabur Identity 登入的 kubeconfig。
Your computer server (k3s) browser ──https://identity.example.test──▶ Traefik :443 ──▶ zeabur-identity (zeabur-identity) kubectl ──https://identity.example.test:6443──▶ kube-apiserver │ verifies tokens itself; discovery and keys └─▶ https://<zeabur-identity ClusterIP> (internal TLS)準備兩台機器:
- 你的電腦:執行瀏覽器和
kubectl,可使用 macOS、Linux 或 Windows。以下指令使用 POSIX shell,Windows 請使用 WSL。 - 伺服器:執行 k3s 和 Zeabur Identity。你可以使用 VPS、家用實驗室主機、備用電腦或本機 VM,只要你的電腦能從網路連到這台 Ubuntu 機器即可。
在你的電腦準備好:
kubectl。- kubelogin:用來開啟瀏覽器登入的
kubectl外掛。可用 Homebrew 的brew install int128/kubelogin/kubelogin、Nix 的nixpkgs#kubelogin-oidc,或先安裝 krew,再執行kubectl krew install oidc-login。 - mkcert:在執行瀏覽器的環境執行一次
mkcert -install,讓該環境信任本機 CA。指令會視需要要求你輸入密碼。Firefox 另需 NSS 的certutil,詳見 mkcert 的 README。使用 WSL 時,瀏覽器在 Windows 執行,因此要安裝 Windows 版,例如winget install FiloSottile.mkcert。這裡和步驟 4 都要從 WSL 呼叫mkcert.exe。 - 能以
ssh連到伺服器。
伺服器需要:
- Ubuntu 24.04 或更新版本,架構為
amd64或arm64。 - 4 個 CPU、8 GiB 記憶體及 30 GiB 可用磁碟空間。
curl和 cosign 3.x。- 允許你的電腦連到 TCP 連接埠 443(登入)和 6443(Kubernetes API)。
請使用備用機器或 VM。以下操作會安裝 k3s 並修改設定。
範例使用下列名稱,操作時請換成你的值:
| 名稱 | 範例值 |
|---|---|
| 伺服器主機名稱 | identity.example.test |
| Issuer(Zeabur Identity 的公開網址) | https://identity.example.test |
| 伺服器 SSH 登入帳號 | ubuntu@identity.example.test,使用者需有 sudo 權限 |
你的電腦必須能用這個主機名稱連到伺服器。使用自己的網域時,請新增 DNS 記錄。.test 名稱無法在網際網路上解析,需要改在 hosts 檔案新增記錄:Linux 和 macOS 使用 /etc/hosts,Windows 使用 C:\Windows\System32\drivers\etc\hosts。
WSL 啟動時會複製 Windows 的 hosts 檔案。修改後,請在 Windows 執行 wsl --shutdown,再開啟新的 WSL 終端機。
保持兩個終端機開啟:一個在你的電腦,另一個登入伺服器。各步驟會標明要在哪裡操作。在你電腦的終端機設定 SSH 登入帳號,供後續指令使用:
SERVER=ubuntu@identity.example.test下載並驗證 Zeabur Identity
Section titled “下載並驗證 Zeabur Identity”從你的電腦登入伺服器:
ssh "$SERVER"以下指令會使用 GitHub Releases 上的最新版本,並印出版本號,例如 v0.2.0。要固定其他版本,請自行設定 version。x86_64 的 arch 設為 amd64,aarch64 設為 arm64:
version=$(curl -fsSLo /dev/null -w '%{url_effective}' https://github.com/zeabur-oss/identity/releases/latest)version=${version##*/}echo "$version"arch=arm64mkdir -p ~/zeabur-identity-download下載 CLI、附有簽章的校驗碼及映像檔摘要。以下指令會先驗證檔案,再安裝 zeabur-identityctl:
( set -eu cd ~/zeabur-identity-download url="https://github.com/zeabur-oss/identity/releases/download/$version" archive="zeabur-identityctl-$version-linux-$arch.tar.gz" for file in "$archive" SHA256SUMS SHA256SUMS.sigstore.json digests.txt; do curl --fail --location "$url/$file" --output "$file" done identity='^https://github\.com/zeabur-oss/(identity|\.github)/\.github/workflows/release\.yml@' cosign verify-blob --bundle SHA256SUMS.sigstore.json \ --certificate-identity-regexp "$identity" \ --certificate-github-workflow-repository zeabur-oss/identity \ --certificate-github-workflow-ref "refs/tags/$version" \ --certificate-oidc-issuer https://token.actions.githubusercontent.com SHA256SUMS sha256sum --check --strict --ignore-missing SHA256SUMS tar -xzf "$archive" sudo install -m 0755 zeabur-identityctl /usr/local/bin/zeabur-identityctl zeabur-identityctl --version)最後一行會印出不含 v 的版本號,例如 zeabur-identityctl 0.2.0。任何一項檢查失敗,指令都會在這行之前停止。
接著驗證映像檔和 SBOM,也就是映像檔內的相依套件清單。ZEABUR_IDENTITY_IMAGE 會儲存這個版本的確切映像檔摘要,安裝時會用到:
ZEABUR_IDENTITY_IMAGE=$(awk '$1 == "index" {print $2}' ~/zeabur-identity-download/digests.txt)export ZEABUR_IDENTITY_IMAGEidentity='^https://github\.com/zeabur-oss/(identity|\.github)/\.github/workflows/release\.yml@'cosign verify --certificate-identity-regexp "$identity" \ --certificate-github-workflow-repository zeabur-oss/identity \ --certificate-github-workflow-ref "refs/tags/$version" \ --certificate-oidc-issuer https://token.actions.githubusercontent.com "$ZEABUR_IDENTITY_IMAGE"cosign verify-attestation --type cyclonedx --certificate-identity-regexp "$identity" \ --certificate-github-workflow-repository zeabur-oss/identity \ --certificate-github-workflow-ref "refs/tags/$version" \ --certificate-oidc-issuer https://token.actions.githubusercontent.com "$ZEABUR_IDENTITY_IMAGE"兩個驗證指令都成功後再繼續。支援的平台與其他發行檔見發行檔與驗證。
安裝 k3s
Section titled “安裝 k3s”在伺服器執行以下指令,把主機名稱加入 API 伺服器憑證,並建立空的驗證設定檔。步驟 5 會填入 Zeabur Identity 的設定。
sudo mkdir -p /etc/rancher/k3s/zeabur-identity /etc/rancher/k3s/config.yaml.dsudo tee /etc/rancher/k3s/zeabur-identity/authn.yaml >/dev/null <<'EOF'apiVersion: apiserver.config.k8s.io/v1kind: AuthenticationConfigurationanonymous: enabled: falsejwt: []EOFsudo tee /etc/rancher/k3s/config.yaml >/dev/null <<'EOF'# The name your computer uses for the API server.tls-san: [identity.example.test]EOFsudo tee /etc/rancher/k3s/config.yaml.d/50-zeabur-identity.yaml >/dev/null <<'EOF'kube-apiserver-arg: - "authentication-config=/etc/rancher/k3s/zeabur-identity/authn.yaml"EOFcurl -sfL https://get.k3s.io | sudo INSTALL_K3S_VERSION=v1.37.2+k3s1 sh -
install -d -m 700 ~/.kubesudo install -o "$USER" -m 600 /etc/rancher/k3s/k3s.yaml ~/.kube/configexport KUBECONFIG="$HOME/.kube/config"~/.kube/config 是具備 cluster-admin 權限的憑證。只用它安裝元件,或在 Zeabur Identity 停止服務時救援,不要用於日常操作。把它留在伺服器,並依保留不經過 Zeabur Identity 的入口另存加密副本,離線保管。
確認節點和 k3s 內建的 Ingress 已就緒:
kubectl wait --for=condition=Ready node --all --timeout=180skubectl -n kube-system wait --for=create deployment/traefik --timeout=180skubectl -n kube-system rollout status deployment/traefik --timeout=180s安裝 cert-manager。步驟 5 會用它簽發及更新憑證,供 kube-apiserver 連到 Zeabur Identity 時使用:
kubectl apply --server-side -f https://github.com/cert-manager/cert-manager/releases/download/v1.21.3/cert-manager.yamlkubectl -n cert-manager wait deployment --all --for=condition=Available --timeout=300s安裝 Zeabur Identity
Section titled “安裝 Zeabur Identity”在同一個伺服器 shell 執行:
export ZEABUR_IDENTITY_ISSUER=https://identity.example.testzeabur-identityctl crds | kubectl apply --server-side -f -kubectl wait --for=condition=Established --timeout=90s \ crd/users.identity.zeabur.com crd/credentials.identity.zeabur.com \ crd/oauthclients.identity.zeabur.com crd/grants.identity.zeabur.com \ crd/invitations.identity.zeabur.comzeabur-identityctl manifests --image "$ZEABUR_IDENTITY_IMAGE" \ --serve-arg=--invitations | kubectl apply --server-side -f -kubectl -n zeabur-identity rollout status deploy/zeabur-identity --timeout=180s最後一個指令會印出 deployment "zeabur-identity" successfully rolled out。
產生的資源清單包含 RBAC、Service 和 Deployment。Zeabur Identity 管理的資源都放在 zeabur-identity Namespace。--invitations 會啟用邀請使用者的連結。
請繼續使用這個伺服器 shell。如果開啟新的 shell,先重新設定以下環境變數:
export KUBECONFIG="$HOME/.kube/config"export ZEABUR_IDENTITY_ISSUER=https://identity.example.testZEABUR_IDENTITY_IMAGE=$(kubectl -n zeabur-identity get deployment zeabur-identity -o jsonpath='{.spec.template.spec.containers[0].image}')export ZEABUR_IDENTITY_IMAGE用 HTTPS 公開登入頁面
Section titled “用 HTTPS 公開登入頁面”瀏覽器需要受信任的 HTTPS 連線才能建立通行密鑰。這裡使用 mkcert,供你的測試電腦使用。公開服務應改用公開 CA 簽發的憑證,例如使用 cert-manager。
在你的電腦上,用 mkcert 簽發憑證,主機名稱填入 Issuer 的主機名稱,再把憑證複製到伺服器。只複製憑證和對應的私鑰,CA 私鑰要留在你的電腦。使用 WSL 時,將 mkcert 改成 mkcert.exe。
mkcert -cert-file zeabur-identity.crt -key-file zeabur-identity.key identity.example.testssh "$SERVER" 'install -d -m 700 ~/zeabur-identity-tls'scp zeabur-identity.crt zeabur-identity.key "$SERVER":zeabur-identity-tls/rm zeabur-identity.key在伺服器把憑證存成 Secret,並建立 Ingress。k3s 的 Traefik 會在連接埠 443 接收 HTTPS,再把請求轉送到 Zeabur Identity:
kubectl -n zeabur-identity create secret tls zeabur-identity-tls \ --cert=$HOME/zeabur-identity-tls/zeabur-identity.crt --key=$HOME/zeabur-identity-tls/zeabur-identity.keykubectl apply -f - <<'EOF'apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: zeabur-identity namespace: zeabur-identity annotations: traefik.ingress.kubernetes.io/router.entrypoints: websecure traefik.ingress.kubernetes.io/router.tls: "true"spec: ingressClassName: traefik tls: - hosts: [identity.example.test] secretName: zeabur-identity-tls rules: - host: identity.example.test http: paths: - path: / pathType: Prefix backend: service: name: zeabur-identity port: {number: 80}EOF等幾秒,讓 Traefik 載入路由與憑證,再從你的電腦檢查。回傳的 JSON 文件中,issuer 應為 https://identity.example.test:
curl --cacert "$(mkcert -CAROOT)/rootCA.pem" \ https://identity.example.test/.well-known/openid-configuration在 WSL 使用 Windows 版 mkcert 時,mkcert -CAROOT 會輸出 Windows 路徑,需要先轉換:
curl --cacert "$(wslpath "$(mkcert.exe -CAROOT | tr -d '\r')")/rootCA.pem" \ https://identity.example.test/.well-known/openid-configuration公開服務前,限制登入請求頻率
Section titled “公開服務前,限制登入請求頻率”公開伺服器應依用戶端 IP 位址限制登入端點的請求頻率。如果伺服器只有你能連到,可以略過這個小節。
在伺服器把 Traefik 的 externalTrafficPolicy 設為 Local,讓 k3s 的負載平衡器保留訪客位址。這樣就能分別計算每個位址的請求次數:
kubectl apply -f - <<'EOF'apiVersion: helm.cattle.io/v1kind: HelmChartConfigmetadata: {name: traefik, namespace: kube-system}spec: valuesContent: |- service: spec: externalTrafficPolicy: LocalEOF這裡採用 k3s 的 Traefik 設定方式。多節點叢集使用 Local 時,只有執行 Traefik Pod 的節點會接收流量。
接著替登入路由加入限流設定:
kubectl apply -f - <<'EOF'apiVersion: traefik.io/v1alpha1kind: Middlewaremetadata: {name: zeabur-identity-rate-limit, namespace: zeabur-identity}spec: rateLimit: average: 10 period: 1s burst: 20---apiVersion: networking.k8s.io/v1kind: Ingressmetadata: name: zeabur-identity-rate-limited namespace: zeabur-identity annotations: traefik.ingress.kubernetes.io/router.entrypoints: websecure traefik.ingress.kubernetes.io/router.tls: "true" traefik.ingress.kubernetes.io/router.middlewares: zeabur-identity-zeabur-identity-rate-limit@kubernetescrdspec: ingressClassName: traefik tls: - hosts: [identity.example.test] secretName: zeabur-identity-tls rules: - host: identity.example.test http: paths: - {path: /authorize, pathType: Exact, backend: {service: {name: zeabur-identity, port: {number: 80}}}} - {path: /token, pathType: Exact, backend: {service: {name: zeabur-identity, port: {number: 80}}}} - {path: /par, pathType: Exact, backend: {service: {name: zeabur-identity, port: {number: 80}}}} - {path: /revoke, pathType: Exact, backend: {service: {name: zeabur-identity, port: {number: 80}}}} - {path: /login, pathType: Prefix, backend: {service: {name: zeabur-identity, port: {number: 80}}}} - {path: /setup, pathType: Prefix, backend: {service: {name: zeabur-identity, port: {number: 80}}}} - {path: /upstream, pathType: Prefix, backend: {service: {name: zeabur-identity, port: {number: 80}}}}EOF從你的電腦同時送出 60 個請求,檢查限流結果。預期約 20 個回傳 200,其餘回傳 429 Too Many Requests:
ca="$(mkcert -CAROOT)/rootCA.pem"for i in $(seq 1 60); do curl -s -o /dev/null -w '%{http_code}\n' --cacert "$ca" https://identity.example.test/login &done | sort | uniq -c這些數值是起始設定,需要依使用情境調整:
- 同一個 NAT 後方的使用者共用 IP 位址,也共用限額。如果辦公室成員每天早上同時登入,請調高限額。
- 如果 Traefik 前方還有其他代理伺服器,只信任該代理伺服器送來的轉送標頭,否則所有人會共用同一個限額,或有人能偽造標頭繞過限制。
詳細設定見 Traefik 的 RateLimit 選項。
設定 kube-apiserver 信任 Zeabur Identity
Section titled “設定 kube-apiserver 信任 Zeabur Identity”先確認你能不經過 Zeabur Identity 存取叢集。在伺服器檢查步驟 2 的管理員 kubeconfig:
sudo k3s kubectl --kubeconfig=/etc/rancher/k3s/k3s.yaml auth whoamisudo k3s kubectl --kubeconfig=/etc/rancher/k3s/k3s.yaml get --raw=/readyz任一指令失敗就先停止,修好後再繼續。如果還沒另存離線加密副本,現在先完成備份。
kube-apiserver 會自行驗證 Zeabur Identity 的權杖,並用內部 HTTPS 連線取得簽章金鑰。zeabur-identityctl kube-config 會請 cert-manager 簽發這條連線使用的憑證,再輸出 kube-apiserver 所需的設定。指令也會輸出 k3s 的設定指示,包括重新啟動一次;步驟 2 已經做完這部分,可以略過。
kube-apiserver 約每分鐘檢查一次設定檔。比較修改前後成功重新載入的次數,就能確認新設定是否生效。在伺服器 shell 定義以下兩個輔助函式:
# Successful reloads of the authentication configuration so far.reloads() { kubectl get --raw /metrics | awk '/^apiserver_authentication_config_controller_automatic_reloads_total/ && /status="success"/ { n += $NF } END { print n + 0 }'}# Waits up to three minutes for the count to pass $1; prints the k3s log if it never does.wait_for_reload() { for _ in $(seq 1 36); do [ "$(reloads)" -gt "$1" ] && return 0 sleep 5 done echo "Authentication configuration did not reload; inspect k3s logs." >&2 sudo journalctl -u k3s --since "5 minutes ago" --no-pager return 1}把新設定寫入暫存檔,再一次移到正式位置,避免 kube-apiserver 讀到尚未寫完的檔案。接著等待重新載入:
before=$(reloads)zeabur-identityctl kube-config --distro k3s > ~/authn.yaml && sudo install -m 600 ~/authn.yaml /etc/rancher/k3s/zeabur-identity/.authn.yaml.tmp && sudo mv /etc/rancher/k3s/zeabur-identity/.authn.yaml.tmp /etc/rancher/k3s/zeabur-identity/authn.yaml && wait_for_reload "$before"註冊 kubectl 並設定第一位管理員
Section titled “註冊 kubectl 並設定第一位管理員”使用 Zeabur Identity 登入的應用程式都要註冊為用戶端。在伺服器註冊 kubectl,讓 kubelogin 在 127.0.0.1 接收登入結果:
zeabur-identityctl client create kubectl --type public \ --redirect-uri http://127.0.0.1:8000/ --audience kubernetes | kubectl apply -f -取得第一個管理員的一次性設定連結:
zeabur-identityctl setup-link --username admin這會建立 admin 使用者,帳號類型為管理員帳號。管理員帳號預設用通行密鑰登入,或用密碼加上驗證器 App 的驗證碼登入,並取得 Kubernetes 的完整權限 cluster-admin。詳見管理員帳號與工作階段。
回到你的電腦,用瀏覽器開啟指令顯示的連結,選擇建立通行密鑰。沒有通行密鑰時,改選使用密碼,設定至少 15 個字元的密碼,用驗證器 App 掃描 QR code、輸入驗證碼,再保存 10 組備援碼。連結只能使用一次,60 分鐘後就會到期。需要新連結時,再執行一次指令。
在伺服器確認管理員已有登入方式。輸出會列出一個 Credential,USER 欄是 admin:
kubectl -n zeabur-identity get credentials設定你電腦上的 kubectl
Section titled “設定你電腦上的 kubectl”在你的電腦執行以下指令,從伺服器複製叢集的 CA 憑證。
CA=$(ssh "$SERVER" "grep certificate-authority-data ~/.kube/config | awk '{print \$2}'")接著寫入 kubeconfig。登入後,工作階段結束前 kubelogin 都會用更新權杖自動換發權杖:
mkdir -p ~/.kubecat > ~/.kube/zeabur-identity-k3s.yaml <<EOFapiVersion: v1kind: Configclusters:- name: zeabur-identity-k3s cluster: server: https://identity.example.test:6443 certificate-authority-data: $CAusers:- name: passkey user: exec: apiVersion: client.authentication.k8s.io/v1 interactiveMode: IfAvailable command: kubectl args: [oidc-login, get-token, --oidc-issuer-url=https://identity.example.test, --oidc-client-id=kubectl, --oidc-use-access-token, --listen-address=127.0.0.1:8000, --oidc-redirect-url=http://127.0.0.1:8000/]contexts:- {name: zeabur-identity-k3s, context: {cluster: zeabur-identity-k3s, user: passkey}}current-context: zeabur-identity-k3sEOFKrew 和 Nix 會把 kubelogin 安裝成 kubectl 外掛,可直接使用這份設定。使用 Homebrew 安裝時,改成 command: kubelogin,並從 args 移除 oidc-login。
使用 WSL 時,kubelogin 在 Linux 內執行,無法取得 Windows 版 mkcert 信任的 CA。請使用步驟 4 檢查時的路徑,把 CA 加入 args:
wslpath "$(mkcert.exe -CAROOT | tr -d '\r')" # e.g. /mnt/c/Users/you/AppData/Local/mkcert --certificate-authority=/mnt/c/Users/you/AppData/Local/mkcert/rootCA.pem,kubelogin 也無法從 WSL 開啟 Windows 瀏覽器,會改為顯示 Please visit the following URL in your browser manually: http://127.0.0.1:8000/。在瀏覽器開啟該位址,即可繼續登入。
保留 --oidc-use-access-token。kube-apiserver 只接受 Zeabur Identity 的存取權杖,不接受 ID 權杖。
kubelogin 預設把更新權杖存放在 ~/.kube/cache/oidc-login。加入 --token-cache-storage=keyring 可改存系統鑰匙圈。使用 --token-cache-storage=none 時,kubelogin 不儲存權杖,每次執行 kubectl 都會開啟登入頁面。
登入並測試權限
Section titled “登入並測試權限”在你的電腦執行以下指令,登入後查詢身分、列出 Pod,並測試建立與刪除 ConfigMap:
export KUBECONFIG=~/.kube/zeabur-identity-k3s.yamlkubectl auth whoami # opens the browser; user zeabur:admin, group zeabur:adminskubectl get pods -A # allowed: zeabur:admins is bound to cluster-adminkubectl -n default create configmap zeabur-identity-permission-check --from-literal=check=okkubectl -n default delete configmap zeabur-identity-permission-check- 管理員權杖會自動更新:權杖有效期為 5 分鐘,更新時不需要你操作。首次登入 12 小時後,或停止使用
kubectl達 1 小時後,需要重新登入。多團隊共用叢集時,閒置期限為 15 分鐘。一般帳號的權杖有效期為 10 分鐘。詳見管理員帳號與工作階段。 - 管理員帳號只用於管理工作:日常操作請邀請自己建立一般帳號,再依需求替群組綁定權限。詳見管理使用者。
- 使用者、登入方式和工作階段都是 Kubernetes 物件:都放在
zeabur-identityNamespace。通行密鑰或密碼是Credential,登入後的工作階段是Grant。在伺服器執行kubectl -n zeabur-identity get users,credentials,grants可列出這些物件,操作方式見管理使用者。
接著設定備份與還原、升級與回滾,並保留不經過 Zeabur Identity 的入口。日常使用可從管理使用者開始,加入 GitHub、Google 或自家的 OpenID 服務作為登入方式,也能註冊用戶端,讓 Grafana 或 Argo CD 等應用程式使用 Zeabur Identity 登入。
| 畫面或結果 | 接著處理 |
|---|---|
| 瀏覽器顯示憑證錯誤,或沒有通行密鑰選項 | 在執行瀏覽器的環境執行 mkcert -install,確認該環境信任 mkcert 的 CA。檢查憑證名稱是否符合 Issuer 的主機名稱。 |
kubectl 顯示 Unauthorized | 檢查步驟 5 的設定是否成功載入。設定檔有錯時,kube-apiserver 會忽略這個檔案,檔案再次變更後才會重新讀取。在 k3s 伺服器執行 sudo journalctl -u k3s | grep -i "authentication config" 檢視原因。 |
| 連接埠 8000 已占用 | kubelogin 會用這個連接埠接收瀏覽器重新導向的請求。請同時修改 --listen-address 和 --oidc-redirect-url,改用其他連接埠。已註冊的重新導向 URI 在 127.0.0.1 上可以用任何連接埠。 |
在伺服器解除安裝 k3s。這會移除整個叢集、Zeabur Identity,以及叢集內安裝的所有其他資源:
sudo /usr/local/bin/k3s-uninstall.shrm -rf ~/zeabur-identity-tls ~/authn.yaml ~/zeabur-identity-download接著在你的電腦執行:
rm ~/.kube/zeabur-identity-k3s.yamlrm -rf ~/.kube/cache/oidc-login