K8S中数据存储之配置存储

配置存储

  • 在Kubernetes中,ConfigMapSecret是两种核心资源,用于存储和管理应用程序的配置数据和敏感信息。理解它们的功能和最佳实践对于提高Kubernetes应用程序的安全性和配置管理的效率至关重要。

ConfigMap

  • ConfigMap是一种API对象,允许你存储非敏感配置数据,如环境变量、数据库URL等。它以键值对的形式存储数据,便于应用程序访问必要的配置。ConfigMap可以直接挂载到容器中或作为环境变量注入到容器中,从而使得应用程序能够访问存储的配置数据,而无需修改应用程序代码。
bash 复制代码
[root@k8s-master ~]#  vim configmap.yaml
[root@k8s-master ~]# kubectl apply -f configmap.yaml 
configmap/configmap created
[root@k8s-master ~]#  kubectl get configmaps configmap -n test
NAME        DATA   AGE
configmap   1      8s
[root@k8s-master ~]# vim pod-configmap.yaml
[root@k8s-master ~]# kubectl apply -f pod-configmap.yaml 
pod/pod-configmap created
[root@k8s-master ~]# kubectl get pod -n test -o wide
NAME            READY   STATUS              RESTARTS   AGE   IP               NODE        NOMINATED NODE   READINESS GATES
pod-configmap   0/1     ContainerCreating   0          6s    <none>           k8s-node1   <none>           <none>
pod1            1/1     Running             0          22m   10.244.169.129   k8s-node2   <none>           <none>
pod2            1/1     Running             0          22m   10.244.36.74     k8s-node1   <none>           <none>
[root@k8s-master ~]# kubectl get pod -n test -w
NAME            READY   STATUS              RESTARTS   AGE
pod-configmap   0/1     ContainerCreating   0          11s
pod1            1/1     Running             0          22m
pod2            1/1     Running             0          22m
^C[root@k8s-master ~]# kubectl get pod -n test -w
NAME            READY   STATUS              RESTARTS   AGE
pod-configmap   0/1     ContainerCreating   0          14s
pod1            1/1     Running             0          23m
pod2            1/1     Running             0          23m
pod-configmap   1/1     Running             0          25s
^C[root@k8s-master ~]# kubectl exec -it pod-configmap -n test /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@pod-configmap:/# 
root@pod-configmap:/# 
root@pod-configmap:/# cd /configmap/config/
root@pod-configmap:/configmap/config# ll
bash: ll: command not found
root@pod-configmap:/configmap/config# ls
msg
root@pod-configmap:/configmap/config# cat msg 
username: root
address: meiguo
root@pod-configmap:/configmap/config# ex  
bash: ex: command not found
root@pod-configmap:/configmap/config# 
root@pod-configmap:/configmap/config# 
root@pod-configmap:/configmap/config# 
root@pod-configmap:/configmap/config# exit
exit
command terminated with exit code 127
[root@k8s-master ~]# cat pod-configmap.yaml 
---
apiVersion: v1
kind: Pod
metadata:
  name: pod-configmap
  namespace: test
spec:
  containers:
  - name: nginx
    image: nginx:1.17.1
    volumeMounts:
    - name: config
      mountPath: /configmap/config
  volumes:
  - name: config
    configMap:
      name: configmap

Secret

  • 在 Kubernetes 中,Secret 对象确实是用来存储敏感信息的一种资源例如密码、秘钥、证书等等。它与 ConfigMap 类似,但设计目的不同。

  • Opaque

    • 这种类型的 Secret 用于存储少量的敏感数据,如密码、令牌或密钥。

    • 数据以 Base64 编码格式存储,这意味着虽然数据在存储时被编码,但仍然可以通过 Base64 解码来查看原始数据,因此安全性相对较低。

bash 复制代码
[root@k8s-master ~]# echo -n "root"  | base64
cm9vdA==
[root@k8s-master ~]# echo -n "123456"  | base64
MTIzNDU2
[root@k8s-master ~]#  vim secret.yaml
[root@k8s-master ~]# kubectl apply -f secret.yaml 
secret/mysecret created
[root@k8s-master ~]# kubetcl describe secret mysecret 
-bash: kubetcl: command not found
[root@k8s-master ~]# kubectl describe secret mysecret 
Name:         mysecret
Namespace:    default
Labels:       <none>
Annotations:  <none>

Type:  Opaque

Data
====
pass:  6 bytes
user:  4 bytes
[root@k8s-master ~]# kubectl get secret | grep Opaque
mysecret              Opaque                                2      40s
[root@k8s-master ~]# cat secret.yaml 
apiVersion: v1
kind: Secret
metadata:
  name: mysecret
type: Opaque
data:
  user: cm9vdA==  # base64编码的"root"
  pass: MTIzNDU2  # base64编码的"123456"
相关推荐
张忠琳3 小时前
【NVIDIA】 NVIDIA Container Toolkit v1.19.1 — OCI 模块超深度分析之三
云原生·容器·架构·kubernetes·nvidia
阿里云云原生4 小时前
让 AI Agent 看见正在发生的业务,阿里云 EventHouse 正式商业化
云原生
Elastic 中国社区官方博客4 小时前
将你的 Grafana Kubernetes 仪表板迁移到 Elastic Observability:相同的 PromQL,30 倍更快的查询
大数据·人工智能·elasticsearch·搜索引擎·容器·kubernetes·grafana
AOwhisky5 小时前
Python 学习笔记(第十五期)——运维自动化(下·后篇):堡垒机实战——paramiko高阶篇
运维·python·学习·云原生·自动化·运维开发
风曦Kisaki7 小时前
#企业级docker私有仓库构建:harbor仓库与阿里云镜像仓库
阿里云·docker·容器
期待着20137 小时前
docker 安装 ,在centos7.9
运维·docker·容器
微三云 - 廖会灵 (私域系统开发)8 小时前
电商系统从单体到微服务拆分实践:拆什么、怎么拆、拆完后怎么办
微服务·云原生·架构
骑上单车去旅行9 小时前
Docker Compose 命令完全指南:从构建到运维
docker·容器·eureka
前端Baymax10 小时前
K8s PodCrashLoopBackOff假阳性排查
云原生·容器·kubernetes
spider_xcxc11 小时前
K8s 部署学习笔记
docker·容器·kubernetes·云计算·k8s