kubectl 通過訪問 Kubernetes API 來執(zhí)行命令。我們也可以通過對(duì)應(yīng)的TLS key, 使用curl 或是 golang client做同樣的事。
API 請(qǐng)求必須使用 JSON 格式來發(fā)送。kubectl 的作用是將 yaml 轉(zhuǎn)換為 JSON 格式進(jìn)行 API 請(qǐng)求。
1、我們從查看 kubectl 的配置文件開始,需要:三個(gè)證書和 API server 的地址:
[root@master work]# cat /root/.kube/config apiVersion: v1 clusters: - cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpN21jRHpST1dSaXAKMUU3NkYwOFIvdFV6L0kzWE5XcmU4ZEpsNjlUVnhieGdsZHJhQ0V4UC93VVBaTCtJK3NOMGRBMWFtMHRLVzJJKwpVZ21ISi9aU2dIbjUvUTFNZlpCakh0VUFqTDV2VmdSSzRZV05wVnpqK1JCd0YwNWduTUF4TTVCcWRmcEZ2aGI1CkJCRThIUk9xanJIZi82QTdKN2JwTTArMXlBQmZXMGpjcERZcTlsVnRxczU3ZTl4dVdVb2Y1WENPNyszZG5KQXgKa0dxWHNxN1Z1UmsyaUM4TWZBMk9mT3M5RmdNeFpOYk1UcHZZajZ4N3JjM21NTWh5VVQwdktOaTVXanR1UG53eAptd01yeEJqRjhqOUkybVZNZUJzQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFJTlc5Y0Ewc0tubm16UmVXTTFMWVhZRWRGWjEKNjIwcUVVeWs0VHV2MmxvZzlWcEtLTzhKOG5iOXdjRVcrWFB2NWYyYmtNbUtsTmxaYzJFOEIzOWloU2NiZzBmQgp4QmdpejAwYmxxMXRNNjBCTmc3d2UwM3ZnOFRlZ1hvZGRkTzd3djB6ZXo3U0tGYmg0d2VCRnN4TFNaWGtwL1VQCkNkc2s4ZllKSG1ZbVhlZ1kvTS9vWTl2OHdlVEgzRXpuWEgxY1RxaytkOVdlUWF3aHVYV3UrVC8zL0Q3dmRQYksKeUNtY3BrdmlCZUlHQXVwR0pDN0JTVzFUb05ra3h2bXRsQVZnYlVmdlh3U0srVDVHZGd0Z2lETGUzSkdHbTM1egpLVHBqWlBWL0lxYzZra1BZNFF1R3FqcFQvelMzaTNrdWI0THlBU1FjZHVNc1hUdG8zTVdmdkZxUWRGST0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo= server: https://172.21.0.15:6443 name: kubernetes contexts: - context: cluster: kubernetes user: kubernetes-admin name: kubernetes-admin@kubernetes current-context: kubernetes-admin@kubernetes kind: Config preferences: {} users: - name: kubernetes-admin user: client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUM4akNDQWRxZ0F3SUJBZ0lJR1VLN08xdUFIREF3RFFZSktvWklodmNOQVFFTEJRQXdGVEVUTUJFR0ExVUUKQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4T1RFeU1ETXdOekEzTXpSYUZ3MHlNREV5TURJd056QTNNemhhTURReApGekFWQmdOVkJBb1REbk41YzNSbGJUcHRZWE4wWlhKek1Sa3dGd1lEVlFRREV4QnJkV0psY201bGRHVnpMV0ZrCmJXbHVNSUlCSWpBTkJna3Foa2lHOXcwQkFRRUZBQU9DQVE4QU1JSUJDZ0tDQVFFQWZoNUl4bndrMHRrdXdabUdnMVU2ckJ4VDgxU0RYbFgvd1kKSGEzU1BWUk9HaElURXFYS08wT09Ia3BkbEpDeGpJUm50Y005UHlybnBXK0NPVlNwb1JQSUpoLzdLWHZKTytDeApDaGk0eXlxc2pxbzE5WHlSUUNoZ2hpNnJHTnFETlVhTVd1STBOM1VUeEh2L3ozQkg2QWdIOVNvL2RKRmZXQnlkCmJzQXUrd0lEQVFBQm95Y3dKVEFPQm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LVnVXCi0tLS0tRU5EIFJTQSBQUklWQVRFIEtFWS0tLS0tCg==
2、我們將會(huì)把證書設(shè)為環(huán)境變量。在設(shè)置時(shí)候請(qǐng)檢查每一個(gè)參數(shù)。我們從 client-certificate-data 開始。
export clientcert=$(grep client-cert ~/.kube/config |cut -d" " -f 6) echo $clientcert
3、使用類似的命令將 client-key-data 保存為環(huán)境變量
export clientkey=$(grep client-key-data ~/.kube/config |cut -d" " -f 6) echo $clientkey
4、然后是 certificate-authority-data
export certauth=$(grep certificate-authority-data ~/.kube/config |cut -d" " -f 6) echo $certauth
5、加密這些變量,供 curl 使用:
[root@master k8s-cert]# echo $clientcert | base64 -d > ./client.pem [root@master k8s-cert]# echo $clientkey | base64 -d > ./client-key.pem [root@master k8s-cert]# echo $certauth | base64 -d > ./ca.pem
6、從配置文件中讀取 server 地址:
kubectl config view |grep serverserver: https://172.21.0.15:6443
7、使用 curl 和剛剛加密的密鑰文件來訪問 API server:
curl --cert ./client.pem --key ./client-key.pem --cacert ./ca.pem https://172.21.0.15:6443/api/v1/pods
?
?
?
?
本文摘自 :https://blog.51cto.com/u