0
点赞
收藏
分享

微信扫一扫

html学习笔记02-基础,元素

ITWYY 2023-06-29 阅读 83
kubernetes

【云原生Kubernetes】11-ConfigMap解析

文章目录

简介

ConfigMap的作用

了解ConfigMap和Pod

  • ConfigMap 是一个 API 对象, 让你可以存储其他对象所需要使用的配置。 和其他 Kubernetes 对象都有一个 spec 不同的是,ConfigMap 使用 databinaryData 字段。这些字段能够接收键-值对作为其取值。databinaryData 字段都是可选的。data 字段设计用来保存 UTF-8 字符串,而 binaryData 则被设计用来保存二进制数据作为 base64 编码的字串。

  • ConfigMap 的名字必须是一个合法的 DNS 子域名。

  • databinaryData 字段下面的每个键的名称都必须由字母数字字符或者 -_. 组成。在 data 下保存的键名不可以与在 binaryData 下出现的键名有重叠。

ConfigMap 的 data 字段包含配置数据。如下例所示,它可以简单 (如用 --from-literal 的单个属性定义)或复杂 (如用 --from-file 的配置文件或 JSON blob 定义)

apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2016-02-18T19:14:38Z
  name: example-config
  namespace: default
data:
  # 使用 --from-literal 定义的简单属性
  example.property.1: hello
  example.property.2: world
  # 使用 --from-file 定义复杂属性的例子
  example.property.file: |-
    property.1=value-1
    property.2=value-2
    property.3=value-3 

创建ConfigMap

  • 可以使用 kubectl create configmap 创建;
  • 可以在 xxx.yaml 中使用kind:configmap资源来创建

1. 使用kubectl create configmap 创建

  • 格式语法
kubectl create configmap <映射名称> <数据源>

2. 使用configmap资源创建ConfigMap

  • 定义configmap资源文件
apiVersion: v1
kind: ConfigMap
metadata:
  name: my-config-map
data:
  key1: value1
  key2: value2

使用–from-file基于一个目录来创建 ConfigMap

​ 可以使用 kubectl create configmap 基于同一目录中的多个文件创建 ConfigMap。 当你基于目录来创建 ConfigMap 时,kubectl 识别目录下文件名可以作为合法键名的文件, 并将这些文件打包到新的 ConfigMap 中。普通文件之外的所有目录项都会被忽略 (例如:子目录、符号链接、设备、管道等等)。

  • 创建本地目录:
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

[root@master configmap]# ls -l
total 8
-rw-r--r-- 1 root root 157 May 29 02:44 game.properties
lrwxrwxrwx 1 root root   8 May 29 02:46 log -> /var/log
-rw-r--r-- 1 root root  83 May 29 02:44 ui.properties
  • 创建configmap
[root@master ~]# kubectl create configmap game-config --from-file=configure-pod-container/configmap/
configmap/game-config created
[root@master ~]#
  • 查看configmap详细信息
[root@master ~]# kubectl describe configmaps 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>
[root@master ~]#
  • 查看configmap的yaml格式
[root@master ~]# kubectl get configmaps 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: "2023-05-28T18:47:57Z"
  name: game-config
  namespace: default
  resourceVersion: "316804"
  uid: b99b03cf-d7d8-4f41-9eca-60edcf77369c
[root@master ~]#

使用–from-file基于文件创建 ConfigMap

​ 可以使用 kubectl create configmap 基于单个文件或多个文件创建 ConfigMap

  • 基于单个文件创建configmap
[root@master ~]# kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties
configmap/game-config-2 created
[root@master ~]#
  • 查看详细信息
[root@master ~]# kubectl get configmap game-config-2
NAME            DATA   AGE
game-config-2   1      26s
[root@master ~]#
[root@master ~]# kubectl describe  configmap game-config-2
Name:         game-config-2
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
Events:  <none>
[root@master ~]#

可以多次使用 --from-file 参数,从多个数据源创建 ConfigMap。

  • 基于多个文件创建configmap
[root@master ~]# kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties
configmap/game-config-2 created
[root@master ~]#
  • 查看详细信息
[root@master ~]# kubectl get configmap game-config-2 -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: "2023-05-28T19:00:24Z"
  name: game-config-2
  namespace: default
  resourceVersion: "317879"
  uid: 4423fe0b-fb74-4d2d-8b37-e435bbc173d2
[root@master ~]#

定义从文件创建 ConfigMap 时要使用的键

​ 在使用 --from-file 参数时,你可以定义在 ConfigMap 的 data 部分出现键名, 而不是按默认行为使用文件名。

kubectl create configmap  configmap-name  --from-file=<我的键名>=<文件路径>
  • 创建示例
[root@master configmap]# kubectl create configmap test --from-file=text11=game.properties
configmap/test created
[root@master configmap]#
  • 查看详细信息
[root@master configmap]# kubectl describe configmap test
Name:         test
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
text11:
----
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>
[root@master configmap]#

使用–from-env-file选项基于 env 文件创建 ConfigMap

  • 将示例文件下载
# 将示例文件下载到 `configure-pod-container/configmap/` 目录
wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
wget https://kubernetes.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties
  • Env 文件 game-env-file.properties 如下所示
[root@master ~]# cat configure-pod-container/configmap/game-env-file.properties
enemies=aliens
lives=3
allowed="true"

# This comment and the empty line above it are ignored
[root@master ~]#
  • 创建configmap
[root@master ~]# kubectl create configmap game-config-env-file --from-env-file=configure-pod-container/configmap/game-env-file.properties
configmap/game-config-env-file created
[root@master ~]#
  • 查看详细信息
[root@master ~]# kubectl describe configmap game-config-env-file
Name:         game-config-env-file
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
lives:
----
3
allowed:
----
"true"
enemies:
----
aliens
Events:  <none>
[root@master ~]#
  • 查看yaml信息
[root@master ~]# kubectl get configmap game-config-env-file -o yaml
apiVersion: v1
data:
  allowed: '"true"'
  enemies: aliens
  lives: "3"
kind: ConfigMap
metadata:
  creationTimestamp: "2023-05-28T19:26:22Z"
  name: game-config-env-file
  namespace: default
  resourceVersion: "320111"
  uid: b7651222-2a42-4701-8a48-201a8492b0f5
[root@master ~]#

使用–from-literal根据字面值创建 ConfigMap

可以将 kubectl create configmap--from-literal 参数一起使用, 通过命令行定义文字值。

[root@master configmap]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created
[root@master configmap]#

可以传入多个键值对。命令行中提供的每对键值在 ConfigMap 的 data 部分中均表示为单独的条目。

[root@master configmap]# kubectl get configmaps special-config -o yaml
apiVersion: v1
data:
  special.how: very
  special.type: charm
kind: ConfigMap
metadata:
  creationTimestamp: "2023-05-28T19:40:30Z"
  name: special-config
  namespace: default
  resourceVersion: "321325"
  uid: 431d078c-80c0-4b75-a49a-29cb66dbe859
[root@master configmap]#

基于生成器创建 ConfigMap

  • 还可以基于生成器(Generators)创建 ConfigMap,然后将其应用于集群的 API 服务器上创建对象。 生成器应在目录内的 kustomization.yaml 中指定。
  • 个人任务使用生成器(Generators)创建 ConfigMap没有太大必要,直接使用上面的方法即可,如果想了解请查阅官方文档

使用ConfigMap

使用 ConfigMap 数据定义容器环境变量

在Pod中通过env来设置

1. 在 ConfigMap 中将环境变量定义为键值对:

  • 创建configmap
[root@master configmap]# kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created
[root@master configmap]#
  • 查看configmap
[root@master ~]# kubectl describe configmap special-config
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
special.how:
----
very

special.type:
----
charm
Events:  <none>
[root@master ~]#
  • 创建Pod使用configmap定义容器的环境变量
apiVersion: v1
kind: Pod   
metadata:  
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: busybox
      command: [ "/bin/sh", "-c", "env" ]
      env:
        # 定义环境变
        - name: xhz
          valueFrom:
            configMapKeyRef:
              # ConfigMap 包含你要赋给 xhz 的值
              name: special-config
              # 指定与取值相关的键名
              key: special.how
        - name: flf
          valueFrom:
            configMapKeyRef:
              # ConfigMap 包含你要赋给 flf 的值
              name: special-config
              # 指定与取值相关的键名
              key: special.type
  restartPolicy: Never
  • 创建pod,并查看pod日志
[root@master configmap]# kubectl apply  -f pod1.yml
pod/dapi-test-pod created
[root@master configmap]#
[root@master configmap]# kubectl logs   dapi-test-pod  |grep xhz
xhz=very
[root@master configmap]# kubectl logs   dapi-test-pod  |grep flf
flf=charm
[root@master configmap]#

在Pod中使用envFrom来设置

  • 查看configmap
[root@master configmap]# kubectl describe configmap game-config-env-file
Name:         game-config-env-file
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
allowed:
----
"true"
enemies:
----
aliens
lives:
----
3
Events:  <none>
[root@master configmap]#
  • 创建Pod使用configmap定义容器的环境变量
apiVersion: v1
kind: Pod   
metadata:  
  name: dapi-test-pod-2
spec:
  containers:
    - name: test-container-2
      image: busybox
      command: [ "/bin/sh", "-c", "env" ]
      envFrom:
        - configMapRef:
            name: game-config-env-file
  restartPolicy: Never
  • 创建pod。查看信息
[root@master configmap]#
[root@master configmap]# kubectl apply  -f pod2.yaml
pod/dapi-test-pod-2 created
[root@master configmap]# kubectl logs dapi-test-pod-2 |grep allowed
allowed="true"
[root@master configmap]# kubectl logs dapi-test-pod-2 |grep enemies
enemies=aliens
[root@master configmap]#
[root@master configmap]# kubectl logs dapi-test-pod-2 |grep lives
lives=3
[root@master configmap]#

总结

将 ConfigMap 数据添加到容器的文件

  • 上面所述的,将configmap数据添加到容器的环境变量,而这些configmap都是通过–from-env-file,字面键值对来创建的;然后那些通过--from-file 创建 ConfigMap 时,文件名成为存储在 ConfigMap 的 data 部分中的键, 文件内容成为键对应的值,这个时候就需要通过volume来使用configmap中的数据。(当然通过–from-env-file,字面键值对来创建的configmap,也可以通过volume来导入到容器的文件中)
  • 数据会展现为 UTF-8 字符编码的文件。如果使用其他字符编码, 可以使用 binaryData.(详情参阅 ConfigMap 对象)。

通过volumes来使用configmap中的数据

1. --from-env-file来创建的configmap

  • 创建configmap
[root@master ~]# kubectl create configmap game-config-env-file --from-env-file=configure-pod-container/configmap/game-env-file.properties
configmap/game-config-env-file created
[root@master ~]#
[root@master ~]# kubectl describe configmap game-config-env-file
Name:         game-config-env-file
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
allowed:
----
"true"
enemies:
----
aliens
lives:
----
3
Events:  <none>
[root@master ~]#
  • 通过valume将configmap中的数据导入
apiVersion: v1
kind: Pod
metadata:
  name: test-pod-3
spec:
  containers:
    - name: test-container-3
      image: busybox
      imagePullPolicy: IfNotPresent
      command: [ "/bin/sh", "-c", "sleep 3600"]
      volumeMounts:
        - name: configmap-volume
          mountPath: /etc/config
  volumes:
    - name: configmap-volume
      configMap:
        name: game-config-env-file
  restartPolicy: Never
  • 进入容器查看数据
[root@master configmap]# kubectl exec  -it test-pod-3  /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # cd /etc/config/
/etc/config # ls
allowed  enemies  lives
/etc/config # ls -l
total 0
lrwxrwxrwx    1 root     root            14 May 28 21:27 allowed -> ..data/allowed
lrwxrwxrwx    1 root     root            14 May 28 21:27 enemies -> ..data/enemies
lrwxrwxrwx    1 root     root            12 May 28 21:27 lives -> ..data/lives
/etc/config # cat allowed
/etc/config # cat enemies
aliens/etc/config #
/etc/config # cat lives
3/etc/config #

2. --from-literal来创建的configmap

  • 创建的configmap
[root@master configmap]#  kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm
configmap/special-config created
[root@master configmap]#
[root@master configmap]# kubectl describe configmap special-config
Name:         special-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
special.how:
----
very
special.type:
----
charm
Events:  <none>
[root@master configmap]#
  • 通过valume将configmap中的数据导入
apiVersion: v1
kind: Pod
metadata:
  name: test-pod-4
spec:
  containers:
    - name: test-container-4
      image: busybox
      imagePullPolicy: IfNotPresent
      command: [ "/bin/sh", "-c", "sleep 3600"]
      volumeMounts:
        - name: configmap-volume
          mountPath: /etc/config
  volumes:
    - name: configmap-volume
      configMap:
        name: special-config
  restartPolicy: Never
  • 创建并进入容器查看数据
[root@master configmap]# kubectl apply  -f pod4.yaml
pod/test-pod-4 created
[root@master configmap]#
[root@master configmap]# kubectl exec -it test-pod-4 /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # cd /etc/config/
/etc/config # ls -l
total 0
lrwxrwxrwx    1 root     root            18 May 28 21:44 special.how -> ..data/special.how
lrwxrwxrwx    1 root     root            19 May 28 21:44 special.type -> ..data/special.type
/etc/config # cat special.how
very
/etc/config #
/etc/config # cat special.type
charm
etc/config #
/etc/config #

3. --from-file来创建的configmap

  • 查看confimap
[root@master configmap]# 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>
[root@master configmap]#
  • 通过valume将configmap中的数据导入
apiVersion: v1
kind: Pod
metadata:
  name: test-pod-5
spec:
  containers:
    - name: test-container-5
      image: busybox
      imagePullPolicy: IfNotPresent
      command: [ "/bin/sh", "-c", "sleep 3600"]
      volumeMounts:
        - name: configmap-volume
          mountPath: /etc/config
  volumes:
    - name: configmap-volume
      configMap:
        name: game-config
  restartPolicy: Never
  • 创建并进入容器查看数据
[root@master configmap]# kubectl exec -it test-pod-5 /bin/sh
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
/ # cd /etc/config/
/etc/config # ls -l
total 0
lrwxrwxrwx    1 root     root            22 May 28 21:49 game.properties -> ..data/game.properties
lrwxrwxrwx    1 root     root            20 May 28 21:49 ui.properties -> ..data/ui.properties
/etc/config # cat 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/etc/config #
/etc/config # cat ui.properties
color.good=purple
color.bad=yellow
allow.textmode=true
how.nice.to.look=fairlyNice
/etc/config #
/etc/config #

将 ConfigMap 数据添加到卷中的特定文件

​ 使用 path 字段为特定的 ConfigMap 项目指定预期的文件路径。 在这里,ConfigMap 中键 SPECIAL_LEVEL 的内容将挂载在 config-volume 卷中 /etc/config/keys 文件中。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.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

ConfigMap其他说明

可选的 ConfigMap

你可以在 Pod 规约中将对 ConfigMap 的引用标记为 可选(optional)。 如果 ConfigMap 不存在,那么它在 Pod 中为其提供数据的配置(例如:环境变量、挂载的卷)将为空。 如果 ConfigMap 存在,但引用的键不存在,那么数据也是空的。

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: gcr.io/google_containers/busybox
      command: ["/bin/sh", "-c", "env"]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: a-config
              key: akey
              optional: true # 将环境变量标记为可选
  restartPolicy: Never

你也可以在 Pod 规约中将 ConfigMap 提供的卷和文件标记为可选。 此时 Kubernetes 将总是为卷创建挂载路径,即使引用的 ConfigMap 或键不存在。

ConfigMap限制

  • 在 Pod 规约中引用某个 ConfigMap 之前,必须先创建这个对象, 或者在 Pod 规约中将 ConfigMap 标记为 optional(请参阅可选的 ConfigMaps)。 如果所引用的 ConfigMap 不存在,并且没有将应用标记为 optional 则 Pod 将无法启动。 同样,引用 ConfigMap 中不存在的主键也会令 Pod 无法启动,除非你将 Configmap 标记为 optional

  • 如果你使用 envFrom 来基于 ConfigMap 定义环境变量,那么无效的键将被忽略。 Pod 可以被启动,但无效名称将被记录在事件日志中(InvalidVariableNames)。 日志消息列出了每个被跳过的键。例如:

kubectl get events

##输出与此类似:
LASTSEEN FIRSTSEEN COUNT NAME          KIND  SUBOBJECT  TYPE      REASON                            SOURCE                MESSAGE
0s       0s        1     dapi-test-pod Pod              Warning   InvalidEnvironmentVariableNames   {kubelet, 127.0.0.1}  Keys [1badkey, 2alsobad] from the EnvFrom configMap default/myconfig were skipped since they are considered invalid environment variable names.
  • ConfigMap 位于确定的命名空间中。 每个 ConfigMap 只能被同一名字空间中的 Pod 引用.

  • 你不能将 ConfigMap 用于静态 Pod, 因为 Kubernetes 不支持这种用法。

ConfigMap操作实例

使用 ConfigMap 来配置mysql

  • 创建mysql的configmap文件
[root@master configmap]# kubectl delete -f mysql-pod.yml
pod "mysql-pod" deleted
[root@master configmap]# cat mysql-map
MYSQL_ROOT_PASSWORD=root123
MYSQL_DATABASE=test
MYSQL_USER=uos
MYSQL_PASSWORD=uos123
[root@master configmap]#
[root@master configmap]# kubectl create configmap mysql-map --from-env-file=mysql-map
configmap/mysql-map created
[root@master configmap]#
[root@master configmap]#
[root@master configmap]# kubectl describe configmap mysql-map
Name:         mysql-map
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
MYSQL_PASSWORD:
----
uos123
MYSQL_ROOT_PASSWORD:
----
root123
MYSQL_USER:
----
uos
MYSQL_DATABASE:
----
test
Events:  <none>
[root@master configmap]#
  • 创建mysql pod的资源文件
apiVersion: v1
kind: Pod
metadata:
  name: mysql-pod

spec:
  containers:
    - name: mysql-container
      image: mysql:5.7
      imagePullPolicy: IfNotPresent
      envFrom:
        - configMapRef:
            name: mysql-map

      ports:
        - containerPort: 3306
        
##当然也可以使用env逐个导入
      env:
        - name: MYSQL_ROOT_PASSWORD
          valumeFrom:
            configMapKeyRef:
              name: mysql-map
              key: MYSQL_ROOT_PASSWORD
        - name: MYSQL_DATABASE
          valumeFrom:
            configMapKeyRef:
              name: mysql-map
              key: MYSQL_DATABASE
        .....
  • 创建Pod,查看Pod信息
[root@master configmap]# kubectl apply -f mysql-pod.yml
pod/mysql-pod created
[root@master configmap]#
[root@master configmap]# kubectl get pods mysql-pod -o wide
NAME        READY   STATUS    RESTARTS   AGE   IP             NODE              NOMINATED NODE   READINESS GATES
mysql-pod   1/1     Running   0          26s   10.244.2.102   192.168.194.131   <none>           <none>
[root@master configmap]# 
  • 登录测试
[root@master configmap]# mysql -uroot -proot123 -h10.244.2.102
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MySQL [(none)]> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
| test               |
+--------------------+
5 rows in set (0.00 sec)

MySQL [(none)]> exit
Bye
[root@master configmap]# mysql -uuos -puos123 -h10.244.2.102
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.36 MySQL Community Server (GPL)

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MySQL [(none)]> Ctrl-C -- exit!
Aborted
[root@master configmap]#

使用 ConfigMap 来配置 Redis

  • 创建ConfigMap
[root@master configmap]# cat example-redis-config.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: ""
[root@master configmap]#
[root@master configmap]# kubectl apply  -f example-redis-config.yaml
configmap/example-redis-config created
[root@master configmap]#
[root@master configmap]#
[root@master configmap]# kubectl describe configmap example-redis-config
Name:         example-redis-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
redis-config:
----

Events:  <none>
[root@master configmap]#
### 数据redis-config为空
  • 创建redis Pod资源文件
[root@master configmap]# cat 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:
    - name: data
      mountPath: /redis-master-data

    - name: config
      mountPath: /redis-master
  volumes:
    - name: data
      emptyDir: {}

    - name: config
      configMap:
        name: example-redis-config
        items:
        - key: redis-config
          path: redis.conf
[root@master configmap]#
[root@master configmap]#
  • 查看Pod信息
[root@master configmap]# kubectl apply  -f redis-pod.yaml
pod/redis created
[root@master configmap]#
[root@master configmap]# kubectl get  pods redis -o wide
NAME    READY   STATUS    RESTARTS   AGE     IP            NODE              NOMINATED NODE   READINESS GATES
redis   1/1     Running   0          3m41s   10.244.1.46   192.168.194.130   <none>           <none>
[root@master configmap]# 
  • 使用 kubectl exec 进入 pod,运行 redis-cli 工具检查当前配置:
[root@master configmap]# 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"
127.0.0.1:6379>
  • 现在,向 example-redis-config ConfigMap 添加一些配置:
apiVersion: v1
kind: ConfigMap
metadata:
  name: example-redis-config
data:
  redis-config: |
    maxmemory 2mb
    maxmemory-policy allkeys-lru  
  • 更新configmap,并查看
[root@master configmap]# kubectl apply  -f example-redis-config.yaml
configmap/example-redis-config configured
[root@master configmap]#
[root@master configmap]#
[root@master configmap]# 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
Events:  <none>
[root@master configmap]#
  • 再次验证redis
[root@master configmap]# 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"
127.0.0.1:6379>

举报

相关推荐

【HTML】02-基础标签

HTML+CSS学习02-基本标签

HTML基础-02-文本格式化

02-组件基础

02-表单&css基础

【JavaSE基础】02-面向对象

初学html基础笔记整理02

0 条评论