您好,登錄后才能下訂單哦!
這篇文章主要講解了“k8s中kubeconfig怎么配置使用”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“k8s中kubeconfig怎么配置使用”吧!
kubeconfig文件保存了k8s集群的集群、用戶、命名空間、認(rèn)證的信息。kubectl命令使用kubeconfig文件來獲取集群的信息,然后和API server進(jìn)行通訊。
注意:用于配置對(duì)集群的訪問的文件稱為kubecconfig文件。也就是說,kubeconfig文件中包含的內(nèi)容是集群的配置。但是,并不是必須有個(gè)文件名字叫kubeconfig
默認(rèn)情況下,kubectl命令從$HOME/.kube目錄下查找一個(gè)名字叫做config的文件??梢酝ㄟ^KUBECONFIG環(huán)境變量或者--kubeconfig參數(shù)來指定其他的kubeconfig文件。
kubeconfig中主要由如下部分組成:
clusters (集群)
users(用戶)
context(上下文)
在實(shí)際的使用中的如下場(chǎng)景:
kubelet使用證書認(rèn)證(kubelet和api server進(jìn)行認(rèn)證)
用戶使用token進(jìn)行認(rèn)證
管理員為不同的用戶提供不同的證書
都可以使用kubeconfig來組織起集群、用戶、命名空間的信息。同樣,也可以使用context在集群和命名空間之間進(jìn)行切換。
在kubeconfig中,context中將訪問一個(gè)集群的參數(shù)進(jìn)行分組。訪問這個(gè)context名稱就是訪問這個(gè)參數(shù)組。context就是一組信息的別名,舉例來說,當(dāng)在高德中使用家的地址,公司的地址就是一個(gè)別名,就能迅速的定位到具體的地址信息。
每個(gè)context都有3個(gè)參數(shù):
cluster (集群)
namespace(命名空間)
user(用戶)
默認(rèn)情況下,kubectl命令從current context中來獲取參數(shù),然后與集群進(jìn)行通訊。
如果有KUBECONFIG環(huán)境變量,看到的配置是合并的配置
[root@nccztsjb-node-11 ~]# kubectl config view apiVersion: v1 clusters: - cluster: certificate-authority-data: DATA+OMITTED server: https://k8s.apiserver.io:6443 name: cluster.local contexts: - context: cluster: cluster.local user: kubernetes-admin-cluster.local name: kubernetes-admin-cluster.local@cluster.local current-context: kubernetes-admin-cluster.local@cluster.local kind: Config preferences: {} users: - name: kubernetes-admin-cluster.local user: client-certificate-data: REDACTED client-key-data: REDACTED [root@nccztsjb-node-11 ~]#
通過kubectl config view得到的配置可能是來自一個(gè)kubeconfig文件,也可能是來自多個(gè)kubeconfig文件合并的結(jié)果。
(1)集群有CA認(rèn)證,并且將證書的輸入嵌入到配置文件中
kubectl config --kubeconfig=config-demo set-cluster development --server=https://1.2.3.4 --embed-certs --certificate-authority=ca.crt
指定--kubeconfig參數(shù)后,所有的配置都會(huì)寫到對(duì)應(yīng)的文件中。如果不指定--kubeconfig參數(shù)那么就會(huì)將配置文件寫到~/.kube/config這個(gè)文件中。
--certificate-authority后面是ca證書的路徑,需要確保這個(gè)證書是存在的。
查看config-demo文件的內(nèi)容
[root@nccztsjb-node-11 config-exercise]# cat config-demo apiVersion: v1 clusters: - cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM2VENDQWRHZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQ0FYRFRJeE1USXdNakE0TURjMU5sb1lEekl4TVRFeE1URXhNRGd3TnpVMldqQVZNUk13RVFZRApWUVFERXdwcmRXSmxjbTVsZEdWek1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBCnFaWkVNZDhORHpoek5hTE1ab2tCenlabVUzQWh7R1Z4OW15MzJxaTB3UzkzS3Jib0xMUWhSSUhCYnYzdzZCbCsKZlBYaUdSeCt4OVpKY2lta1BqdmJtQ0ZpKzEvQmsxZkFONlNBSi9ISjJoenQxcExZbW41NXVVclpqdmFCTXNUdAp5U3B4QUlOUWNNU1NJYUFyeHF1VDJ0QjYzWkMvTHloQVFWWG4xL2lYS3hLTmpidjVlS21BdlRRTlQrNlMxaFIxCkJNQXNjSFc0R1lhNXVBTFZBTmZrVlpGaG5GMmt2cVMyRzRDMXV4emZtOFpyUnREOExRUjFmTnk1VnpnVkJLQjMKT2RKQkExNnl4YXk5N2JjaEtqenpqTmxKMFBlVDBOdFZZWVRlZ3F1ZG5iRURRSEdObC91Ym5UcGtWWHpFUGRKNAo3a3FQREdWUzRXYi9DZXpVcm9IQ1hRSURBUUFCbzBJd1FEQU9CZ05WSFE4QkFmOEVCQU1DQXFRd0R3WURWUjBUCkFRSC9CQVV3QXdFQi96QWRCZ05WSFE0RUZnUVVmQldoK3hIdlVSdFdKdUVuUUlYTHRDUzh3MEl3RFFZSktvWkkKaHZjTkFRRUxCUUFEZ2dFQkFHeHNvSXJOZkJYRkp1RjZwYmtvSkh4eVdCSkp0Qzlud1VabWNCMmYzT0xTZTN4LwpQQU5oYm9LcHZ6ME5ocm5IQ2VnUk1rWGlmSmhKZW9VU25sYUhJaWpjVlFDZ09sTDVnT0EyeFZkRk01MlZCTk5jCnN1Qmp2MHJoQnZXMWxaYWVEUGJuaGp3dGY5elp2WG4vMUkvZ1RKSVRiNmFVdk55b1U3WHY2c3RmN1NjaExwU3kKZmVwRjNKRFc5TXRvem9yMHhoV0U3M0FrZHE1bnE4OFlSdEcxK3UwRTJXbDJ5U20yR0dzRUkvcU1HOXlvd0NsRgozSnhOQm45MEk3V0xKd0pETGZZMXYwWXdRNDR3QUJteGN4R01qVzNmOHAvL3pxSHNaaXJGdTR3QUlUK003Mmw3CkhDbXAwbm84VXpKMUdMUFcxNzF5TWNsdlJNM3h3SENtZHd0VktBMD0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo= server: https://1.2.3.4 name: development contexts: null current-context: "" kind: Config preferences: {} users: null [root@nccztsjb-node-11 config-exercise]#
已經(jīng)設(shè)置好了集群
(2)集群沒有證書的
kubectl config --kubeconfig=config-demo set-cluster development --server=https://1.2.3.4
查看證書內(nèi)容
[root@nccztsjb-node-11 config-exercise]# kubectl config --kubeconfig=config-demo set-cluster development --server=https://1.2.3.4 Cluster "development" set. [root@nccztsjb-node-11 config-exercise]# cat config-demo apiVersion: v1 clusters: - cluster: server: https://1.2.3.4 name: development contexts: null current-context: "" kind: Config preferences: {} users: null [root@nccztsjb-node-11 config-exercise]
(3)集群有證書,但是證書驗(yàn)證的
kubectl config --kubeconfig=config-demo set-cluster scratch --server=https://5.6.7.8 --insecure-skip-tls-verify
查看證書的內(nèi)容
[root@nccztsjb-node-11 config-exercise]# cat config-demo apiVersion: v1 clusters: - cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM2VENDQWRHZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQ0FYRFRJeE1USXdNakE0TURjMU5sb1lEekl4TVRFeE1URXhNRGd3TnpVMldqQVZNUk13RVFZRApWUVFERXdwcmRXSmxjbTVsZEdWek1JSUJJakFOQmdrcWhraUc5dzBCQVFFRkFBT0NBUThBTUlJQkNnS0NBUUVBCnFaWkVNZDhORHpoek5hTE1ab2tCenlabVUzQWh7R1Z4OW15MzJxaTB3UzkzS3Jib0xMUWhSSUhCYnYzdzZCbCsKZlBYaUdSeCt4OVpKY2lta1BqdmJtQ0ZpKzEvQmsxZkFONlNBSi9ISjJoenQxcExZbW41NXVVclpqdmFCTXNUdAp5U3B4QUlOUWNNU1NJYUFyeHF1VDJ0QjYzWkMvTHloQVFWWG4xL2lYS3hLTmpidjVlS21BdlRRTlQrNlMxaFIxCkJNQXNjSFc0R1lhNXVBTFZBTmZrVlpGaG5GMmt2cVMyRzRDMXV4emZtOFpyUnREOExRUjFmTnk1VnpnVkJLQjMKT2RKQkExNnl4YXk5N2JjaEtqenpqTmxKMFBlVDBOdFZZWVRlZ3F1ZG5iRURRSEdObC91Ym5UcGtWWHpFUGRKNAo3a3FQREdWUzRXYi9DZXpVcm9IQ1hRSURBUUFCbzBJd1FEQU9CZ05WSFE4QkFmOEVCQU1DQXFRd0R3WURWUjBUCkFRSC9CQVV3QXdFQi96QWRCZ05WSFE0RUZnUVVmQldoK3hIdlVSdFdKdUVuUUlYTHRDUzh3MEl3RFFZSktvWkkKaHZjTkFRRUxCUUFEZ2dFQkFHeHNvSXJOZkJYRkp1RjZwYmtvSkh4eVdCSkp0Qzlud1VabWNCMmYzT0xTZTN4LwpQQU5oYm9LcHZ6ME5ocm5IQ2VnUk1rWGlmSmhKZW9VU25sYUhJaWpjVlFDZ09sTDVnT0EyeFZkRk01MlZCTk5jCnN1Qmp2MHJoQnZXMWxaYWVEUGJuaGp3dGY5elp2WG4vMUkvZ1RKSVRiNmFVdk55b1U3WHY2c3RmN1NjaExwU3kKZmVwRjNKRFc5TXRvem9yMHhoV0U3M0FrZHE1bnE4OFlSdEcxK3UwRTJXbDJ5U20yR0dzRUkvcU1HOXlvd0NsRgozSnhOQm45MEk3V0xKd0pETGZZMXYwWXdRNDR3QUJteGN4R01qVzNmOHAvL3pxSHNaaXJGdTR3QUlUK003Mmw3CkhDbXAwbm84VXpKMUdMUFcxNzF5TWNsdlJNM3h3SENtZHd0VktBMD0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo= server: https://1.2.3.4 name: development - cluster: insecure-skip-tls-verify: true server: https://5.6.7.8 name: scratch contexts: null current-context: "" kind: Config preferences: {} users: null [root@nccztsjb-node-11 config-exercise]#
(4)刪除集群
kubectl config --kubeconfig=config-demo unset clusters.development
(1)設(shè)置用戶,使用客戶端的證書和客戶端密鑰,并且將證書數(shù)據(jù)嵌入到配置文件中
kubectl config --kubeconfig=config-demo set-credentials developer --client-certificate=fake-cert-file --client-key=fake-key-seefile --embed-certs=true
注意:客戶端證書和key必須要存在。
(2)設(shè)置用戶,使用用戶名和密碼
kubectl config --kubeconfig=config-demo set-credentials experimenter --username=exp --password=some-password
(3)刪除用戶
kubectl config --kubeconfig=config-demo unset users.experimenter
experimenter這個(gè)就是使用set-credentials參數(shù)后面的名字。
kubectl config --kubeconfig=config-demo set-context dev-frontend --cluster=development --namespace=frontend --user=developer
設(shè)置context將集群、命名空間、用戶進(jìn)行分組。即在dev-fronted這個(gè)context的以developer用戶的信息訪問developement集群的frontend命名空間。
kubectl config --kubeconfig=config-demo set-context dev-storage --cluster=development --namespace=storage --user=developer
dev-stroage這個(gè)context訪問stroage命名空間。
查看通過以上的所有配置形成的kubeconfig文件
[root@nccztsjb-node-11 config-exercise]# cat config-demo 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-seefile - name: experimenter user: password: some-password. username: exp
kubectl config --kubeconfig=config-demo use-context dev-frontend
查看當(dāng)前的context
kubectl config --kubeconfig=config-demo current-context
kubectl config --kubeconfig=config-demo view --minify
[root@nccztsjb-node-11 config-exercise]# kubectl config --kubeconfig=config-demo view --minify 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-seefile
那么kubeconfig中的其他的信息就不會(huì)顯示
[root@nccztsjb-node-11 config-exercise]# kubectl config --kubeconfig=config-demo get-contexts CURRENT NAME CLUSTER AUTHINFO NAMESPACE * dev-frontend development developer frontend dev-storage development developer storage exp-scratch scratch experimenter default
感謝各位的閱讀,以上就是“k8s中kubeconfig怎么配置使用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)k8s中kubeconfig怎么配置使用這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。