K8S - Java微服务配置之ConfigMap

前言

参考文档:https://kubernetes.io/zh-cn/docs/tasks/configure-pod-container/configure-pod-configmap/#interim-cleanup

使用 ConfigMaps 和 Secrets 配置环境变量,使用 MicroProfile Config 来消费这些变量

可以使用以下方式为docker容器设置环境变量:

  • Dockerfile
  • kubernetes.yml
  • Kubernetes ConfigMaps
  • Kubernetes Secrets

使用 ConfigMaps 或 Secrets 这两种最新的环境变量设置方式:

  • 变量会注入到你的微服务里,多个容器可以重复使用
  • 不同的容器也可以使用不同的环境变量

ConfigMap

可以理解为K8S里的一个 明文存储 的 key-value键值对的 资源对象

允许你注入数据到pod应用中,同时将应用的配置与镜像解耦,以保持容器应用的可移植性。比如使用相同的容器镜像,通过配置实现本地开发环境、测试环境、生产环境

在 ConfigMap 中保存的数据不可超过 1MiB 。如果需要保存超出此尺寸限制的数据, 可以考虑挂载存储卷或者使用独立的数据库或者文件服务。

1. 作用

引入 Configmap 资源对象,对服务的扩容和配置修改,实现统一管理。

● 可以当成 volume 挂载到 pod 中

● 将配置信息与 docker 镜像解耦,以实现镜像的可移植性和可复用性

● 实现配置共享

java代码中,貌似也有3种方法可以读取configmap中配置,这个感兴趣的自己下去搞吧

2. 创建ConfigMap

shell 复制代码
#这里的 map-name 必须是标准  DNS子域名 格式,data-source 可以是 目录、文件或字面值
kubectl create configmap <map-name> <data-source>

#清理ConfigMap
kubectl delete configmap special-config
kubectl delete configmap env-config
kubectl delete configmap -l 'game-config in (config-4,config-5)'

2.1 通过目录创建ConfigMap

shell 复制代码
mkdir -p configure-pod-container/configmap/

# Download the sample files into `configure-pod-container/configmap/` directory
wget https://kubernetes.io/examples/configmap/game.properties -O configure-pod-container/configmap/game.properties
wget https://kubernetes.io/examples/configmap/ui.properties -O configure-pod-container/configmap/ui.properties

# Create the ConfigMap
kubectl create configmap game-config --from-file=configure-pod-container/configmap/

# 获取注入的配置详情
kubectl describe configmaps game-config

kubectl get configmaps game-config -o yaml

这里可以不用纠结,直接用官方给的线上环境做测试也很OK:https://killercoda.com/playgrounds/scenario/kubernetes

我因为本地已有环境,直接在本地测试了

2.2 通过文件创建ConfigMap

shell 复制代码
#通过文件加载配置变量
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties
#查看加载结果
kubectl describe configmaps game-config-2

#也可以同时加载多个文件
kubectl create configmap game-config-2 --from-file=configure-pod-container/configmap/game.properties --from-file=configure-pod-container/configmap/ui.properties



#也可以通过 env 文件创建, --from-env-file 加载 env-file 文件
wget https://kubernetes.io/examples/configmap/game-env-file.properties -O configure-pod-container/configmap/game-env-file.properties
wget https://kubernetes.io/examples/configmap/ui-env-file.properties -O configure-pod-container/configmap/ui-env-file.properties
cat configure-pod-container/configmap/game-env-file.properties
enemies=aliens
lives=3
allowed="true"

kubectl create configmap game-config-env-file \
       --from-env-file=configure-pod-container/configmap/game-env-file.properties
#从 Kubernetes 1.23 版本开始,kubectl 支持多次指定 --from-env-file 参数来从多个数据源创建 ConfigMap。       


#在使用 --from-file 参数时,你可以定义在 ConfigMap 的 data 部分出现键名, 而不是按默认行为使用文件名

2.3 根据参数值创建 ConfigMap

shell 复制代码
kubectl create configmap special-config --from-literal=special.how=very --from-literal=special.type=charm

#查看
kubectl get configmaps special-config -o yaml

#内容如下
apiVersion: v1
kind: ConfigMap
metadata:
  creationTimestamp: 2022-02-18T19:14:38Z
  name: special-config
  namespace: default
  resourceVersion: "651"
  uid: dadce046-d673-11e5-8cd0-68f728db1985
data:
  special.how: very
  special.type: charm

2.4 基于生成器创建ConfigMap

你还可以基于生成器(Generators)创建 ConfigMap,然后将其应用于集群的 API 服务器上创建对象。 生成器应在目录内的 kustomization.yaml 中指定。

shell 复制代码
#基于 configure-pod-container/configmap/game.properties 文件生成一个 ConfigMap
#创建包含 ConfigMapGenerator 的 kustomization.yaml 文件
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-4
  labels:
    game-config: config-4
  files:
  - configure-pod-container/configmap/game.properties
EOF

这里感觉坑爹了,跟着官方文档操作出错了。。。。

https://access.crunchydata.com/documentation/postgres-operator/latest/tutorials/day-two/monitoring

我们随便选取一种方式更新一下 kustomize 到最新版的 v5.4.3,发现依然报错,猜测可能是kubectl与 kustomize 版本兼容性问题,先跳过吧,不影响学习,不用一直在这里纠结

3. 临时清理

shell 复制代码
kubectl delete configmap special-config
kubectl delete configmap env-config
kubectl delete configmap -l 'game-config in (config-4,config-5)'

4. ConfigMap的使用

4.1 使用 ConfigMap 数据定义容器环境变量

使用单个 ConfigMap 中的数据定义容器环境变量

yaml 复制代码
#在 ConfigMap 中将环境变量定义为键值对
kubectl create configmap special-config --from-literal=special.how=very

#将 ConfigMap 中定义的 special.how 赋值给 Pod 规约中的 SPECIAL_LEVEL_KEY 环境变量
#pods/pod-single-configmap-env-variable.yaml
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox:1.27.2
      #image: registry.k8s.io/busybox
      command: [ "/bin/sh", "-c", "sleep 3600" ]
      #command: [ "/bin/sh", "-c", "env" ]
      env:
        # 定义环境变量
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              # ConfigMap 包含你要赋给 SPECIAL_LEVEL_KEY 的值
              name: special-config
              # 指定与取值相关的键名
              key: special.how
  restartPolicy: Never

#创建 Pod,这里创建时拉取镜像失败,我我改了下配置,拉取 busybox:1.27.2后OK
kubectl create -f https://kubernetes.io/examples/pods/pod-single-configmap-env-variable.yaml
#kubectl create -f pod-single-configmap-env-variable.yaml

#kubectl describe pod dapi-test-pod 显示如下 -> OK
Containers:
  test-container:
    ...
    Environment:
      SPECIAL_LEVEL_KEY:  <set to the key 'special.how' of config map 'special-config'>  Optional: false

4.2 使用多个ConfigMap定义容器环境变量

yaml 复制代码
#configmap/configmaps.yaml
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  special.how: very
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: env-config
  namespace: default
data:
  log_level: INFO

#pods/pod-multiple-configmap-env-variable.yaml   
apiVersion: v1
kind: Pod
metadata:
  name: weiheng-dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox:1.27.2
      #command: [ "/bin/sh", "-c", "env" ]
      command: [ "/bin/sh", "-c", "sleep 3600" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: special.how
        - name: LOG_LEVEL
          valueFrom:
            configMapKeyRef:
              name: env-config
              key: log_level
  restartPolicy: Never

#创建pod
kubectl create -f pod-multiple-configmap-env-variable.yaml  
#进去后查看
kubectl exec -it weiheng-dapi-test-pod -- /bin/sh

#kubectl describe pod dapi-test-pod 可看到环境变量如下
Environment:
      SPECIAL_LEVEL_KEY:  <set to the key 'special.how' of config map 'special-config'>  Optional: false
      LOG_LEVEL:          <set to the key 'log_level' of config map 'env-config'>        Optional: false

#删除pod
kubectl delete pod dapi-test-pod --now

进入容器后打印变量

容器退出后查看

4.3 在Pod 命令中使用 ConfigMap 定义的环境变量

shell 复制代码
apiVersion: v1
kind: Pod
metadata:
  name: dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox:1.27.2
      command: [ "/bin/echo", "$(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]
      env:
        - name: SPECIAL_LEVEL_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_LEVEL
        - name: SPECIAL_TYPE_KEY
          valueFrom:
            configMapKeyRef:
              name: special-config
              key: SPECIAL_TYPE
  restartPolicy: Never

4.4 将 ConfigMap 数据添加到一个卷中

yaml 复制代码
#创建
#kubectl create -f https://kubernetes.io/examples/configmap/configmap-multikeys.yaml  
#更新
#kubectl apply -f https://kubernetes.io/examples/configmap/configmap-multikeys.yaml  

#pods/pod-configmap-volume.yaml 
apiVersion: v1
kind: Pod
metadata:
  name: weiheng2-dapi-test-pod
spec:
  containers:
    - name: test-container
      image: registry.k8s.io/busybox:1.27.2
      command: [ "/bin/sh", "-c", "ls /etc/config/" ]
      volumeMounts:
      - name: config-volume
        mountPath: /etc/config
  volumes:
    - name: config-volume
      configMap:
        # 提供包含要添加到容器中的文件的 ConfigMap 的名称
        name: special-config
  restartPolicy: Never

#创建POD  
kubectl create -f pod-configmap-volume.yaml
kubectl exec -it weiheng2-dapi-test-pod -- /bin/sh

4.5 配置文件热更新

yaml 复制代码
#我把这个文件拿到本地来修改一下
wget https://kubernetes.io/examples/configmap/configmap-multikeys.yaml  

#我把 very 改为 fantastic
apiVersion: v1
kind: ConfigMap
metadata:
  name: special-config
  namespace: default
data:
  SPECIAL_LEVEL: fantastic
  #SPECIAL_LEVEL: very
  SPECIAL_TYPE: charm
    

热更新OK

相关推荐
Linux运维老纪4 小时前
DNS缓存详解(DNS Cache Detailed Explanation)
计算机网络·缓存·云原生·容器·kubernetes·云计算·运维开发
元气满满的热码式12 小时前
K8S部署DevOps自动化运维平台
运维·kubernetes·devops
IT艺术家-rookie17 小时前
k8s--部署k8s集群--控制平面节点
容器·kubernetes
Elastic 中国社区官方博客18 小时前
使用 Ollama 和 Kibana 在本地为 RAG 测试 DeepSeek R1
大数据·数据库·人工智能·elasticsearch·ai·云原生·全文检索
Linux运维老纪1 天前
windows部署deepseek之方法(The Method of Deploying DeepSeek on Windows)
linux·人工智能·分布式·云原生·运维开发·devops
Elastic 中国社区官方博客2 天前
Elastic Cloud Serverless 获得主要合规认证
大数据·数据库·elasticsearch·搜索引擎·云原生·serverless·全文检索
超级阿飞2 天前
在K8s中部署动态nfs存储provisioner
云原生·容器·kubernetes·nfs
赵渝强老师2 天前
【赵渝强老师】K8s中Pod探针的TCPSocketAction
云原生·容器·kubernetes
努力的小T2 天前
Linux二进制部署K8s集群的平滑升级教程
linux·运维·服务器·云原生·容器·kubernetes·云计算
2的n次方_2 天前
Eureka 服务注册和服务发现的使用
spring boot·spring cloud·云原生·eureka·服务发现