Kubernetes——k8s集群外主机通过kubectl访问集群
k8s集群外主机通过kubectl访问集群
你可以使用 Kubectl 命令行工具管理 Kubernetes 集群。kubectl
在 $HOME/.kube
目录中查找一个名为 config
的配置文件。你可以通过设置 KUBECONFIG 环境变量或设置 --kubeconfig
参数来指定其它 kubeconfig 文件。
官网相关资源:
问题背景:
需要在 kubernetes 集群外访问 kubernetes 中的资源对象。由于 kubectl 命令行工具管理 kubernetes 集群,需要 kubeconfig 的配置文件,其中包含获取访问 kube-apiserver 地址、证书、用户名等信息。
环境准备:
[root@k8s-master-01 ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION k8s-master-01-192.168.153.201 Ready control-plane,master 65m v1.20.10 k8s-master-02-192.168.153.202 Ready control-plane,master 65m v1.20.10 k8s-worker-01-192.168.153.211 Ready worker 65m v1.20.10 k8s-worker-02-192.168.153.212 Ready worker 65m v1.20.10 [root@k8s-master-01 ~]#
操作配置:
在 master 集群节点上执行如下命令:
# 1 设置集群参数(注意:单master集群为master节点私网IP,高可用集群为虚拟IP) kubectl config set-cluster kubernetes \ --server=https://192.168.153.200:16443 \ --certificate-authority=/etc/kubernetes/pki/ca.pem \ --embed-certs=true \ --kubeconfig=config # 2 设置客户端认证参数 kubectl config set-credentials cluster-admin \ --certificate-authority=/etc/kubernetes/pki/ca.pem \ --embed-certs=true \ --client-key=/etc/kubernetes/pki/admin.key \ --client-certificate=/etc/kubernetes/pki/admin.pem \ --kubeconfig=config # 3 设置上下文参数 kubectl config set-context default \ --cluster=kubernetes \ --user=cluster-admin \ --kubeconfig=config # 4 设置默认上下文 kubectl config use-context default --kubeconfig=config
当前目录下会生成 config 文件,config 文件内容如下:
[root@k8s-master-01 ~]# cat config apiVersion: v1 clusters: - cluster: certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMrakNDQWVLZ0F3SUJBZ0lVRDQ5Mndpd3E4Z2VqN0p6RnVLNjREZXNUSnJzd0RRWUpLb1pJaHZjTkF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wdzY3YkZ3SmpmaktpMVo5MzBxRHdKS1V5SEpGTTZ0aHVpWVFWZkNRZnBmaUtSegpEdnluYktRdUQrMms5UllLSEk2RXBWdzRiTHdXcTBMUWMrZXliSWV5N0x0Qjlad2QxZmdvNHJwT29odXJES0p4CjNVTTJsZ1hBcHZFOVV3U2Z3SlNBcUFRQ0MvcytqcUhmUjVTQmtLazY4UjRkSUIxVzFGRnNPQkM2TlFrd0J3PT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo= server: https://192.168.153.200:16443 name: kubernetes contexts: - context: cluster: kubernetes user: cluster-admin name: default current-context: default kind: Config preferences: {} users: - name: cluster-admin user: client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSURERENDQWZTZ0F3SUJBZ0lVTU9GR3lYWjBLY0hWUkhWR2gwclhSN2grSHpFd0RRWUpLb1pJaHZjTkFRRUwKQlFBd0ZURVRNQkVHQTFVRUF3d0thM1ZpWlhKdVpYUmxjekFlRncweU1UQTNNamt4TmpJMk16WmFGdzB6TVRBMwpNamN4TmpJMk16WmFNRFF4RnpBVkJnTlZCQW9NRG5ONWMzUmxiVHB0WVhOMFpYSnpNUmt3RndZRFZRUUREQkJyCmRXSmxjbTVsZEdWekxXRmtiV2x1TUlJQklqQU5CZ2txaGtpRzl3MEJBUUVGQUFPQ0FROEFNSUlCQ2dLQ0FRRUEKbkI5NDBsK0d4dzR0YkNhWlBTQnhrTzJUZ0pEVzZPdXBlQ0FiNEMxN2JrSThWWWpuWVZjVHBhSFFTMTdIV1I5UQpzRGhHbEVzYUJZOWRpK3ZZd0xqam4wYmo0ckpINWZ5WjhvL1d6L3FpMVhXQ1RPQUhOSDAyeE9zMnRmUnNjbVdQCjlEcVRQS1pTQ1pzWW1ROWhFb2lDbkE2eElSaEVManBQakhjK21vZFY5TnMzWFUzQUNPUjZXMncyR01HWHdWY0oKMHVxeG1VVEV5UitSNHlUZkdMSWVaOVNvdUVCYURHSVVSNUR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FQOVVFUER2b3VGT09jSVI5VUNtNTlqbjdSWk1tL25pdFhEUHRDbktCUQpCR2M5bWhtelM1dmYvcW4rU0lGYXI5eURad0JYQVNTYUZ2cFlrRm5VZWJtSE5tdmloQzZuUlJaaXEyUlhqd040CjEveEdCNzErZ285V0Y0cGdXMWp2aGRySnVQbDBBc1JGbTJ0V1AyRUNnWUVBa0dMTXNFYmE3ZGRzS1dEc3JHRWMKc2ZEUFkzU3JhMkYzeEdMQTZnV1hQZ0wvcjRWR1gvb2VuN0xpdklQRUpBV2NsNVcyOFhTeHRvTFljTWpidEZMKwpjSkRaTktWcUNGUStPR1FVaTN5dWlTOWgvMFVNL3pLTlY1Vm5EZlM5d09McFZOYlFlTUpZekFKOWJydVBWUmJhCmJmeUtxZDdlSk9Za1lhdkdkWXhVbHpFQ2dZQWdIVnExeGg5d0xOdWQvMk1YZnZTUkVYaU9LaThHVXRxaVR5Z1IKOHlkcXA2MzFVeDNCY2dkN0ZNeWlWZHowR2ZzZmMxQUdwc2R5cWlCTmJxWFFvcmkrQVZ3M2tNY3VlUHVqRDlCOQpVWFB6Vk40RUwzWWgrVFR1d3JJVm5oKzZNN2VQVmJ6UEtmWWhZeldVN3JIRGQ0bFF4d2R3Nlo0MUJSOHZJNjAyCkd4NE5nUUtCZ1FDSFhxeTc2SWo4Wkk3ajVLSEFmREhycFhjSmpOQVI0NGFJQmdOYnpEZG5oYy95em9CSHZPNHkKUFpvY2ZyWERmQWVsclBSSXlPamUzcmlFRVdxODAvS0RsN2xSa2lCRDdJekNMNjIvcjdYWGJRTjc3aEltVjRFMwozKzdSdmRqK2xNTlRaR1FHdFlMNjUvcGJtOGZkd0RteG5vcVZkZTJhcUtzUFhYQmh2ZGx3Wmc9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo= [root@k8s-master-01 ~]#
将 config 拷贝到 kubernetes 集群外节点上,并在该节点上执行 kubectl-v1.20.10 --kubeconfig=config get nodes。注意:kubectl 版本和 kubernetes 中的 kubectl 版本一致。
[root@localhost ~]# ./kubectl-v1.20.10 --kubeconfig=config get nodes NAME STATUS ROLES AGE VERSION k8s-master-01-192.168.153.201 Ready control-plane,master 79m v1.20.10 k8s-master-02-192.168.153.202 Ready control-plane,master 79m v1.20.10 k8s-worker-01-192.168.153.211 Ready worker 79m v1.20.10 k8s-worker-02-192.168.153.212 Ready worker 79m v1.20.10 [root@localhost ~]#
成功!