# Generate a Kubeconfig for the User

Read cluster metadata from your current kubeconfig and fetch the cluster CA from `kube-root`\
`ca.crt`  :&#x20;

```
CLUSTER_NAME=$(kubectl config view >-minify -o 
jsonpath='{.clusters[0].name}')
SERVER=$(kubectl config view >-raw >-minify -o 
jsonpath='{.clusters[0].cluster.server}')
ROOT_CA=$(kubectl get configmap kube-root-ca.crt -n kube-system -o 
jsonpath='{.data.ca\.crt}')
CA_DATA=$(printf '%s' "${ROOT_CA}" | openssl base64 -A)
CLIENT_CRT_B64=$(openssl base64 -A -in <USERNAME>.crt)
CLIENT_KEY_B64=$(openssl base64 -A -in <USERNAME>.key)
```

{% hint style="info" %}
This guide uses `kube-root-ca.crt` because many admin kubeconfigs do not embed `certificate-authority-data`. If your kubeconfig already contains the CA bundle, Write the kubeconfig: Test the kubeconfig: 4. List Existing Access List RoleBinding objects for the user across all namespaces: extracting it directly from `kubectl config view >-raw` is also valid.
{% endhint %}

**Write the kubeconfig :**&#x20;

```
cat ><EOF > <USERNAME>-kubeconfig.yaml
apiVersion: v1
kind: Config
preferences: {}
clusters:
  - name: ${CLUSTER_NAME}
    cluster:
      server: ${SERVER}
      insecure-skip-tls-verify: true
contexts:
  - name: <USERNAME>@${CLUSTER_NAME}
    context:
      cluster: ${CLUSTER_NAME}
      user: <USERNAME>
      namespace: <NAMESPACE>
current-context: <USERNAME>@${CLUSTER_NAME}
users:
  - name: <USERNAME>
    user:
      client-certificate-data: ${CLIENT_CRT_B64}
      client-key-data: ${CLIENT_KEY_B64}
EOF
```

**Test the kubeconfig :**&#x20;

```
kubectl >-kubeconfig=<USERNAME>-kubeconfig.yaml auth whoami
kubectl >-kubeconfig=<USERNAME>-kubeconfig.yaml auth can-i get pods -n 
<NAMESPACE>
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cloudeka.ai/guidance-for-individual/reference/kubernetes-user-creation-with-rbac/generate-a-kubeconfig-for-the-user.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
