找回密码
 立即注册
首页 业界区 业界 secret和configmap

secret和configmap

晦险忿 2025-8-2 14:58:37
一、secret

1、什么是secret?


  • 这个类型主要就是存储密码或者一些隐私一些内容
  • 主要就是保存这些东西的
  • 值都是base64加密的
  • 都是基于命名空间的
2、创建secret方式

1、命令行创建
  1. [root@k-master 8-2]# kubectl create secret --help
  2. Create a secret using specified subcommand.
  3. Available Commands:
  4.   docker-registry   Create a secret for use with a Docker registry
  5.   generic           Create a secret from a local file, directory, or literal value # 常用的
  6.   tls               Create a TLS secret
  7. Usage:
  8.   kubectl create secret [flags] [options]
  9. Use "kubectl <command> --help" for more information about a given command.
  10. Use "kubectl options" for a list of global command-line options (applies to all commands).
  11. [root@k-master 8-2]# kubectl create secret generic --help
  12. # 这些有的是案例的
  13. [root@k-master 8-2]# kubectl create secret generic --help | grep from
  14.   kubectl create secret generic my-secret --from-file=path/to/bar
  15.   kubectl create secret generic my-secret --from-file=ssh-privatekey=path/to/id_rsa --from-file=ssh-publickey=path/to/id_rsa.pub
  16.   kubectl create secret generic my-secret --from-literal=key1=supersecret --from-literal=key2=topsecret
  17.   kubectl create secret generic my-secret --from-file=ssh-privatekey=path/to/id_rsa --from-literal=passphrase=topsecret
  18.   # Create a new secret named my-secret from env files
  19.   kubectl create secret generic my-secret --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env
  20.     --from-env-file=[]:  # 通过环境变量创建
  21.     --from-file=[]:  # 通过文件创建
  22.     --from-literal=[]:  # 键值对的方式
  23.   kubectl create secret generic NAME [--type=string] [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run=server|client|none] [options]
  24. [root@k-master 8-2]# kubectl create secret generic mysec1 --from-literal=name1=aaa1 --from-literal=name2=bbb2
  25. secret/mysec1 created
  26. # 上面创建了2个数据
  27. [root@k-master 8-2]# kubectl get secrets
  28. NAME     TYPE     DATA   AGE
  29. mysec1   Opaque   2      3s
  30. [root@k-master 8-2]# kubectl describe secrets mysec1
  31. Name:         mysec1
  32. Namespace:    default
  33. Labels:       <none>
  34. Annotations:  <none>
  35. Type:  Opaque  # 基于base64密钥进行解密和加密
  36. Data
  37. ====
  38. name2:  4 bytes  # 值默认是加密的
  39. name1:  4 bytes
复制代码

  • 显示加密后的密钥
  1. [root@k-master 8-2]# kubectl get secrets mysec1 -o yaml
  2. apiVersion: v1
  3. data:
  4.   name1: YWFhMQ==
  5.   name2: YmJiMg==
  6. kind: Secret
  7. metadata:
  8.   creationTimestamp: "2025-08-02T07:00:20Z"
  9.   name: mysec1
  10.   namespace: default
  11.   resourceVersion: "150234"
  12.   uid: b89398bb-a512-41bf-9032-31d6e15e2fa0
  13. type: Opaque
  14. # 使用base64进行解密
  15. [root@k-master 8-2]# echo -n  YWFhMQ== | base64 -d
  16. aaa1[root@k-master 8-2]#
复制代码
2、文件方式


  • 将内容保存到文件中去
  • 文件名相当于是键,内容是值
  1. [root@k-master 8-2]# echo redhat > name3
  2. [root@k-master 8-2]# ls
  3. name3
  4. [root@k-master 8-2]# cat name3
  5. redhat
  6. [root@k-master 8-2]# kubectl create secret generic mysec2 --from-file=name3
复制代码
3、环境变量方式
  1. cat 1.txt
  2. name1=aaa1
  3. [root@k-master 8-2]# kubectl create secret generic mysec3 --from-env-file=1.txt
  4. secret/mysec3 created
复制代码
4、通过yaml文件生成
  1. # 通过查看命令帮助
  2. [root@k-master ~]# kubectl explain secret --recursive | less
  3. [root@k-master 8-2]# cat secret1.yaml
  4. apiVersion: v1
  5. kind: Secret
  6. metadata:
  7.   name: mysec4
  8. data:
  9.   name1: cXFxCg==
复制代码
3、使用secret方式

1、卷使用secret


  • 将这个secret挂载到容器里面去即可
  • 以卷的方式来挂载这个secret和confgimap的话,适用场景就是一些服务的配置文件
  • 在pod里面存在的方式,文件名就是键
  1. [root@k-master 8-2]# cat pod1.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   creationTimestamp: null
  6.   labels:
  7.     run: pod1
  8.   name: pod1
  9. spec:
  10.   containers:
  11.   - image: nginx
  12.     imagePullPolicy: IfNotPresent
  13.     name: pod1
  14.     volumeMounts: # 挂载到这个/mnt目录下
  15.     - name: v1
  16.       mountPath: /mnt
  17.   volumes:  # 使用secret作为卷
  18.   - name: v1
  19.     secret:
  20.       secretName: mysec1
  21. # 将这个密钥写到这个文件里面去了
  22. # 文件名就是这个键,内容就是值
  23. # 在pod里面的存在的方式文件名就是键
  24. [root@k-master 8-2]# kubectl exec -ti pod1  -- ls /mnt
  25. name1  name2
复制代码
  1. # nginx的页面来使用卷挂载网站页面
复制代码
2、变量方式使用secret


  • mysql运行时,需要环境变量,因此上面的文件不方便
  1. [root@k-master 8-2]# cat pod1.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   creationTimestamp: null
  6.   labels:
  7.     run: pod1
  8.   name: pod1
  9. spec:
  10.   containers:
  11.   - image: nginx
  12.     imagePullPolicy: IfNotPresent
  13.     name: pod1
  14.     env:
  15.     - name: env1  # 环境变量名
  16.       valueFrom: # 这个值来自于这个secret
  17.         secretKeyRef:
  18.           name: mysec1
  19.           key: name1  # 使用的键是这个name1,env1的值使用就是name1对应的值
  20. # 进入容器使用环境变量
  21. [root@k-master 8-2]# kubectl exec -ti pod1 -- /bin/bash
  22. root@pod1:/# echo $env1
  23. aaa1
复制代码

  • 创建一个mysql容器
  1. [root@k-master 8-2]# cat mysql.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   creationTimestamp: null
  6.   labels:
  7.     run: mysql1
  8.   name: mysql1
  9. spec:
  10.   containers:
  11.   - image: mysql
  12.     imagePullPolicy: IfNotPresent
  13.     name: mysql1
  14.     ports:
  15.     - containerPort: 3306
  16.     env:
  17.     - name: MYSQLROOT_PASSWORD # 环境变量键
  18.       valueFrom:
  19.         secretKeyRef:
  20.           name: mysec1
  21.           key: name1  # 使用的值就是name1键对应的值
  22.     resources: {}
  23.   dnsPolicy: ClusterFirst
  24.   restartPolicy: Always
  25. status: {}
复制代码

  • 远程登录mysql,密码是aaa1
  1. [root@k-master 8-2]# kubectl get pod -o wide
  2. NAME     READY   STATUS    RESTARTS   AGE   IP              NODE      NOMINATED NODE   READINESS GATES
  3. mysql1   1/1     Running   0          84s   10.244.82.180   k-node1   <none>           <none>
  4. pod1     1/1     Running   0          16m   10.244.82.172   k-node1   <none>           <none>
  5. # 语法,-u 可以要空格可以不需要空格
  6. # 但是呢,这个密码的话不能有空格
  7. [root@k-master 8-2]# mysql -uroot -paaa1 -h 10.244.82.180
  8. Welcome to the MariaDB monitor.  Commands end with ; or \g.
  9. Your MySQL connection id is 9
  10. Server version: 9.4.0 MySQL Community Server - GPL
  11. Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
  12. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  13. MySQL [(none)]>
复制代码
二、configmap

1、什么是configmap


  • 主要就是保存配置文件用的
  • 明文的方式保存
  • 跟上面secret的操作一模一样
2、创建configmap方式

1、命令行创建
  1. # 查看命令行帮助
  2. [root@k-master 8-2]# kubectl create configmap --help | grep -i from
  3.   kubectl create configmap my-config --from-file=path/to/bar
  4.   kubectl create configmap my-config --from-file=key1=/path/to/bar/file1.txt --from-file=key2=/path/to/bar/file2.txt
  5.   kubectl create configmap my-config --from-literal=key1=config1 --from-literal=key2=config2
  6.   # Create a new config map named my-config from the key=value pairs in the file
  7.   kubectl create configmap my-config --from-file=path/to/bar
  8.   # Create a new config map named my-config from an env file
  9.   kubectl create configmap my-config --from-env-file=path/to/foo.env --from-env-file=path/to/bar.env
  10.     --from-env-file=[]:
  11.     --from-file=[]:
  12.     --from-literal=[]:
  13.   kubectl create configmap NAME [--from-file=[key=]source] [--from-literal=key1=value1] [--dry-run=server|client|none] [options]
  14. [root@k-master 8-2]# kubectl create configmap cm1 --from-literal=name1=qqq
  15. configmap/cm1 created
  16. [root@k-master 8-2]# kubc^C
  17. [root@k-master 8-2]# kubectl get cm
  18. NAME               DATA   AGE
  19. cm1                1      4s
  20. kube-root-ca.crt   1      13d
  21. [root@k-master 8-2]# kubectl describe cm cm1
  22. Name:         cm1
  23. Namespace:    default
  24. Labels:       <none>
  25. Annotations:  <none>
  26. Data
  27. ====
  28. name1: # 键
  29. ----
  30. qqq  # 文件的内容
  31. BinaryData
  32. ====
  33. Events:  <none>
复制代码
2、文件创建


  • 文件名相当于是键,值是文件里面的内容
  1. [root@k-master 8-2]# kubectl create configmap cm2  --from-file=name4
  2. configmap/cm2 created
  3. [root@k-master 8-2]# kubectl describe cm cm2
  4. Name:         cm2
  5. Namespace:    default
  6. Labels:       <none>
  7. Annotations:  <none>
  8. Data
  9. ====
  10. name4:  # 键
  11. ----
  12. qqq  # 文件的内容
  13. BinaryData
  14. ====
  15. Events:  <none>
复制代码
3、环境变量和yaml创建
  1. [root@k-master 8-2]# kubectl create configmap cm3 --from-env-file=2.txt
  2. configmap/cm3 created
  3. [root@k-master 8-2]# kubectl get cmck^C
  4. [root@k-master 8-2]# kubectl describe cm cm3
  5. Name:         cm3
  6. Namespace:    default
  7. Labels:       <none>
  8. Annotations:  <none>
  9. Data
  10. ====
  11. name1:
  12. ----
  13. qqq
  14. BinaryData
  15. ====
  16. Events:  <none>
复制代码
  1. [root@k-master 8-2]# kubectl apply -f cm4.yaml
  2. configmap/cm4 created
  3. [root@k-master 8-2]# kubectl describe configmaps cm4
  4. Name:         cm4
  5. Namespace:    default
  6. Labels:       <none>
  7. Annotations:  <none>
  8. Data
  9. ====
  10. name1:
  11. ----
  12. qqq
  13. BinaryData
  14. ====
  15. Events:  <none>
复制代码
3、使用configmap方式

1、通过卷的方式使用
  1. [root@k-master 8-2]# cat pod2.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   creationTimestamp: null
  6.   labels:
  7.     run: pod2
  8.   name: pod2
  9. spec:
  10.   containers:
  11.   - image: nginx
  12.     imagePullPolicy: IfNotPresent
  13.     name: pod2
  14.     volumeMounts:
  15.     - name: c1
  16.       mountPath: /mnt
  17.     resources: {}
  18.   volumes:
  19.   - name: c1
  20.     configMap:
  21.       name: cm4
  22.   dnsPolicy: ClusterFirst
  23.   restartPolicy: Always
  24. status: {}
  25. # 卷挂载的话,以键是文件的存在形式存在的
  26. [root@k-master 8-2]# kubectl exec -ti pod2  -- cat  /mnt/name1
  27. qqq[root@k-master 8-2]#
复制代码
2、通过变量的方式使用
  1. [root@k-master 8-2]# cat pod2.yaml
  2. apiVersion: v1
  3. kind: Pod
  4. metadata:
  5.   creationTimestamp: null
  6.   labels:
  7.     run: pod2
  8.   name: pod2
  9. spec:
  10.   containers:
  11.   - image: nginx
  12.     imagePullPolicy: IfNotPresent
  13.     name: pod2
  14.     resources: {}
  15.     env:
  16.     - name: env1 # 环境变量键
  17.       valueFrom:
  18.         configMapKeyRef: # 使用的值是configmap
  19.           name: cm4
  20.           key: name1 # 值是name1对应的值
  21.   dnsPolicy: ClusterFirst
  22.   restartPolicy: Always
  23. status: {}
  24. [root@k-master 8-2]# kubectl exec -ti pod2 -- /bin/bash
  25. root@pod2:/# echo $env1
  26. qqq
  27. root@pod2:/#
复制代码
三、实验

1、configmap配置nginx网页目录


  • data字段数据写法
[code][/code]
来源:豆瓜网用户自行投稿发布,如果侵权,请联系站长删除

相关推荐

您需要登录后才可以回帖 登录 | 立即注册