K8S 创建 ConfigMap

ConfigMap是一种API对象,用来将非加密数据保存到键值对中。可以用作环境变量、命令行参数或者存储卷中的配置文件。

ConfigMap可以将环境变量配置信息和容器镜像解耦,便于应用配置的修改。如果需要存储加密信息时可以使用Secret对象。

一、通过命令行创建

1、使用文件创建

# 创建一个文件
echo "hello,world" > hello

# 创建 configmap
kubectl create configmap hello-config --from-file=hello

# 查看
[root@master yaml]# kubectl get cm
NAME           DATA   AGE
hello-config   1      4s

# 查看信息
[root@master yaml]# kubectl describe cm hello-config
Name:         hello-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
hello:
----
hello,world

Events:  <none>

2、通过文件夹创建

指定目录的所有文件都会被用在 configmap 里面创建一个键值对,键名就是文件名,键值就是文件内容

# 创建 2个文件
echo "tom" > student
echo "chinese" > course

mkdir grade
mv student course grade/

# 从目录创建
kubectl create configmap grade-config --from-file=grade/

[root@master configmap]# kubectl get cm
NAME         DATA   AGE
dir-config   2      5s

# 查看详细
[root@master configmap]# kubectl describe cm grade-config
Name:         dir-config
Namespace:    default
Labels:       <none>
Annotations:  <none>

Data
====
course:
----
chinese

student:
----
tom

Events:  <none>

3、使用文字值创建

# 创建
kubectl create configmap student-config --from-literal=tom="male" --from-literal=alice="female"

# 通过 yaml 查看
[root@master yaml]# kubectl get cm student-config -o yaml
apiVersion: v1
data:
  alice: female
  tom: male
kind: ConfigMap
metadata:
  creationTimestamp: "2021-03-01T14:18:28Z"
  name: student-config
  namespace: default
  resourceVersion: "809307"
  selfLink: /api/v1/namespaces/default/configmaps/student-config
  uid: 667ab4b4-9350-4fd9-9212-c222133038c0

 二、通过 资源清单文件 yaml 创建

1、创建 yaml

vim configmap-test.yaml

# 内容
apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
data:
  user: Tom
  gender: Male

2、创建 configmap

kubectl apply -f configmap-test.yaml

3、查看 configmap

[root@master yaml]# kubectl get cm
NAME          DATA   AGE
test-config   2      9m33s

4、通过 yaml 查看 configmap

[root@master yaml]# kubectl get cm -o yaml
apiVersion: v1
items:
- apiVersion: v1
  data:
    gender: male
    userName: Tom
  kind: ConfigMap
  metadata:
    annotations:
      kubectl.kubernetes.io/last-applied-configuration: |
        {"apiVersion":"v1","data":{"gender":"male","userName":"Tom"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test-config","namespace":"default"}}
    creationTimestamp: "2021-03-01T13:46:55Z"
    name: test-config
    namespace: default
    resourceVersion: "804471"
    selfLink: /api/v1/namespaces/default/configmaps/test-config
    uid: cd79260f-1737-474e-9bf8-fd8820df05c9
kind: List
metadata:
  resourceVersion: ""
  selfLink: ""

 5、通过 decripbe 查看

[root@master yaml]# kubectl describe cm test-config
Name:         test-config
Namespace:    default
Labels:       <none>
Annotations:  kubectl.kubernetes.io/last-applied-configuration:
                {"apiVersion":"v1","data":{"gender":"male","userName":"Tom"},"kind":"ConfigMap","metadata":{"annotations":{},"name":"test-config","namespa...

Data
====
gender:
----
male
userName:
----
Tom
Events:  <none>

三、configmap 的使用

使用 configmap 代替环境变量,可以通过下面二种方法

1、使用 valueFrom,configMapKeyRef,name,key 指定要用的 key

2、还可以通过 envFrom,configMapRef,name 使得 configmap 中的所有 key/value 键值对 都自动变成环境变量

1、创建 yaml 

vim test-pod-configmap.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-pod-configmap
spec:
  containers:
  - name: test-alpine
    image: alpine
    command: ["/bin/sh","-c","env; sleep 600"]
    envFrom:
    - configMapRef:
        name: test-config
    env:
    - name: KEY-01
      valueFrom:
        configMapKeyRef:
          name: student-config
          key: tom
    - name: KEY-02
      valueFrom:
        configMapKeyRef:
          name: student-config
          key: alice

 2、创建 pod

kubectl apply -f test-pod-configmap.yaml

3、查看环境变量

kubectl logs test-pod-configmap

# 容器启动输出 env
[root@master yaml]# kubectl logs test-pod-configmap  或者 kubectl exec -it test-pod-configmap /bin/sh
KUBERNETES_SERVICE_PORT=443
KUBERNETES_PORT=tcp://10.96.0.1:443
MYAPP_SVC_PORT_8081_TCP=tcp://10.109.63.67:8081
HOSTNAME=test-pod-configmap
SHLVL=1
HOME=/root

代入 环境变量
userName=Tom
KEY-01=male
gender=male
KEY-02=female

......

 在数据卷中使用 configmap

1、创建 yaml 

vim test-pod-configmap-volume.yaml

apiVersion: v1
kind: Pod
metadata:
  name: test-pod-configmap-volume
spec:
  containers:
  - name: test-alpine
    image: alpine
    command: ["/bin/sh","-c","env; sleep 600"]
    volumeMounts:
    - name: config-volume
      mountPath: /etc/config
  volumes:
  - name: config-volume
    configMap:
      name: student-config

2、创建 pod

kubectl apply -f test-pod-configmap-volume.yaml

3、查看

kubectl exec -it test-pod-configmap-volume /bin/sh
/ # ls /etc/config/
jack  tom

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值