# Kubernetes clusters

Give an MCP client read-only access to a Kubernetes cluster through Teleport, from your dev computer or from inside a beam.



In this guide, you give an MCP client access to a Kubernetes cluster that is enrolled in Teleport. The client runs the open source [`kubernetes-mcp-server`](https://github.com/containers/kubernetes-mcp-server), which talks to the Kubernetes API through Teleport with a short-lived Teleport certificate. The client never sees a cluster token or a long-lived kubeconfig.

Language models can call any tool the MCP server offers, so the agent gets a read-only role. Teleport allows only read verbs, Kubernetes RBAC allows only the built-in `view` permissions, and the MCP server runs in read-only mode.

You connect in one of two ways:

- **From your dev computer:** your MCP client runs locally, and `tsh kube login` points it at the cluster.
- **From inside a beam:** your agent runs in a beam, which reaches the cluster with the role you delegate to it.

## How it works

1. The Teleport Kubernetes Service runs next to your cluster and connects out to Teleport. Beams can't run a Kubernetes cluster, so the cluster runs on your own infrastructure.
2. Your MCP client starts `kubernetes-mcp-server` as a local stdio server. The server reads the kubeconfig that `tsh` writes, which sends every request to the Teleport proxy.
3. When the client calls a tool, the server makes Kubernetes API requests. Teleport checks your roles for the cluster, the resource and the verb, then forwards the request, impersonating the Kubernetes groups in your role. Kubernetes RBAC applies on top.

Teleport records each Kubernetes API request as a `kube.request` event in the audit log. Unlike the [internal services](/docs/mcp/internal-services/) and [databases](/docs/mcp/databases/) guides, Teleport doesn't see MCP tool calls here. It enforces access at the Kubernetes API.

## Prerequisites

- A cluster with Teleport Beams enabled.
- A Kubernetes cluster enrolled in Teleport. See [Enroll a Kubernetes cluster](https://goteleport.com/docs/enroll-resources/kubernetes-access/getting-started/). This guide assumes it has the label `env: dev`.
- Permission to create roles and edit your own user, for example with the `editor` role.
- `kubectl` access to the Kubernetes cluster as an administrator, to create a role binding.
- `tsh`, `tctl` and `jq`, and Node.js 20 or later for `npx`.
- An MCP client that can start a stdio command. The examples use [Claude Code](https://code.claude.com/docs/en/quickstart). On your dev computer, install it and sign in. Beams include Claude Code and Node.js, and Claude Code is already connected to the LLM gateway.

**Install the client tools and sign in**

Beams is a [Teleport](https://goteleport.com) project and uses [official Teleport client tools](https://goteleport.com/download/client-tools/).

The client tools include `tsh` to sign in and run commands, and `tctl` to administer your cluster if you have permission.



Cluster: `example.beams.run`. Edit this example to use your cluster name.

Install version **18.11.10** of the client:

**macOS**

Download and open the macOS installer, then follow its instructions.

```sh
curl -O https://cdn.teleport.dev/teleport-tools-18.11.10.pkg
open teleport-tools-18.11.10.pkg
```

**Linux**

For Linux x86-64, download and install the client tools:

```sh
curl -O https://cdn.teleport.dev/teleport-v18.11.10-linux-amd64-bin.tar.gz
tar -xzf teleport-v18.11.10-linux-amd64-bin.tar.gz
sudo ./teleport/install
```

**Windows**

For Windows x86-64, run these commands in PowerShell. Add the extraction directory (`$env:LOCALAPPDATA\Teleport`) to your user PATH to use `tsh` in future terminals; the last command adds it to this terminal's PATH.

```powershell
Invoke-WebRequest -Uri https://cdn.teleport.dev/teleport-v18.11.10-windows-amd64-bin.zip -OutFile teleport.zip
Expand-Archive -Path teleport.zip -DestinationPath "$env:LOCALAPPDATA\Teleport" -Force
$env:Path += ";$env:LOCALAPPDATA\Teleport"
```

Follow the official client [tools install guide](https://goteleport.com/download/client-tools/) for other versions and architectures.

Check that you’re running v18.11.10 of the client with `tsh version`.

```sh
tsh version
```

**Passwordless**

I sign in without a password (e.g. a passkey).

```sh
tsh login --proxy='example.beams.run' --auth=passwordless
```

**Password \& MFA**

I have a password & MFA set up for my account:

Replace `you@example.com` with your Teleport username.

```sh
tsh login --proxy='example.beams.run' --user=you@example.com
```

**SSO**

I sign in with SSO (e.g. Okta, GitHub, SAML).

```sh
tsh login --proxy='example.beams.run'
```

## 1. Create a read-only role

1. In the Kubernetes cluster, bind a group to the built-in `view` ClusterRole. `view` can read most resources but not Secrets. Save this as `beams-agent-view.yaml`:

   ```yaml
   apiVersion: rbac.authorization.k8s.io/v1
   kind: ClusterRoleBinding
   metadata:
     name: beams-agent-view
   subjects:
     - kind: Group
       name: beams-agent-view
       apiGroup: rbac.authorization.k8s.io
   roleRef:
     kind: ClusterRole
     name: view
     apiGroup: rbac.authorization.k8s.io
   ```

   **Admin terminal**

   ```sh
   kubectl apply -f beams-agent-view.yaml
   ```

2. In Teleport, create a role that reaches clusters labelled `env: dev` as that group, with read verbs only. Save it as `kube-agent-readonly.yaml`:

   ```yaml
   kind: role
   version: v8
   metadata:
     name: kube-agent-readonly
   spec:
     allow:
       kubernetes_labels:
         env: dev
       kubernetes_groups:
         - beams-agent-view
       kubernetes_resources:
         # Namespaced resources, such as pods and deployments
         - kind: '*'
           api_group: '*'
           namespace: '*'
           name: '*'
           verbs: [get, list, watch]
         # Cluster-wide resources, such as namespaces and nodes
         - kind: '*'
           api_group: '*'
           namespace: ''
           name: '*'
           verbs: [get, list, watch]
   ```

   Change `kubernetes_labels` to match your cluster's labels. To limit the agent to some namespaces, list them in `namespace`. See [Kubernetes access controls](https://goteleport.com/docs/enroll-resources/kubernetes-access/controls/).

3. Create the role and add it to your user while keeping your existing roles. You can delegate only roles that you hold:

   **Local terminal**

   ```sh
   tctl create -f kube-agent-readonly.yaml
   me=$(tsh status --format=json | jq -r .active.username)
   roles=$(tctl get "users/$me" --format=json | jq -r '.[0].spec.roles | join(",")')
   tctl users update "$me" --set-roles="$roles,kube-agent-readonly"
   ```

4. Log in again, so that your session includes the new role.

## 2. Configure the MCP server

`kubernetes-mcp-server` reads its settings from a TOML file. Create `kube-mcp.toml` in your project directory. Read-only mode hides the tools that create, change or delete resources:

```toml
read_only = true
toolsets = ["core"]
```

The `core` toolset lists and reads pods, events, namespaces, nodes and other resources, and reads pod logs. To add Helm tools, add `"helm"` to `toolsets`. See the [configuration reference](https://github.com/containers/kubernetes-mcp-server/blob/main/docs/configuration.md).

The examples pin the server version. Newer versions may change configuration options or tools.

## 3. Connect from your dev computer

1. List the clusters you can reach, and log in to one. Replace `my-cluster` with its name:

   **Local terminal**

   ```sh
   tsh kube ls
   tsh kube login my-cluster
   ```

   `tsh kube login` updates your default kubeconfig, which the MCP server reads. The kubeconfig uses `tsh` to get short-lived certificates.

2. In the project directory with `kube-mcp.toml`, add the MCP server to the project's Claude Code configuration:

   **Local terminal**

   ```sh
   claude mcp add kubernetes -s project -- npx -y kubernetes-mcp-server@0.1.3 --config "$PWD/kube-mcp.toml"
   ```

   For other clients, add the same command to the client's MCP configuration. For example, in Cursor's `mcp.json`:

   ```json
   {
     "mcpServers": {
       "kubernetes": {
         "command": "npx",
         "args": ["-y", "kubernetes-mcp-server@0.1.3", "--config", "/path/to/kube-mcp.toml"]
       }
     }
   }
   ```

   In VS Code, run `code --add-mcp` with the same command, name and arguments.

3. Start Claude Code in the project directory and approve the `kubernetes` server when it asks. Then check that it's connected:

   **Local terminal**

   ```sh
   claude mcp list
   ```

4. Ask Claude Code:

   ```text
   Using the kubernetes MCP tools, list the namespaces and the pods in the
   default namespace, then delete one of the pods.
   ```

   It lists the namespaces and pods but has no tool to delete them. Even without read-only mode, Teleport rejects the delete with `403 Forbidden`, because the role allows only `get`, `list` and `watch`.

## 4. Connect from inside a beam

An agent beam reaches the cluster with the roles you delegate, not your own. Inside a beam, `tsh` is already signed in as the beam identity. `tsh kube login` doesn't work in a beam. Use `tsh proxy kube --exec` instead, which starts a local proxy and runs a command with `KUBECONFIG` pointing at it.

1. Start the agent beam with only the read-only role, and open its shell:

   **Local terminal**

   ```sh
   tsh beams add --roles=kube-agent-readonly
   ```

2. Inside the beam, check that the cluster is listed, then create the MCP server configuration and add the server to Claude Code:

   **Inside the agent beam**

   ```sh
   tsh kube ls
   printf 'read_only = true\ntoolsets = ["core"]\n' > kube-mcp.toml
   claude mcp add kubernetes -- npx -y kubernetes-mcp-server@0.1.3 --config "$PWD/kube-mcp.toml"
   ```

3. Start Claude Code through the Kubernetes proxy, in a mode that skips permission prompts. Replace `my-cluster` with the cluster's name. Teleport RBAC and Kubernetes RBAC still limit everything it does.

   **Inside the agent beam**

   ```sh
   tsh proxy kube my-cluster --exec --exec-cmd claude --exec-arg=--dangerously-skip-permissions
   ```

   Claude Code and the MCP server it starts inherit `KUBECONFIG` from the proxy. The proxy stops when you quit Claude Code.

4. Enter the following prompt:

   ```text
   Using the kubernetes MCP tools, find any pods that are not running in the
   default namespace and show their recent events and logs. Then restart them.
   ```

The agent reads pods, events and logs but can't restart anything:

- In read-only mode, the MCP server has no tools that change resources.
- If the agent calls the Kubernetes API another way, Teleport rejects anything other than `get`, `list` and `watch`.
- Kubernetes RBAC denies reading Secrets, because the `view` ClusterRole doesn't include them.

The agent may then file a [Just-in-Time access request](/docs/identity/access-requests/) for more access. Nothing changes unless a reviewer approves the request.

## Teleport behind a TLS-terminating load balancer

If a load balancer or reverse proxy in front of Teleport terminates TLS, `tsh kube login` can't reach the cluster directly. Use a local proxy on your dev computer too:

**Local terminal**

```sh
tsh proxy kube my-cluster --exec --exec-cmd claude
```

Alternatively, start a proxy on a fixed port with `tsh proxy kube my-cluster -p 8888`, and set `kubeconfig` in `kube-mcp.toml` to the kubeconfig path it prints. [Teleport Connect](https://goteleport.com/docs/connect-your-client/teleport-clients/teleport-connect/) can also run the proxy. Its terminal sets `KUBECONFIG`.

## Clean up

1. On your dev computer, remove the MCP server from the project configuration:

   **Local terminal**

   ```sh
   claude mcp remove kubernetes -s project
   ```

2. Delete the agent beam:

   **Local terminal**

   ```sh
   tsh beams ls
   tsh beams rm <agent-beam>
   ```

3. Remove the role from your user, then delete it:

   **Local terminal**

   ```sh
   me=$(tsh status --format=json | jq -r .active.username)
   roles=$(tctl get "users/$me" --format=json | jq -r '[.[0].spec.roles[] | select(. != "kube-agent-readonly")] | join(",")')
   tctl users update "$me" --set-roles="$roles"
   tctl rm roles/kube-agent-readonly
   ```

4. Remove the Kubernetes role binding:

   **Admin terminal**

   ```sh
   kubectl delete clusterrolebinding beams-agent-view
   ```

## Troubleshoot

- **The cluster doesn't appear in `tsh kube ls`.** Your roles don't match its labels. Check `kubernetes_labels` against the labels `tsh kube ls` shows to an administrator.
- **`403 Forbidden` for every request.** The Teleport role allows the request, but the Kubernetes group has no permissions. Check the role binding with `kubectl get clusterrolebinding beams-agent-view -o yaml`.
- **The MCP server fails with "unknown flag".** Recent versions of `kubernetes-mcp-server` read settings such as `read_only` and `kubeconfig` from the TOML file, not from flags.
- **Tool calls fail after a while.** The `tsh` session expired. Run `tsh login` on your dev computer, then retry the call. In a beam, restart `tsh proxy kube`.
