K8s配置管理configmap和secret

k8s中的secret和configmap是为了让POD和配置解耦,使得从集群外部可以想容器内部注入配置信息、环境变量等功能
ConfigMap扮演了K8S集群中配置中心的角色,ConfigMap定义了Pod的配置信息,可以以存储卷的形式挂载至Pod中的应用程序配置文件目录,从ConfigMap中读取配置信息
ConfigMap是明文保存的,如果用来保存数据库账号密码这类信息可以使用通过secret来保存,secret的功能和ConfigMap一样,不过secret是通过Base64的编码机制保存配置信息
可以通过命令行的方式来创建configmap,如下

kubectl create configmap nginx-config --from-literal=nginx_server_port=8080 --from-literal=nginx_server_name=www.sulao.cn
configmap/my-config created

亦可以通过调用配置文件的方式来创建configmap,yaml文件内容如下

vi vhost.conf
server {
  listen 8080;
  server_name www.sulao.cn;
  root /data/www;
}
kubectl create configmap nginx-config --from-file=./vhost.conf
vi nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
spec:
  containers: 
  - name: nginx
    image: nginx
    volumeMounts :              #定义容器使用存储卷挂载
    - name: config              #使用存储卷的名称
      mountPath: /etc/nginx/conf.d/
volumes:
- name: config                  #存储卷名称
  configMap:                    #存储卷类型:这里为configmap而不是nfs其他的文件系统,可以指定configmap资源为存储卷
    name: nginx-config          #configmap名称,这里为我们刚才创建的cm名称
    items :                     #使用cm中的key
    - key: vhost.conf           #key名称
      path: sulao.cn.conf   #表示映射为文件时文件名是什么

--from-file=./vhost.conf #利用文件来传递参数,没有给key名称默认为文件名称为key,这里所以文件名就是vhost.conf,文件内容为vhost.conf文件内的内容,当然也可以指定文件名,那么我们创建configmap是需要这样命名

kubectl create configmap nginx-config --from-file=vhost./vhost.conf

通过yaml文件创建configmap

vi nginx-cm.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx_server_port: "8080"
  nginx_server_name: "www.sulao.cn"
kubectl apply -f nginx-cm.yaml

configma配置没有spec,同时他的data内配置信息需要使用引号包裹,不然会报错
上述configmap配置好了,然后我们需要了解如何使用上述的configmap的配置,我们先来看看configmap
kubectl get cm
cm为configmap的简写,可通过kubectl api-resources命令查看K8S所有资源和对应简写
最开始我们也讲过configmap定义了配置信息,可以通过注入和挂在两种方式来提供配置信息,接下来我们分别用不同的例子来呈现
环境注入的方式
1)通过valueFrom导入configmap

vi nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    name: nginx-pod
spec:
  containers:
  - name: nginx
    image: nginx
    env:
    - name: NGINX_SERVER_PORT
      valueFrom:
        configMapKeyRef:
          name: nginx-config
          key: nginx_server_port
    - name: NGINX_SERVER_NAME
      valueFrom:
        configMapKeyRef:
          name: nginx-config
          key: nginx_server_name
kubectl apply -f nginx-pod.yaml


spec.containers.env中的name就是要传入容器中的变量,configMapKeyRef就是指向configmap资源并使用name叫nginx-config的key配置

NGINX_SERVER_PORT=nginx_server_port=8080
NGINX_SERVER_NAME=nginx_server_name=www.sulao.cn

2)通过envFrom导入configmap

vi nginx-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    name: nginx-pod
    config: configmap
spec:
  containers:
  - name: nginx
    image: nginx
    envFrom:
      - configMapRef:
          name: nginx-config
kubectl apply -f nginx-pod.yaml

3)通过数据卷的方式挂在configmap

vi configmap-volume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: configmap-volume
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: configmap-volume           #指定数据卷名
          mountPath: /etc/config           #表示把conifg-volume数据卷挂载到容器的/etc/config目录下
  volumes:
    - name: configmap-volume               #给数据卷起名
      configMap:                           #数据卷挂载configmap
        name: nginx-config                 #挂载的configmap名字
kubectl apply -f configmap-volume.yaml

可以登录容器内查看/etc/config是否挂载成功,这样挂在的configmap可以进行热更新,但是会有延迟。
secret用起来和configmap基本一致,下面直接修改上面configmap例子,一下就看懂了
当 Secret 配置文件中未作显式设定时,默认的 Secret 类型是 Opaque。 当你使用 kubectl 来创建一个 Secret 时,你会使用 generic 子命令来标明 要创建的是一个 Opaque 类型 Secret。
Opaque类型的数据一个map类型,要求value是base64编码格式
通过yaml文件创建secret

vi secret-test.yaml
apiVersion: v1
kind: Secret
metadata:
  name: account-config
type: Opaque
data:
  username: c3VwZXJhZG1pbgo=
  password: cGFzc3BwcHAK
kubectl apply -f secret-test.yaml

作为容器环境变量这样使用

vi account-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: nginx-pod
  labels:
    name: nginx-pod
spec:
  containers:
  - name: nginx
    image: nginx
    env:
    - name: ACCOUNT_USERNAME
      valueFrom:
        secretKeyRef:                   #主要是这里要从configMapKeyRef修改为secretKeyRef
          name: account-config
          key: username
    - name: ACCOUNT_PASSWORD
      valueFrom:
        configMapKeyRef:                #主要是这里要从configMapKeyRef修改为secretKeyRef
          name: account-config
          key: password
kubectl apply -f account-pod.yaml

通过挂载的方式如下

vi account-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: secret-volume
spec:
  containers:
    - name: nginx
      image: nginx
      volumeMounts:
        - name: secret-volume           #指定数据卷名
          mountPath: /etc/config        #表示把conifg-volume数据卷挂载到容器的/etc/config目录下
          readOnly: true
  volumes:
    - name: secret-volume               #给数据卷起名
      secret:                           #数据卷挂载configmap
        secretName: account-config      #挂载的configmap名字
kubectl apply -f account-pod.yaml

用起来基本和configmap没有差别

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://sulao.cn/post/828.html

我要评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。