更新时间:2025-07-25 GMT+08:00
本地集群KubeConfig文件
获取本地集群KubeConfig文件
KubeConfig是Kubernetes集群中组织有关集群、用户、命名空间和身份认证机制信息的配置文件,Kubectl使用KubeConfig来获取集群的信息并与API server进行通信。
获取本地集群的KubeConfig需要使用ucs-ctl工具,获取步骤如下:
- 使用ucs-ctl获取集群名称。
./ucs-ctl get cluster
- 使用ucs-ctl导出指定集群的KubeConfig。
./ucs-ctl get kubeconfig -c test-redhat86 -o kubeconfig
可以使用ucs-ctl get kubeconfig -h查看获取KubeConfig所使用到的参数。
- -c, --cluster string:指定待导出KubeConfig的集群名。
- -e, --eip string:指定API server的eip。
- -o, --output string:指定KubeConfig导出文件名。
- -p, --secretPath string:指定待导出KubeConfig的加密物料路径。
更新本地集群证书文件
为应对本地集群证书信息泄露或过期情况,或进行例行的安全维护,可以使用ucs-ctl对本地集群的证书文件进行更新。
- 更新证书可以选择tls或者ca类型。
./ucs-ctl kcm update-cert {cluster_name} --mode tls
- 若需要再次更新,需先删除证书目录的文件,然后进行更新,否则将会报错:error: "update cert recordFile[\"var/paas/ucs/{cluster_name}/cert_update/status.json\"] already exists, please use the option \"-- retry or -r\"to retry it".
rm -rf /var/paas/ucs/{cluster_name}/cert_update
使用本地集群KubeConfig文件
拿到ucs-ctl生成的KubeConfig之后,还需要使KubeConfig在节点上生效,步骤如下:
- 复制KubeConfig到节点上。
scp /local/path/to/kubeconfig user@remote:/remote/path/to/kubeconfig
- 如果当前节点添加过EnableSecretEncrypt环境变量,需要先取消。
unset EnableSecretEncrypt
- 使KubeConfig生效,可选方法:
- 方法一:复制到默认位置。
mv /remote/path/to/kubeconfig $HOME/.kube/config
- 方法二:环境变量指定。
export KUBECONFIG=/remote/path/to/kubeconfig
- 方法三:命令行参数指定。
kubectl --kubeconfig=/remote/path/to/kubeconfig
- 方法一:复制到默认位置。
上述操作之后,就可以访问本地集群的API server了。关于KubeConfig更详细的用法可参考:使用kubeconfig文件组织集群访问Kubernetes。
父主题: 管理本地集群