K8s对象 -configmap(从文件创建,用yml文件创建)

1. 从文件创建

1.1 --from-file

从文件创建,且挂载后文件名和源文件一致。

- 语法

kubectl create configmap Configmap_Name --from-file=File_Name -n Namespace_Name

说明:
这种方法创建的configmap,默认以File_Name作为key。
而configmap挂载到容器中之后将以这个key作为容器中文件的名字。
因此创建configmap的源文件必须与容器内要挂载的文件名一致。
如果要修改key名,看1.2节

- 示例

需求:从.env 文件创建一个名为 web-env的congfigmap,之后需要挂载到pod中/etc/.env

  • 创建.env文件
    IOT_WEB_API_HOST=10.252.xxx.xxx
    IOT_WEB_API_PORT=30103
    IOT_WEB_PROJECT_NAME=xxxxxxx
  • 从该文件创建configmap
 kubectl create configmap web-env --from-file=.env -n xinfa

- 查看

说明: configmap中 data下 .env为key。之后文件内容为该key的值

# kubectl edit -n xinfa configmaps web-env
apiVersion: v1
data:
  .env: |
    IOT_WEB_API_HOST=10.252.xxx.xxx
    IOT_WEB_API_PORT=30103
    IOT_WEB_PROJECT_NAME=xxxxxxx
kind: ConfigMap
metadata:
  creationTimestamp: "2019-11-29T05:33:36Z"
  name: web-env
  namespace: xinfa
  resourceVersion: "9432225"
  selfLink: /api/v1/namespaces/xinfa/configmaps/web-env
  uid: ca830a23-1269-11ea-82c2-005056a7d888

1.2 --from-literal

从文件创建,挂载后的文件名可自定义(和源文件不同)

- 语法

kubectl create configmap Configmap_Name --from-file=KEY_Name=File_Name --from-file=KEY_Nname=File_Name  -n Namespace_Name

- 示例

需求:从env 文件创建一个名为 web-env的congfigmap,之后需要挂载到pod中/etc/.env

  • 创建 env文件
IOT_WEB_API_HOST=10.252.xxx.xxx
IOT_WEB_API_PORT=30103
IOT_WEB_PROJECT_NAME=xxxxxxx
  • 创建configmap
[root@DoM01 yml]# kubectl create configmap web-env --from-file=.env=env
configmap/config-env created
  • 查看
# kubectl edit configmaps web-env

可见configmap内容如下:

# Please edit the object below. Lines beginning with a '#' will be ignored,
# and an empty file will abort the edit. If an error occurs while saving this file will be
# reopened with the relevant failures.
#
apiVersion: v1
data:
  .env: |
    IOT_WEB_API_HOST=10.252.xxx.xxx
    IOT_WEB_API_PORT=30103
    IOT_WEB_PROJECT_NAME=xxxxxxx
kind: ConfigMap
metadata:
  creationTimestamp: "2021-02-03T09:47:36Z"
  name: web-env
  namespace: default
  resourceVersion: "15083053"
  selfLink: /api/v1/namespaces/default/configmaps/web-env
  uid: 45cde575-d55b-4242-8b6d-ef0303196c2b

上文可见,key变成了.env 而并不是1.1中使用原文件名作为key了。

2. 从yaml文件创建

  • yml文件

文件叫什么名字无所谓,就叫configmap.yml吧,内容如下:

apiVersion: v1
kind: ConfigMap
metadata:
  name: mysqlcnf
  namespace: mysql-test
data:
  my.cnf: |
    # 这里是mysql的配置文件
    # 虽然主公这个懒家伙什么也没有留下,但是不影响mysql启动
    !includedir /etc/mysql/conf.d/
    !includedir /etc/mysql/mysql.conf.d/
  • 创建
kubectl create -f configmap.yml

3. configMap使用

3.1 key 是目标文件名

- 使用方法

key名是挂载到容器中的文件名,如果不是使用3.2中的方法。

    spec:
      containers:
      - name: xinfa01-web
        image: 10.252.xxx.xxx/xinfa/xxxx
        imagePullPolicy: IfNotPresent
        env:
        - name: TZ
          value: "Asia/Shanghai"
        ports:
        - containerPort: 3080
          protocol: TCP
          name: http
        volumeMounts:
        - mountPath: /build
          name: web-server
        # 添加以下三行,指明挂载到容器中哪个位置
        - mountPath: /etc/.env  #容器中的全路径 
          name: web-env
          subPath: .env   #容器中的文件名
      volumes:
      - name: web-server
        persistentVolumeClaim:
          claimName:  pv-xf01-web
      # 添加以下三行,挂载名为web-env的configmp
      - name: web-env
        configMap:
          name: web-env
      imagePullSecrets:
      - name: my-harbor

- 完整的示例

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: mysql
  namespace: mysql-test
spec:
  replicas: 1
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: harbocto.xxx.com.cn/public/mysql:5.7
        imagePullPolicy: IfNotPresent
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: yunweizhidao
        - name: MYSQL_REPLICATION_USER
          value: "liuwei"
        - name: MYSQLREPLICAITONPASSWO
          value: "1qaz@WSX"
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
        - mountPath: /etc/mysql/my.cnf
          name: mysqlcnf
          subPath: my.cnf
      volumes:
      - name: mysql-data
        persistentVolumeClaim:
          claimName: mysql
      - name: mysqlcnf
        configMap:
          name: mysqlcnf

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysqlcnf
  namespace: mysql-test
data:
  my.cnf: |
    # 这里是mysql的配置文件
    # 虽然鸿渐这个懒家伙什么也没有留下,但是不影响mysql启动
    !includedir /etc/mysql/conf.d/
    !includedir /etc/mysql/mysql.conf.d/
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  namespace: mysql-test
spec:
  type: NodePort
  ports:
  - port: 3306
    targetPort: 3306
    nodePort: 30200
  selector:
    app: mysql
  selector:
    app: mysql

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mysql
  namespace: mysql-test
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi

3.2 key名不是目标文件名

需求:将名为mysqlcnf 的configmap中,key为mycnf的内容,挂载到容器中为/etc/mysql/my.cnf

        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
        - name: mysqlcnf
          mountPath: /etc/mysql/my.cnf
          subPath: my.cnf
      volumes:
      - name: mysql-data
        persistentVolumeClaim:
          claimName: mysql
      - name: mysqlcnf
        configMap:
          name: mysqlcnf
          # 添加如下内容即可
          items:
          - key: mycnf
            path: my.cnf

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以通过以下步骤在Kubernetes中使用YAML文件创建Prometheus: 1. 创建ConfigMap 首先,需要创建一个ConfigMap,该ConfigMap将包含Prometheus的配置文件。可以使用以下命令创建ConfigMap: ``` kubectl create configmap prometheus-config --from-file=prometheus.yml ``` 其中,prometheus.yml是Prometheus的配置文件。 2. 创建Deployment 接下来,需要创建一个Deployment,该Deployment将运行Prometheus容器。可以使用以下命令创建Deployment: ``` apiVersion: apps/v1 kind: Deployment metadata: name: prometheus-deployment spec: replicas: 1 selector: matchLabels: app: prometheus template: metadata: labels: app: prometheus spec: containers: - name: prometheus image: prom/prometheus:v2.26.0 args: - "--config.file=/etc/prometheus/prometheus.yml" ports: - containerPort: 9090 volumeMounts: - name: config-volume mountPath: /etc/prometheus/ volumes: - name: config-volume configMap: name: prometheus-config ``` 在这个Deployment定义中,我们指定了Prometheus容器的镜像和端口。还将Prometheus的配置文件挂载到了容器的/etc/prometheus/目录中。 3. 创建Service 最后,需要创建一个Service来公开Prometheus容器的端口。可以使用以下命令创建Service: ``` apiVersion: v1 kind: Service metadata: name: prometheus-service spec: selector: app: prometheus type: ClusterIP ports: - name: prometheus port: 9090 targetPort: 9090 ``` 在这个Service定义中,我们指定了Prometheus容器的端口和类型。 完成上述步骤后,可以使用以下命令检查Prometheus是否已成功部署: ``` kubectl get pods kubectl get services ``` 如果一切正常,应该会看到一个运行中的Pod和一个运行中的Service。现在就可以通过Service的ClusterIP访问Prometheus了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

玄德公笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值