Skip to content

Instantly share code, notes, and snippets.

@vishnuhd
Last active September 2, 2020 07:09
Show Gist options
  • Save vishnuhd/2ed8d19ce52c46e6dbdef229158865cf to your computer and use it in GitHub Desktop.
Save vishnuhd/2ed8d19ce52c46e6dbdef229158865cf to your computer and use it in GitHub Desktop.
Setup admin service account
  • Get the context :
CONTEXT=$(kubectl config current-context)
  • Create the required service account and cluster role binding :
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-service-account
  namespace: kube-system

---

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cluster-admin
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-service-account
  namespace: kube-system
# This service account uses the ClusterAdmin role -- this is not necessary, 
# more restrictive roles can by applied.
kubectl apply --context $CONTEXT \
    -f service-account.yml
  • Get the TOKEN for the account :
TOKEN=$(kubectl get secret --context $CONTEXT \
   $(kubectl get serviceaccount admin-service-account \
       --context $CONTEXT \
       -n kube-system \
       -o jsonpath='{.secrets[0].name}') \
   -n kube-system \
   -o jsonpath='{.data.token}' | base64 --decode)
  • Set the config :
kubectl config set-credentials ${CONTEXT}-token-user --token $TOKEN

kubectl config set-context $CONTEXT --user ${CONTEXT}-token-user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment