Skip to content

Instantly share code, notes, and snippets.

@sunnoy
Last active August 9, 2019 02:47
Show Gist options
  • Save sunnoy/b0600981226eaccabae3cad758e0b17b to your computer and use it in GitHub Desktop.
Save sunnoy/b0600981226eaccabae3cad758e0b17b to your computer and use it in GitHub Desktop.
kubectl配置认证
#创建一个专属的namespace
kubectl create ns spinnaker
#创建一个sa
kubectl create serviceaccount spinnaker-service-account -n spinnaker
#进行sa的集群角色绑定
kubectl create clusterrolebinding spinnaker-service-account --clusterrole cluster-admin --serviceaccount=spinnaker:spinnaker-service-account
#获取sa的token-secret
TOKEN_SECRET=$(kubectl get serviceaccount -n spinnaker spinnaker-service-account -o jsonpath='{.secrets[0].name}')
#获取token
TOKEN=$(kubectl get secret -n spinnaker $TOKEN_SECRET -o jsonpath='{.data.token}' | base64 --decode)
#添加user,这里是token,还可以是客户端证书
kubectl config set-credentials spinnaker-token-user --token $TOKEN
#添加用户使用客户端证书
kubectl config set-credentials clinet --client-certificate=path/to/certfile --client-key=path/to/keyfile
#添加用户使用http认证
kubectl config set-credentials http --username=basic_user --password=basic_password
#添加集群
kubectl config set-cluster k8s --server=https://1.2.3.4:6443 \
--certificate-authority=path/to/certificate/authority \
--insecure-skip-tls-verify=true
#设定当前的认证
kubectl config set-context [NAME | --current] [--cluster=cluster_nickname]
[--user=user_nickname] [--namespace=namespace] [options]
#用户和cluster就组成一个context --current 为修改为当前的context
kubectl config set-context k8s --cluster=k8s --user=spinnaker-token-user --namespace=namespace
#查看当前kubeconfig中的context
kubectl config get-contexts
#指定某一个context为当前在用的context
kubectl config use-context k8s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment