Updated on 2024-11-01 GMT+08:00

kubectl Configuration Guide

CCI allows you to use native or customized kubectl to create resources such as workloads. Native kubectl is recommended.

Downloading kubectl

Download the kubectl of version 1.19 from the Kubernetes version release page.

For a device that uses the Apple M1 chip, download the kubectl corresponding to the darwin-arm64 architecture.

Downloading cci-iam-authenticator

Download the cci-iam-authenticator binary from the CCI official website. The latest version is v2.6.17.

Table 1 lists the addresses for downloading cci-iam-authenticator.

Installing and Configuring kubectl

Perform the following operations to install and configure kubectl on a Linux OS. For more details, see Install Tools.

  1. Grant the execute permission on kubectl downloaded in Downloading kubectl and save it to the PATH directory.

    chmod +x ./kubectl

    mv ./kubectl $PATH

    In the preceding command, $PATH indicates the PATH directory (for example, /usr/local/bin). Replace it with the actual value.

    You can run the following command to view the kubectl version:

    kubectl version --client=true

    Client Version: version.Info{Major:"1", Minor:"19", GitVersion:"v1.19.0", GitCommit:"e19964183377d0ec2052d1f1fa930c4d7575bd50", GitTreeState:"clean", BuildDate:"2020-08-26T14:30:33Z", GoVersion:"go1.15", Compiler:"gc", Platform:"linux/amd64"}

  2. Configure IAM authentication information and persistently store it to the local host.

    1. Grant the execute permission on cci-iam-authenticator downloaded in Downloading cci-iam-authenticator and save it to the PATH directory.

      chmod +x ./cci-iam-authenticator

      mv ./cci-iam-authenticator $PATH

    2. Initialize the cci-iam-authenticator configuration.
      You can initialize the cci-iam-authenticator configuration by using either of the following methods:
      • Using AK/SK

        cci-iam-authenticator generate-kubeconfig --cci-endpoint=https://$endpoint --ak=xxxxxxx --sk=xxxxxx

        endpoint is an endpoint of CCI. For details about CCI endpoints, see Regions and Endpoints. For details about how to obtain the AK and SK, see Obtaining an AK/SK. ak is the access key in the file, and sk is the secret key in the file.

        For example, if endpoint is https://cci.cn-north-4.myhuaweicloud.com, the value of ak is my-ak, and the value of sk is ABCDEFAK.., run the following command:

        cci-iam-authenticator generate-kubeconfig --cci-endpoint=https://cci.cn-north-4.myhuaweicloud.com --ak=my-ak --sk=ABCDEFAK..

        Information similar to the following is displayed:

        Switched to context "cci-context-cn-north-4-my-ak"

        In the preceding command, cci-context-cn-north-4-my-ak is the context name, which can be viewed by running kubectl config get-contexts.

      • Using username and password

        cci-iam-authenticator generate-kubeconfig --cci-endpoint=https://$endpoint --domain-name=xxxxxxx --user-name=xxxxxx --password='xxxxxx'

        In the preceding command, endpoint is the CCI endpoint, domain-name is the tenant name, user-name is the IAM username, and password is the IAM user password. Replace them with the actual values.

        • If there is no IAM user, set user-name to the same value as domain-name. Alternatively, do not set user-name.
        • For details about IAM endpoints, see Regions and Endpoints. Note that the endpoint must be in the same region as CCI.
        • If kubectl is used in an insecure environment, you are advised to configure environment variables to reconfigure the authentication information after this step is complete. For details, see Configuring kubectl in an Insecure Environment.

  3. After the configuration is complete, you can run kubectl commands to perform operations on CCI resources.

    For example, run the following command to view the namespace resources in CN North-Beijing4:

    kubectl get ns

    No resources found.

    The command output shows that there is no namespace in CN North-Beijing4. Before creating resources in CCI, create a namespace by following the procedure described in Namespace and Network.

  • When you access public cloud services through APIs, the username/password or AK/SK pair is required to encrypt the requests. This mechanism ensures the confidentiality and integrity of the requests as well as the correctness of the identities of both parties. Keep the $HOME/.kube/config configuration file secure to prevent unauthorized use of the AK/SK.
  • If the function of caching tokens is enabled to improve access performance, the tokens are saved as files in the $HOME/.cci/cache directory. Delete the tokens in a timely manner when they are unnecessary.
  • If your access key is used by an unauthorized person due to loss or leakage, you can delete the access key or notify the administrator of resetting it and then configure the access key again.
  • Deleted access keys cannot be recovered.

Configuring kubectl in an Insecure Environment

  1. Follow the preceding steps to install and configure kubectl.
  2. Edit the kubeconfig file and delete sensitive information.

    On a Linux OS, the kubeconfig file is stored in $HOME/.kube/config by default.
    Table 2 Sensitive information to be deleted

    Command Flag

    Environment Value

    Description

    --domain-name

    DOMAIN_NAME

    Tenant name

    --user-name

    USER_NAME

    Sub-user name

    --password

    PASSWORD

    User password

    --ak

    ACCESS_KEY_ID

    Access key ID

    --sk

    SECRET_ACCESS_KEY

    Secret access key

    --cache

    CREDENTIAL_CACHE

    Whether to cache tokens

    For details about more parameters, see cci-iam-authenticator Usage Reference.

  3. Configure the environment variables corresponding to the deleted parameters. For example, configure AK, SK, and whether to cache tokens.

    Hardcoded or plaintext AK and SK are risky. For security, encrypt your AK and SK and store them in the configuration file or as environment variables.

    export ACCESS_KEY_ID={Access Key} #Replace it with HUAWEICLOUD_SDK_AK.

    export SECRET_ACCESS_KEY={Secret Key} #Replace it with HUAWEICLOUD_SDK_SK.

    export CREDENTIAL_CACHE=false

    kubectl get ns

    Information similar to the following is displayed:

    No resources found.

Obtaining an AK/SK

AK: access key ID. It is a unique ID associated with an SK. AK is used together with SK to sign requests.

SK: secret access key. It is used together with an AK to sign requests. They can identify request senders and prevent requests from being modified.

  1. Log in to the management console.
  2. Hover over the username and select My Credentials from the drop-down list.
  3. Choose Access Keys from the navigation pane.
  4. Click Create Access Key, and enter the verification code.
  5. Click OK to generate an access key and download it.

    Keep the AK/SK file confidential to prevent information leakage.

Obtaining CCI Endpoints

Obtain the endpoint from the Regions and Endpoints page, as shown in the following table.

Table 3

Region Name

Region

Endpoint

LA-Sao Paulo1

sa-brazil-1

cci.sa-brazil-1.myhuaweicloud.com