0
点赞
收藏
分享

微信扫一扫

8.应用配置:特殊卷ConfigMap和Secret

特殊卷

ConfigMap和Secret是Kubernetes系统上两种特殊类型的存储卷
    ◼ ConfigMap用于为容器中的应用提供配置数据以定制程序的行为,而敏感
的配置信息,例如密钥、证书等则通常由Secret来配置
    ◼ ConfigMap和Secret将相应的配置信息保存于资源对象中,而后在Pod对象
上支持以存储卷的形式将其挂载并加载相关的配置,从而降低了配置与镜
像文件的耦合关系,提高了镜像复用能力
    ◼ Kubernetes借助于ConfigMap对象实现了将配置文件从容器镜像中解耦,从
而增强了工作负载的可移植性,使其配置更易于更改和管理,并避免了将
配置数据硬编码到Pod配置清单中
此二者都属于名称空间级别,只能被同一名称空间中的Pod引用
ConfigMap和Secret资源都是数据承载类的组件,是Kubernetes API的标准资源类型,是一等公民
    ◼ 主要负责提供key-value格式的数据项,其值支持
         ◆单行字符串:常用于保存环境变量值,或者命令行参数等
         ◆多行字串:常用于保存配置文件的内容
         apiVersion: v1
         kind: ConfigMap
         metadata:
          name: myapp-confs
         data:
          PORT: "8080"
          myserver-status.cfg: |
           location /nginx-status {
             stub_status on;
             access_log off;
           }

    ◼ 资源规范中不使用spec字段,而是直接使用特定的字段嵌套定义key-value数据
         ◆ConfigMap支持使用data或binaryData字段嵌套一至多个键值数据项
         ◆Secret支持使用data或stringData(非base64编码的明文格式)字段嵌套一至多个键值数据项
    ◼ 从Kubernetes v1.19版本开始,ConfigMap和Secret支持使用immutable字段创建不可变实例

ConfigMap和Secret

卷插件:ConfigMap和Secret
资源类型:一等公民
名称空间级别的资源,只能用于配置同一名称空间中的pod中的容器化应用

创建ConfigMap对象

创建ConfigMap对象的方法有两种:
  ◼ 命令式命令
    字面量:kubectl create configmap NAME --from-litera=key1=value1
    从文件加载:kubectl create configmap NAME --from-file=[key=]/PATH/TO/DIR/FILE
    从目录加载:kubectl create configmap NAME --from-file=/PATH/TO/DIR
  ◼ 配置文件
    命令式:kubectl create -f
    声明式:kubectl apply -f

示例

字面量:kubectl create configmap nfs --from-litera=port="8080" --from-litera=host=10.0.0.1
从文件加载:kubectl create configmap nginx-cfg --from-file=nginx.conf/mysqlserver.conf --dry-run=client -o yaml > configmap-nginx-cfg.yaml
从目录加载:kubectl create configmap nginx-cfg --from-file=nginx-conf.d/ -o yaml > configmap-nginx-cfg.yaml

引用ConfigMap对象

◼环境变量赋值
  ◆引用ConfigMap对象上特定的key,以valueFrom赋值给Pod上指定的环境变量
  ◆在Pod上使用envFrom一次性导入ConfigMap对象上的所有key-value,key(也可以统一附加特定前缀)即为环境变量名,
value自动成为相应的变量值
◼ConfigMap卷
  ◆在Pod上将ConfigMap对象引用为存储卷,而后整体由容器mount至某个目录下
     ⚫ key转为文件名,value即为相应的文件内容
  ◆在Pod上定义configMap卷时,仅引用其中的部分key,而后由容器mount至目录下
  ◆在容器上仅mount configMap卷上指定的key

通过环境变量引用 
apiVersion: v1
kind: Pod
metadata:
 name: configmaps-env-demo
spec:
 containers:
 - image: ikubernetes/demoapp:v1.0
   name: demoapp
   env:
   - name: PORT                  #键的值赋给这个环境变量
    valueFrom:
     configMapKeyRef:
      name: demoapp-config        #cm的名字
      key: demoapp.port           #键的名字
      optional: false             #该键值是否可选
   - name: HOST
    valueFrom:
     configMapKeyRef:
      name: demoapp-config
      key: demoapp.host
      optional: true              #该键值是否可选,为true时如果cm或key不存在,不会导致失败

通过存储卷引用
apiVersion: v1
kind: Pod
metadata:
 name: configmaps-volume-demo
spec:
 containers:
 - image: nginx:alpine
  name: nginx-server
  volumeMounts:
  - name: ngxconfs                   #卷名
    mountPath: /etc/nginx/conf.d/    #挂载目录
    readOnly: true
volumes:
- name: ngxconfs                 #指定卷名
 configMap:
  name: nginx-config-files       #引用的configmap资源名
  optional: false

示例:

通过环境变量引用
[root@k8s-master02 Kubernetes_Advanced_Practical_2rd]#cd chapter6
[root@k8s-master02 chapter6]#vim configmaps-env-demo.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: demoapp-config
  namespace: default
data:
  demoapp.port: "8080"
  demoapp.host: 0.0.0.0
---
apiVersion: v1
kind: Pod
metadata:
  name: configmaps-env-demo
  namespace: default
spec:
  containers:
  - image: ikubernetes/demoapp:v1.0
    name: demoapp
    env:
    - name: PORT
      valueFrom:
        configMapKeyRef:
          name: demoapp-config
          key: demoapp.port
          optional: false
    - name: HOST
      valueFrom:
        configMapKeyRef:
          name: demoapp-config
          key: demoapp.host
          optional: true
 创建:        
[root@k8s-master02 chapter6]#kubectl apply -f configmaps-env-demo.yaml
configmap/demoapp-config created
pod/configmaps-env-demo created
查询已创建:
[root@k8s-master02 chapter6]#kubectl get cm
NAME               DATA   AGE
demoapp-config     2      46s

强制删除pod:
kubectl delete pods NAME --force --grace-period=0

创建cm卷:
[root@k8s-master02 chapter6]#kubectl create configmap nginx-config-files --from-file=nginx-conf.d/
通过存储卷引用:
[root@k8s-master02 chapter6]#vim configmaps-volume-demo.yaml
apiVersion: v1
kind: Pod
metadata:
  name: configmaps-volume-demo
  namespace: default
spec:
  containers:
  - image: nginx:alpine
    name: nginx-server
    volumeMounts:
    - name: ngxconfs
      mountPath: /etc/nginx/conf.d/
      readOnly: true
  volumes:
  - name: ngxconfs
    configMap:
      name: nginx-config-files
      optional: false
      
kubectl apply -f configmaps-volume-demo.yaml

进入交互式接口:
kubectl exec -it configmaps-volume-demo -- /bin/sh
cd /etc/nginx/conf.d
ls
syserver-gzip.cfg  syserver-status.cfd  syserver.conf

secret资源

secret命令三大类别:
docker-registry:docker镜像仓库服务上的docker认证信息
generic:一般类别,不便区分的类别
tls:专用于保存数字证书和匹配私钥的

资源类型:

创建Secret资源

支持类似于ConfigMap的创建方式,但Secret有类型子命令,而且不同类型在data或stringData字段中支
持嵌套使用的key亦会有所有同;
命令式命令
  ◼ generic
    ◆kubectl create secret generic NAME [--type=string] [--from-file=[key=]source] [--from-literal=key1=value1]
    ◆除了后面docker-registry和tls命令之外的其它类型,都可以使用该命令中的--type选项进行定义,但有些类型有key的特
定要求
  ◼ tls
    ◆kubectl create secret tls NAME --cert=path/to/cert/file --key=path/to/key/file
    ◆通常,其保存cert文件内容的key为tls.crt,而保存private key的key为tls.key
  ◼ docker-registry
    ◆kubectl create secret docker-registry NAME --docker-username=user --docker-password=password --docker-email=email [--
docker-server=string] [--from-file=[key=]source]
    ◆通常,从已有的json格式的文件加载生成的就是dockerconfigjson类型,命令行直接量生成的也是该类型

资源示例

generic

创建:
[root@k8s-master02 chapter6]#kubectl create secret generic mysql-secret --from-literal=root.pass=magedu.com --from-literal=db.name=wpdb --from-literal=db.user=wpuser --from-literal=db.pass=magedu123 -o yaml > secret-mysql.yaml
[root@k8s-master02 chapter6]#kubectl apply -f secret-mysql.yaml
secret/mysql-secret created
查看:
[root@k8s-master02 chapter6]#kubectl get secret
NAME           TYPE     DATA   AGE
mysql-secret   Opaque   4      23s
查看生成的yaml文件,密码是加密的:
[root@k8s-master02 chapter6]#kubectl get secret -o yaml
apiVersion: v1
items:
- apiVersion: v1
  data:
    db.name: d3BkYg==
    db.pass: bWFnZWR1MTIz
    db.user: d3B1c2Vy
    root.pass: bWFnZWR1LmNvbQ==
  kind: Secret
  metadata:
    creationTimestamp: "2022-11-13T10:40:17Z"
    name: mysql-secret
    namespace: default
    resourceVersion: "415590"
    uid: 24f53317-a4c8-4dd4-a5e1-b6793f20f163
  type: Opaque
kind: List
metadata:
  resourceVersion: ""
使用base64解码:
[root@k8s-master02 chapter6]#echo bWFnZWR1MTIz |base64 -d
magedu123

基于环境变量引用secret:
[root@k8s-master02 chapter6]#vim secrets-env-demo.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: secrets-env-demo
  namespace: default
spec:
  containers:
  - name: mariadb
    image: mariadb
    imagePullPolicy: IfNotPresent
    env:
    - name: MYSQL_ROOT_PASSWORD
      valueFrom:
        secretKeyRef:
          name: mysql-root-authn
          key: password

tls

基于证书和私钥文件创建文件yaml配置文件:
[root@k8s-master02 chapter6]#ls certs.d/
nginx.crt  nginx.key

[root@k8s-master02 chapter6]#kubectl create secret tls nginx-certs --cert=certs.d/nginx.crt --key=certs.d/nginx.key --dry-run=client -o yaml > secret-nginx-certs.yaml
[root@k8s-master02 chapter6]#vim secret-nginx-certs.yaml
kind: Secret
metadata:
  creationTimestamp: null
  name: nginx-ssl-secret      #引用哪个名字,这里就写哪个名字
type: kubernetes.io/tls


创建secret:
[root@k8s-master02 chapter6]#kubectl apply -f secret-nginx-certs.yaml 
secret/nginx-ssl-secret created

创建一个configmap:
[root@k8s-master02 chapter6]#kubectl create configmap nginx-sslvhosts-confs --from-file=nginx-ssl-conf.d
configmap/nginx-sslvhosts-confs created

配置pod实例文件:
# Maintainer: MageEdu <mage@magedu.com>
# URL: http://www.magedu.com
---
apiVersion: v1
kind: Pod
metadata:
  name: secrets-volume-demo
  namespace: default
spec:
  containers:
  - image: nginx:alpine
    name: ngxserver
    volumeMounts:
    - name: nginxcerts
      mountPath: /etc/nginx/certs/
      readOnly: true
    - name: nginxconfs
      mountPath: /etc/nginx/conf.d/
      readOnly: true
  volumes:
  - name: nginxcerts
    secret:
      secretName: nginx-ssl-secret   #引用证书和私钥
  - name: nginxconfs
    configMap:
      name: nginx-sslvhosts-confs    #引用配置文件
      optional: false

创建pod:
[root@k8s-master02 chapter6]#kubectl apply -f secrets-volume-demo.yaml 
pod/secrets-volume-demo created
[root@k8s-master02 chapter6]#kubectl get pods
NAME                         READY   STATUS        RESTARTS       AGE
secrets-volume-demo          0/1     Pending       0              9s

查询证书等详细信息:
openssl s_client -connect  IP:端口

image pull secret

docker需要下载镜像时,有时候是从私有仓库下载,
需要先保存镜像仓库的地址和密码为一个secret,
然后在pod中引用这个secret
apiVersion: v1
kind: Pod
metadata:
 name: demoapp
 namespace: default
spec:
 containers:
  - name: demoapp
   image: ikubernetes/demoapp:v1.0
imagePullSecrets:
  - name: ikuberneteskey

举报

相关推荐

0 条评论