Skip to main content

Machine & Workload Identity with Kubernetes Access

Report an Issue

Teleport protects and controls access to Kubernetes clusters. Machine & Workload Identity can be used to grant machines secure, short-lived access to these clusters.

In this guide, you will configure tbot to produce credentials that can be used to access a Kubernetes cluster enrolled with your Teleport cluster.

Prerequisites

  • A running Teleport cluster accessible at a hostname with a valid TLS certificate. If you want to get started with Teleport, sign up for a free trial or set up a demo environment.

  • The tctl and tsh clients.

    Installing tctl and tsh clients
    1. Determine the version of your Teleport cluster. The tctl and tsh clients must be at most one major version behind your Teleport cluster version. Send a GET request to the Proxy Service at /v1/webapi/find and use a JSON query tool to obtain your cluster version. Replace teleport.example.com:443 with the web address of your Teleport Proxy Service:

      TELEPORT_DOMAIN=teleport.example.com:443
      TELEPORT_VERSION="$(curl -s https://$TELEPORT_DOMAIN/v1/webapi/find | jq -r '.server_version')"
    2. Follow the instructions for your platform to install tctl and tsh clients:

      Download the signed macOS .pkg installer for Teleport, which includes the tctl and tsh clients:

      curl -O https://cdn.teleport.dev/teleport-${TELEPORT_VERSION?}.pkg

      In Finder double-click the pkg file to begin installation.

      danger

      Using Homebrew to install Teleport is not supported. The Teleport package in Homebrew is not maintained by Teleport and we can't guarantee its reliability or security.

    Connecting with TLS routing disabled

    This guide's commands assume your Teleport cluster uses TLS routing (proxy_listener_mode: multiplex), where the tctl and tsh clients reach every Teleport service through the Proxy Service's web address on port 443. If you're not sure whether this applies to your cluster, check with whoever manages it.

    If your cluster uses separate listener ports instead, adjust ports as follows:

    • tsh commands (e.g., tsh login --proxy=...): continue using the Proxy Service web address on port 3080 (or 443 if behind a load balancer). Do not change these to port 3025.

    • Direct tctl or Auth Service API commands: use port 3025 for the Auth Service gRPC listener:

      tctl status --auth-server=teleport.example.com:3025
  • If you have not already connected your Kubernetes cluster to Teleport, follow Enroll a Kubernetes Cluster.
  • Check that you can connect to your Teleport cluster and verify that you can run tctl and tsh commands using your current credentials.
    1. Assign teleport.example.com to the domain name of the Teleport Proxy Service in your cluster and [email protected] to your Teleport username.

    2. Authenticate to your Teleport cluster. This depends on whether your shell is interactive or not.

      In an interactive shell: Run the following command. By default, this triggers a multi-factor authentication prompt:

      tsh login --proxy=teleport.example.com --user=[email protected]
      tctl status

      Cluster teleport.example.com

      Version 19.0.0-dev

      CA pin sha256:abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678abdc1245efgh5678

      On non-interactive environments: If you are running tsh and tctl as an AI agent, in a CI/CD environment, or similar, make sure the TELEPORT_IDENTITY_FILE environment variable is assigned to a valid file path with credentials for your cluster. tsh and tctl read the file path from the environment variable and do not require a separate authentication step. If there is no identity file available, we recommend that you set up Machine ID to provision one automatically.

      When executing tctl commands with an identity file, you must pass the --auth-server flag to provide the Teleport Auth Service address, which is not included in the identity file. If you provide the Proxy Service address, tctl connects to the Proxy Service, which forwards traffic to and from the Teleport Auth Service. Update 443 to 3025 if you are contacting the Auth Service directly with tctl:

      tctl status --auth-server=teleport.example.com:443

      For tsh commands that read an identity file, you must pass the --proxy flag, which points tsh to the address of the Teleport Proxy Service:

      tsh status --proxy=teleport.example.com

      Ensure client commands can access your identity file. Replace path/to/identity/file with the path to your identity file:

      export TELEPORT_IDENTITY_FILE="${TELEPORT_IDENTITY_FILE:-path/to/identity/file}"

      Add the --auth-server or --proxy flags to all subsequent tctl and tsh commands.

    If you can connect to the cluster and run the tctl status command, you can use your current credentials to run subsequent tctl commands from your workstation. If you host your own Teleport cluster, you can also run tctl commands on the computer that hosts the Teleport Auth Service for full permissions.
  • To configure the Kubernetes cluster, your client system will need to have kubectl installed. See the Kubernetes documentation for installation instructions.
  • tbot must already be installed and configured on the machine that will access Kubernetes clusters. For more information, see the deployment guides.
  • To demonstrate connecting to the Kubernetes cluster, the machine that will access Kubernetes clusters will need to have kubectl installed. See the Kubernetes documentation for installation instructions.

Step 1/3. Configure Teleport and Kubernetes RBAC

First, we need to configure the RBAC for both Teleport and Kubernetes in order to grant the bot the correct level of access.

When forwarding requests to the Kubernetes API on behalf of a bot, the Teleport Proxy attaches the groups configured (using kubernetes_groups) in the bot's Teleport roles to the request. These groups are then used to configure a RoleBinding or ClusterRoleBinding in Kubernetes to grant specific permissions within the Kubernetes cluster to the bot.

For the purpose of this guide, we will bind the editor group to the default edit ClusterRole that is preconfigured in most Kubernetes clusters to give the bot read and write access to resources in all the cluster namespaces.

When configuring this for a production environment, you should consider:

  • If RoleBinding should be used instead of ClusterRoleBinding to limit the bot's access to a specific namespace.
  • If a Role should be created that grants the bot the least privileges necessary rather than using a pre-existing general Role such as edit.

To bind the editor group to the edit Cluster Role, execute:

kubectl create clusterrolebinding teleport-editor-edit \ --clusterrole=edit \ --group=editor

With the appropriate RoleBinding configured in Kubernetes to grant access to a specific group, you now need to add this group to the role that the bot will impersonate when producing credentials. You also need to grant the bot access through Teleport to the cluster itself. This is done by creating a role that grants the necessary permissions and then assigning this role to a Bot.

Create a file called role.yaml with the following content:

kind: role
version: v7
metadata:
  name: example-role
spec:
  allow:
    kubernetes_labels:
      '*': '*'
    kubernetes_groups:
    - editor
    kubernetes_resources:
    - kind: "*"
      namespace: "*"
      name: "*"
      verbs: ["*"]

Replace example-role with a descriptive name related to your use case.

Adjust the allow field for your environment:

  • kubernetes_labels should be adjusted to grant access to only the clusters that the bot will need to access. The value shown, '*': '*' will grant access to all Kubernetes clusters.
  • editor must match the name of the group you specified in the RoleBinding or ClusterRoleBinding.
  • kubernetes_resources can be used to apply additional restrictions to what the bot can access within the Kubernetes cluster. These restrictions are layered upon the RBAC configured within the Kubernetes role itself.

Use tctl create -f ./role.yaml to create the role.

tip

You can also create and edit roles using the Web UI. Go to Access -> Roles and click Create New Role or pick an existing role to edit.

Now, use tctl bots update to add the role to the Bot. Replace example with the name of the Bot you created in the deployment guide and example-role with the name of the role you just created:

tctl bots update example --add-roles example-role

Step 2/3. Configure a Kubernetes tbot output service

Now, tbot needs to be configured with an output service to produce the Kubernetes credentials and client configuration file. This is done using the kubernetes/v2 service type.

The Kubernetes clusters you wish to make available must be specified using entries in the selectors list. In this example, example-k8s-cluster will be selected using a name selector, and all clusters with the label environment=dev will be selected as well.

Output services must also be configured with a destination. In this example, the directory type will be used. This will write artifacts to a specified directory on disk. Ensure that this directory can be written to by the Linux user that tbot runs as, and that it can be read by the Linux user that will be accessing the Kubernetes cluster.

Modify your tbot configuration to add a kubernetes/v2 service:

services:
  - type: kubernetes/v2
    selectors:
      # Specify the name of the Kubernetes cluster you wish the credentials to
      # grant access to. Note that wildcards are not supported.
      - name: example-k8s-cluster

      # Specify a label selector to dynamically select many clusters at once.
      # All labels in a selector must match for a cluster to be selected, and
      # multiple separate selectors can be specified if desired. Note that
      # wildcards are not supported.
      - labels:
          environment: dev
    destination:
      type: directory
          # For this guide, /opt/machine-id is used as the destination directory.
          # You may wish to customize this. Multiple output services cannot
          # share the same destination.
      path: /opt/machine-id

Ensure you replace example-k8s-cluster with the name of the Kubernetes cluster as registered in Teleport and adjust /opt/machine-id if you wish.

Apply your configuration. The command to execute depends on how you are running tbot.

If you are running tbot as a background service, execute the following command, which instructs tbot to reload its configuration:

sudo systemctl reload tbot

If you are running tbot in one-shot mode, before you attempt to access your credentials later, you must execute tbot, pointing it to the location of its configuration file (/etc/tbot.yaml in the example below):

tbot start -c /etc/tbot.yaml

Step 3/3. Connect to your Kubernetes cluster

Once tbot has been run with the new service configured, a file called kubeconfig.yaml should have been generated in the destination directory you specified. This contains all the information necessary for kubectl to connect to the Kubernetes cluster through the Teleport Proxy.

To use kubeconfig.yaml with kubectl, the --kubeconfig flag or KUBECONFIG environment variable can be provided to kubectl:

kubectl --kubeconfig /opt/machine-id/kubeconfig.yaml get pods -A

Or, set the KUBECONFIG environment variable:

export KUBECONFIG=/opt/machine-id/kubeconfig.yaml
kubectl get pods -A

If you selected multiple clusters, they will be exposed as separate contexts within the generated kubeconfig.yaml, and will be named following the format $teleportClusterName-$kubeClusterName. To target a specific cluster, use the --context flag:

kubectl --kubeconfig /opt/machine-id/kubeconfig.yaml --context=example.teleport.sh-my-kube-cluster get pods -A

Note that the first selected cluster in tbot.yaml will be used as the default context. If using label selectors, the default context may vary over time if clusters are added or removed in Teleport.

If new matching clusters are added or removed in Teleport, kubeconfig.yaml will be regenerated to reflect the change on the bot's next certificate renewal. If needed, the tbot process can be restarted or signaled (pkill -USR1 tbot) to trigger an immediate reload. Note that modifications to kubeconfig.yaml, such as changes to the current-context field, will be overwritten.

Whilst this guide has demonstrated kubeconfig.yaml being used with kubectl, this format is compatible with most Kubernetes tools including:

  • Helm
  • Lens
  • ArgoCD

Next steps