配置项是一种存储应用所需配置信息的资源类型。它可以作为容器运行环境中的环境变量,便于应用部署后灵活变更容器配置,也可以通过挂载配置文件的方式向容器中注入配置信息。本文介绍如何使用Kubernetes ConfigMap创建和管理命名空间中的配置项。
前提条件
SAE支持以下两种命令行工具的使用方式,您可以选择其中任意一种方式。本文将以Kubectl Plugin作为默认命令行工具,介绍SAE命令行工具的使用方法。
方式一:Serverless Devs工具以及SAE插件
- 已在本地安装Serverless Devs工具。
- 已配置阿里云认证信息。
使用Serverless Devs工具的config命令配置权限。更多信息,请参见Config命令。
- 已初始化工作区。
使用Serverless Devs工具的 init命令,把当前目录初始化为工作区。
s init start-saectl -d ./
- 安装SAE插件
执行任意SAE插件的命令行,触发Serverless Devs工具安装SAE插件。
s saectl get ns
方式二:Kubectl以及SAE插件
- 已在本地安装Kubectl的SAE插件。
执行以下脚本,快速在本地安装SAE最新版本的Kubectl插件。
curl -fsSL https://sae-component-software.oss-cn-hangzhou.aliyuncs.com/kubectl_plugin/install.sh -o install-sae.sh sudo sh ./install-sae.sh
- 已配置阿里云认证信息。
以环境变量认证方式配置阿里云密钥:
export ALICLOUD_ACCESS_KEY="************" export ALICLOUD_SECRET_KEY="************" export ALICLOUD_REGION="cn-shenzhen"
查看已有配置
- 执行以下命令,查看某个地域(Region)下所有的配置列表。
% kubectl sae get configmap -A
预期输出。NAMESPACE NAME DATA AGE default a 2 424d default b 2 12d test test-config 1 297d test example-configmap 3 20d
- 执行以下命令,查看某个命名空间(Namespace)下的配置列表。
% kubectl sae configmap -n test
预期输出。NAME DATA AGE test-config 2 297d example-configmap 2 20d
查看ConfigMap,显示字段如下。- NAME:命名空间中ConfigMap的名称。
- DATA:配置中数据条目个数。
- AVAILABLE:配置存在时间,单位为d(天)。
- 执行以下命令,查看具体配置的内容。
% kubectl sae get configmap/example-configmap -n test -oyaml
预期输出。apiVersion: v1 data: database: mongodb database_uri: mongodb://localhost:27018 keys: "image.public.key=771 \nrsa.public.key=42\n" kind: ConfigMap metadata: creationTimestamp: "2022-12-06T10:15:25Z" labels: abc: foo name: example-configmap namespace: test resourceVersion: "1672115604539" uid: "1644"
创建新的配置
- 编写ConfigMap。
使用YAML格式编写配置,示例如下,并保存为 cm.yaml。
apiVersion: v1 kind: ConfigMap metadata: name: database-configmap namespace: test data: database: mysql database_uri: mysql://localhost:2309
- 创建配置。
使用 apply命令行创建新的配置,执行如下命令。
% kubectl sae apply -f ./cm.yaml configmap/database-configmap created
更新配置内容
- 展开查看:使用YAML文件更新配置内容
首先修改YAML文件的内容,然后使用apply命令行执行变更。
kubectl sae apply -f ./cm.yaml
- 展开查看:在线编辑配置
kubectl sae edit configmap/database-configmap -n test
使用配置
- 展开查看:使用ConfigMap配置容器的环境变量
apiVersion: apps/v1 kind: Deployment metadata: ... spec: minReadySeconds: 10 replicas: 2 selector: matchLabels: app: web strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 0 type: RollingUpdate template: metadata: creationTimestamp: null labels: app: web spec: containers: - env: - name: k1 value: s1 envFrom: - configMapRef: name: database-configmap image: registry-vpc.cn-shenzhen.aliyuncs.com/sae-****/wordpress:1.0 imagePullPolicy: Always name: main
- 展开查看:将ConfigMap配置挂载到容器中作为文件使用
apiVersion: apps/v1 kind: Deployment metadata: ... spec: minReadySeconds: 10 replicas: 2 selector: matchLabels: app: web template: metadata: creationTimestamp: null labels: app: web spec: containers: - env: - name: k1 value: s1 image: registry-vpc.cn-shenzhen.aliyuncs.com/sae-****/wordpress:1.0 imagePullPolicy: Always name: main resources: limits: cpu: "1" memory: 2Gi requests: cpu: "1" memory: 2Gi volumeMounts: - mountPath: /etc/config name: user-example-configmap subPath: database_uri terminationGracePeriodSeconds: 30 volumes: - configMap: name: database-configmap name: user-example-configmap