PaaS/Kubernetes

(Kubernetes) 외부에서 Kubernetes 리소스 접근을 위한 SA Token 생성

armyost 2021. 10. 8. 18:33
728x90

Jenkins등 외부에서 Kubernetes 자원에 접근이 필요한 경우가 있다.

이럴때는 SA를 생성하고 해당 credential로 API서버에 인증을 받아서 수행해야 한다.

아래는 SA를 만들고 해당 SA의 Credential까지 얻는 방법이다. (이 Credential을 Jenkins에 등록하면 됩니다.)

 


# Create a ServiceAccount named `jenkins-robot` in a given namespace.

$ kubectl -n <namespace> create serviceaccount jenkins-robot



# The next line gives `jenkins-robot` administator permissions for this namespace.
# * You can make it an admin over all namespaces by creating a `ClusterRoleBinding` instead of a `RoleBinding`.
# * You can also give it different permissions by binding it to a different `(Cluster)Role`.

$ kubectl -n <namespace> create rolebinding jenkins-robot-binding --clusterrole=cluster-admin --serviceaccount=<namespace>:jenkins-robot


# Get the name of the token that was automatically generated for the ServiceAccount `jenkins-robot`.

$ kubectl -n <namespace> get serviceaccount jenkins-robot -o go-template --template='{{range .secrets}}{{.name}}{{"\n"}}{{end}}'
jenkins-robot-token-d6d8z



# Retrieve the token and decode it using base64.

$ kubectl -n <namespace> get secrets jenkins-robot-token-d6d8z -o go-template --template '{{index .data "token"}}' | base64 -d