[Yandex Cloud documentation](../../../index.md) > [Yandex Managed Service for Kubernetes](../../index.md) > [Step-by-step guides](../index.md) > Managing a node group > Managing Kubernetes node labels

# Managing Kubernetes node labels

Individual nodes in node groups are Yandex Compute Cloud virtual machines with automatically generated names. To configure nodes, follow the steps below or [other node group management guides](../index.md#node-group).

{% note alert %}

Do not change node VM settings, including names, network interfaces, and SSH keys, using the Compute Cloud interfaces or SSH connections to the VM.

This can disrupt the operation of individual nodes, node groups, and the whole Managed Service for Kubernetes cluster.

{% endnote %}

## Restrictions for Kubernetes label names and values {#restrictions}

[Kubernetes labels](../../concepts/index.md#node-labels) are `key:value` pairs.

Kubernetes label keys of nodes may consist of two parts separated by `/`: prefix and name.

A prefix is an optional part of a key. Follow these prefix requirements:
* It must be a DNS subdomain, i.e., a series of DNS tags separated by `.`.
* It may be up to 253 characters long.
* The last character must be followed by `/`.

A name is a required part of a key. Follow these naming requirements:
* It may be up to 63 characters long.
* It may contain lowercase Latin letters, numbers, and `-_.` symbols.
* Use a letter or number for the first and last characters.

The same rules apply to the value as to the name.

Label example: `app.kubernetes.io/name: mysql`, where `app.kubernetes.io/` is the prefix, `name` is the name, and `mysql`, the value.

## Adding Kubernetes labels when creating a node group {#node-group-creation}

You can add Kubernetes labels to all Managed Service for Kubernetes nodes in a [node group](../../concepts/index.md#node-group) at once. To do this, specify the labels in the `node_labels` parameter when [creating a Managed Service for Kubernetes node group](node-group-create.md).

1. Create a [Managed Service for Kubernetes cluster](../../concepts/index.md#kubernetes-cluster).

   You can use an existing Managed Service for Kubernetes cluster or [create a new one](../kubernetes-cluster/kubernetes-cluster-create.md).

1. Create a node group with Kubernetes labels:

   {% list tabs group=instructions %}

   - Management console {#console}

     1. In the [management console](https://console.yandex.cloud), select the [folder](../../../resource-manager/concepts/resources-hierarchy.md#folder) where you created the Managed Service for Kubernetes cluster.
     1. Navigate to **Managed Service for&nbsp;Kubernetes**.
     1. Select the Managed Service for Kubernetes cluster to create a node group for.
     1. On the Managed Service for Kubernetes cluster page, go to the **Node management** tab.
     1. Click **Create a node group**.
     1. Enter a name for the Managed Service for Kubernetes node group.
     1. In the **Kubernetes version** field, select the Kubernetes version for the Managed Service for Kubernetes nodes.
     1. Under **Scaling**:
        * Select the scaling policy type.
        * Specify the number of nodes in the Managed Service for Kubernetes node group.
     1. Under **Changes during creation and updates**, specify the maximum number of [VMs](../../../compute/concepts/vm.md) by which you can exceed or reduce the Managed Service for Kubernetes group size.
     1. Under **Computing resources**:
        * Select a [platform](../../../compute/concepts/vm-platforms.md).
        * Specify the required number of vCPUs, [guaranteed vCPU performance](../../../compute/concepts/performance-levels.md), and the amount of RAM.
     1. Under **Storage**:
        * Specify the Managed Service for Kubernetes node **Disk type**:
          * `HDD`: Standard network drive; HDD network block storage.
          * `SSD`: Fast network drive; SSD network block storage.
          * `Non-replicated SSD`: Network drive with enhanced performance achieved by eliminating redundancy. You can only change the size of this disk type in 93 GB increments.
          * `SSD IO`: Network drive with the same performance specifications as `Non-replicated SSD`, plus redundancy. You can only change the size of this disk type in 93 GB increments.
        * Specify the Managed Service for Kubernetes node [disk](../../../compute/concepts/disk.md) size.
     1. Under **Network settings**:
        * In the **Public address** field, select the IP address assignment method:
          * `Auto` to assign random [IP addresses](../../../vpc/concepts/address.md) from the Yandex Cloud address pool to the nodes.
          * `No address`: Do not assign public IP addresses.
        * Specify how Managed Service for Kubernetes nodes should be placed across the [availability zones](../../../overview/concepts/geo-scope.md) and [networks](../../../vpc/concepts/network.md#network).
     1. Under **Access**, specify the credentials to access the Managed Service for Kubernetes node:
        * In the **Login** field, enter the username.
        * In the **SSH key** field, paste the contents of the [public key](../node-connect-ssh.md#creating-ssh-keys) file.
     1. Under **Maintenance window settings**:
        * In the **Maintenance frequency / Disable** field, select your preferred [maintenance](../../concepts/release-channels-and-updates.md#updates) window:
          * `Disable`: Automatic updates disabled.
          * `Anytime`: Updates allowed at any time.
          * `Daily`: Updates will take place within the time interval specified in the **Maintenance window start time in UTC** field.
          * `Custom`: Updates will take place within the time interval specified in the **Weekly schedule** field.
     1. Under **Additional**:
        * In the **Node labels** field, click **Add  label** and specify its key and value. Add multiple labels if needed.
     1. Click **Create**.

   - CLI {#cli}

     Create a Managed Service for Kubernetes node group:

     ```bash
     yc managed-kubernetes node-group create \
      --name k8s-labels-node \
      --cluster-name k8s-labels \
      --disk-type network-ssd \
      --fixed-size 1 \
      --node-labels environment=production,apps/tier=backend
     ```

     Where:

     * `--name`: Managed Service for Kubernetes node group name.
     * `--cluster-name`: Name of the Managed Service for Kubernetes cluster to create the node group in.
     * `--disk-type`: [Disk type](../../../compute/concepts/disk.md) on the Managed Service for Kubernetes node.
     * `--fixed-size`: Number of Managed Service for Kubernetes nodes in the group.
     * `--node-labels`: Managed Service for Kubernetes node labels. You can specify multiple labels separated by commas.

     Result:

     ```text
     done (2m19s)
     id: catkuapro07e********
     cluster_id: abcsk1s2f3fm********
     created_at: "2020-09-24T13:32:24Z"
     name: k8s-labels-node
     status: RUNNING
     node_template:
       platform_id: standard-v2
       resources_spec:
         memory: "4294967296"
         cores: "2"
         core_fraction: "100"
       boot_disk_spec:
         disk_type_id: network-ssd
         disk_size: "103079215104"
       v4_address_spec: {}
       scheduling_policy: {}
     scale_policy:
       fixed_scale:
         size: "1"
     allocation_policy:
       locations:
       - zone_id: ru-central1-a
         subnet_id: e9bm87gkjd81********
     deploy_policy:
       max_expansion: "3"
     instance_group_id: cl1v2gh33j1c********
     node_version: "1.17"
     version_info:
       current_version: "1.17"
     maintenance_policy:
       auto_upgrade: true
       auto_repair: true
       maintenance_window:
         anytime: {}
     node_labels:
       apps/tier: backend
       environment: production
     ```

   - Terraform {#tf}

     {% note warning %}

     A Managed Service for Kubernetes node group will be recreated.

     {% endnote %}

     1. Open the current configuration file describing the Managed Service for Kubernetes node group.

        For more on how to create such a file, see [Creating a node group](node-group-create.md).
     1. Add the `node_labels` property to the Managed Service for Kubernetes node group description:

        ```hcl
        resource "yandex_kubernetes_node_group" "<node_group_name>" {
          cluster_id = yandex_kubernetes_cluster.<cluster_name>.id
          ...
          node_labels = {
            "<label_1>" = "<value_1>"
            "<label_2>" = "<value_2>"
            ...
          }
        }
        ```

     1. Make sure the configuration files are correct.

        1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
        1. Run this command:
        
           ```bash
           terraform validate
           ```
        
           Terraform will show any errors found in your configuration files.

     1. Confirm updating the resources.

        1. Run this command to view the planned changes:
        
           ```bash
           terraform plan
           ```
        
           If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
        
        1. If everything looks correct, apply the changes:
           1. Run this command:
        
              ```bash
              terraform apply
              ```
        
           1. Confirm updating the resources.
           1. Wait for the operation to complete.

        {% note warning "Timeouts" %}
        
        The Terraform provider sets time limits for operations with Managed Service for Kubernetes cluster node groups:
        
        * Creating and editing: 60 minutes.
        * Deleting: 20 minutes.
        
        Operations in excess of this time will be interrupted.
        
        {% cut "How do I modify these limits?" %}
        
        Add the `timeouts` section to the cluster node group description, e.g.:
        
        ```hcl
        resource "yandex_kubernetes_node_group" "<node_group_name>" {
          ...
          timeouts {
            create = "1h30m"
            update = "1h30m"
            delete = "60m"
          }
        }
        ```
        
        {% endcut %}
        
        {% endnote %}

        For more information, see [this Terraform provider guide](../../../terraform/resources/kubernetes_node_group.md).

   - API {#api}

     To create a Managed Service for Kubernetes node group, use the [create](../../managed-kubernetes/api-ref/NodeGroup/create.md) REST API method for the [NodeGroup](../../managed-kubernetes/api-ref/NodeGroup/index.md) resource or the [NodeGroupService/Create](../../managed-kubernetes/api-ref/grpc/NodeGroup/create.md) gRPC API call.

   {% endlist %}

1. Get information about the created node group with Kubernetes labels:

   {% list tabs group=instructions %}

   - Management console {#console}

     1. In the [management console](https://console.yandex.cloud), select the folder where you created the Managed Service for Kubernetes cluster.
     1. Navigate to **Managed Service for&nbsp;Kubernetes**.
     1. Select the Managed Service for Kubernetes cluster where you created the node group.
     1. On the Managed Service for Kubernetes cluster page, go to the **Node management** tab.
     1. Open the page of one of the Managed Service for Kubernetes nodes and navigate to the **Labels** tab. The tab lists the system and user Kubernetes node labels.

   - CLI {#cli}

     1. [Install kubect](https://kubernetes.io/docs/tasks/tools/install-kubectl) and [configure it to work with the new cluster](../connect/index.md#kubectl-connect).
     1. View all the nodes in a Managed Service for Kubernetes cluster:

        ```bash
        kubectl get nodes
        ```

        Result:

        ```bash
        NAME                        STATUS   ROLES    AGE  VERSION
        catkuapro07e********-hgjd   Ready    <none>   1h   v1.17.8
        catkuapro07e********-lskc   Ready    <none>   1h   v1.17.8
        ```

     1. Get information about the Managed Service for Kubernetes cluster node:

        ```bash
        kubectl describe node catkuapro07e********-hgjd
        ```

        Result:

        ```bash
        Name:               catkuapro07e********-hgjd
        Roles:              <none>
        Labels:             apps/tier=backend
                            beta.kubernetes.io/arch=amd64
                            beta.kubernetes.io/instance-type=standard-v2
                            beta.kubernetes.io/os=linux
                            environment=production
                            failure-domain.beta.kubernetes.io/zone=ru-central1-a
                            kubernetes.io/arch=amd64
                            kubernetes.io/hostname=catkuapro07e********-hgjd
                            kubernetes.io/os=linux
                            node.kubernetes.io/kube-proxy-ds-ready=true
                            node.kubernetes.io/masq-agent-ds-ready=true
                            node.kubernetes.io/node-problem-detector-ds-ready=true
                            yandex.cloud/node-group-id=catkuapro07e********
                            yandex.cloud/pci-topology=k8s
                            yandex.cloud/preemptible=false
        ```

   - API {#api}

     To view information about a Managed Service for Kubernetes node, use the [list](../../managed-kubernetes/api-ref/NodeGroup/list.md) REST API method for the [NodeGroup](../../managed-kubernetes/api-ref/NodeGroup/index.md) resource or the [NodeGroupService/List](../../managed-kubernetes/api-ref/grpc/NodeGroup/list.md) gRPC API call.

   {% endlist %}

## Adding a Kubernetes label to an existing node group {#assign-label}

Adding Kubernetes labels does not result in recreation of a node group.

{% list tabs group=instructions %}

- 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.

  To add a Kubernetes label to an existing node group, run this command:

  ```bash
  yc managed-kubernetes node-group add-node-labels \
     --id <node_group_ID> \
     --labels <key>=<value>, ...
  ```

  The command contains the following parameters:

  * `--id`: Node group ID. You can [get it with the list](node-group-list.md#list) of node groups in the Managed Service for Kubernetes cluster.
  * `--labels`: Kubernetes labels in `<key>=<value>` format. You can specify one or multiple labels separated by commas.

- Terraform {#tf}

  To add a Kubernetes label to an existing node group:

  1. Open the current Terraform configuration file describing the Managed Service for Kubernetes node group.

     For more on how to create such a file, see [Creating a node group](node-group-create.md).

  1. In the node group description, add the `node_labels` section:

     ```hcl
     resource "yandex_kubernetes_node_group" "<node_group_name>" {
       ...
       node_labels {
         "<label_name>" = "<label_value>"
         ...
       }
       ...
     }
     ```

     You can add multiple labels by specifying each label in a separate line.

  1. Make sure the configuration files are correct.

     1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
     1. Run this command:
     
        ```bash
        terraform validate
        ```
     
        Terraform will show any errors found in your configuration files.

  1. Confirm updating the resources.

     1. Run this command to view the planned changes:
     
        ```bash
        terraform plan
        ```
     
        If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
     
     1. If everything looks correct, apply the changes:
        1. Run this command:
     
           ```bash
           terraform apply
           ```
     
        1. Confirm updating the resources.
        1. Wait for the operation to complete.

     {% note warning "Timeouts" %}
     
     The Terraform provider sets time limits for operations with Managed Service for Kubernetes cluster node groups:
     
     * Creating and editing: 60 minutes.
     * Deleting: 20 minutes.
     
     Operations in excess of this time will be interrupted.
     
     {% cut "How do I modify these limits?" %}
     
     Add the `timeouts` section to the cluster node group description, e.g.:
     
     ```hcl
     resource "yandex_kubernetes_node_group" "<node_group_name>" {
       ...
       timeouts {
         create = "1h30m"
         update = "1h30m"
         delete = "60m"
       }
     }
     ```
     
     {% endcut %}
     
     {% endnote %}

     For more information, see [this Terraform provider guide](../../../terraform/resources/kubernetes_node_group.md).

- API {#api}

  {% note info %}
  
  Below, see parameters for the REST API request body. To specify a parameter in a gRPC API call, convert its name from [lower CamelCase](https://en.wikipedia.org/wiki/Camel_case) to [snake_case](https://en.wikipedia.org/wiki/Snake_case), e.g., `minResourcePresetId` becomes `min_resource_preset_id`.
  
  {% endnote %}

  To add a Kubernetes label to an existing node group, use the [update](../../managed-kubernetes/api-ref/NodeGroup/update.md) REST API method for the [NodeGroup](../../managed-kubernetes/api-ref/NodeGroup/index.md) resource or the [NodeGroupService/Update](../../managed-kubernetes/api-ref/grpc/NodeGroup/update.md) gRPC API call, and provide the following in the request:

  * Kubernetes labels in the `nodeLabels` parameter.
  * `nodeLabels` parameter to update in the `updateMask` parameter.

  {% note warning %}
  
  The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
  
  {% endnote %}

{% endlist %}

## Removing a Kubernetes label from a node group {#remove-label}

Removing Kubernetes labels does not result in recreation of a node group.

{% list tabs group=instructions %}

- 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.

  To remove a Kubernetes label from a node group, run this command:

  ```bash
  yc managed-kubernetes node-group remove-node-labels \
     --id <node_group_ID> \
     --labels <label_key>, ...
  ```

  The command contains the following parameters:

  * `--id`: Node group ID. You can [get it with the list](node-group-list.md#list) of node groups in the Managed Service for Kubernetes cluster.
  * `--labels`: Keys of the Kubernetes labels to remove. You can specify one or multiple labels separated by commas.

- Terraform {#tf}

  To remove a Kubernetes label from a node group:

  1. Open the current Terraform configuration file describing the Managed Service for Kubernetes node group.

     For more on how to create such a file, see [Creating a node group](node-group-create.md).

  1. In the node group description, remove the Kubernetes labels you no longer need from `node_labels`.

  1. Make sure the configuration files are correct.

     1. In the command line, navigate to the directory that contains the current Terraform configuration files defining the infrastructure.
     1. Run this command:
     
        ```bash
        terraform validate
        ```
     
        Terraform will show any errors found in your configuration files.

  1. Confirm updating the resources.

     1. Run this command to view the planned changes:
     
        ```bash
        terraform plan
        ```
     
        If you described the configuration correctly, the terminal will display a list of the resources to update and their parameters. This is a verification step that does not apply changes to your resources.
     
     1. If everything looks correct, apply the changes:
        1. Run this command:
     
           ```bash
           terraform apply
           ```
     
        1. Confirm updating the resources.
        1. Wait for the operation to complete.

     {% note warning "Timeouts" %}
     
     The Terraform provider sets time limits for operations with Managed Service for Kubernetes cluster node groups:
     
     * Creating and editing: 60 minutes.
     * Deleting: 20 minutes.
     
     Operations in excess of this time will be interrupted.
     
     {% cut "How do I modify these limits?" %}
     
     Add the `timeouts` section to the cluster node group description, e.g.:
     
     ```hcl
     resource "yandex_kubernetes_node_group" "<node_group_name>" {
       ...
       timeouts {
         create = "1h30m"
         update = "1h30m"
         delete = "60m"
       }
     }
     ```
     
     {% endcut %}
     
     {% endnote %}

     For more information, see [this Terraform provider guide](../../../terraform/resources/kubernetes_node_group.md).

- API {#api}

  {% note info %}
  
  Below, see parameters for the REST API request body. To specify a parameter in a gRPC API call, convert its name from [lower CamelCase](https://en.wikipedia.org/wiki/Camel_case) to [snake_case](https://en.wikipedia.org/wiki/Snake_case), e.g., `minResourcePresetId` becomes `min_resource_preset_id`.
  
  {% endnote %}

  To remove a Kubernetes label from a node group, use the [update](../../managed-kubernetes/api-ref/NodeGroup/update.md) REST API method for the [NodeGroup](../../managed-kubernetes/api-ref/NodeGroup/index.md) resource or the [NodeGroupService/Update](../../managed-kubernetes/api-ref/grpc/NodeGroup/update.md) gRPC API call, and provide the following in the request:

  * New list of Kubernetes labels in the `nodeLabels` parameter. If you want to remove all the labels, provide `"nodeLabels": {}` in your request.
  * `nodeLabels` parameter to update in the `updateMask` parameter.

  {% note warning %}
  
  The API method will assign default values to all the parameters of the object you are modifying unless you explicitly provide them in your request. To avoid this, list the settings you want to change in the `updateMask` parameter as a single comma-separated string.
  
  {% endnote %}

{% endlist %}