k8s-kubernetes-configmap

网友投稿 682 2022-10-29

k8s-kubernetes-configmap

k8s-kubernetes-configmap

k8s-kubernetes-configmap

​​1.configmap​​​​2.configmap创建​​

​​2.1 key-value字符串创建​​​​2.2 env文件创建​​​​2.3 从目录创建​​​​2.4 yaml/json创建​​

​​3.使用​​

​​3.1 pod内env​​​​3.2 command​​​​3.3 volume挂载​​

​​4.总结​​

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: v1kind: Podmetadata: name: tomcat-study namespace: study labels: mytomcat: studyspec: 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:

3.2 command

先创建一个configmap

在command中使用configmp需要将configmap先用3.1的方式设置为环境变量,然后在command中用$(envName)的方式使用。

useforcommand.yaml

apiVersion: v1kind: Podmetadata: name: tomcat-study namespace: study labels: mytomcat: studyspec: 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:

已经启动了。。

3.3 volume挂载

创建configmap可以根据文件及目录进行创建,同样的,在使用的时候,可以根据configmap恢复成文件目录

根据这个目录创建configmap

在pod中使用

useforvolume.yaml

apiVersion: v1kind: Podmetadata: name: tomcat-study namespace: study labels: mytomcat: studyspec: 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:

4.总结

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:DayPeriodFormatter 一个日期本地化(早上,下午,晚上等)的格式化程序
下一篇:一个简单的基于注解的Android库,用于生成onActivityForResult处理程序
相关文章

 发表评论

暂时没有评论,来抢沙发吧~