当前位置: 首页 > news >正文

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

#这里的 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

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-configkubectl get configmaps game-config -o yaml

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

我因为本地已有环境,直接在本地测试了
在这里插入图片描述
在这里插入图片描述

2.2 通过文件创建ConfigMap

#通过文件加载配置变量
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

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:38Zname: special-confignamespace: defaultresourceVersion: "651"uid: dadce046-d673-11e5-8cd0-68f728db1985
data:special.how: veryspecial.type: charm

2.4 基于生成器创建ConfigMap

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

#基于 configure-pod-container/configmap/game.properties 文件生成一个 ConfigMap
#创建包含 ConfigMapGenerator 的 kustomization.yaml 文件
cat <<EOF >./kustomization.yaml
configMapGenerator:
- name: game-config-4labels:game-config: config-4files:- 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. 临时清理

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 中的数据定义容器环境变量

#在 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-containerimage: registry.k8s.io/busybox:1.27.2#image: registry.k8s.io/busyboxcommand: [ "/bin/sh", "-c", "sleep 3600" ]#command: [ "/bin/sh", "-c", "env" ]env:# 定义环境变量- name: SPECIAL_LEVEL_KEYvalueFrom:configMapKeyRef:# ConfigMap 包含你要赋给 SPECIAL_LEVEL_KEY 的值name: special-config# 指定与取值相关的键名key: special.howrestartPolicy: 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定义容器环境变量

#configmap/configmaps.yaml
apiVersion: v1
kind: ConfigMap
metadata:name: special-confignamespace: default
data:special.how: very
---
apiVersion: v1
kind: ConfigMap
metadata:name: env-confignamespace: 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-containerimage: registry.k8s.io/busybox:1.27.2#command: [ "/bin/sh", "-c", "env" ]command: [ "/bin/sh", "-c", "sleep 3600" ]env:- name: SPECIAL_LEVEL_KEYvalueFrom:configMapKeyRef:name: special-configkey: special.how- name: LOG_LEVELvalueFrom:configMapKeyRef:name: env-configkey: log_levelrestartPolicy: 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: falseLOG_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 定义的环境变量

apiVersion: v1
kind: Pod
metadata:name: dapi-test-pod
spec:containers:- name: test-containerimage: registry.k8s.io/busybox:1.27.2command: [ "/bin/echo", "$(SPECIAL_LEVEL_KEY) $(SPECIAL_TYPE_KEY)" ]env:- name: SPECIAL_LEVEL_KEYvalueFrom:configMapKeyRef:name: special-configkey: SPECIAL_LEVEL- name: SPECIAL_TYPE_KEYvalueFrom:configMapKeyRef:name: special-configkey: SPECIAL_TYPErestartPolicy: Never

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

#创建
#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-containerimage: registry.k8s.io/busybox:1.27.2command: [ "/bin/sh", "-c", "ls /etc/config/" ]volumeMounts:- name: config-volumemountPath: /etc/configvolumes:- name: config-volumeconfigMap:# 提供包含要添加到容器中的文件的 ConfigMap 的名称name: special-configrestartPolicy: Never#创建POD  
kubectl create -f pod-configmap-volume.yaml
kubectl exec -it weiheng2-dapi-test-pod -- /bin/sh

在这里插入图片描述

4.5 配置文件热更新

#我把这个文件拿到本地来修改一下
wget https://kubernetes.io/examples/configmap/configmap-multikeys.yaml  #我把 very 改为 fantastic
apiVersion: v1
kind: ConfigMap
metadata:name: special-confignamespace: default
data:SPECIAL_LEVEL: fantastic#SPECIAL_LEVEL: verySPECIAL_TYPE: charm

在这里插入图片描述
热更新OK


http://www.mrgr.cn/news/5751.html

相关文章:

  • go 系列实现websocket
  • 买了服务器后如何正确挂载数据盘|什么是系统盘,什么是数据盘
  • 控制台小游戏—扫雷
  • 海外活动策划:5个必备的活动策划推广工具
  • Java学习笔记(02)接口的使用
  • 改编版猜数字小游戏,猜错了就黑屏(整蛊版本)
  • C语言从头学49—文件操作(四)
  • R 语言学习教程,从入门到精通,R Excel 文件使用(18)
  • 深信服研发面试经验分享
  • @PathVariable:Spring MVC中的路径变量解析
  • 台球助教在线预约小程序源码开发:打造便捷高效的台球学习新体验
  • docker常见指令——镜像指令and容器指令
  • 如何让python爬虫的数据可视化?
  • java设计模式--创建型设计模式
  • 【Python】链式、嵌套调用、递归、函数栈帧、参数默认值和关键字参数
  • k8s高版本(1,28)部署NodePort模式下的ingress-nginx的详细过程及应用案例
  • 掌握 Rust 中的 YAML 魔法:Serde_yaml 使用指南
  • 微软正式确认将在近期关闭经典Windows控制面板
  • C# --- 异步编程(Async/Await)
  • C++/Qt 多媒体