当前位置 : 主页 > 编程语言 > java >

k8s-kubernetes-configmap

来源:互联网 收集:自由互联 发布时间:2022-07-22
k8s-kubernetes-configmap ​​1.configmap​​ ​​2.configmap创建​​ ​​2.1 key-value字符串创建​​ ​​2.2 env文件创建​​ ​​2.3 从目录创建​​ ​​2.4 yaml/json创建​​ ​​3.使用​​ ​​


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

k8s-kubernetes-configmap_统一配置


当然也可以使用格式化输出形式查看

k8s-kubernetes-configmap_configMap_02


k8s-kubernetes-configmap_kubernetes_03


k8s-kubernetes-configmap_统一配置_04


为了后面例子能够串起来,我们删除这个configmap,然后重新创建一个:

k8s-kubernetes-configmap_kubernetes_05


k8s-kubernetes-configmap_k8s_06

2.2 env文件创建

k8s-kubernetes-configmap_kubernetes_07

2.3 从目录创建

k8s-kubernetes-configmap_configMap_08

2.4 yaml/json创建

k8s-kubernetes-configmap_k8s_09


k8s-kubernetes-configmap_pod内如何使用configMap_10

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:

k8s-kubernetes-configmap_统一配置_11

3.2 command

先创建一个configmap

k8s-kubernetes-configmap_k8s_12


在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:

k8s-kubernetes-configmap_configMap_13


k8s-kubernetes-configmap_kubernetes_14


已经启动了。。

3.3 volume挂载

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

k8s-kubernetes-configmap_pod内如何使用configMap_15


根据这个目录创建configmap

k8s-kubernetes-configmap_pod内如何使用configMap_16


在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:

k8s-kubernetes-configmap_configMap_17

4.总结

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


【本文由:阿里云代理 http://www.56aliyun.com 复制请保留原URL】
上一篇:知道Java,你知道JavaScript吗?
下一篇:没有了
网友评论