k8s之configmap

1.ConfigMap

1.1创建方式

# 命令格式
kubectl create configmap <映射名称> <数据源>
​
# <映射名称> 是为 ConfigMap 指定的名称,<数据源> 是要从中提取数据的目录、 文件或者字面值。

1.2获取ConfigMap信息

1)kubectl describe
2)kubectl get

1.3创建 ConfigMap

1.3.1基于目录创建

# 创建本地目录
mkdir -p configure-pod-container/configmap/
​
# 将实例文件下载到 `configure-pod-container/configmap/` 目录
wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties
​
# 创建 configmap
[root@vm21 yaml]# kubectl create configmap game-config --from-file=configure-pod-container/configmap/
configmap/game-config created
​
# 查看configmap
[root@vm21 yaml]# kubectl get configmap game-config
NAME          DATA   AGE
game-config   2      23s
​
# 查看game-config内容
[root@vm21 yaml]# 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
​
​
BinaryData
====
​
Events:  <none>
​
# 以yaml格式输出game-config内容
[root@vm21 yaml]# 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: "2022-04-10T12:49:45Z"
  name: game-config
  namespace: default
  resourceVersion: "7531"
  uid: 0f84e076-f5c7-42a7-8dee-5e5a349f9c1e
​

1.3.2基于文件创建

# 生成game-config2,可以连续指定多个--from-file
[root@vm21 yaml]# kubectl create configmap game-config2 --from-file=configure-pod-container/configmap/ui.properties
configmap/game-config2 created
​
# 查看game-config2
[root@vm21 yaml]# kubectl get configmap
NAME               DATA   AGE
game-config        2      6m58s
game-config2       1      11s
kube-root-ca.crt   1      83m
​
# 查看game-config2描述
[root@vm21 yaml]# kubectl describe configmap game-config2
Name:         game-config2
Namespace:    default
Labels:       <none>
Annotations:  <none>
​
Data
====
ui.properties:
----
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
​
​
BinaryData
====
​
Events:  <none>
​
# 以yaml格式输出game-config2内容
[root@vm21 yaml]# kubectl get configmap game-config2 -o yaml
apiVersion: v1
data:
  ui.properties: |
    color.good=purple
    color.bad=yellow
    allow.textmode=true
    how.nice.to.look=fairlyNice
kind: ConfigMap
metadata:
  creationTimestamp: "2022-04-10T12:56:32Z"
  name: game-config2
  namespace: default
  resourceVersion: "8103"
  uid: 01fd7498-c4d2-4692-97bf-f93d1dc09e67
​
# 下载示例环境文件
wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
​
# 显示env文件内容
[root@vm21 yaml]# cat configure-pod-container/configmap/game-env-file.properties
enemies=aliens
lives=3
allowed="true"
​
# 从环境文件创建 ConfigMap
[root@vm21 yaml]# kubectl create configmap game-config-3 --from-env-file=configure-pod-container/configmap/game-env-file.properties
configmap/game-config-3 created
​
# 查看已生成的configmap
[root@vm21 yaml]# kubectl get configmap
NAME               DATA   AGE
game-config        2      19m
game-config-3      3      10s
game-config2       1      12m
kube-root-ca.crt   1      95m
​
# 查看game-config-3描述
[root@vm21 yaml]# kubectl describe configmap game-config-3
Name:         game-config-3
Namespace:    default
Labels:       <none>
Annotations:  <none>
​
Data
====
allowed:
----
"true"
enemies:
----
aliens
lives:
----
3
​
BinaryData
====
​
Events:  <none>
​
# 以yaml格式描述game-config-3
[root@vm21 yaml]# kubectl get configmap game-config-3 -o yaml
apiVersion: v1
data:
  allowed: '"true"'
  enemies: aliens
  lives: "3"
kind: ConfigMap
metadata:
  creationTimestamp: "2022-04-10T13:08:35Z"
  name: game-config-3
  namespace: default
  resourceVersion: "9120"
  uid: 88161d80-c0b4-4a7b-9e3d-f1e56f17d545
​
# 当多次使用 --from-env-file 来从多个数据源创建 ConfigMap 时,仅仅最后一个 env 文件有效
​
​
# 定义从文件创建 ConfigMap 时要使用的键,格式如下:
kubectl create configmap game-config-3 --from-file=<我的键名>=<文件路径>
​
# 
[root@vm21 yaml]# kubectl create configmap game-config-4 --from-file=game-special-key=configure-pod-container/configmap/game.properties
configmap/game-config-4 created
​
[root@vm21 yaml]# kubectl get configmap
NAME               DATA   AGE
game-config        2      23m
game-config-3      3      4m44s
game-config-4      1      9s
game-config2       1      16m
kube-root-ca.crt   1      99m
​
[root@vm21 yaml]# 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
​
BinaryData
====
​
Events:  <none>
​
​
[root@vm21 yaml]# kubectl get configmap game-config-4 -o yaml
apiVersion: v1
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
kind: ConfigMap
metadata:
  creationTimestamp: "2022-04-10T13:13:10Z"
  name: game-config-4
  namespace: default
  resourceVersion: "9503"
  uid: c4fb1651-b8c9-450d-861f-224265fc03da

1.3.3根据字面值

[root@vm21 yaml]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created
​
​
[root@vm21 yaml]# kubectl get configmap
NAME               DATA   AGE
game-config        2      28m
game-config-3      3      9m28s
game-config-4      1      4m53s
game-config2       1      21m
kube-root-ca.crt   1      104m
special-config     2      7s
​
​
[root@vm21 yaml]# kubectl describe configmap special-config
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  <none>
​
Data
====
special.how:
----
very
special.type:
----
charm
​
BinaryData
====
​
Events:  <none>
​
​
[root@vm21 yaml]# kubectl get configmap special-config -o yaml
apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  creationTimestamp: "2022-04-10T13:17:56Z"
  name: special-config
  namespace: default
  resourceVersion: "9900"
  uid: a9331311-c7b4-4098-b59d-01f77d3e4b24

1.3.4基于生成器

[root@vm21 configmap]# mkdir -p /opt/yaml/configure-pod-container/configmap/kubectl
​
​
[root@vm21 configmap]# cp /opt/yaml/configure-pod-container/configmap/game.properties /opt/yaml/configure-pod-container/configmap/kubectl/
​
​
[root@vm21 yaml]# kubectl apply -k .
configmap/game-config-4-tbg7c4gc77 created
​
​
[root@vm21 yaml]# kubectl get configmap
NAME                       DATA   AGE
game-config-4-tbg7c4gc77   1      11s
kube-root-ca.crt           1      125m
​
​
[root@vm21 yaml]# kubectl describe configmap/game-config-4-tbg7c4gc77
Name:         game-config-4-tbg7c4gc77
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
​
BinaryData
====
​
Events:  <none>
​
​
[root@vm21 yaml]# kubectl get configmap/game-config-4-tbg7c4gc77 -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
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"game.properties":"enemies=aliens\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.passphrase=UUDDLRLRBABAS\nsecret.code.allowed=true\nsecret.code.lives=30"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"game-config-4-tbg7c4gc77","namespace":"default"}}
  creationTimestamp: "2022-04-10T13:38:33Z"
  name: game-config-4-tbg7c4gc77
  namespace: default
  resourceVersion: "11578"
  uid: a2bdac5c-5b6b-4683-b23b-512981210187
​
# 请注意,生成的 ConfigMap 名称具有通过对内容进行散列而附加的后缀, 这样可以确保每次修改内容时都会生成新的 ConfigMap。
​
​
# 定义从文件生成 ConfigMap 时要使用的键
# 在 ConfigMap 生成器中,你可以定义一个非文件名的键名。 
# 例如,从 configure-pod-container/configmap/game.properties 文件生成 ConfigMap, 但使用 game-special-key 作为键名:
​
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-5
  files:
  - game-special-key=configure-pod-container/configmap/kubectl/game.properties
EOF
​
[root@vm21 yaml]# kubectl apply -k ./
configmap/game-config-5-tfhf8f4fkf created
​
​
[root@vm21 yaml]# kubectl get configmap
NAME                       DATA   AGE
game-config-4-tbg7c4gc77   1      7m11s
game-config-5-tfhf8f4fkf   1      9s
kube-root-ca.crt           1      132m
​
[root@vm21 yaml]# kubectl describe configmap/game-config-5-tfhf8f4fkf
Name:         game-config-5-tfhf8f4fkf
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
​
BinaryData
====
​
Events:  <none>
​
​
[root@vm21 yaml]# kubectl get configmap/game-config-5-tfhf8f4fkf -o yaml
apiVersion: v1
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
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"game-special-key":"enemies=aliens\nlives=3\nenemies.cheat=true\nenemies.cheat.level=noGoodRotten\nsecret.code.passphrase=UUDDLRLRBABAS\nsecret.code.allowed=true\nsecret.code.lives=30"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"game-config-5-tfhf8f4fkf","namespace":"default"}}
  creationTimestamp: "2022-04-10T13:45:35Z"
  name: game-config-5-tfhf8f4fkf
  namespace: default
  resourceVersion: "12242"
  uid: 4647a12d-780c-456c-87a6-044d12c5c3b9
​

1.4使用ConfigMap

1.4.1环境变量方式

# 1.在 ConfigMap 中将环境变量定义为键值对:
kubectl create configmap special-config --from-literal=special.how=very
​
# 2.将 ConfigMap 中定义的 special.how 赋值给 Pod 规约中的 SPECIAL_LEVEL_KEY 环境变量
vi example-pod.yaml
​
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox:latest                    #可以到https://hub.docker.com/搜索镜像包
      command: [ "/bin/sh", "-c", "env" ]
      env:
        # 定义环境变量
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
  restartPolicy: Never
​
#
kubectl create -f example-pod.yaml

1.4.2数据卷方式

vi pod-configmap-volume-specific-key.yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
---
# 使用存储在 ConfigMap 中的数据填充卷
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # 提供包含要添加到容器中的文件的 ConfigMap 的名称
        name: special-config
  restartPolicy: Never
---
# 使用存储在 ConfigMap 中的数据填充卷
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: k8s.gcr.io/busybox
      command: [ "/bin/sh","-c","cat /etc/config/keys" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: special-config
        items:
        - key: SPECIAL_LEVEL
          path: keys
  restartPolicy: Never
  
# 创建容器
kubectl apply -f pod-configmap-volume-specific-key.yaml

1.5实战

接下来,使用上述已讲过的内容,亲自实践部署一个redis容器

1.5.1ConfigMap 配置 Redis

cd /opt/yaml/redis
cat <<EOF >example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: ""
EOF

1.5.2应用yaml文件

[root@vm21 yaml]# kubectl apply -f example-redis-config.yaml
configmap/example-redis-config created

1.5.3创建容器yaml清单

cd /opt/yaml/redis
vi redis-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: redis
spec:
  containers:
  - name: redis
    image: redis:5.0.4
    command:
      - redis-server
      - "/redis-master/redis.conf"
    env:
    - name: MASTER
      value: "true"
    ports:
    - containerPort: 6379
    resources:
      limits:
        cpu: "0.1"
    volumeMounts:
    - mountPath: /redis-master-data
      name: data
    - mountPath: /redis-master
      name: config
  volumes:
    - name: data
      emptyDir: {}
    - name: config
      configMap:
        name: example-redis-config
        items:
        - key: redis-config
          path: redis.conf

1.5.4应用yaml文件

[root@vm21 redis]# kubectl apply -f redis-pod.yaml
pod/redis created

1.5.5查看容器状态

[root@vm21 redis]# kubectl get pods -n default
NAME    READY   STATUS    RESTARTS   AGE
redis   1/1     Running   0          2m21s

1.5.6查看ConfigMap键值

[root@vm21 redis]# kubectl describe configmap/example-redis-config
Name:         example-redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>
​
Data
====
redis-config:
----
​
​
BinaryData
====
​
Events:  <none>

1.5.7容器操作

[root@vm21 redis]# kubectl exec -it redis -- redis-cli
# 查看 maxmemory
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "0"
# 查看maxmemory-policy
127.0.0.1:6379> CONFIG GET maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"

1.5.8更新ConfigMap键值

cd /opt/yaml/redis
vi example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru

1.5.9应用yaml更新

[root@vm21 redis]# kubectl apply -f example-redis-config.yaml
configmap/example-redis-config configured

1.5.10确认ConfigMap已更新

[root@vm21 redis]# kubectl describe configmap/example-redis-config
Name:         example-redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>
​
Data
====
redis-config:
----
maxmemory 2mb
maxmemory-policy allkeys-lru
​
​
BinaryData
====
​
Events:  <none>

1.5.11验证容器配置

[root@vm21 redis]# kubectl exec -it redis -- redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "0"
127.0.0.1:6379> CONFIG GET maxmemory-policy
1) "maxmemory-policy"
2) "noeviction"
​
# 配置值未更改,因为需要重新启动 Pod 才能从关联的 ConfigMap 中获取更新的值。

1.5.12重建容器

# 删除已有容器
[root@vm21 redis]# kubectl delete pod redis
pod "redis" deleted
​
# 重新创建容器
[root@vm21 redis]# kubectl apply -f redis-pod.yaml
pod/redis created
​

1.5.13验证容器配置项

[root@vm21 redis]# kubectl exec -it redis -- redis-cli
127.0.0.1:6379> CONFIG GET maxmemory
1) "maxmemory"
2) "2097152"
127.0.0.1:6379> CONFIG GET maxmemory-policy
1) "maxmemory-policy"
2) "allkeys-lru"

1.5.14清理实验内容

# 清理pod
[root@vm21 redis]# kubectl delete -f redis-pod.yaml
pod "redis" deleted
​
# 清理configmap
[root@vm21 redis]# kubectl delete -f example-redis-config.yaml
configmap "example-redis-config" deleted

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值