k8s-ConfigMap

 

[root@wyl01  cm]# cat env-config.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO

[root@wyl01  cm]# kubectl get cm env-config -o yaml
apiVersion: v1
data:
  log_level: DEBUG
kind: ConfigMap
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"v1","data":{"log_level":"INFO"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"env-config","namespace":"default"}}
  creationTimestamp: "2019-12-27T02:35:19Z"
  name: env-config
  namespace: default
  resourceVersion: "2028897"
  selfLink: /api/v1/namespaces/default/configmaps/env-config
  uid: 0208b7ce-2811-4bb4-b12c-faafff39c857

 
[root@wyl01  cm]# kubectl describe cm env-config 
Name:         env-config
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"log_level":"INFO"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"env-config","namespace":"default"}}

Data
====
log_level:
----
INFO
Events:  <none>
[root@wyl01 cm]# cat special-config.yaml 
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
  special.type: charm

#将两个ConfigMap文件注入到pod环境中

[root@wyl01 cm]# cat test-pod.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: ikubernetes/myapp:v1
      command: ["/bin/sh","-c","env"]
      env:                       
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config   
              key: special.how      
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.type
      envFrom:               
        - configMapRef: 
            name: env-config
  restartPolicy: Never

[root@wyl01 cm]# vim test2-pod.yaml 

apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod2
spec:
  containers:
    - name: test-container2
      image: ikubernetes/myapp:v1
      command: ["/bin/sh","-c","echo $(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)"]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.type
  restartPolicy: Never

ConfigMap 的热更新


[root@wyl01 cm]# cat test4-deploy.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-nginx
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-nginx
  template:
    metadata:
      labels:
        app: my-nginx
    spec:
      containers:
        - name: my-nginx
          image: ikubernetes/myapp:v1
          ports:
            - containerPort: 80
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
      volumes:
        - name: config-volume
          configMap:
            name: env-config

等待15s,值会变成INFO

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
k8s中的ConfigMap是一种用于存储配置数据的资源对象,可以在容器中使用这些配置数据。PV(Persistent Volume)是一种持久化存储资源,用于将存储卷与Pod进行绑定。下面是关于k8s中的nginx ConfigMap和PV的介绍和演示: 1. 创建nginx ConfigMap: ```shell kubectl create configmap nginx-config --from-file=/etc/config/ ``` 这将创建一个名为nginx-configConfigMap,并将/etc/config/目录下的所有文件作为配置数据。 2. 查看ConfigMap信息: ```shell kubectl get configmap nginx-config ``` 这将显示名为nginx-configConfigMap的详细信息。 3. 在Pod中使用ConfigMap: 可以在Pod的配置文件中使用ConfigMap来注入配置数据。例如,在Pod的配置文件中添加以下内容: ```yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx-container image: nginx volumeMounts: - name: config-volume mountPath: /etc/config volumes: - name: config-volume configMap: name: nginx-config ``` 这将在Pod中创建一个名为config-volume的卷,并将nginx-config ConfigMap的配置数据挂载到Pod的/etc/config目录下。 4. 创建PV: ```shell kubectl apply -f pv.yaml ``` 其中pv.yaml是一个包含PV定义的YAML文件。 5. 查看PV信息: ```shell kubectl get pv ``` 这将显示所有PV的详细信息。 6. 将PV与Pod绑定: 可以在Pod的配置文件中使用PV来绑定持久化存储。例如,在Pod的配置文件中添加以下内容: ```yaml apiVersion: v1 kind: Pod metadata: name: nginx-pod spec: containers: - name: nginx-container image: nginx volumeMounts: - name: data-volume mountPath: /data volumes: - name: data-volume persistentVolumeClaim: claimName: my-pvc ``` 这将在Pod中创建一个名为data-volume的卷,并将名为my-pvc的PersistentVolumeClaim与之绑定。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值