這期內容當中小編將會給大家帶來有關kubectl 中怎么配置多集群訪問,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
假設用戶有兩個集群,一個用于正式開發工作(development),一個用于其它臨時用途(scratch)。 在 development
集群中,前端開發者在名為 frontend
的命名空間下工作, 存儲開發者在名為 storage
的命名空間下工作。 在 scratch
集群中, 開發人員可能在默認命名空間下工作,也可能視情況創建附加的命名空間。 訪問開發集群需要通過證書進行認證。 訪問其它臨時用途的集群需要通過用戶名和密碼進行認證。
創建名為 config-exercise
的目錄。 在 config-exercise
目錄中,創建名為 config-demo
的文件,其內容為:
apiVersion: v1 kind: Config preferences: {} clusters: - cluster: name: development - cluster: name: scratch users: - name: developer - name: experimenter contexts: - context: name: dev-frontend - context: name: dev-storage - context: name: exp-scratch
配置文件描述了集群、用戶名和上下文。 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 scratch --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-scratch --cluster=scratch --namespace=default --user=experimenter
打開 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: scratch contexts: - context: cluster: development namespace: frontend user: developer name: dev-frontend - context: cluster: development namespace: storage user: developer name: dev-storage - context: cluster: scratch namespace: default user: experimenter name: exp-scratch current-context: "" kind: Config preferences: {} users: - name: developer user: client-certificate: fake-cert-file client-key: fake-key-file - name: experimenter user: password: some-password username: exp
每個上下文包含三部分(集群、用戶和命名空間),例如, 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-scratch
:
kubectl config --kubeconfig=config-demo use-context exp-scratch
現在用戶 kubectl
下達的任何命令都將應用于 scratch
集群的默認命名空間。 同時,命令會使用 exp-scratch
上下文中所列用戶的憑證。
查看更新后的當前上下文 exp-scratch
相關的配置:
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
環境變量當前的值,以便稍后恢復。 例如,在 Linux 中:
export KUBECONFIG_SAVED=$KUBECONFIG
KUBECONFIG
環境變量是配置文件路徑的列表,該列表在 Linux 和 Mac 中以冒號分隔,在 Windows 中以分號分隔。 如果有 KUBECONFIG
環境變量,請熟悉列表中的配置文件。
臨時添加兩條路徑到 KUBECONFIG
環境變量中。 例如,在 Linux 中:
export KUBECONFIG=$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: scratch namespace: default user: experimenter name: exp-scratch
更多關于 kubeconfig 文件如何合并的信息,請參考 使用 kubeconfig 文件組織集群訪問
如果用戶已經擁有一個集群,可以使用 kubectl
與集群進行交互。 那么很可能在 $HOME/.kube
目錄下有一個名為 config
的文件。
進入 $HOME/.kube
目錄, 看看那里有什么文件。 通常會有一個名為 config
的文件,目錄中可能還有其他配置文件。 請簡單地熟悉這些文件的內容。
如果有 $HOME/.kube/config
文件,并且還未列在 KUBECONFIG
環境變量中, 那么現在將它追加到 KUBECONFIG
環境變量中。 例如,在 Linux 中:
export KUBECONFIG=$KUBECONFIG:$HOME/.kube/config
在配置練習目錄中輸入以下命令,來查看當前 KUBECONFIG
環境變量中列舉的所有文件合并后的配置信息:
kubectl config view
將 KUBECONFIG
環境變量還原為原始值。 例如,在 Linux 中:
export KUBECONFIG=$KUBECONFIG_SAVED
上述就是小編為大家分享的kubectl 中怎么配置多集群訪問了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。