当前位置:网站首页>Use of kubernetes storage volumes

Use of kubernetes storage volumes

2022-06-24 21:13:00 The smell of tobacco

stay Kubernetes in , There are different ways to mount content , Simply record how they are configured .

ConfigMap

Configure the content

Content configuration

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
data: 
  #  Add configured  key-value  Content 
  test-key: test-value

introduce

apiVersion: v1
kind: Pod
spec: 
  containers: 
    - name: test
      image: nginx
      env:
        #  scene 1:  Can be used to configure environment variables 
        - name: ENV_NAME
          valueFrom: 
            #  Specify a  configMap  One of the  key  As  value
            configMapKeyRef:
              name: test-config
              key: test-key
      #  Define the data volume to be hung 
      volumeMounts:
        - name: config-volume
          mountPath: /etc/config
      envFrom:
         #  scene 2:  Import its entire as  env
         - configMapRef:
           name: test-config
  volumes: 
    - name: config-volume
      #  scene 3:  take  ConfigMap  Import as data 
      #  After import , key As the file name , value  As the content of the document  
      configMap:
        name: test-config

Secret

Store some encrypted information , Current encryption is base64

Configure mount

Its use is basically the same as ConfigMap identical , Simply change the field name

Content configuration

apiVersion: v1
kind: Secret
metadata: 
  name: test-secret
type: Opaque
data: 
  #  What's in store here is  base64  Content of coding 
  password: cGFzc3dvcmQ=

introduce

apiVersion: v1
kind: Pod
spec: 
  containers: 
    - name: test
      image: nginx
      env:
        #  scene 1:  Can be used to configure environment variables 
        - name: ENV_NAME
          valueFrom: 
            #  Specify a  configMap  One of the  key  As  value
            secretKeyRef: 
              name: test-secret
              key: password
      #  Define the data volume to be hung 
      volumeMounts:
        - name: config-volume
          mountPath: /etc/config
      envFrom:
         #  scene 2:  Import its entire as  env
         - secretRef:
           name: test-secret
  volumes: 
    - name: config-volume
      #  scene 3:  take  Secret  Import as data 
      #  After import , key As the file name , value  As the content of the document  
      secret:
        secretName: test-secret

Image warehouse authentication

When the pulled image warehouse is a private warehouse , Authentication content needs to be added

Content configuration

apiVersion: v1
kind: Secret
metadata:
  name: test-register-secret
type: kubernetes.io/dockerconfigjson
data:
  #  file  ~/.docker/config.json  Of  base64  Code content 
  #  Its content is  json: {"auths":{"test.pull.com":{"username":"admin","password":"123456","email":"[email protected]","auth":"YWRtaW46MTIzNDU2"}}}
  .dockerconfigjson: eyJhdXRocyI6eyJ0ZXN0LnB1bGwuY29tIjp7InVzZXJuYW1lIjoiYWRtaW4iLCJwYXNzd29yZCI6IjEyMzQ1NiIsImVtYWlsIjoiaHVqaW5nbmJAcXEuY29tIiwiYXV0aCI6IllXUnRhVzQ2TVRJek5EVTIifX19

This thing takes a little effort to generate through the configuration file , All can be generated directly from the command line :

kubectl create secret docker-registry test-register-secret \
  --docker-server=< Your mirror warehouse server > \
  --docker-username=< Your username > \
  --docker-password=< Your password > \
  --docker-email=< Your email address >

You can view existing through the command secret Authentication content :

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

introduce

apiVersion: v1
kind: Pod
spec: 
  containers: 
    - name: test
      image: nginx
  #  Specify the configuration information used to pull the image 
  imagePullSecrets: 
    - name: test-register-secret

Volume

To be frank , It is the mounting of the disk .

Take a simple example

apiVersion: v1
kind: Pod
spec: 
  containers: 
    - name: test
      image: nginx
      #  Define the data volume to be hung 
      volumeMounts:
        - name: config-volume
          mountPath: /etc/config
  volumes: 
    - name: config-volume
      hostPath: 
        path: /usr/etc/nginx/config

The local directory /usr/etc/nginx/config Hanging from the container /etc/config On .

Pay attention to some of them hostPath Parameters , It can also be replaced by other , k8s Supports many types of mounted volumes , Here are no examples , The details can be obtained through kubectl explain pod.spec.volumes see . Briefly list some common feelings :

  • emptyDir: stay Node Open an empty directory for sharing .
  • hostPath: Designated mount Node The local path
原网站

版权声明
本文为[The smell of tobacco]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211318415683.html