k8s 配置管理configmap

52次阅读
没有评论

root@k8s111:/home/k8s# kubectl get cm
NAME               DATA   AGE
kube-root-ca.crt   1      110m # 默认的

kubectl create configmap
kubectl create cm
kubectl create cm -h # 查看帮助、Examples
db.properties
username=root
password=123456

redis.properties
host:127.0.0.1
port:6379
kubectl create configmap my-config --from-file=config # 基于文件夹创建配置
kubectl get cm
kubectl describe cm my-config
root@k8s111:/home/k8s# kubectl describe cm my-config
Name:         my-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
db.properties:
----
username=root
password=123456

redis.properties:
----
host:127.0.0.1
port:6379


BinaryData
====

Events:  <none>

基于文件创建

基于文件别名

基于Keyvalue创建

# env-test-pod.yaml
apiVersion: v1
kind: Pod
metadata:
  name: test-env-po
spec:
  containers:
    - name: env-test
      image: alpine
      command: ["/bin/sh", "-c", "env;sleep 3600"]
      imagePullPolicy: IfNotPresent
      env:
      - name: JAVA_VM_OPTS
        valueFrom:
          configMapKeyRef:
            name: test-env-config # configMap的名字
            key: JAVA_OPTS_TEST # 表示从 name 的ConfigMap 中获取名字为key的value,将其赋值给本地环境变量 JAVA_VM_OPTS
      - name: APP
        valueFrom:
          configMapKeyRef:
            name: test-env—config
            key: APP_NAME
  restartPolicy: Never
kubectl logs -f test-env-cm
apiVersion: v1
kind: Pod
metadata:
  name: test-config-po
spec:
  containers:
    - name: config-test
      image: alpine
      command: ["/bin/sh", "-c", "env;sleep 3600"]
      imagePullPolicy: IfNotPresent
      volumeMounts: # 加载数据卷
      - name: db-config # 表示加载volumes属性中哪个数据卷
        mountPaht: "/usr/local/mysql/conf" # 想要将数据卷中的文件加载到哪个目录下
        readOnly: true # 是否只读
  volumes: # 数据卷挂载 configmap、sercret
    - name: db-config # 数据卷名字,随意设置
      configMap: # 数据卷类型为 ConfigMap
        name: test-dir-config # configmap的名字,必须跟想要加载的configmap相同
        items: # 对 configmap 中的key进行映射,定义了items,那么只会将配置了的key进行映射,如果步子,如果不指定,默认会将configmap中所有key全部转换为一个个同名的文件
        - key: "db.properties" # configMap 中的key
          path: "db.properties" # 将该 key 的值转换为文件
  restartPolicy: Never

测试

kubectl exec -it test-config-po -- sh
cd /usr/loca/mysql/conf
ls

正文完
 0
wujingquan
版权声明:本站原创文章,由 wujingquan 于2025-06-11发表,共计1677字。
转载说明:Unless otherwise specified, all articles are published by cc-4.0 protocol. Please indicate the source of reprint.
评论(没有评论)