Kubernetes(k8s) 配置管理

目录

 一、Secret

1、概述

2、创建及使用

2.1、用kubectl create secret命令创建Secret

2.2、YAML文件创建

3、变量引用

4、文件挂载

二、configmap

1、概述

2、configmap作用

3、configmap特点

4、创建方法

4.1、命令行直接创建

 4.2、通过文件/目录创建

​编辑  4.3、通过目录创建

4.4、YAML创建

新创一个configmap用于测试

4.5、变量引入

4.6、文件挂载


 一、Secret

1、概述

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

secret 可选参数有三种:

  1. generic: 通用类型,通常用于存储密码数据。
  2. tls:此类型仅用于存储私钥和证书。
  3. docker-registry: 若要保存 docker 仓库的认证信息的话,就必须使用此种类型来创建。

Secret 类型:

  1. Service Account:用于被 serviceaccount 引用。serviceaccout 创建时 Kubernetes 会默认创建对应的 secret。Pod 如果使用了 serviceaccount,对应的 secret 会自动挂载到 Pod 的/run/secrets/kubernetes.io/serviceaccount 目录中。
  2. Opaque:base64 编码格式的 Secret,用来存储密码、秘钥等。可以通过 base64 --decode 解码获得原始数据,因此安全性弱。
  3. kubernetes.io/dockerconfigjson:用来存储私有 docker registry 的认证信息。

Pod 需要先引用才能使用某个 secret,Pod 有 3 种方式来使用 secret:

  1. 作为挂载到一个或多个容器上的卷 中的文件。
  2. 作为容器的环境变量。
  3. 由 kubelet 在为 Pod 拉取镜像时使用。

2、创建及使用

2.1、用kubectl create secret命令创建Secret

mkdir -p /opt/demo
cd /opt/demo
echo -n 'admin' > ./username.txt
echo -n '1f2d1e2e67df' > ./password.txt
 
kubectl create secret generic db-user-pass --from-file=./username.txt --from-file=./password.txt
 
kubectl get secret
 
kubectl describe secret db-user-pass

2.2、YAML文件创建

通过手动加密,基于base64加密

[root@master demo]# echo -n "admin" | base64
YWRtaW4=
[root@master demo]# echo -n "12345678" |base64
MTIzNDU2Nzg=

创建YAML

apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  username: YWRtaW4=
  password: MTIzNDU2Nzg=

kubectl apply -f secret.yaml 
kubectl get secret
kubectl describe secret mysecret

3、变量引用

apiVersion: v1
kind: Pod
metadata:
  name: secretpod
spec:
  containers:
  - name: busybox
    image: busybox
    command: [ "/bin/sh","-c","echo $(SECRET_USERNAME) $(SECRET_PASSWORD)" ]
    env:
      - name: SECRET_USERNAME
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: username
      - name: SECRET_PASSWORD
        valueFrom:
          secretKeyRef:
            name: mysecret
            key: password
  restartPolicy: Never
 

kubectl apply -f secret-pod.yaml 
kubectl get pod
kubectl logs secretpod

4、文件挂载

将 Secret 挂载到 Volume 中

vim pod_secret_volume.yaml
apiVersion: v1
kind: Pod
metadata:
  name: secret-volume-pod
spec:
  containers:
  - name: nginx
    image: nginx
    volumeMounts:
    - name: secret-volume
      mountPath: "/etc/secret"
      readOnly: true
  volumes:
  - name: secret-volume
    secret:
      secretName: mysecret

 进入pod可以看到/etc/secret下有password和username两个文件,查看内容和我们创建的secret内容吻合。

二、configmap

1、概述

Configmap 是 k8s 中的资源对象,用于保存非机密性的配置的,数据可以用 key/value 键值对的
形式保存,也可通过文件的形式保存。

2、configmap作用

我们在部署服务的时候,每个服务都有自己的配置文件,

  1. 如果一台服务器上部署多个服务:nginx、tomcat、apache等,那么这些配置都存在这个节点上
  2. 假如一台服务器不能满足线上高并发的要求,需要对服务器扩容,扩容之后的服务器还是需要部署多个服务:nginx、tomcat、apache,新增加的服务器上还是要管理这些服务的配置
  3. 如果有一个服务出现问题,需要修改配置文件,每台物理节点上的配置都需要修改,这种方式肯定满足不了线上大批量的配置变更要求
  4. 所以,k8s 中引入了 Configmap资源对象,可以当成 volume 挂载到 pod 中,实现统一的配置管理。

3、configmap特点

  1. Configmap 是 k8s 中的资源, 相当于配置文件,可以有一个或者多个 Configmap;
  2. Configmap 可以做成 Volume,k8s pod 启动之后,通过 volume 形式映射到容器内部指定目录上;
  3. 容器中应用程序按照原有方式读取容器特定目录上的配置文件;
  4. 在容器看来,配置文件就像是打包在容器内部特定目录,整个过程对应用没有任何侵入。

4、创建方法

4.1、命令行直接创建

直接在命令行中指定 configmap 参数创建,通过**–from-literal** 指定参数

#创建
kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=nginx
#查看configmap
kubectl describe configmap nginx-config

 4.2、通过文件/目录创建

 通过指定文件创建一个 configmap,–from-file=<文件/目录>

vim nginx.conf
 
server {
 server_name www.nginx.com;
 listen 80;
 root /home/nginx/www/
}

 通过指定文件创建

 kubectl create configmap www-nginx --from-file=www=/opt/config/nginx.conf
 kubectl describe configmap www-nginx

  4.3、通过目录创建

 当某些服务存在多个配置文件时,放在同一目录下,我们可以指定目录,进行创建

kubectl create configmap www-nginx2 --from-file=/opt/config/
kubectl describe configmap www-nginx2

4.4、YAML创建

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-conf
  labels:
    app: nginx-conf
data:
  nginx.conf: |
    server {
     server_name www.nginx.com;
     listen 80;
     root /home/nginx/www/
    }

新创一个configmap用于测试

apiVersion: v1
kind: ConfigMap
metadata:
  name: test
  labels:
    app: test
data:
  li: "liy"
  yo: "hello world"

4.5、变量引入

创建一个pod,引用configmap

apiVersion: v1
kind: Pod
metadata:
  name: mypod
spec:
  containers:
    - name: busybox
      image: busybox
      command: [ "/bin/sh", "-c", "echo $(LEVEL) $(TYPE)" ]
      env:
        - name: LEVEL
          valueFrom:
            configMapKeyRef:
              name: test
              key: xy
        - name: TYPE
          valueFrom:
            configMapKeyRef:
              name: test
              key: hi
  restartPolicy: Never                         

4.6、文件挂载

apiVersion: v1
kind: Pod
metadata:
  name: mypod1
spec:
  containers:
    - name: busybox
      image: busybox
      command: [ "/bin/sh","-c","cat /etc/config/yo" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        name: test
  restartPolicy: Never

猜你喜欢

转载自blog.csdn.net/weixin_56270746/article/details/126248828