HostPath将persistentVolume分配给群集中的特定工作节点 [英] HostPath assign persistentVolume to the specific work node in cluster

查看:68
本文介绍了HostPath将persistentVolume分配给群集中的特定工作节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用kubeadm创建集群,我有一个主节点和工作节点.

Using kubeadm to create a cluster, I have a master and work node.

现在,我想在工作节点中共享一个persistentVolume,它将与Postgres pod绑定.

Now I want to share a persistentVolume in the work node, which will be bound with Postgres pod.

期望代码会在工作节点的路径/postgres中创建persistentVolume,但是hostPath似乎在群集中不起作用,我应该如何将此属性分配给特定的节点?

Expecting the code will create persistentVolume in the path /postgres of work node, but it seems the hostPath will not work in a cluster, how should I assign this property to the specific node?

kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv-postgres
  labels:
    type: local
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/postgres"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-postgres
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  strategy: {}
  template:
    metadata:
      labels:
        app: postgres
    spec:
      dnsPolicy: ClusterFirstWithHostNet
      hostNetwork: true
      volumes:
      - name: vol-postgres
        persistentVolumeClaim:
          claimName: pvc-postgres
      containers:
      - name: postgres
        image: postgres:12
        imagePullPolicy: Always
        env:
        - name: DB_USER
          value: postgres
        - name: DB_PASS
          value: postgres
        - name: DB_NAME
          value: postgres
        ports:
        - name: postgres
          containerPort: 5432
        volumeMounts:
        - mountPath: "/postgres"
          name: vol-postgres
        livenessProbe:
          exec:
            command:
            - pg_isready
            - -h
            - localhost
            - -U
            - postgres
          initialDelaySeconds: 30
          timeoutSeconds: 5
        readinessProbe:
          exec:
            command:
            - pg_isready
            - -h
            - localhost
            - -U
            - postgres
          initialDelaySeconds: 5
          timeoutSeconds: 1
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
spec:
  ports:
  - name: postgres
    port: 5432
    targetPort: postgres
  selector:
    app: postgres

推荐答案

按照文档.

hostPath卷将来自主机节点文件系统的文件或目录装载到Pod中.这不是大多数Pod所需要的,但是它为某些应用程序提供了强大的逃生舱口.

A hostPath volume mounts a file or directory from the host node’s filesystem into your Pod. This is not something that most Pods will need, but it offers a powerful escape hatch for some applications.

简而言之,hostPath类型是指节点(机器或VM)资源,您将在其中调度pod.这意味着您已经需要在该节点上拥有此文件夹. 要分配资源以指定节点,您必须在您的服务器中使用 nodeSelector DeploymentPV.

In short, hostPath type refers to node (machine or VM) resource, where you will schedule pod. It mean that you already need to have this folder on this node. To assign resources to specify node you have to use nodeSelector in your Deployment, PV.

根据情况,使用hostPath并不是最好的主意,但是我将在下面提供示例YAML,它可能向您展示概念.基于您的YAML,但带有nginx image.

Depends on the scenario, using hostPath is not the best idea, however I will provide below example YAMLs which might show you concept. Based on your YAMLs but with nginx image.

kind: PersistentVolume
apiVersion: v1
metadata:
  name: pv-postgres
spec:
  capacity:
    storage: 2Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/tmp/postgres" ## this folder need exist on your node. Keep in minds also who have permissions to folder. Used tmp as it have 3x rwx
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - ubuntu18-kubeadm-worker1    

---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc-postgres
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  strategy: {}
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - image: nginx
        name: nginx      
        volumeMounts:
        - mountPath: /home    ## path to folder inside container
          name: vol-postgres
      affinity:               ## specified affinity to schedule all pods on this specific node with name ubuntu18-kubeadm-worker1
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - ubuntu18-kubeadm-worker1  
      dnsPolicy: ClusterFirstWithHostNet
      hostNetwork: true
      volumes:
      - name: vol-postgres
        persistentVolumeClaim:
          claimName: pvc-postgres

persistentvolume/pv-postgres created
persistentvolumeclaim/pvc-postgres created
deployment.apps/postgres created

不幸的是,PV以1:1的关系绑定到PVC,因此每次都需要创建PV和PVC.

Unfortunately PV is bounded to PVC in 1:1 relationship, so for each time, you would need to create PV and PVC.

但是,如果您使用的是hostPath,则可以在不包含PVPVCDeployment YAML中指定nodeAffinityvolumeMountsvolumes.

However if you are using hostPath it's enough to specify nodeAffinity, volumeMounts and volumes in Deployment YAML without PV and PVC.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  selector:
    matchLabels:
      app: postgres
  replicas: 1
  strategy: {}
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
      - image: nginx:latest
        name: nginx      
        volumeMounts:
        - mountPath: /home    
          name: vol-postgres
      affinity:               
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - ubuntu18-kubeadm-worker1  
      dnsPolicy: ClusterFirstWithHostNet
      hostNetwork: true
      volumes:
      - name: vol-postgres
        hostPath:
          path: /tmp/postgres

deployment.apps/postgres created

user@ubuntu18-kubeadm-master:~$ kubectl get pods
NAME                        READY   STATUS    RESTARTS   AGE
postgres-77bc9c4566-jgxqq   1/1     Running   0          9s
user@ubuntu18-kubeadm-master:~$ kk exec -ti postgres-77bc9c4566-jgxqq /bin/bash
root@ubuntu18-kubeadm-worker1:/# cd home
root@ubuntu18-kubeadm-worker1:/home# ls
test.txt  txt.txt

这篇关于HostPath将persistentVolume分配给群集中的特定工作节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆