0
点赞
收藏
分享

微信扫一扫

Kubernetes资源_03_ConfigMap全解析


系列文章目录

文章目录

  • ​​系列文章目录​​
  • ​​前言​​
  • ​​一、ConfigMap​​
  • ​​二、ConfigMap的创建​​
  • ​​2.1 创建方式1:命令行创建configmap​​
  • ​​2.2 创建方式2:根据配置文件中创建configmap​​
  • ​​2.3 创建方式3:通过yaml文件创建configmap​​
  • ​​三、ConfigMap的使用​​
  • ​​3.1 使用方式1:环境变量env使用configmap​​
  • ​​3.2 使用方式2:命令行command使用configmap​​
  • ​​3.3 使用方式3:volume挂载使用configmap​​
  • ​​总结​​

前言

一、ConfigMap

​官网​​​:​​https://kubernetes.io/docs/tasks/configure-pod-container/configure-pod-configmap/​​

ConfigMaps allow you to decouple configuration artifacts from image content to keep containerized applications portable.

说白了就是用来保存配置数据的键值对,也可以保存单个属性,也可以保存配置文件。

所有的配置内容都存储在etcd中,创建的数据可以供Pod使用。

二、ConfigMap的创建

2.1 创建方式1:命令行创建configmap

# 创建一个名称为my-config的ConfigMap,key值时db.port,value值是'3306'   literal表示命令行
kubectl create configmap my-config --from-literal=db.port='3306'
kubectl get configmap
kubectl get configmap my-config -o yaml

Kubernetes资源_03_ConfigMap全解析_容器

2.2 创建方式2:根据配置文件中创建configmap

创建一个文件,名称为app.properties

name=jack
age=17

kubectl create configmap app --from-file=./app.properties
kubectl get configmap
kubectl get configmap app -o yaml

演示如下:

Kubernetes资源_03_ConfigMap全解析_命令行_02

2.3 创建方式3:通过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

kubectl apply -f configmaps.yaml
kubectl get configmap
kubectl get configmap special-config -o yaml
kubectl get configmap env-config -o yaml

演示如下:

Kubernetes资源_03_ConfigMap全解析_容器_03

三、ConfigMap的使用

  • 使用方式

(1)通过环境变量的方式,直接传递给pod
使用configmap中指定的key
使用configmap中所有的key
(2)通过在pod的命令行下运行的方式(启动命令中)
(3)作为volume的方式挂载到pod内

  • 注意

(1)ConfigMap必须在Pod使用它之前创建
(2)使用envFrom时,将会自动忽略无效的键
(3)Pod只能使用同一个命名空间的ConfigMap

3.1 使用方式1:环境变量env使用configmap

需要使用valueFrom、configMapKeyRef、name:
(1) valueFrom在最外层,表示是env某个key对应的value值
(2) configMapKeyRef 在 valueFrom 下面一层,表示这个 valueFrom 的值来自某个结构
(3) name 又在 configMapKeyRef 下面一层,

vi test-pod.yaml 
kubectl apply -f test-pod.yaml
kubectl logs pod-name

apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "env" ]
env:
# Define the environment variable
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
name: special-config
# Specify the key associated with the value
key: special.how
restartPolicy: Never

Kubernetes资源_03_ConfigMap全解析_kubernetes_04

3.2 使用方式2:命令行command使用configmap

key的话指定要用到的key

vi test-pod.yaml 
kubectl apply -f test-pod2.yaml
kubectl logs pod-name

apiVersion: v1
kind: Pod
metadata:
name: dapi-test-pod
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "env" ]
env:
# Define the environment variable
- name: SPECIAL_LEVEL_KEY
valueFrom:
configMapKeyRef:
# The ConfigMap containing the value you want to assign to SPECIAL_LEVEL_KEY
name: special-config
# Specify the key associated with the value
key: special.how
restartPolicy: Never

Kubernetes资源_03_ConfigMap全解析_命令行_05

3.3 使用方式3:volume挂载使用configmap

vi pod-myconfigmap-v2.yaml 
kubectl apply -f pod-myconfigmap-v2.yaml
kubectl logs pod-name

apiVersion: v1
kind: Pod
metadata:
name: pod-configmap2
spec:
containers:
- name: test-container
image: busybox
command: [ "/bin/sh", "-c", "ls /etc/config/" ]
volumeMounts:
- name: config-volume
mountPath: /etc/config
volumes:
- name: config-volume
configMap:
name: special-config
restartPolicy: Never

Kubernetes资源_03_ConfigMap全解析_docker_06

总结


举报

相关推荐

0 条评论