3

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

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

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

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
4

2 回答 2

3

根据文档

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

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

取决于场景,使用hostPath不是最好的主意,但是我将在下面提供可能向您展示概念的示例 YAML。基于您的 YAML,但使用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。

但是,如果您使用hostPath它就足以指定nodeAffinity,volumeMountsvolumesDeploymentYAML 中没有PVand 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
于 2020-02-18T14:27:22.440 回答
1

有办法实现它。您可以将卷挂载到 NAS 或使用磁盘创建存储集群,并为此创建持久卷和持久卷声明。如果您的用例是在本地存储中具有持久性,那么您可以在集群节点之一中创建一个本地存储存储类,并且该卷空间可以被集群中的任何 pod 使用。要创建本地存储存储类,请参阅此(https://kubernetes.io/blog/2019/04/04/kubernetes-1.14-local-persistent-volumes-ga/

于 2020-02-16T14:08:10.760 回答