配置訪問多個叢集

本頁面展示瞭如何使用配置檔案來配置對多個叢集的訪問。在你的叢集、使用者和上下文定義在一個或多個配置檔案中後,你可以使用 kubectl config use-context 命令快速切換叢集。

準備工作

你需要有一個 Kubernetes 叢集,並且 kubectl 命令列工具已配置為與你的叢集通訊。建議在本教程中,在一個至少有兩個非控制平面主機的節點組成的叢集上執行。如果你還沒有叢集,可以透過使用 minikube 來建立一個,或者使用這些 Kubernetes 操場之一

要檢查 kubectl 是否已安裝,請執行 kubectl version --client。kubectl 版本應與你的叢集 API 伺服器的次要版本相差一個版本之內

定義叢集、使用者和上下文

假設你有兩個叢集,一個用於開發工作,一個用於測試工作。在 development 叢集中,你的前端開發人員在名為 frontend 的名字空間中工作,儲存開發人員在名為 storage 的名字空間中工作。在你的 test 叢集中,開發人員在預設的名字空間中工作,或者他們根據需要建立輔助名字空間。訪問開發叢集需要證書認證。訪問測試叢集需要使用者名稱和密碼認證。

建立一個名為 config-exercise 的目錄。在你的 config-exercise 目錄中,建立一個名為 config-demo 的檔案,內容如下:

apiVersion: v1
kind: Config
preferences: {}

clusters:
- cluster:
  name: development
- cluster:
  name: test

users:
- name: developer
- name: experimenter

contexts:
- context:
  name: dev-frontend
- context:
  name: dev-storage
- context:
  name: exp-test

配置檔案描述了叢集、使用者和上下文。你的 config-demo 檔案具有描述兩個叢集、兩個使用者和三個上下文的框架。

進入你的 config-exercise 目錄。輸入以下命令向你的配置檔案新增叢集詳細資訊:

kubectl config --kubeconfig=config-demo set-cluster development --server=https://1.2.3.4 --certificate-authority=fake-ca-file
kubectl config --kubeconfig=config-demo set-cluster test --server=https://5.6.7.8 --insecure-skip-tls-verify

向你的配置檔案新增使用者詳細資訊:

kubectl config --kubeconfig=config-demo set-credentials developer --client-certificate=fake-cert-file --client-key=fake-key-seefile
kubectl config --kubeconfig=config-demo set-credentials experimenter --username=exp --password=some-password

向你的配置檔案新增上下文詳細資訊:

kubectl config --kubeconfig=config-demo set-context dev-frontend --cluster=development --namespace=frontend --user=developer
kubectl config --kubeconfig=config-demo set-context dev-storage --cluster=development --namespace=storage --user=developer
kubectl config --kubeconfig=config-demo set-context exp-test --cluster=test --namespace=default --user=experimenter

開啟你的 config-demo 檔案檢視新增的詳細資訊。除了開啟 config-demo 檔案之外,你還可以使用 config view 命令。

kubectl config --kubeconfig=config-demo view

輸出顯示了兩個叢集、兩個使用者和三個上下文

apiVersion: v1
clusters:
- cluster:
    certificate-authority: fake-ca-file
    server: https://1.2.3.4
  name: development
- cluster:
    insecure-skip-tls-verify: true
    server: https://5.6.7.8
  name: test
contexts:
- context:
    cluster: development
    namespace: frontend
    user: developer
  name: dev-frontend
- context:
    cluster: development
    namespace: storage
    user: developer
  name: dev-storage
- context:
    cluster: test
    namespace: default
    user: experimenter
  name: exp-test
current-context: ""
kind: Config
preferences: {}
users:
- name: developer
  user:
    client-certificate: fake-cert-file
    client-key: fake-key-file
- name: experimenter
  user:
    # Documentation note (this comment is NOT part of the command output).
    # Storing passwords in Kubernetes client config is risky.
    # A better alternative would be to use a credential plugin
    # and store the credentials separately.
    # See https://kubernetes.club.tw/docs/reference/access-authn-authz/authentication/#client-go-credential-plugins
    password: some-password
    username: exp

上述的 fake-ca-filefake-cert-filefake-key-file 是證書檔案路徑的佔位符。你需要將它們更改為環境中證書檔案的實際路徑。

有時你可能希望在此處嵌入 Base64 編碼的資料而不是單獨的證書檔案;在這種情況下,你需要為鍵新增字尾 -data,例如 certificate-authority-dataclient-certificate-dataclient-key-data

每個上下文都是一個三元組(叢集,使用者,名字空間)。例如,dev-frontend 上下文表示:“使用 developer 使用者的憑據訪問 development 叢集的 frontend 名字空間”。

設定當前上下文

kubectl config --kubeconfig=config-demo use-context dev-frontend

現在,無論何時輸入 kubectl 命令,該操作都將應用於 dev-frontend 上下文中列出的叢集和名字空間。並且該命令將使用 dev-frontend 上下文中列出的使用者的憑據。

要僅檢視與當前上下文相關的配置資訊,請使用 --minify 標誌。

kubectl config --kubeconfig=config-demo view --minify

輸出顯示了與 dev-frontend 上下文相關的配置資訊

apiVersion: v1
clusters:
- cluster:
    certificate-authority: fake-ca-file
    server: https://1.2.3.4
  name: development
contexts:
- context:
    cluster: development
    namespace: frontend
    user: developer
  name: dev-frontend
current-context: dev-frontend
kind: Config
preferences: {}
users:
- name: developer
  user:
    client-certificate: fake-cert-file
    client-key: fake-key-file

現在假設你想在測試叢集中工作一段時間。

將當前上下文更改為 exp-test

kubectl config --kubeconfig=config-demo use-context exp-test

現在你發出的任何 kubectl 命令都將應用於 test 叢集的預設名字空間。並且該命令將使用 exp-test 上下文中列出的使用者的憑據。

檢視與新的當前上下文 exp-test 相關的配置。

kubectl config --kubeconfig=config-demo view --minify

最後,假設你想在 development 叢集的 storage 名字空間中工作一段時間。

將當前上下文更改為 dev-storage

kubectl config --kubeconfig=config-demo use-context dev-storage

檢視與新的當前上下文 dev-storage 相關的配置。

kubectl config --kubeconfig=config-demo view --minify

建立第二個配置檔案

在你的 config-exercise 目錄中,建立一個名為 config-demo-2 的檔案,內容如下:

apiVersion: v1
kind: Config
preferences: {}

contexts:
- context:
    cluster: development
    namespace: ramp
    user: developer
  name: dev-ramp-up

上述配置檔案定義了一個名為 dev-ramp-up 的新上下文。

設定 KUBECONFIG 環境變數

檢視你是否有名為 KUBECONFIG 的環境變數。如果有,請儲存 KUBECONFIG 環境變數的當前值,以便以後可以恢復它。例如:

Linux

export KUBECONFIG_SAVED="$KUBECONFIG"

Windows PowerShell

$Env:KUBECONFIG_SAVED=$ENV:KUBECONFIG

KUBECONFIG 環境變數是配置檔案的路徑列表。在 Linux 和 Mac 上,列表以冒號分隔;在 Windows 上,以分號分隔。如果你有 KUBECONFIG 環境變數,請熟悉列表中包含的配置檔案。

暫時將兩個路徑附加到你的 KUBECONFIG 環境變數。例如:

Linux

export KUBECONFIG="${KUBECONFIG}:config-demo:config-demo-2"

Windows PowerShell

$Env:KUBECONFIG=("config-demo;config-demo-2")

在你的 config-exercise 目錄中,輸入此命令:

kubectl config view

輸出顯示了從 KUBECONFIG 環境變數中列出的所有檔案合併的資訊。特別地,請注意合併的資訊包含來自 config-demo-2 檔案的 dev-ramp-up 上下文和來自 config-demo 檔案的三個上下文。

contexts:
- context:
    cluster: development
    namespace: frontend
    user: developer
  name: dev-frontend
- context:
    cluster: development
    namespace: ramp
    user: developer
  name: dev-ramp-up
- context:
    cluster: development
    namespace: storage
    user: developer
  name: dev-storage
- context:
    cluster: test
    namespace: default
    user: experimenter
  name: exp-test

有關 kubeconfig 檔案如何合併的更多資訊,請參見使用 kubeconfig 檔案組織叢集訪問

探索 $HOME/.kube 目錄

如果你已經有一個叢集,並且可以使用 kubectl 與該叢集進行互動,那麼在 $HOME/.kube 目錄中你可能有一個名為 config 的檔案。

進入 $HOME/.kube,檢視那裡有哪些檔案。通常會有一個名為 config 的檔案。此目錄中也可能存在其他配置檔案。簡單熟悉一下這些檔案的內容。

將 $HOME/.kube/config 附加到你的 KUBECONFIG 環境變數中

如果你有一個 $HOME/.kube/config 檔案,並且它尚未列在你的 KUBECONFIG 環境變數中,請現在將其附加到你的 KUBECONFIG 環境變數中。例如:

Linux

export KUBECONFIG="${KUBECONFIG}:${HOME}/.kube/config"

Windows Powershell

$Env:KUBECONFIG="$Env:KUBECONFIG;$HOME\.kube\config"

檢視從現在列在你的 KUBECONFIG 環境變數中的所有檔案合併的配置資訊。在你的 config-exercise 目錄中,輸入:

kubectl config view

清理

將你的 KUBECONFIG 環境變數恢復到其原始值。例如:

Linux

export KUBECONFIG="$KUBECONFIG_SAVED"

Windows PowerShell

$Env:KUBECONFIG=$ENV:KUBECONFIG_SAVED

檢查 kubeconfig 所代表的主體

認證到集群后,你將獲得哪些屬性(使用者名稱、組)並不總是顯而易見的。如果你同時管理多個叢集,這可能更具挑戰性。

有一個 kubectl 子命令可以檢查你所選的 Kubernetes 客戶端上下文的主體屬性,例如使用者名稱:kubectl auth whoami

閱讀客戶端認證資訊的 API 訪問以瞭解更多詳細資訊。

下一步

最後修改於 2023 年 7 月 12 日太平洋標準時間上午 1:25:修訂文件主頁 (9520b96a61)