在kubernetes 为不同的项目创建了不同的SerivceAccount,那么如何通过ServiceAccount创建 kubectl config文件呢?使用下面脚本即可
# your server name goes here server=https://localhost:8443 # the name of the secret containing the service account token goes here name=default-token-sg96k ca=$(kubectl get secret/$name -o jsonpath='{.data.ca.crt}') token=$(kubectl get secret/$name -o jsonpath='{.data.token}' | base64 --decode) namespace=$(kubectl get secret/$name -o jsonpath='{.data.namespace}' | base64 --decode) echo " apiVersion: v1 kind: Config clusters: - name: default-cluster cluster: certificate-authority-data: ${ca} server: ${server} contexts: - name: default-context context: cluster: default-cluster namespace: default user: default-user current-context: default-context users: - name: default-user user: token: ${token} " > sa.kubeconfig