ConfigMap 资源对象使用key-value形式的键值对来配置数据,这些数据可以在Pod里面使用

1、定义configmap的yaml

kind: ConfigMap
apiVersion: v1
metadata:
  name: config-demo
  namespace: default
data:
  data.1: hello
  data.2: world
  redis.conf: |
    host=192.168.100.88
    port=6379
kubectl create -f config-demo.yaml

2、查看

[root@k8s-node1 k8s]# kubectl get configmap
NAME          DATA   AGE
config-demo   3      5m

[root@k8s-node1 k8s]# kubectl describe configmaps/config-demo
Name:         config-demo
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
data.1:
----
hello
data.2:
----
world
redis.conf:
----
host=192.168.100.88
port=6379

Events:  <none>

3、使用configmap

创建pod

apiVersion: v1
kind: Pod
metadata:
  name: test-configmap
spec:
  containers:
    - name: test-configmap
 
      image: 172.16.10.190:8008/helloworld:0.0.5
      command: [ "/bin/sh","-c","cat /etc/config/path/to/redis.conf" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config

      ports:
 
        - containerPort: 8080
  volumes:
    - name: config-volume
      configMap:
        name: config-demo
        items:
        - key: redis.conf
          path: path/to/redis.conf
  imagePullSecrets:

    - name: myregistrykey6
kubectl create -f test.yaml

4、查看日志

可以发现获得到了配置的信息

[root@k8s-node1 k8s]# kubectl logs test-configmap
host=192.168.100.88
port=6379
Logo

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

更多推荐