「Kubernetes 實作手冊:基礎入門篇」學習筆記(七):使用 kubeconfig 配置叢集

前言

本文為「Kubernetes 實作手冊:基礎入門篇」課程的學習筆記。

簡介

kubeconfig 用於管理多個 Kubernetes 的叢集、使用者、命名空間及認證等資訊。

使用

kubectl 預設會去找 $HOME/.kube/config 路徑。

以 Rancher 建立的叢集為例,定義了 Cluster、Context 和 User 資訊,其 kubeconfig 檔如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
apiVersion: v1
kind: Config
clusters:
- name: "sandbox"
cluster:
server: "https://172.17.8.111/k8s/clusters/c-xxlj4"
certificate-authority-data: "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM3akNDQ\
WRhZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFvTVJJd0VBWURWUVFLRXdsMGFHVXQKY\
21GdVkyZ3hFakFRQmdOVkJBTVRDV05oZEhSc1pTMWpZVEFlRncweU1URXlNVEV3T1RNeU1UZGFGd\
zB6TVRFeQpNRGt3T1RNeU1UZGFNQ2d4RWpBUUJnTlZCQW9UQ1hSb1pTMXlZVzVqYURFU01CQUdBM\
VVFQXhNSlkyRjBkR3hsCkxXTmhNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ\
0tDQVFFQXlZTnB2MUtpakpqd1ZiZ1MKenlnT2dIb0lZb2QxUUJHc2FSbmFQQ3pIbWlLcE5xMWtpO\
VE4L3NSdlFuUUNiTUNFc1VLZTZXQ0cyak5zOUtPcApzeC9uQlRqY0hHTHE1VHpYaDJ5V2VRSFpUL\
2hDODNKTE5aUEFacXB5U1hoU0NlazBXOFlTUUhyVG1sNFY1TUUxCmdIcmZqdDk1VnIzbGIrVGdtd\
HVFVFFUSzhDSUhDN2F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MGlzdkRPaSs3blN0d\
DBaOGtSRTNVMTdDMWJBN01hOWdHNHpQTzRHcnFUNTJ2ckV5MTcwd1BIVlJ1SwpsVmM1TTBRWFRFb\
3lnVlhBaFQ3RFRlSmJvazgzMk9INDdjbTZwRWZ2MTZXUEsvRUgrRUE9Ci0tLS0tRU5EIENFUlRJR\
klDQVRFLS0tLS0K"

users:
- name: "sandbox"
user:
token: "kubeconfig-user-g8s6b.c-xxlj4:ffb8wjsbnbd8vbx8xj7cqxnrmhv2pflf84l2pwwvlxdcdfn6ssbjw7"

contexts:
- name: "sandbox"
context:
user: "sandbox"
cluster: "sandbox"
- name: "sandbox-k8s-dev"
context:
user: "sandbox"
cluster: "sandbox-k8s-dev"

current-context: "sandbox"

Rancher 定義了兩個叢集,一個是 Rancher Server,另一個是 Kubernetes API Server。

做法

將以上 kubeconfig 檔存成名為 rancher 的檔案。

指令

使用 config view 指令查看名為 rancherkubeconfig 檔。例如:

1
kubectl config view --kubeconfig rancher

使用 config get-contexts 指令取得 rancher 環境下的 Cluster 列表。例如:

1
kubectl config get-contexts --kubeconfig rancher

切換在 rancher 環境裡,要使用的 Cluster。例如:

1
kubectl config use-context sandbox --kubeconfig rancher

顯示在 rancher 環境裡,正在使用的 Cluster。例如:

1
kubectl config current-context --kubeconfig rancher

取得 Pod 列表。

1
kubectl get pods -n kube-system --kubeconfig rancher

參考資料