Site icon CloudHedge

Setting up Kubernetes API Access Using Service Account

Setting Up Kubernetes API Access

While using Kubernetes clusters of different distributions like – AKS, GKE, EKS, OpenShift, and ICP we need to give specific privileges to a specific user/user group. During this process, to give restricted access to a cluster we can make use of a service account.

Before we begin, it is assumed that you have already created a Kubernetes cluster and have configured kubectl to connect to it. Moreover, this blog post describes how service accounts can be set up and their behavior in a cluster which is recommended in a Kubernetes project.

Follow the below procedure to quickly setup up Kubernetes API Access using a Service Account:

$ kubectl create serviceaccount test-user
$ kubectl create clusterrolebinding test-user-binding --clusterrole=cluster-admin --serviceaccount=default:test-user
$ kubectl get secrets
NAME                    TYPE       DATA AGE
default-token-2kf6w kubernetes.io/service-account-token                      3           30d
k8s-nginx-ingress-token-h79rc kubernetes.io/service-account-token            3           21h
test-user-token-l8mrf         kubernetes.io/service-account-token            3           12m
$ kubectl describe secret test-user-token-l8mrf
Name: test-user-token-l8mrf
Namespace: default
Labels: <none>
Annotations: kubernetes.io/service-account.name=test-user
kubernetes.io/service-account.uid=aa1c318a-bc3d-11e8-b171-023b9d05d78
Type: kubernetes.io/service-account-token
Data
====
ca.crt: 33605 bytes
namespace: 7 bytes
token: eyJhb3ciOi . . . [output snipped]
$ export TOKEN=$(kubectl get secret test-user-token-l8mrf -o=jsonpath="{.data.token}" | base64 -D -i -)

Note:

$ curl -H "Authorization: Bearer $TOKEN" https://api.cluster-address/api/v1/pods -k
"kind": "PodList",
"apiVersion": "v1",
"metadata": {
"selfLink": "/api/v1/pods",
"resourceVersion": "4781803"
},
"items": [
  { ... [output snipped]

Results:

apiVersion: v1
clusters:
cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZ….[snipped]
server: https://xx.xxx.xx.xxx
name: gke_cluster-138506_us-east1-b_gc-test1
contexts:
context:
cluster: gke_cluster-138506_us-east1-b_gc-test1
user: gke_cluster-138506_us-east1-b_gc-test1
name: gke_cluster-138506_us-east1-b_gc-test1
current-context: gke_cluster-138506_us-east1-b_gc-test1
kind: Config
preferences: {}
users:
name: gke_cluster-138506_us-east1-b_gc-test1
user:
auth-provider:
config:
access-token: ya29.c.El_YBjiORx69G_PzzJ...[snippet]
cmd-args: config config-helper --format=json
cmd-path: path
expiry: "2019-03-26T06:42:04Z"
expiry-key: '{.credential.token_expiry}'
token-key: '{.credential.access_token}'
name: gcp
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0F….[snipped]
server: https://xxx.xxx.xx.XXX
name: gke_cluster-138506_us-east1-b_gc-test1
contexts:
context:
cluster: gke_cluster-138506_us-east1-b_gc-test1
user: test-user
name: gke_cluster-138506_us-east1-b_gc-test1
current-context: gke_cluster-138506_us-east1-b_gc-test1
kind: Config
preferences: {}
users:
name: test-user
user:
token: eyJhb3ciOi . . . [output snipped]

And that’s a wrap! CloudHedge uses BYOC (Bring Your Own Cluster) feature to import any Kubernetes Cluster during the migration and deployment of mission-critical apps from legacy to cloud. In my next blog post, I would be covering how to bring and deploy your own cluster using Cruize, a CloudHedge product. Contact us – hello@cloudhedge.io to migrate your age-old legacy applications to cloud.

Exit mobile version