zoukankan      html  css  js  c++  java
  • Kubernetes存储卷的使用

    Kubernetes中, 有这不同方式的内容挂载, 简单记录一下他们的配置方式.

    ConfigMap

    配置内容

    内容配置

    apiVersion: v1
    kind: ConfigMap
    metadata:
      name: test-config
    data: 
      # 添加配置的 key-value 内容
      test-key: test-value
    

    引入

    apiVersion: v1
    kind: Pod
    spec: 
      containers: 
        - name: test
          image: nginx
          env:
            # 场景1: 可以用来配置环境变量
            - name: ENV_NAME
              valueFrom: 
                # 指定某一个 configMap 中的某个 key 作为 value
                configMapKeyRef:
                  name: test-config
                  key: test-key
          # 定义进行挂在的数据卷
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
          envFrom:
             # 场景2: 将其整个导入作为 env
             - configMapRef:
               name: test-config
      volumes: 
        - name: config-volume
          # 场景3: 将 ConfigMap 作为数据进行导入
          # 导入后, key作为文件名, value 作为文件内容 
          configMap:
            name: test-config
    

    Secret

    存放一些加密信息, 当前加密就是base64...

    配置挂载

    其使用方式基本和ConfigMap相同, 就是单纯的换个字段名

    内容配置

    apiVersion: v1
    kind: Secret
    metadata: 
      name: test-secret
    type: Opaque
    data: 
      # 这里存放的是 base64 编码的内容
      password: cGFzc3dvcmQ=
    

    引入

    apiVersion: v1
    kind: Pod
    spec: 
      containers: 
        - name: test
          image: nginx
          env:
            # 场景1: 可以用来配置环境变量
            - name: ENV_NAME
              valueFrom: 
                # 指定某一个 configMap 中的某个 key 作为 value
                secretKeyRef: 
                  name: test-secret
                  key: password
          # 定义进行挂在的数据卷
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
          envFrom:
             # 场景2: 将其整个导入作为 env
             - secretRef:
               name: test-secret
      volumes: 
        - name: config-volume
          # 场景3: 将 Secret 作为数据进行导入
          # 导入后, key作为文件名, value 作为文件内容 
          secret:
            secretName: test-secret
    

    镜像仓库鉴权

    当拉取的镜像仓库是私有仓库时, 需要增加鉴权内容

    内容配置

    apiVersion: v1
    kind: Secret
    metadata:
      name: test-register-secret
    type: kubernetes.io/dockerconfigjson
    data:
      # 文件 ~/.docker/config.json 的 base64 编码内容
      # 其内容就是个 json: {"auths":{"test.pull.com":{"username":"admin","password":"123456","email":"hujingnb@qq.com","auth":"YWRtaW46MTIzNDU2"}}}
      .dockerconfigjson: eyJhdXRocyI6eyJ0ZXN0LnB1bGwuY29tIjp7InVzZXJuYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6IjEyMzQ1NiIsImVtYWlsIjoiaHVqaW5nbmJAcXEuY29tIiwiYXV0aCI6IllXUnRhVzQ2TVRJek5EVTIifX19
    

    这玩意通过配置文件生成费点劲, 所有可以通过命令行直接生成:

    kubectl create secret docker-registry test-register-secret \
      --docker-server=<你的镜像仓库服务器> \
      --docker-username=<你的用户名> \
      --docker-password=<你的密码> \
      --docker-email=<你的邮箱地址>
    

    可以通过命令查看现有 secret 鉴权内容:

    kubectl get secret test-register-secret --output="jsonpath={.data.\.dockerconfigjson}" | base64 --decode

    引入

    apiVersion: v1
    kind: Pod
    spec: 
      containers: 
        - name: test
          image: nginx
      # 指定拉取镜像是使用的配置信息
      imagePullSecrets: 
        - name: test-register-secret
    

    Volume

    直白的说, 就是磁盘的挂载.

    举个简单的使用例子

    apiVersion: v1
    kind: Pod
    spec: 
      containers: 
        - name: test
          image: nginx
          # 定义进行挂在的数据卷
          volumeMounts:
            - name: config-volume
              mountPath: /etc/config
      volumes: 
        - name: config-volume
          hostPath: 
            path: /usr/etc/nginx/config
    

    上面将本地的目录 /usr/etc/nginx/config 挂在到的容器的 /etc/config 上.

    注意看其中的hostPath参数, 还可以替换成其他的, k8s支持很多类型的挂载卷, 这里就不一一举例了, 具体可通过kubectl explain pod.spec.volumes 查看. 简单列举几个感觉比较常用的:

    • emptyDir: 在 Node 上开一个空的目录用于共享.
    • hostPath: 指定挂载 Node 本地路径
  • 相关阅读:
    冒泡排序的java实现
    linux磁盘挂载
    Spring第九篇:primary指定bean为首选对象
    Spring第八篇:容器中bean对象的创建顺序
    Spring第六篇:依赖的手动注入
    Spring第五篇:Spring bean的作用域
    Spring第四篇:bean实例的创建方式
    SpringBoot 整合 kaptcha 验证码
    Java 创建线程池的方式
    MySQL IF() 函数用法
  • 原文地址:https://www.cnblogs.com/hujingnb/p/15707873.html
Copyright © 2011-2022 走看看