Skip to content

Commit 86e48d5

Browse files
authored
Merge pull request #203291 from shashankbarsin/main
Create service account token
2 parents cb7e0d9 + c915fb8 commit 86e48d5

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

articles/azure-arc/kubernetes/cluster-connect.md

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ az connectedk8s enable-features --features cluster-connect -n $CLUSTER_NAME -g $
117117
- If you are using Kubernetes native ClusterRoleBinding or RoleBinding for authorization checks on the cluster, with the `kubeconfig` file pointing to the `apiserver` of your cluster for direct access, you can create one mapped to the Azure AD entity (service principal or user) that needs to access this cluster. Example:
118118
119119
```console
120-
kubectl create clusterrolebinding admin-user-binding --clusterrole cluster-admin --user=$AAD_ENTITY_OBJECT_ID
120+
kubectl create clusterrolebinding demo-user-binding --clusterrole cluster-admin --user=$AAD_ENTITY_OBJECT_ID
121121
```
122122
123123
- If you are using Azure RBAC for authorization checks on the cluster, you can create an Azure role assignment mapped to the Azure AD entity. Example:
@@ -147,7 +147,7 @@ az connectedk8s enable-features --features cluster-connect -n $CLUSTER_NAME -g $
147147
- If you are using Kubernetes native ClusterRoleBinding or RoleBinding for authorization checks on the cluster, with the `kubeconfig` file pointing to the `apiserver` of your cluster for direct access, you can create one mapped to the Azure AD entity (service principal or user) that needs to access this cluster. Example:
148148
149149
```console
150-
kubectl create clusterrolebinding admin-user-binding --clusterrole cluster-admin --user=$AAD_ENTITY_OBJECT_ID
150+
kubectl create clusterrolebinding demo-user-binding --clusterrole cluster-admin --user=$AAD_ENTITY_OBJECT_ID
151151
```
152152
153153
- If you are using Azure RBAC for authorization checks on the cluster, you can create an Azure role assignment mapped to the Azure AD entity. Example:
@@ -165,47 +165,67 @@ az connectedk8s enable-features --features cluster-connect -n $CLUSTER_NAME -g $
165165
1. With the `kubeconfig` file pointing to the `apiserver` of your Kubernetes cluster, create a service account in any namespace (the following command creates it in the default namespace):
166166
167167
```console
168-
kubectl create serviceaccount admin-user
168+
kubectl create serviceaccount demo-user
169169
```
170170

171171
1. Create ClusterRoleBinding or RoleBinding to grant this [service account the appropriate permissions on the cluster](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#kubectl-create-rolebinding). Example:
172172

173173
```console
174-
kubectl create clusterrolebinding admin-user-binding --clusterrole cluster-admin --serviceaccount default:admin-user
174+
kubectl create clusterrolebinding demo-user-binding --clusterrole cluster-admin --serviceaccount default:demo-user
175175
```
176176

177-
1. Get the service account's token using the following commands:
177+
1. Create a service account token:
178178

179179
```console
180-
$SECRET_NAME=(kubectl get serviceaccount admin-user -o jsonpath='{$.secrets[0].name}')
180+
kubectl apply -f - <<EOF
181+
apiVersion: v1
182+
kind: Secret
183+
metadata:
184+
name: demo-user-secret
185+
annotations:
186+
kubernetes.io/service-account.name: demo-user
187+
type: kubernetes.io/service-account-token
188+
EOF
181189
```
182190

183191
```console
184-
$TOKEN=(kubectl get secret ${SECRET_NAME} -o jsonpath='{$.data.token}' | base64 -d | sed $'s/$/\\\n/g')
192+
$TOKEN=(kubectl get secret demo-user-secret -o jsonpath='{$.data.token}' | base64 -d | sed $'s/$/\\\n/g')
185193
```
186194

187195
### [Azure PowerShell](#tab/azure-powershell)
188196

189197
1. With the `kubeconfig` file pointing to the `apiserver` of your Kubernetes cluster, create a service account in any namespace (the following command creates it in the default namespace):
190198

191199
```console
192-
kubectl create serviceaccount admin-user
200+
kubectl create serviceaccount demo-user
193201
```
194202

195203
1. Create ClusterRoleBinding or RoleBinding to grant this [service account the appropriate permissions on the cluster](https://kubernetes.io/docs/reference/access-authn-authz/rbac/#kubectl-create-rolebinding). Example:
196204

197205
```console
198-
kubectl create clusterrolebinding admin-user-binding --clusterrole cluster-admin --serviceaccount default:admin-user
206+
kubectl create clusterrolebinding demo-user-binding --clusterrole cluster-admin --serviceaccount default:demo-user
199207
```
200208

201-
1. Get the service account's token using the following commands:
209+
1. Create a service account token by :
202210

203211
```console
204-
$SECRET_NAME = (kubectl get serviceaccount admin-user -o jsonpath='{$.secrets[0].name}')
212+
kubectl apply -f demo-user-secret.yaml
213+
```
214+
215+
Contents of `demo-user-secret.yaml`:
216+
217+
```yaml
218+
apiVersion: v1
219+
kind: Secret
220+
metadata:
221+
name: demo-user-secret
222+
annotations:
223+
kubernetes.io/service-account.name: demo-user
224+
type: kubernetes.io/service-account-token
205225
```
206226

207227
```console
208-
$TOKEN = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String((kubectl get secret $SECRET_NAME -o jsonpath='{$.data.token}'))))
228+
$TOKEN = ([System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String((kubectl get secret demo-user-secret -o jsonpath='{$.data.token}'))))
209229
```
210230

211231
---

0 commit comments

Comments
 (0)