k8s配置中心-configmap,Secret密码

k8s配置中心-configmap,Secret

​ 在生产环境中经常会遇到需要修改配置文件的情况,传统的修改方式不仅会影响到服务的正常运行,而且操作步骤也很繁琐。为了解决这个问题,kubernetes项目从1.2版本引入了ConfigMap功能,用于将应用的配置信息与程序的分离。这种方式不仅可以实现应用程序被的复用,而且还可以通过不同的配置实现更灵活的功能。在创建容器时,用户可以将应用程序打包为容器镜像后,通过环境变量或者外接挂载文件的方式进行配置注入。ConfigMap && Secret 是K8S中的针对应用的配置中心,它有效的解决了应用挂载的问题,并且支持加密以及热更新等功能,可以说是一个k8s提供的一件非常好用的功能。

创建ConfigMap

# 创建名称空间
apiVersion: v1
kind: Namespace
metadata:
  name: sg-bs
  labels:
    app: sg-bs
---

# ConfigMap是名称空间级资源
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-configmap
  namespace: sg-bs
data: # 健 : 值
  level: debug

使用ConfigMap

## 使用挂载方式,将配置文件挂载到容器中
# 使用
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-config
spec:
  selector:
    matchLabels:
      app: nginx-config
  template:
    metadata:
      labels:
        app: nginx-config
    spec:
      containers:
        - name: nginx
          image: nginx
          volumeMounts: # 挂载
            - mountPath: /etc/nginx/conf.d  # 挂载路径
              name: nginx-config-configmap # 存储卷名字
    
      volumes:
        - name: nginx-config
          persistentVolumeClaim:
            claimName: nginx-config
        - name: nginx-config-configmap
          configMap:
            name: test-configmap # ConfigMap名字
            items:
              - key: level
                path: level # 最终路径为:/etc/nginx/conf.d/level

subPath参数

# configmap热更新
## 修改configmap中的文件,可以同步到所有的挂载此configmap的容器中(仅仅同步到容器中),但是如果使用subPath参数,则热更新失效。

## configMap挂载会直接覆盖原来的目录,如果不覆盖则需要使用subPath参数(subPath参数只能够针对文件,同时不支持热更新)


apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  default.conf: |
    server {
        listen       80;
        listen  [::]:80;
        server_name  _;
        location / {
            root   /usr/share/nginx/html;
            index  index.html index.php;
        }
        location ~ \.php$ {
            root           /usr/share/nginx/html;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME  /usr/share/nginx/html$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
  index.php: |
    <?php

    phpinfo();

    ?>
---
kind: Service
apiVersion: v1
metadata:
  name: nginx-config
spec:
  ports:
    - port: 80
      targetPort: 80
      nodePort: 30089
  selector:
    app: nginx-config
  type: NodePort
---
kind: Deployment
apiVersion: apps/v1
metadata:
  name: nginx-config
spec:
  selector:
    matchLabels:
      app: nginx-config
  template:
    metadata:
      labels:
        app: nginx-config
    spec:
      containers:
        - name: php
          image: alvinos/php:wordpress-v2
          volumeMounts:
            - mountPath: /usr/share/nginx/html
              name: nginx-config-configmap

        - name: nginx
          image: alvinos/nginx:wordpress-v2
          volumeMounts:
            - mountPath: /usr/share/nginx/html/index.php
              name: nginx-config-configmap
              subPath: index.php

            - mountPath: /etc/nginx/conf.d
              name: nginx-config-configmap
      volumes:
        - name: nginx-config-configmap
          configMap:
            name: nginx-config
            items:
              - key: index.php
                path: index.php

Secret

​ Secret解决了密码、token、密钥等敏感数据的配置问题,而不需要把这些敏感数据暴露到镜像或者Pod Spec中。Secret可以以Volume或者环境变量的方式使用。

Secret用来保存敏感数据,保存之前就必须将文件进行base64加密,挂载到pod中,自动解密。
默认使用Opaque类型。

Secret有四种类型:
Service Account :用来访问Kubernetes API,由Kubernetes自动创建,并且会自动挂载到Pod的/run/secrets/kubernetes.io/serviceaccount目录中;

Opaque :base64编码格式的Secret,用来存储密码、密钥等;

kubernetes.io/dockerconfigjson :用来存储私有docker registry的认证信息

tls类型:访问证书

官方文档

https://kubernetes.io/zh/docs/concepts/configuration/secret/

编写secret清单

kind: Secret
apiVersion: v1
metadata:
  name: discuz-mysql-secrer
  namespace: discuz-mysql
type: Opaque
data:
  passwd: MTIzNDU2Cg==  # echo "123456"| base64

使用secret

​ Secret 可以作为数据卷被挂载,或作为环境变量 暴露出来以供 Pod 中的容器使用。它们也可以被系统的其他部分使用,而不直接暴露在 Pod 内。 例如,它们可以保存凭据,系统的其他部分将用它来代表你与外部系统进行交互。

apiVersion: batch/v1
kind: Job
metadata:
  name: test-job
spec:
  template:
    metadata:
      labels:
        app: job
        deploy: discuz
    spec:
      restartPolicy: OnFailure # 重启策略
      containers:
        - name: mysql
          image: mysql:5.7
          command:
            - "/bin/bash"
            - "-c"
            - "/usr/bin/mysqldump -uroot -p`cat /mnt/passwd`"
          volumeMounts: # 挂载
            - mountPath: /opt
              name: job-sql-dump
            - mountPath: /mnt # 密码挂载目录
              name: passwd
      volumes:
        - name: password-secret
          secret:
            secretName: discuz-mysql-secrer
            items:
              - key: passwd
                path: passwd
posted @ 2021-12-09 17:01  Jeff的技术栈  阅读(967)  评论(0编辑  收藏  举报
回顶部