cci-iam-authenticator 使用参考
cci-iam-authenticator作为k8s client端的认证插件,主要提供了generate-kubeconfig 和token两个子命令。
A tool to authenticate to CCI using HuaweiCloud IAM credentials Usage:
cci-iam-authenticator [command]
Available Commands:
generate-kubeconfig Generate or modify kubeconfig files based on user configuration help Help about any command
Use "cci-iam-authenticator [command] --help" for more information about a command.
其中,Flags主要为日志选项。
token
token子命令用于获取用户token,获取token的认证方式有用户名/密码、ak/sk两种,
选择其中一种即可。
Authenticate using HuaweiCloud IAM and get token for CCI Usage:
cci-iam-authenticator token [flags]
Flags:
开发指南 2 使用 kubectl(推荐)
--ak string IAM access key ID
--cache Cache the token credential on disk until it expires (default true) --domain-name string IAM domain name, typically your account name
-h, --help help for token
--iam-endpoint string HuaweiCloud IAM endpoint, i.e. https://iam.cn-north-4.myhuaweicloud.com (default "https://iam.myhuaweicloud.com")
--insecure-skip-tls-verify If true, the iam server's certificate will not be checked for validity. (default true)
--password string IAM user password
--project-id string IAM project id, project id and project name should not be empty at same time --project-name string IAM project name, project id and project name should not be empty at same time --sk string IAM secret access key
--token-only Return token only for other tool integration
--user-name string IAM user name. Same as domain-name when using main account, otherwise use iam user name
其中,Flags分为用户名密码、AKSK和公共配置。
表2-4 用户名/密码配置
Command Flag Environment Value Description
domain-name DOMAIN_NAME 租户名,即账号名,详情 请参见https://
support.huaweicloud.co m/usermanual-ca/
ca_01_0001.html
user-name USER_NAME 子用户名,即IAM用户名。若不配置与domain-name一致。
详情请参见https://
support.huaweicloud.co m/usermanual-ca/
ca_01_0001.html
password PASSWORD 用户或子用户密码。
表2-5 AK/SK 配置
Command Flag Environment Value Description
ak ACCESS_KEY_ID ak、sk的获取方法请参见 获取AK/SK,ak为文件中 Access Key部分,sk为文 件中Secret Key部分。
sk SECRET_ACCESS_KEY
表2-6 公共配置
Command Flag Environment Value Description
iam-endpoint IAM_ENDPOINT 云容器实例的Endpoint,
必须配置,详情请参见
cci-iam-authenticator使
用参考。project-name PROJECT_NAME 项目名,详情请参见
https://
support.huaweicloud.co m/usermanual-ca/
ca_01_0001.html
project-id PROJECT_ID 项目ID,详情请参见https://
cache CREDENTIAL_CACHE 是否开启将IAM Token缓 存到本地,提高访问性
user、context配置,并将当前的kubeconfig context切换到此次配置的结果。 默认情 况下会对用户的配置进行校验,尝试访问IAM及CCI,确保用户配置的IAM认证信息、
CCI地址可用。
Generate or modify kubeconfig files based on user configuration.
Sets a cluster entry, a user entry and a context entry in kubeconfig and use this context as the current-context.
The loading order follows these rules:
1. If the --kubeconfig flag is set, then only that file is loaded. The flag may only be set once and no merging takes
place.
2. If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path delimiting rules for
your system). These paths are merged. When a value is modified, it is modified in the file that defines the stanza. When
a value is created, it is created in the first file that exists. If no files in the chain exist, then it creates the last file in the list.
3. Otherwise, ${HOME}/.kube/config is used and no merging takes place.
Examples:
# Generate kubeconfig to ${HOME}/.kube/config using aksk
cci-iam-authenticator generate-kubeconfig cci-endpoint=https://cci.cn-north-4.myhuaweicloud.com
--开发指南 2 使用 kubectl(推荐)
ak=*** --sk=***
# Generate kubeconfig to ${HOME}/.kube/config using domain name and password
cci-iam-authenticator generate-kubeconfig cci-endpoint=https://cci.cn-north-4.myhuaweicloud.com --domain-name=*** --password=***
Usage:
cci-iam-authenticator generate-kubeconfig [flags]
Flags:
--ak string IAM access key ID
--cache Cache the token credential on disk until it expires (default true) --cci-endpoint string CCI server endpoint, i.e. https://cci.cn-north-4.myhuaweicloud.com --domain-name string IAM domain name, typically your account name
-h, --help help for generate-kubeconfig
--iam-endpoint string HuaweiCloud IAM endpoint, i.e. https://iam.cn-north-4.myhuaweicloud.com (default "https://iam.myhuaweicloud.com")
--insecure-skip-tls-verify If true, the iam server's certificate will not be checked for validity. (default true)
--kubeconfig string use a particular kubeconfig file --password string IAM user password
--project-id string IAM project id, project id and project name should not be empty at same time --project-name string IAM project name, project id and project name should not be empty at same time --sk string IAM secret access key
--token-only Return token only for other tool integration
--user-name string IAM user name. Same as domain-name when using main account, otherwise use iam user name
--validation Validate kubeconfig by trying to access CCI with existing config (default true)
同一个kubeconfig可以包含多个环境、认证信息,用户可以通过同一份IAM认证配 置,仅修改cci-endpoint生成多个region的kubeconfig,例如:
# 生成北京4的kubeconfig,并切换到对应context
$ cci-iam-authenticator generate-kubeconfig cci-endpoint=https://cci.cn-north-4.myhuaweicloud.com --ak=my-ak --sk=xxxxxx
Switched to context "cci-context-cn-north-4-my-ak".
# 生成上海1的kubeconfig,并切换到对应context
$ cci-iam-authenticator generate-kubeconfig cci-endpoint=https://cci.cn-east-3.myhuaweicloud.com --ak=my-ak --sk=xxxxxx
Switched to context "cci-context-cn-east-3-my-ak".
# 切换到北京4的context
$ kubectl config use-context cci-context-cn-north-4-my-ak
3 Namespace 和 Network
Namespace(命名空间)是一种在多个用户之间划分资源的方法。适用于用户中存在 多个团队或项目的情况。当前云容器实例提供“通用计算型”和“GPU型”两种类型 的资源,创建命名空间时需要选择资源类型,后续创建的负载中容器就运行在此类型 的集群上。
● 通用计算型:支持创建含CPU资源的容器实例及工作负载,适用于通用计算场 景。
● GPU型:支持创建含GPU资源的容器实例及工作负载,适用于深度学习、科学计 算、视频处理等场景。
Network是云容器实例扩展的一种Kubernetes资源对象,用于关联VPC及子网,从而 使得容器实例能够使用公有云的网络资源。
Namespace 与网络的关系
从网络角度看,命名空间对应一个虚拟私有云(VPC)中一个子网,如图3-1所示,在 创建命名空间时会关联已有VPC或创建一个新的VPC,并在VPC下创建一个子网。后续 在该命名空间下创建Pod、Service等资源时都会在对应的VPC及子网之内,且占用子网 中的IP地址。
通常情况下,如果您在同一个VPC下还会使用其他服务的资源,您需要考虑您的网络 规划,如子网网段划分、IP数量规划等,确保有可用的网络资源。
图3-1 命名空间与 VPC 子网的关系
开发指南 3 Namespace 和 Network
哪些情况下适合使用多个命名空间
name: namespace-test labels:
sys_enterprise_project_id: "0"
annotations:
namespace.kubernetes.io/flavor: general-computing spec:
finalizers:
- kubernetes
● namespace.kubernetes.io/flavor: general-computing:指定命名空间类型。
命令空间的类型有如下两种:
– general-computing:通用计算型,支持创建含CPU资源的容器实例及工作 负载,适用于通用计算场景。
– gpu-accelerated:GPU型,支持创建含GPU资源的容器实例及工作负载,
适用于深度学习、科学计算、视频处理等场景。
假如上面Namespace定义的文件名称为ns.yaml,则执行kubectl create -f ns.yaml即 可创建命名空间,-f 表示从文件创建。
# kubectl create -f ns.yaml namespace/namespace-test created
执行kubectl get ns查询namespace是否创建成功,ns为namespace的缩写。
# kubectl get ns
NAME STATUS AGE
图3-2 Namespace-异常
创建 Network
Namespace创建好后,需要为Namespace创建网络策略,关联VPC及子网。
以下示例创建一个名为test-network的Network。
apiVersion: networking.cci.io/v1beta1 kind: Network
metadata:
annotations:
network.alpha.kubernetes.io/default-security-group: security-group-id network.alpha.kubernetes.io/domain-id: domain-id
network.alpha.kubernetes.io/project-id: project-id name: test-network
spec:
availableZone: cnnorth-1a cidr: 192.168.0.0/24 attachedVPC: vpc-id networkID: network-id networkType: underlay_neutron subnetID: subnet-id
说明
此处VPC和子网的网段不能为10.247.0.0/16,10.247.0.0/16是云容器实例预留给Service的网 段。如果您使用此网段,后续可能会造成IP冲突,导致负载无法创建或服务不可用;如果您不需 要通过Service访问,而是直接访问Pod,则可以使用此网段。
上面参数获取方法如下:
● network.alpha.kubernetes.io/domain-id:账号ID,可以在我的凭证获取。
● network.alpha.kubernetes.io/project-id:项目ID,可以在我的凭证获取。
● network.alpha.kubernetes.io/default-security-group:安全组ID,可以在安全组 控制台获取,如下图。
图3-3 获取安全组 ID
● availableZone:az名称,当前仅支持“cnnorth-1a”、“cn-north-4a”或“cn-east-3a”。
开发指南 3 Namespace 和 Network
● cidr:子网网段。
● attachedVPC:虚拟私有云的ID,可在VPC控制台获取。
图3-4 获取 VPC ID
● networkID:子网的网络ID,可在VPC控制台 > 子网中获取。
图3-5 获取子网的网络 ID
● networkType:网络类型,当前仅支持underlay_neutron网络模式。
● subnetID:子网ID,可在VPC控制台 > 子网获取。
图3-6 获取子网 ID
假如上面Network定义的文件名称为network.yaml,则执行kubectl create -f network.yaml即可创建命名空间,-f 表示从文件创建。这里--namespace namespace-test是指定在namespace-test这个命名空间下创建。
# kubectl create -f network.yaml --namespace namespace-test network.networking.cci.io/test-network created
登录云容器实例控制台,单击左侧导航栏“命名空间”,您可以看到命令空间创建成 功,且状态为“正常”。
图3-7 Namespace-正常
为 kubectl 上下文指定 Namespace
上面创建Network是在指定的Namespace下创建的,本文档后续的的资源创建都是在 某个命名空间下操作,每次都指定命名空间比较麻烦,您可以为kubectl上下文指定命 名空间,这样在某个上下文中,创建的资源就都是在某个命名空间下,方便操作。
指定Namespace只需要在设置上下文命令中添加一个“--namespace”选项,如下所 示。
kubectl config set-context $context --namespace=$ns
其中,$ns为Namespace的名称;$context 为上下文的名称,可以自定义,也可执行 如下命令获取:
# kubectl config get-contexts
CURRENT NAME CLUSTER AUTHINFO NAMESPACE
cci-context-cn-east-3-1C8PNI0POPPCSFGXPM6S cci-cluster-cn-east-3 cci-user-cn-east-3-1C8PNI0POPPCSFGXPM6S
* cci-context-cn-east-3-hwuser_xxx cci-cluster-cn-east-3 cci-user-cn-east-3-hwuser_xxx kubernetes-admin@kubernetes kubernetes kubernetes-admin
假设,上面创建的Namespace名称为namespace-test,则示例如下。
# kubectl config set-context cci-context --namespace=namespace-test
开发指南 3 Namespace 和 Network
指定Namespace后,就可以使用 kubectl 命令直接操作云容器实例的相关资源。如下 所示,执行kubectl get pod,查看Pod资源,一切正常。
# kubectl get pod No resources found.