1.configmap

configmap用于保存配置数据的键值对,可以用来保存单个属性,也可以用来保存配置文件。configmap和secret很类似,但他可以更方便的处理不包含敏感信息的字符串。

2.configmap创建

2.1 key-value字符串创建

kubectl create configmap test -n study --from-literal=test.hello=hello --from-literal=testhello.hi=hi

在这里插入图片描述
当然也可以使用格式化输出形式查看
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
为了后面例子能够串起来,我们删除这个configmap,然后重新创建一个:
在这里插入图片描述
在这里插入图片描述

2.2 env文件创建

在这里插入图片描述

2.3 从目录创建

在这里插入图片描述

2.4 yaml/json创建

在这里插入图片描述
在这里插入图片描述

3.使用

3.1 pod内env

创建一个pod:
useforenv.yaml

apiVersion: v1
kind: Pod
metadata: 
  name: tomcat-study
  namespace: study
  labels:
    mytomcat: study
spec: 
  containers:
  - name: tomcat
    env:
    - name: test_command_hello
      valueFrom:
        configMapKeyRef:
          name: testcommand
          key: test.command.hello
    - name: test_command_hi
      valueFrom:
        configMapKeyRef:
          name: testcommand
          key: test.command.hi
    - name: test_env_hello
      valueFrom:
        configMapKeyRef:
          name: testenv
          key: test.env.hello
    - name: testenvhello
      valueFrom:
        configMapKeyRef:
          name: testenv
          key: testenvhello
    - name: test_env_hi
      valueFrom:
        configMapKeyRef:
          name: testenv
          key: test.env.hi
    image: tomcat
    imagePullPolicy: IfNotPresent
    command: ["/usr/local/tomcat/bin/catalina.sh","run"]
    workingDir: /usr/local/tomcat/
    volumeMounts:
    - name: tomcat-log
      mountPath: /usr/local/tomcat/logs/
      readOnly: false
    ports:
    - name: tomcat-80
      containerPort: 80
      hostPort: 10080
      protocol: TCP
    - name: tomcat-8080
      containerPort: 8080
      hostPort: 18080
      protocol: TCP
    - name: tomcat-443
      containerPort: 443
      hostPort: 10443
      protocol: TCP
    resources:
      limits:
        cpu: 500m
        memory: 500Mi
      requests:
        cpu: 200m
        memory: 50Mi
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 180
      timeoutSeconds: 30
      periodSeconds: 600
  volumes:
  - name: tomcat-log
    nfs:
      server: 10.0.228.93
      path: /userdata/testtomcatlog

在这里插入图片描述

3.2 command

先创建一个configmap
在这里插入图片描述
在command中使用configmp需要将configmap先用3.1的方式设置为环境变量,然后在command中用$(envName)的方式使用。
useforcommand.yaml

apiVersion: v1
kind: Pod
metadata: 
  name: tomcat-study
  namespace: study
  labels:
    mytomcat: study
spec: 
  containers:
  - name: tomcat
    image: tomcat
    imagePullPolicy: IfNotPresent
    command: ["/usr/local/tomcat/bin/catalina.sh","$(command_test_run)"]
    workingDir: /usr/local/tomcat/
    env:
    - name: command_test_run
      valueFrom:
        configMapKeyRef:
          name: command-run
          key: command.test.run
    volumeMounts:
    - name: tomcat-log
      mountPath: /usr/local/tomcat/logs/
      readOnly: false
    ports:
    - name: tomcat-80
      containerPort: 80
      hostPort: 10080
      protocol: TCP
    - name: tomcat-8080
      containerPort: 8080
      hostPort: 18080
      protocol: TCP
    - name: tomcat-443
      containerPort: 443
      hostPort: 10443
      protocol: TCP
    resources:
      limits:
        cpu: 500m
        memory: 500Mi
      requests:
        cpu: 200m
        memory: 50Mi
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 180
      timeoutSeconds: 30
      periodSeconds: 600
  volumes:
  - name: tomcat-log
    nfs:
      server: 10.0.228.93
      path: /userdata/testtomcatlog

在这里插入图片描述
在这里插入图片描述
已经启动了。。

3.3 volume挂载

创建configmap可以根据文件及目录进行创建,同样的,在使用的时候,可以根据configmap恢复成文件目录
在这里插入图片描述
根据这个目录创建configmap
在这里插入图片描述
在pod中使用
useforvolume.yaml

apiVersion: v1
kind: Pod
metadata: 
  name: tomcat-study
  namespace: study
  labels:
    mytomcat: study
spec: 
  containers:
  - name: tomcat
    env:
    - name: test_command_hello
      valueFrom:
        configMapKeyRef:
          name: testcommand
          key: test.command.hello
    - name: test_command_hi
      valueFrom:
        configMapKeyRef:
          name: testcommand
          key: test.command.hi
    - name: test_env_hello
      valueFrom:
        configMapKeyRef:
          name: testenv
          key: test.env.hello
    - name: testenvhello
      valueFrom:
        configMapKeyRef:
          name: testenv
          key: testenvhello
    - name: test_env_hi
      valueFrom:
        configMapKeyRef:
          name: testenv
          key: test.env.hi
    image: tomcat
    imagePullPolicy: IfNotPresent
    command: ["/usr/local/tomcat/bin/catalina.sh","run"]
    workingDir: /usr/local/tomcat/
    volumeMounts:
    - name: tomcat-log
      mountPath: /usr/local/tomcat/logs/
      readOnly: false
    - name: testdir
      mountPath: /usr/local/test/
      readOnly: false
    ports:
    - name: tomcat-80
      containerPort: 80
      hostPort: 10080
      protocol: TCP
    - name: tomcat-8080
      containerPort: 8080
      hostPort: 18080
      protocol: TCP
    - name: tomcat-443
      containerPort: 443
      hostPort: 10443
      protocol: TCP
    resources:
      limits:
        cpu: 500m
        memory: 500Mi
      requests:
        cpu: 200m
        memory: 50Mi
    livenessProbe:
      tcpSocket:
        port: 80
      initialDelaySeconds: 180
      timeoutSeconds: 30
      periodSeconds: 600
  volumes:
  - name: tomcat-log
    nfs:
      server: 10.0.228.93
      path: /userdata/testtomcatlog
  - name: testdir
    configMap:
      name: testdir

在这里插入图片描述

4.总结

  • configmap就是k8s集群中公共的key-value集合,所有的pod都可以使用。
  • configmap也是k8s中一类资源
  • configmap有4种基本的创建方式:字符串、env、文件目录、定义文件
  • configmap在pod中有3种使用方式:env、command、volume挂载
Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐