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

相关推荐
Lill_bin1 小时前
JVM内部结构解析
jvm·后端·spring cloud·微服务·云原生·ribbon
青云交2 小时前
大数据新视界 --大数据大厂之Kubernetes与大数据:容器化部署的最佳实践
数据库·kubernetes·容器编排·资源管理·大数据处理·扩展性、故障恢复·存储持久化·监控、日志管理、性能提升
二进制杯莫停2 小时前
初识zookeeper
分布式·zookeeper·云原生
StevenZeng学堂3 小时前
【Kubernetes笔记】为什么DNS解析会超时?
网络协议·docker·云原生·kubernetes
爱吃龙利鱼15 小时前
nginx实现https安全访问的详细配置过程
运维·nginx·安全·云原生·https
大白菜和MySQL17 小时前
rockylinux9.4单master节点k8s1.28集群部署
云原生·容器·kubernetes
向往风的男子17 小时前
【从问题中去学习k8s】k8s中的常见面试题(夯实理论基础)(三十)
学习·容器·kubernetes
七夜zippoe1 天前
nacos和eureka的区别详解
云原生·eureka
唐大爹1 天前
kubeadm方式安装k8s续:
云原生·容器·kubernetes
陈小肚1 天前
openeuler 22.03 lts sp4 使用 kubeadm 部署 k8s-v1.28.2 高可用集群
kubernetes