# Enroll a Kubernetes cluster

Enroll one Kubernetes cluster in Teleport and list pods from a beam through a local proxy.

Enroll one Kubernetes cluster in the same Teleport cluster that runs your beams. Then start a beam that can list pods in one namespace.

## Prerequisites

- A Teleport cluster with Beams enabled. You need permission to enroll Kubernetes resources, manage roles and create beams.
- A workstation with `tsh`, `tctl`, Helm and `kubectl`. Sign in to Teleport and configure `kubectl` with administrative access to the target Kubernetes cluster.
- A beam image with `kubectl` installed. The beam already has `tsh` and its own Teleport identity.

## Enroll the cluster

Follow Teleport's [guided Kubernetes enrollment](https://goteleport.com/docs/enroll-resources/kubernetes-access/getting-started/#step-23-follow-guided-enrollment-instructions):

1. In the Teleport Web UI, select **Enroll New Resource** → **Kubernetes**.
2. On your workstation, verify that `kubectl config current-context` points to the cluster you want to enroll. Run the chart repository command from the wizard.
3. Enter `teleport-agent` as the agent namespace and `example-k8s` as the cluster's display name.
4. Run the generated Helm command on your workstation. Add `--set labels.beams-access=example-k8s` to the Helm invocation. This sets the Teleport resource label used by the role below; it is not a Kubernetes object label.
5. Wait for the wizard to detect the new cluster. Keep any enrollment token in the generated command out of the beam and out of source control.

## Grant read-only pod access

On your workstation, save this as `beams-pod-reader.yaml`. This example uses the existing `default` namespace. Replace `default` in this file, the Teleport role and the proxy command if you want to use another namespace.

```yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: beams-pod-reader
  namespace: default
rules:
  - apiGroups: [""]
    resources: [pods]
    verbs: [get, list]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: beams-pod-reader
  namespace: default
subjects:
  - kind: Group
    name: beams-pod-readers
    apiGroup: rbac.authorization.k8s.io
roleRef:
  kind: Role
  name: beams-pod-reader
  apiGroup: rbac.authorization.k8s.io
```

Save this Teleport role as `beam-kube-reader.yaml`. This Teleport role permits only pod reads for the matching cluster and namespace. The Kubernetes role grants those reads to the group that Teleport impersonates.

```yaml
kind: role
version: v8
metadata:
  name: beam-kube-reader
spec:
  allow:
    kubernetes_labels:
      beams-access: example-k8s
    kubernetes_groups: [beams-pod-readers]
    kubernetes_resources:
      - kind: pods
        api_group: ""
        namespace: default
        name: "*"
        verbs: [get, list]
```

Apply both files on your workstation:

**Admin workstation**

```sh
kubectl apply -f beams-pod-reader.yaml
tctl create -f beam-kube-reader.yaml
```

Assign `beam-kube-reader` to your Teleport user through your user or SSO role mapping, then sign in again. Your cluster must allow you to [delegate the role](/docs/identity/delegation/#control-which-roles-can-be-delegated).

## List pods from a beam

Start a beam with the pod-reader role. If your default image doesn't include `kubectl`, add `--phase=kubectl-tools` and replace `kubectl-tools` with the name of a phase that provides it.

**Local terminal**

```sh
tsh beams add --roles=beam-kube-reader --no-share-connected-accounts
```

Inside the beam, confirm that Teleport lists `example-k8s`. Then start a local proxy for a single `kubectl` command:

**Inside the beam**

```sh
tsh kube ls
tsh proxy kube --exec-cmd=kubectl --exec-arg=get --exec-arg=pods --exec-arg=-n --exec-arg=default example-k8s
```

`tsh proxy kube` sets a temporary `KUBECONFIG` for `kubectl` and stops the proxy when the command exits. An empty pod list is valid if the namespace has no pods. For several commands, run `tsh proxy kube --exec example-k8s` to open a shell with the proxy configured, and exit that shell when finished.

Kubernetes isn't connected to VNet in a beam. Use `tsh proxy kube` for `kubectl` rather than `tsh kube login`; use `tsh kube ls` directly for discovery. Do not copy your workstation's kubeconfig into the beam.

When you're done, [delete the beam](/docs/identity/delegation/#3-end-access-when-the-task-is-done). For other enrollment methods, see [Teleport Auto-Discovery](https://goteleport.com/docs/enroll-resources/auto-discovery/).
