> ## Documentation Index
> Fetch the complete documentation index at: https://docs.cloudthinker.io/llms.txt
> Use this file to discover all available pages before exploring further.

# Kubernetes

> Connect a Kubernetes cluster with a read-only kubeconfig so agents can triage failing pods, check node pressure, and review RBAC

Connect a Kubernetes cluster so CloudThinker can find crash-looping and pending pods, read logs and events, check node pressure, and review RBAC and network policies. You upload a **kubeconfig file** that signs in as a dedicated read-only service account; changes such as a rollout restart go through your workspace's Approvals, and run only if you grant write RBAC (see [Write access](#write-access)).

## Prerequisites

* A cluster whose API server CloudThinker can reach over the internet. Commands run in the CloudThinker cloud, so a private-only endpoint fails the connection test. If you restrict API access (EKS public access CIDRs, GKE authorized networks, AKS authorized IP ranges), add CloudThinker's egress IPs instead of opening it to `0.0.0.0/0`. These docs don't publish CloudThinker's egress IP list: ask your CloudThinker contact, or [contact us](https://cloudthinker.io/contact). See [Reaching private services](https://docs.cloudthinker.io/guide/connections/overview.md#reaching-private-services) for other options.
* `kubectl` access to that cluster with permission to create a namespace, a ClusterRole, and a ClusterRoleBinding.
* Managed clusters (EKS, GKE, AKS) hand out kubeconfigs that sign in through `aws`, `gcloud`, or `az` exec plugins, which don't work in CloudThinker. Step 2 builds one that does. Kubernetes checks the service-account token itself, so you don't map it in cloud IAM (no EKS access entry, for example).
* Optional: [Metrics Server](https://github.com/kubernetes-sigs/metrics-server) in the cluster. Without it, agents still report pod status and node conditions, but not live CPU and memory usage.
* The **Admin** role in your CloudThinker workspace, which can add and edit connections. See [Roles](https://docs.cloudthinker.io/guide/security/roles.md).

> [!NOTE]
>
> You get **one** Kubernetes connection per workspace, and agents work in the kubeconfig's **current context**. Upload a kubeconfig for the one cluster you want inspected. For several clusters, use [one workspace per cluster](https://docs.cloudthinker.io/guide/workspaces.md#create-a-workspace), or add a [Rancher connection](https://docs.cloudthinker.io/guide/connections/rancher.md) for inventory and health across the clusters Rancher manages.

## Setup

**1. Create a read-only service account**

Save this manifest as `cloudthinker-reader.yaml`. It also creates one Secret, the service account's token, which only you read in step 2. [Required permissions](#required-permissions) covers what the role allows.

> [accordion] cloudthinker-reader.yaml
>
> ```yaml theme={null}
> apiVersion: v1
> kind: Namespace
> metadata:
>   name: cloudthinker
> ---
> apiVersion: v1
> kind: ServiceAccount
> metadata:
>   name: cloudthinker-reader
>   namespace: cloudthinker
> ---
> apiVersion: rbac.authorization.k8s.io/v1
> kind: ClusterRole
> metadata:
>   name: cloudthinker-reader
> rules:
> - apiGroups: [""]
>   resources: ["pods", "pods/log", "nodes", "namespaces", "services", "endpoints", "events", "configmaps", "persistentvolumeclaims", "persistentvolumes", "resourcequotas", "limitranges", "serviceaccounts", "replicationcontrollers"]
>   verbs: ["get", "list", "watch"]
> - apiGroups: ["apps"]
>   resources: ["deployments", "replicasets", "statefulsets", "daemonsets"]
>   verbs: ["get", "list", "watch"]
> - apiGroups: ["batch"]
>   resources: ["jobs", "cronjobs"]
>   verbs: ["get", "list", "watch"]
> - apiGroups: ["autoscaling"]
>   resources: ["horizontalpodautoscalers"]
>   verbs: ["get", "list", "watch"]
> - apiGroups: ["networking.k8s.io"]
>   resources: ["ingresses", "networkpolicies"]
>   verbs: ["get", "list", "watch"]
> - apiGroups: ["policy"]
>   resources: ["poddisruptionbudgets"]
>   verbs: ["get", "list", "watch"]
> - apiGroups: ["storage.k8s.io"]
>   resources: ["storageclasses"]
>   verbs: ["get", "list", "watch"]
> - apiGroups: ["rbac.authorization.k8s.io"]
>   resources: ["roles", "rolebindings", "clusterroles", "clusterrolebindings"]
>   verbs: ["get", "list", "watch"]
> - apiGroups: ["metrics.k8s.io"]
>   resources: ["pods", "nodes"]
>   verbs: ["get", "list"]
> ---
> apiVersion: rbac.authorization.k8s.io/v1
> kind: ClusterRoleBinding
> metadata:
>   name: cloudthinker-reader
> roleRef:
>   apiGroup: rbac.authorization.k8s.io
>   kind: ClusterRole
>   name: cloudthinker-reader
> subjects:
> - kind: ServiceAccount
>   name: cloudthinker-reader
>   namespace: cloudthinker
> ---
> apiVersion: v1
> kind: Secret
> metadata:
>   name: cloudthinker-reader-token
>   namespace: cloudthinker
>   annotations:
>     kubernetes.io/service-account.name: cloudthinker-reader
> type: kubernetes.io/service-account-token
> ```

Apply it:

```bash theme={null}
kubectl apply -f cloudthinker-reader.yaml
```

It creates the `cloudthinker` namespace, a `cloudthinker-reader` service account, a read-only ClusterRole bound to it, and a long-lived token Secret.

**Success state:** `kubectl get secret cloudthinker-reader-token -n cloudthinker` lists the Secret with a `DATA` count of 3. Kubernetes fills the token in a few seconds after you apply, so if `DATA` shows 0, wait and run the command again.

**2. Build a kubeconfig for that account**

Run this from a shell whose current `kubectl` context is the target cluster. First, read and check the API server address:

```bash theme={null}
SERVER=$(kubectl config view --minify -o jsonpath='{.clusters[0].cluster.server}')
echo $SERVER
```

It must print a public hostname or IP. If it shows `127.0.0.1`, `localhost`, or a private address such as `10.x` or `192.168.x` (common on kind, minikube, k3d, or behind a local proxy), CloudThinker can't reach it, so stop and fix access first.

Then, in the same shell, write `cloudthinker-kubeconfig.yaml` with the token and CA certificate embedded:

```bash theme={null}
CA=$(kubectl get secret cloudthinker-reader-token -n cloudthinker -o jsonpath='{.data.ca\.crt}')
TOKEN=$(kubectl get secret cloudthinker-reader-token -n cloudthinker -o jsonpath='{.data.token}' | base64 --decode)

cat > cloudthinker-kubeconfig.yaml <<EOF
apiVersion: v1
kind: Config
clusters:
- name: target
  cluster:
    server: ${SERVER}
    certificate-authority-data: ${CA}
users:
- name: cloudthinker-reader
  user:
    token: ${TOKEN}
contexts:
- name: cloudthinker
  context:
    cluster: target
    user: cloudthinker-reader
current-context: cloudthinker
EOF
```

On older macOS releases, replace `base64 --decode` with `base64 -D`, and keep the closing `EOF` at the very start of its line.

**Success state:** `kubectl --kubeconfig cloudthinker-kubeconfig.yaml get nodes` lists your nodes. This is the same command CloudThinker runs to test the connection.

**3. Upload the kubeconfig in CloudThinker**

Open **More → Workspace settings → Connections**, select **Kubernetes**, and upload `cloudthinker-kubeconfig.yaml` in the **KUBECONFIG** field. Click **Connect**.

**Success state:** the connection shows **Connecting** and **Verifying**, then a **Connected** status.

## Connection details

| Field | Required | Description | Example |
| - | - | - | - |
| **KUBECONFIG** | Yes | A kubeconfig file. The file picker accepts the `.yaml` extension only, so a `.yml` file can't be selected. Rename it to `.yaml` first. CloudThinker uses its current context | `cloudthinker-kubeconfig.yaml` |

Embed everything in the file. A kubeconfig that points to local certificate files, or signs in through an `exec` plugin, needs files and tools CloudThinker doesn't have. The token-based file from step 2 avoids both.

## Required permissions

The manifest in [step 1](#setup) grants read access (`get`, `list`, `watch`) to the resources agents inspect, plus pod logs and metrics. It can't read Secrets.

Read access is cluster-wide, including `kube-system`, because a ClusterRoleBinding grants the role in every namespace. To cover only some namespaces, replace the ClusterRoleBinding with a RoleBinding to the same ClusterRole in each one. Cluster-scoped resources such as nodes, namespaces, and persistent volumes then drop out of answers, since a RoleBinding can't grant them.

Custom resources aren't covered, so agents can't see CRD-based objects such as Argo CD applications or Istio virtual services. To let them, add a rule with each resource's API group to the ClusterRole.

> [!TIP]
>
> Trim the role to what you need. Drop `configmaps` if yours hold sensitive values, and drop the `rbac.authorization.k8s.io` rule if you don't want RBAC reviews. A `403` in an answer names the resource the role is missing.

## Write access

Agents run a change such as `scale`, `rollout restart`, `cordon`, `drain`, or `apply` only when you ask for it in the conversation, and the read-only role above makes it fail with `403`. **Allow writes** on the connection's detail panel is the second control. It's on by default, but it can't widen what the role allows.

| RBAC on the `cloudthinker-reader` service account | Allow writes | Result |
| - | - | - |
| Read-only rules only | On or off | Read-only. Kubernetes rejects every change with `403`. |
| Write rules granted | Off | Read-only. CloudThinker refuses every change before it reaches the cluster. |
| Write rules granted | On | Agents can propose changes. Your [Rules and Approval mode](https://docs.cloudthinker.io/guide/approvals/overview.md) decide each one. |

To lock writes in CloudThinker as well, open **More → Workspace settings → Connections**, select **Kubernetes**, and turn off the **Allow writes** switch on its detail panel.

Every change an agent makes goes through your workspace's [Approvals](https://docs.cloudthinker.io/guide/approvals/overview.md) first. On the default **Auto · Rules + AI · Ask me** mode, AI judgment can let a safe change run without asking — and an Allow Rule can too, once CloudThinker turns on [Rule enforcement](https://docs.cloudthinker.io/guide/approvals/rules.md#troubleshooting) for your workspace. Choose **Manual** to approve every change yourself.

To allow a specific change, grant only what it needs:

* **Scale and rollout restart** act on namespaced workloads. Use a Role in each namespace involved, with `patch` on `deployments` and `get` plus `patch` on `deployments/scale`. For StatefulSets, add the same for `statefulsets` and `statefulsets/scale`. For DaemonSets, add `daemonsets` (restart only, since they don't scale).
* **Apply** needs `get`, `create`, and `patch` on each resource type in the manifest you apply, in the namespaces it targets.
* **Cordon and drain** act on nodes, which are cluster-scoped, so a namespaced Role can't allow them. Use a ClusterRole with `patch` on `nodes` and `create` on `pods/eviction`. The read role already supplies the `get` and `list` on pods and DaemonSets that drain needs.

The write grants bind to the same `cloudthinker-reader` service account, so one kubeconfig covers reads and changes. If you plan write access from the start, rename the account (for example `cloudthinker-agent`) in both manifests and in step 2.

This example allows scaling deployments in one namespace, plus cordon and drain. Replace `payments` with your namespace, save it as `cloudthinker-write.yaml`, and run `kubectl apply -f cloudthinker-write.yaml`.

```yaml theme={null}
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: cloudthinker-scale
  namespace: payments
rules:
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["patch"]
- apiGroups: ["apps"]
  resources: ["deployments/scale"]
  verbs: ["get", "patch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: cloudthinker-scale
  namespace: payments
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: cloudthinker-scale
subjects:
- kind: ServiceAccount
  name: cloudthinker-reader
  namespace: cloudthinker
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: cloudthinker-node-ops
rules:
- apiGroups: [""]
  resources: ["nodes"]
  verbs: ["patch"]
- apiGroups: [""]
  resources: ["pods/eviction"]
  verbs: ["create"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: cloudthinker-node-ops
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cloudthinker-node-ops
subjects:
- kind: ServiceAccount
  name: cloudthinker-reader
  namespace: cloudthinker
```

**Success state:** `kubectl auth can-i patch deployments/scale -n payments --as=system:serviceaccount:cloudthinker:cloudthinker-reader` prints `yes`.

## Agent capabilities

| Capability | What agents do |
| - | - |
| **Workload triage** | Find crash-looping, pending, and restarting pods, degraded deployments, and warning events, then read logs to explain why |
| **Node pressure** | Report node conditions, capacity, and allocatable resources, and the top CPU and memory consumers when Metrics Server is installed |
| **Capacity and scheduling** | Compare requests and limits with usage, and explain why pods cannot be scheduled |
| **Security and RBAC** | Review role bindings, service accounts, network policies, and privileged workloads |
| **Configuration** | Inspect ingresses, services, storage, autoscalers, and images across namespaces. Custom resources need a rule added to the role |
| **Changes** | Scale, restart, cordon, drain, or apply. Each change goes through Approvals |

### Verify the connection

```text theme={null}
Check the Kubernetes connection: list the namespaces, the node count, and any nodes that are not Ready
```

You get the namespace list, node count, and node status. If usage numbers are missing, the answer says Metrics Server is not installed.

### Example prompts

```text theme={null}
Which pods in the payments namespace are crash-looping, and what do their logs say?
Which nodes are under memory or disk pressure, and what is using the most memory?
Which service accounts are bound to cluster-admin?
```

## Rotate the token

Kubernetes is a single-connection service, so it has no pencil icon or **Edit Instance** dialog while it's healthy. Create the new token first and delete the old Secret last: deleting it first cuts the cluster off before you have a working replacement.

**1. Create a second token Secret**

Save this as `cloudthinker-reader-token-2.yaml`: a second token for the same service account, so both work while you swap.

```yaml theme={null}
apiVersion: v1
kind: Secret
metadata:
  name: cloudthinker-reader-token-2
  namespace: cloudthinker
  annotations:
    kubernetes.io/service-account.name: cloudthinker-reader
type: kubernetes.io/service-account-token
```

Apply it:

```bash theme={null}
kubectl apply -f cloudthinker-reader-token-2.yaml
```

**Success state:** `kubectl get secret cloudthinker-reader-token-2 -n cloudthinker` lists the Secret with a `DATA` count of 3.

**2. Build a kubeconfig from the new token**

Repeat step 2 of [Setup](#setup) with `cloudthinker-reader-token-2` in place of `cloudthinker-reader-token`.

**Success state:** `kubectl --kubeconfig cloudthinker-kubeconfig.yaml get nodes` lists your nodes.

**3. Swap it in CloudThinker**

While the connection shows **Connected**, click **Disconnect** on the **Kubernetes** detail panel, then connect again and upload the new kubeconfig. While it shows **Error**, click **Replace credentials** on the detail panel and upload the new kubeconfig instead, with no disconnect.

**Success state:** the connection shows **Connected**.

**4. Delete the old Secret**

Only now, remove the token you replaced:

```bash theme={null}
kubectl delete secret cloudthinker-reader-token -n cloudthinker
```

A disconnect and reconnect starts a new connection:

* Agents and scheduled [automations](https://docs.cloudthinker.io/guide/automation/automations.md) can't use the cluster between the disconnect and the reconnect.
* Discovery runs again and spends credits.
* **Allow writes** goes back to on. Turn it off again if you had it off.
* Check that [Rules](https://docs.cloudthinker.io/guide/approvals/rules.md) naming the Kubernetes connection still list it.

If your policy bans long-lived credentials, skip the Secret and build the kubeconfig with a short-lived token from `kubectl create token cloudthinker-reader -n cloudthinker --duration=<time>`. The connection breaks when that token expires, so you rebuild and upload the kubeconfig each time.

## Troubleshooting

A failed test shows `Failed to connect to Kubernetes:` followed by the first error line from `kubectl get nodes`. Match that line to an entry below.

> [accordion] Unable to connect to the server
>
> CloudThinker could not reach the API server in the kubeconfig. Check that the `server` address is reachable from the internet, not only from your VPN, and that any API server allowlist permits outside access. Run `kubectl --kubeconfig cloudthinker-kubeconfig.yaml get nodes` from a machine outside your network to confirm.

> [accordion] error: You must be logged in to the server (Unauthorized)
>
> The token is wrong, expired, or was revoked. Check that the `cloudthinker-reader-token` Secret still exists, then rebuild the kubeconfig from step 2 and upload it through [Rotate the token](#rotate-the-token).

> [accordion] Error from server (Forbidden) in an answer
>
> The ClusterRole doesn't cover that resource. Confirm the ClusterRoleBinding is applied, then add the resource to the role.

> [accordion] getting credentials: exec, or no such file or directory
>
> The kubeconfig relies on a local command or file path. Replace it with the token-based kubeconfig from step 2.

> [accordion] No CPU or memory usage in answers
>
> Metrics Server is not installed or not ready. Run `kubectl top nodes` to check. Pod status, events, and node conditions still work without it.

> [accordion] Connection 'k8s' already exists. Only one instance is allowed.
>
> `k8s` is the Kubernetes connection, and a workspace holds one. There's no pencil icon to edit it, so to point CloudThinker at a different cluster, click **Disconnect** on the **Kubernetes** detail panel and connect again with the other kubeconfig.

## Security

* **Least privilege** — grant only the permissions the agents need for your use case; start read-only and widen later.
* **Read-only by default** — use read-only credentials unless you want agents to make changes through this connection.
* **Rotate credentials** — rotate keys and tokens on your normal schedule; CloudThinker picks up the new value when you update the connection.
* **Revoke on offboarding** — remove the credential at the provider when you delete a connection or a teammate leaves.

- **Keep Secrets out of the role.** The manifest above never grants Secret reads, so agents can't read Secret objects. Pod logs and ConfigMaps are readable, so drop `pods/log` or `configmaps` from the role if they hold sensitive data. Env values set directly in a pod spec are readable through `pods`, so move sensitive ones into Secrets.
- **Rotate the token.** The token Secret never expires on its own, so rotate it on your normal schedule. See [Rotate the token](#rotate-the-token).
- **Revoke in one command.** Delete the token Secret to cut access immediately, or delete the `cloudthinker` namespace plus the ClusterRole and binding to remove everything.

## Related

> [card] [Rancher](https://docs.cloudthinker.io/guide/connections/rancher.md)
>
> Read cluster inventory and access across clusters Rancher manages

> [card] [Kubernetes Health Monitoring](https://docs.cloudthinker.io/guide/use-cases/kubernetes-health-monitoring.md)
>
> Put this connection to work on a recurring cluster health check

> [card] [Approvals](https://docs.cloudthinker.io/guide/approvals/overview.md)
>
> Decide which cluster changes run, wait for a person, or are refused

> [card] [Connections Overview](https://docs.cloudthinker.io/guide/connections/overview.md)
>
> How connection status, testing, and discovery work


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.