[Yandex Cloud documentation](../../index.md) > [Yandex Managed Service for Kubernetes](../index.md) > [Tutorials](index.md) > Encrypting secrets in Managed Service for Kubernetes

# Encrypting secrets in Managed Service for Kubernetes

# Encrypting secrets in Yandex Managed Service for Kubernetes

Use [Yandex Key Management Service](../../kms/concepts/key.md) keys to encrypt [secrets](../../lockbox/concepts/secret.md), i.e., confidential information, such as passwords, OAuth tokens, and SSH keys, in [Yandex Managed Service for Kubernetes](../index.md). To do this, specify a Key Management Service key when creating a [Managed Service for Kubernetes cluster](../concepts/index.md#kubernetes-cluster). This key will be used for encryption and decryption.

Make sure you specify the encryption key when creating a Managed Service for Kubernetes cluster, as you cannot add it when updating the cluster.


## Required paid resources {#paid-resources}

The cost of support for the described solution includes a Key Management Service fee: number of active key versions (with `Active` or `Scheduled For Destruction` status) and completed cryptographic operations (see [Key Management Service pricing](../../kms/pricing.md)).


{% list tabs group=instructions %}

- Management console {#console}

  Specify a key when creating a Managed Service for Kubernetes cluster:
  1. In the [management console](https://console.yandex.cloud), select the [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) where you want to create a Managed Service for Kubernetes cluster.
  1. Navigate to **Managed Service for&nbsp;Kubernetes**.
  1. Click **Create cluster**.
  1. In the **Encryption key** field, enter the required key or [create a new one](../../kms/operations/key.md#create).
  1. Enter all the other parameters to [create your cluster](../operations/kubernetes-cluster/kubernetes-cluster-create.md).
  1. Click **Create**.

- CLI {#cli}

  If you do not have the Yandex Cloud CLI yet, [install and initialize it](../../cli/quickstart.md#install).

  The folder used by default is the one specified when [creating](../../cli/operations/profile/profile-create.md) the CLI profile. To change the default folder, use the `yc config set folder-id <folder_ID>` command. You can also specify a different folder for any command using `--folder-name` or `--folder-id`. If you access a resource by its name, the search will be limited to the default folder. If you access a resource by its ID, the search will be global, i.e., through all folders based on access permissions.

  You can specify a key when [creating](../operations/kubernetes-cluster/kubernetes-cluster-create.md) a Managed Service for Kubernetes cluster in two ways:

  - Using the key ID:

    ```bash
    yc managed-kubernetes cluster create \
      ...
      --kms-key-id <key_ID> \
      ...
    ```

  - Using the key name:

    ```bash
    yc managed-kubernetes cluster create \
      ...
      --kms-key-name <key_name> \
      ...
    ```

- Terraform {#tf}

  With [Terraform](https://www.terraform.io/), you can quickly create a cloud infrastructure in Yandex Cloud and manage it using configuration files. These files store the infrastructure description written in HashiCorp Configuration Language (HCL). If you change the configuration files, Terraform automatically detects which part of your configuration is already deployed, and what should be added or removed.
  
  Terraform is distributed under the [Business Source License](https://github.com/hashicorp/terraform/blob/main/LICENSE). The [Yandex Cloud provider for Terraform](https://github.com/yandex-cloud/terraform-provider-yandex) is distributed under the [MPL-2.0](https://www.mozilla.org/en-US/MPL/2.0/) license.
  
  For more information about the provider resources, see the guides on the [Terraform](https://www.terraform.io/docs/providers/yandex/index.html) website or [its mirror](../../terraform/index.md).

  If you do not have Terraform yet, [install it and configure the Yandex Cloud provider](../../tutorials/infrastructure-management/terraform-quickstart.md#install-terraform).
  
  
  To manage infrastructure using Terraform under a service account or user accounts (a Yandex account, a federated account, or a local user), [authenticate](../../terraform/authentication.md) using the appropriate method.

  Specify a key when [creating a Managed Service for Kubernetes cluster](../operations/kubernetes-cluster/kubernetes-cluster-create.md):
  1. Add a section named `kms_provider` to the Managed Service for Kubernetes cluster description:

     ```hcl
     resource "yandex_kubernetes_cluster" "<cluster_name>" {
       ...
       kms_provider {
         key_id = "<key_ID>"
       }
     }
     ```

  1. Make sure the configuration files are correct.

     1. In the command line, go to the folder where you created the configuration file.
     
     1. Run a check using this command:
     
        ```bash
        terraform plan
        ```
     
     If the configuration is described correctly, the terminal will display a list of created resources and their parameters. If the configuration contains any errors, Terraform will point them out. This is a test step; no resources will be created.

  1. Create a Managed Service for Kubernetes cluster.

     1. If the configuration does not contain any errors, run this command:
     
        ```bash
        terraform apply
        ```
     
     1. Confirm that you want to create the resources.
     
     After this, all required resources will be created in the specified folder and the IP addresses of the VMs will be displayed in the terminal. You can check the new resources and their configuration using the [management console](https://console.yandex.cloud).

- API {#api}

  Specify the encryption key when [creating](../operations/kubernetes-cluster/kubernetes-cluster-create.md) a Managed Service for Kubernetes cluster. To do this, use the [create](../managed-kubernetes/api-ref/Cluster/create.md) REST API method for the [Cluster](../managed-kubernetes/api-ref/Cluster/index.md) resource or the [ClusterService/Create](../managed-kubernetes/api-ref/grpc/Cluster/create.md) gRPC API call.

  Provide the key ID in the relevant `kmsProvider` field parameter.

{% endlist %}

Managed Service for Kubernetes works with Key Management Service using the [Key Management Service](https://kubernetes.io/docs/tasks/administer-cluster/kms-provider/) provider mechanism. Managed Service for Kubernetes supports the Key Management Service plugin which is used to encrypt and decrypt data encryption keys (DEK) in Key Management Service. Secrets are encrypted using standard Kubernetes tools.

## Useful links {#see-also}

* [Getting started with Managed Service for Kubernetes](../quickstart.md)
* [Envelope encryption](../../kms/concepts/envelope.md)
* [Creating secrets in Kubernetes](https://kubernetes.io/docs/concepts/configuration/secret/#creating-your-own-secrets)