命令形式如下:
~~~bash
kubectl create configmap <map-name> <data-source>
~~~
其中,`<map-name>`是设置的 configmap 的名字,`<data-source>`可以是目录、文件、或是 key-value 键值对。
`<data-source>`中的可选参数为:`--from-file`/`--from-env-file`(从目录或文件创建) 和`--from-literal`(直接指定键值对创建)。`configmap`也可以缩写为`cm`。
可以在命令行中执行`kubectl create configmap -h`查看具体参数和使用方法。
#### 从目录中创建 ConfigMap
这种方式适用于在一个目录下有多个配置文件。
在`/home/shiyanlou`目录下新建`configmap`文件夹,并在`configmap`文件夹下新建两个文件,分别名为:`game.properties`和`ui.properties`。
`game.properties`中的文件内容为:
~~~text
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
~~~
`ui.properties`中的文件内容为:
~~~text
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
~~~
执行创建:
~~~bash
# 这里设置 configmap 的名称为 game-config
$ kubectl create configmap game-config --from-file=/home/shiyanlou/configmap/
configmap/game-config created
$ kubectl get configmap game-config -o yaml
apiVersion: v1
data:
game.properties: |-
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties: |
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
creationTimestamp: "2019-09-23T08:37:09Z"
name: game-config
namespace: default
resourceVersion: "11509"
selfLink: /api/v1/namespaces/default/configmaps/game-config
uid: 212cd3eb-fd6d-43a4-8aed-d33968da8e1c
$ kubectl describe configmap game-config
Name: game-config
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game.properties:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
Events: <none>
~~~
#### 从文件中创建 ConfigMap
可以从单独的文件或是多个文件中进行创建。
比如:
~~~bash
# 使用单个文件创建
kubectl create configmap game-config-2 --from-file=/home/shiyanlou/configmap/game.properties
# 使用多个文件创建
kubectl create configmap game-config-3 --from-file=/home/shiyanlou/configmap/game.properties --from-file=/home/shiyanlou/configmap/ui.properties
~~~
使用参数`--from-env-file`从 env-file 中创建 ConfigMap。env-file 是包含了环境变量的列表。
在`/home/shiyanlou/configmap`目录下新建`game-env-file.properties`文件,并写入如下内容:
~~~text
enemies=aliens
lives=3
allowed="true"
~~~
执行创建:
~~~bash
$ kubectl create configmap game-config-env-file --from-env-file=/home/shiyanlou/configmap/game-env-file.properties
configmap/game-config-env-file created
$ kubectl get configmap game-config-env-file -o yaml
apiVersion: v1
data:
allowed: '"true"'
enemies: aliens
lives: "3"
kind: ConfigMap
metadata:
creationTimestamp: "2019-09-23T09:00:46Z"
name: game-config-env-file
namespace: default
resourceVersion: "13919"
selfLink: /api/v1/namespaces/default/configmaps/game-config-env-file
uid: 790cbece-1144-4ebe-9ea1-1e151f210ef7
~~~
另外使用文件创建的话,可以使用自定义的名称替代默认使用的文件名,比如:
~~~bash
# 使用 game.properties 文件创建,但是将其重命名为 game-special-key
$ kubectl create configmap game-config-4 --from-file=game-special-key=/home/shiyanlou/configmap/game.properties
configmap/game-config-4 created
# 可以看到这里显示的是 game-special-key,而不再是之前的 game.properties
$ kubectl describe configmap game-config-4
Name: game-config-4
Namespace: default
Labels: <none>
Annotations: <none>
Data
====
game-special-key:
----
enemies=aliens
lives=3
enemies.cheat=true
enemies.cheat.level=noGoodRotten
secret.code.passphrase=UUDDLRLRBABAS
secret.code.allowed=true
secret.code.lives=30
Events: <none>
~~~
#### 使用 key-value 键值对创建 ConfigMap
使用参数`--from-literal`直接在命令中设置 key-value 键值对。
比如:
~~~bash
# 传递多组键值对
$ kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created
$ kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
special.how: very
special.type: charm
kind: ConfigMap
metadata:
creationTimestamp: "2019-09-23T09:16:09Z"
name: special-config
namespace: default
resourceVersion: "15490"
selfLink: /api/v1/namespaces/default/configmaps/special-config
uid: 231c59c3-dc4d-4650-a04f-af51debc94fa
~~~
- Pod 基本用法
- Pod 简介
- 操作 Pod
- 创建 Pod
- 标签
- 标签选择器
- 命名空间
- 删除及更新 Pod
- 副本集(RS)、后台支撑服务集(DaemonSet)、任务(Job)
- 副本集(RS)
- 后台支撑服务集(DaemonSet)
- 任务(Job)
- 使用 ConfigMap 配置应用程序
- ConfigMap 简介
- 创建 ConfigMap 资源对象
- 通过 YAML 配置文件方式创建
- 通过 kubectl 命令行方式创建
- 通过生成器创建 ConfigMap
- 在 Pod 中使用 ConfigMap
- 通过环境变量方式使用 ConfigMap
- 通过卷挂载(volumeMount)方式使用 ConfigMap
- 使用 Secret 传递敏感数据
- Secret 简介
- 默认令牌 Secret
- 创建 Secret
- 使用 kubectl 创建 Secret
- 使用 YAML 文件手动创建 Secret
- 使用生成器创建 Secret
- 使用 Secrets
- 挂载 Secret 到 Pod 中作为卷进行使用
- 使用 Secret 作为环境变量
- 使用镜像拉取 Secret(ImagePullSecrets)
- 多容器 Pod
- 多容器 Pod 简介
- Pod 中容器间的通信
- Pod 容器共享 Volume
- 进程间通信(IPC)
- 容器间网络通信
- Scheduler
- kube-scheduler 简介
- kube-scheduler 职责及调度流程
- 常用参数
- 预选策略(Predicates Policies)
- 优选策略(Priorites Policies)
- 自定义调度器
- 使用 Deployment 进行 Pod 升级回滚
- Deployment 简介
- Deployment 的升级
- 多重更新(Rollover)
- 更新 Deployment 的标签选择器(Label Selector)
- Deployment 的回滚
- Deployment 的暂停与恢复
- Pod 扩容与缩容
- 手动扩缩容
- 自动扩缩容
- 扩缩容算法
- php-apache 自动扩缩容实例
- 配置 HPA
- v1 版本
- v2beta2 版本
- DaemonSet
- DaemonSet 简介
- 运行示例程序
- 滚动更新
- StatefulSet
- StatefulSet 简介
- 运行 nginx 实例
- 扩缩容 StatefulSet
- 更新 StatefulSet
- 删除 StatefulSet
- 非级联删除
- 级联删除
- Pod 管理策略
- Service 基本用法
- Service 简介
- Service 的类型
- 使用命令创建服务
- 使用 YAML 文件创建服务
- Service Discovery
- 环境变量
- DNS
- ClusterIP Service
- ClusterIp Service 简介
- Normal Service
- YAML 文件模板
- 服务负载分发策略 & 多端口服务 & 端口命名
- 一个简单的例子
- Headless Service
- 无 Selector 的服务
- NodePort Service
- NodePort Service 简介及实例
- 扩展:客户端直接访问 Pod
- hostPort
- hostNetwork
- Port Forward
- LoadBalancer与ExternalName
- LoadBalancer Service 简介
- LoadBalancer Service 实例
- 使用 nginx 软件手动实现负载均衡
- 由云服务商提供负载均衡器
- ExternalName Service 简介
- Ingress
- Ingress 简介
- 部署 nginx-ingress-controller
- 部署一个简单的 Nginx 实例
- 不同的 Ingress 策略配置类型
- 配置 Ingress 处理 TLS 传输
