Kubernetes系列-配置存储 ConfigMap & Secret

1 ConfigMap介绍

1.1 概述

在部署应用程序时,我们都会涉及到应用的配置,在容器中,如Docker容器中,如果将配置文件打入容器镜像,这种行为等同于写死配置,每次修改完配置,镜像就得重新构建。当然,我们也可以通过挂载包含该文件的卷进行配置管理和修改。而在k8s中,我们要讲一种更好的方式,即ConfigMap,这种资源对象的出现,更是极大的方便了应用程序的配置管理。
ConfigMap是一个或多个key/value的形式保存在k8s中,内部可以管理变量也可以管理完整的配置文件内容。

1.2 用法

  • 生成容器内的环境变量,在pod中可以通过spec.env或者spec.envFrom进行引用。
  • 设置容器启动命令的启动参数,前提是设置为环境变量。
  • 以卷volume的方式挂载到容器内部的文件或目录,通过spec.volumes引用。

注:在使用命令的时候注意单词: configmap等价于cm,cm算是简写,类似于deployment可以使用命令时写成deploy,service可以写成svc,namespace可以写成ns,pod可以写成po。

1.3 应用场景

configmap配置信息和镜像解耦,实现方式是把配置信息放到configmap对象中,然后在pod中作为volume挂载到pod中,从而实现导入配置的目的

适用场景:

  • 通过configmap给pod定义全局环境变量
  • 通过configmap给pod传递命令行参数,如mysql -u -p中的账户名密码都可以通过它传递
  • 通过configmap给pod中的容器服务提供配置文件,配置文件以挂载到容器的形式适用

注意事项:

  • configmap需要在pod使用它之前创建
  • pod和configmap必须在同一个namespace中才能使用
  • 主要用于非安全加密的配置场景
  • configmap通常用于小于1MB的配置,常用于配置文件

1.4 创建

1.4.1 yaml文件方式创建

示例:

apiVersion: v1
kind: ConfigMap
metadata:
  name: cm-test01
data:
  appconf01: value01
  appconf02: value02

命令:

[root@k8s-master k8s]# kubectl create -f cm-test01.yaml 
configmap/cm-test01 created

1.4.2 命令行方式创建

读取文件方式(也可以是目录)通过--from-file参数从文件中读取。可以指定key的名称,若不指定,则默认使用文件名为key。
案例:如当前目录有一个配置文件为test.properties

[root@k8s-master k8s]# cat test.properties 
key01:value01
key02:value02
conf01: value03

命令:

[root@k8s-master k8s]# kubectl create cm cm-test-file --from-file=test.properties
configmap/cm-test-file created

//指定参数方式,通过--from-literal指定keyxx=valuexx创建confimap中的data内配置属性。

[root@k8s-master k8s]#  kubectl create configmap cm-test-literal --from-literal=key01=value01 --from-literal=key02=value02
configmap/cm-test-literal created

1.5 查询

1.5.1 查询configmap列表

[root@k8s-master k8s]# kubectl get cm
NAME               DATA   AGE
cm-test-file       1      3m51s
cm-test-literal    2      3m30s
cm-test01          2      9m2s
kube-root-ca.crt   1      4d16h

1.5.2 查看configmap详细信息

[root@k8s-master k8s]# kubectl describe cm cm-test01
Name:         cm-test01
Namespace:    default
Labels:       
Annotations:  
 
Data
====
appconf01:
----
value01
appconf02:
----
value02
Events:  
[root@k8s-master k8s]# kubectl describe cm cm-test-file
Name:         cm-test-file
Namespace:    default
Labels:       
Annotations:  
 
Data
====
test.properties:
----
key01:value01
key02:value02
conf01: value03
 
 
Events:  
[root@k8s-master k8s]# kubectl describe cm cm-test-literal
Name:         cm-test-literal
Namespace:    default
Labels:       
Annotations:  
 
Data
====
key01:
----
value01
key02:
----
value02
Events:  

1.5.3 查看yaml输出

[root@k8s-master k8s]# kubectl get cm cm-test01 -o yaml
apiVersion: v1
data:
  appconf01: value01
  appconf02: value02
kind: ConfigMap
metadata:
  creationTimestamp: "2022-08-07T00:18:47Z"
  name: cm-test01
  namespace: default
  resourceVersion: "44876"
  uid: 7492a928-b163-4b86-a3dd-54a7fbe59a10
[root@k8s-master k8s]# kubectl get configmap cm-test-file -o yaml
apiVersion: v1
data:
  test.properties: |+
    key01:value01
    key02:value02
    conf01: value03
 
kind: ConfigMap
metadata:
  creationTimestamp: "2022-08-07T00:23:58Z"
  name: cm-test-file
  namespace: default
  resourceVersion: "45341"
  uid: 818a39b4-cbaf-4643-9541-1119b4d61982
[root@k8s-master k8s]# kubectl get cm cm-test-literal -o yaml
apiVersion: v1
data:
  key01: value01
  key02: value02
kind: ConfigMap
metadata:
  creationTimestamp: "2022-08-07T00:24:19Z"
  name: cm-test-literal
  namespace: default
  resourceVersion: "45372"
  uid: 051849e0-bf0a-4926-a549-2405dc4963cc

1.6 更新

1.6.1 edit

[root@k8s-master k8s]# kubectl edit cm cm-test01
configmap/cm-test01 edited

通过kubectl describe cm cm-test01查看更新是否生效。

[root@k8s-master k8s]# kubectl describe cm cm-test01
Name:         cm-test01
Namespace:    default
Labels:       
Annotations:  
 
Data
====
appconf02:
----
value02
appconf01:
----
value001
Events:  

1.6.2 apply

直接更新yaml文件里的值,通过 kubectl apply -f configmap-test01.yaml重新发布一遍进行更新。

1.7 删除

1.7.1 通过yaml文件方式删除

$ kubectl delete -f configmap-test01.yaml

1.7.2 直接删除资源

kubectl delet cm cm-test01

2 ConfigMap和Pod的使用

容器应用对ConfigMap的使用主要是两种:
1)通过环境变量获取ConfigMap的内容:spec.envspec.envFrom
2)通过卷volume挂载的方式将ConfigMap的内容挂载到容器内部的文件或目录:spec.volumes。

2.1 环境变量方式

2.1.1 sepc.env方式

2.1.1.1 创建pod

[root@k8s-master k8s]# vim pod-test01.yaml
 
apiVersion: v1
kind: Pod
metadata:
  name: cm-pod-test001
spec:
  containers:
  - name: cm-test
    image: tomcat:8
    command: [ "/bin/sh", "-c", "env | grep APP"]
    env:
    - name: APPCONF01 		# 定义环境变量的名称
      valueFrom:	  		# key “appconf01”的值获取
        configMapKeyRef:
          name: cm-test01	# 环境变量的值来自于configmap cm-test01
          key: appconf01	# configmap中的配置key为appconf01
    - name: APPCONF02		# 定义环境变量的名称
      valueFrom:			# key “appconf02”的值获取
        configMapKeyRef:
          name: cm-test01	# 环境变量的值来自于configmap cm-test01
          key: appconf02	# configmap中的配置key为appconf02
  restartPolicy: Never		# 重启策略:从不。

执行创建pod:

[root@k8s-master k8s]# kubectl create -f pod-test01.yaml
pod/cm-test001 created

2.1.1.2 查看pod

[root@k8s-master k8s]# kubectl get pods
[root@k8s /cm/test]#  kubectl get pods
NAME             READY     STATUS      RESTARTS   AGE
cm-pod-test001   0/1       Completed   0          1h

2.1.1.3 查看pod日志

[root@k8s-master k8s]# kubectl logs cm-pod-test001
APPCONF01=value01
APPCONF02=value02

说明容器内部的环境变量使用ConfigMap中进行读取的

2.1.2 spec.envFrom方式

2.1.2.1 创建pod

[root@k8s-master k8s]# vim pod-test02.yaml
 
piVersion: v1
kind: Pod
metadata:
  name: cm-pod-test002
spec:
  containers:
  - name: cm-test2
    image: tomcat:8
    command: [ "/bin/sh", "-c", "env"]
    envFrom:
    - configMapRef:
      name: cm-test01  # 根据ConfigMap cm-test01资源自动生成环境变量
  restartPolicy: Never

执行创建pod:

[root@k8s-master k8s]# kubectl create -f pod-test02.yaml

2.1.2.2 查看pod

[root@k8s-master k8s]#  kubectl get po
NAME             READY     STATUS      RESTARTS   AGE
cm-pod-test001   0/1       Completed   0          2h
cm-pod-test002   0/1       Completed   0          1h
注意:
环境变量的名称受限制:[a-zA-Z][a-zA-Z0-9_]*,不能以数字或非法字符开头。

2.2 卷挂载方式

2.2.1 指定items

[root@k8s-master k8s]#  vim pod-test03.yaml
apiVersion: v1
kind: Pod
metadata:
  name: cm-pod-test003
spec:
  containers:
  - name: cm-test3
    image: tomcat:8
    volumeMounts:
    - name: vm-01-1
      mountPath: /conf
  volumes:
  - name: vm-01-1
    configMap:
      name: cm-test-file
      items:
      - key: key-testproperties
        path: test.properties
  restartPolicy: Never

2.2.2 不指定items

[root@k8s-master k8s]#  vim pod-test04.yaml
apiVersion: v1
kind: Pod
metadata:
  name: cm-pod-test004
spec:
  containers:
  - name: cm-test4
    image: tomcat:8
    volumeMounts:
    - name: vm-02-2
      mountPath: /conf
  volumes:
  - name: vm-02-2
    configMap:
      name: cm-test-file
  restartPolicy: Never

进入容器中查看

[root@k8s-master k8s]# kubectl exec -it cm-pod-test004 -c cm-test4 -- bash

进入容器后,ls /conf查看是否有test.properties文件。

[root@k8s-master k8s]#  kubectl exec -it cm-pod-test004 -c cm-test4  -- bash
root@cm-pod-test004:/usr/local/tomcat# ls /conf
test.properties

2.3 补充

关于--from-file的方式的创建指定key和不指定key的区别
1)不指定key名

创建:

[root@k8s-master k8s]# kubectl create cm cm-test-file --from-file=test.properties

输出:

[root@k8s-master k8s]# kubectl get cm cm-test-file -o yaml

2)指定key
创建:

[root@k8s-master k8s]# kubectl create cm cm-test-file02 --from-file=tp=test.properties

输出:

[root@k8s-master k8s]# kubectl get cm cm-test-file -o yaml

若指定key的名称,configmap中将会使用指定名称;若不指定,则默认使用文件名为key。
 

你可能感兴趣的:(#,Kubernetes系列,kubernetes,容器,云原生)