Kubernetes 使用 kubeconfig 文件来管理集群配置信息,包括连接到 Kubernetes API 服务器所需的认证信息、上下文信息等。在 k8s 集群中导出 kubeconfig 文件可以通过以下步骤实现:
1.使用 kubectl config view 命令查看当前集群的 kubeconfig 配置信息。
$ kubectl config view
2.使用 kubectl config set-cluster 命令设置集群信息,并指定服务器地址、CA 证书等相关参数。
$ kubectl config set-cluster <cluster-name> --server=<kubernetes-api-server-url> --certificate-authority=<ca-cert-file-path>
3.使用 kubectl config set-credentials 命令设置认证信息,并指定认证类型、用户名、密码等参数。
$ kubectl config set-credentials <user-name> --username=<user-name> --password=<password> --auth-provider=oidc
4.使用 kubectl config set-context 命令设置上下文信息,并关联前面设置的 cluster 和 user。
$ kubectl config set-context <context-name> --cluster=<cluster-name> --user=<user-name>
5.使用 kubectl config use-context 命令切换当前上下文为新创建的 context。
$ kubectl config use-context <context-name>
6.最后,使用 kubectl config view 查看新生成的 kubeconfig 文件内容并导出保存即可。
$ kubectl config view > my-kube-config.yaml
其中,kubectl config set-credentials 命令时,如果是 OIDC 认证方式,则可以使用 --auth-provider=oidc 参数指定。
导出的 kubeconfig 文件可以用于远程连接到 k8s 集群执行各种操作,例如使用 kubectl 命令行工具管理集群资源等。




