最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kubernetes - how to manually assign pvcs in the node wise pods in kubernets? - Stack Overflow

programmeradmin6浏览0评论

So i try to set the manually assign Pvcs in the node wise Pods in kubernets So i have one Statefulset with 2 replicas and 2 PVCs The code is like this

      apiVersion: v1
  kind: PersistentVolumeClaim
  metadata:
    name: busybox-pvc-0
  spec:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 1Gi
    storageClassName: standard  # Change this if needed

  ---
  apiVersion: v1
  kind: PersistentVolumeClaim
  metadata:
    name: busybox-pvc-1
  spec:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 1Gi
    storageClassName: standard  # Change this if needed
  ---
  apiVersion: apps/v1
  kind: StatefulSet
  metadata:
    name: busybox
  spec:
    serviceName: "busybox"
    replicas: 2
    selector:
      matchLabels:
        app: busybox
    template:
      metadata:
        labels:
          app: busybox
      spec:
        containers:
          - name: busybox
            image: busybox
            command: ["/bin/sh", "-c", "sleep 3600"]
            volumeMounts:
              - name: busybox-storage-0
                mountPath: /data0
              - name: busybox-storage-1
                mountPath: /data1
        affinity:
          nodeAffinity:
            requiredDuringSchedulingIgnoredDuringExecution:
              nodeSelectorTerms:
                - matchExpressions:
                    - key: kubernetes.io/hostname
                      operator: In
                      values:
                        - minikube
                        - minikube-m02
        volumes:
          - name: busybox-storage-0
            persistentVolumeClaim:
              claimName: busybox-pvc-0
          - name: busybox-storage-1
            persistentVolumeClaim:
              claimName: busybox-pvc-1

So i try to do this code and see the reaction of this code the Output is

busybox-0 → minikube → busybox-pvc-0 , busybox-pvc-1 ,
busybox-1 → minikube-m02 → busybox-pvc-0 , busybox-pvc-1

but i want to this Output :

busybox-0 → minikube → busybox-pvc-0 ,
busybox-1 → minikube-m02 → busybox-pvc-1

in this outputs

Pods is busybox-0 , busybox-1 and Nodes is minikube , minikube-m02 and PVCs is busybox-pvc-0 , busybox-pvc-1

So i try to set the manually assign Pvcs in the node wise Pods in kubernets So i have one Statefulset with 2 replicas and 2 PVCs The code is like this

      apiVersion: v1
  kind: PersistentVolumeClaim
  metadata:
    name: busybox-pvc-0
  spec:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 1Gi
    storageClassName: standard  # Change this if needed

  ---
  apiVersion: v1
  kind: PersistentVolumeClaim
  metadata:
    name: busybox-pvc-1
  spec:
    accessModes:
      - ReadWriteOnce
    resources:
      requests:
        storage: 1Gi
    storageClassName: standard  # Change this if needed
  ---
  apiVersion: apps/v1
  kind: StatefulSet
  metadata:
    name: busybox
  spec:
    serviceName: "busybox"
    replicas: 2
    selector:
      matchLabels:
        app: busybox
    template:
      metadata:
        labels:
          app: busybox
      spec:
        containers:
          - name: busybox
            image: busybox
            command: ["/bin/sh", "-c", "sleep 3600"]
            volumeMounts:
              - name: busybox-storage-0
                mountPath: /data0
              - name: busybox-storage-1
                mountPath: /data1
        affinity:
          nodeAffinity:
            requiredDuringSchedulingIgnoredDuringExecution:
              nodeSelectorTerms:
                - matchExpressions:
                    - key: kubernetes.io/hostname
                      operator: In
                      values:
                        - minikube
                        - minikube-m02
        volumes:
          - name: busybox-storage-0
            persistentVolumeClaim:
              claimName: busybox-pvc-0
          - name: busybox-storage-1
            persistentVolumeClaim:
              claimName: busybox-pvc-1

So i try to do this code and see the reaction of this code the Output is

busybox-0 → minikube → busybox-pvc-0 , busybox-pvc-1 ,
busybox-1 → minikube-m02 → busybox-pvc-0 , busybox-pvc-1

but i want to this Output :

busybox-0 → minikube → busybox-pvc-0 ,
busybox-1 → minikube-m02 → busybox-pvc-1

in this outputs

Pods is busybox-0 , busybox-1 and Nodes is minikube , minikube-m02 and PVCs is busybox-pvc-0 , busybox-pvc-1

Share Improve this question asked Mar 21 at 6:25 om makwanaom makwana 91 bronze badge 4
  • You'd normally use the StatefulSet's volumeClaimTemplates: to automatically create the PVCs; you wouldn't normally create them by hand. That won't help you manually place Pods or storage on specific nodes, but that's also not a typical pattern; why do you want more specific placement? – David Maze Commented Mar 21 at 10:06
  • Thankyou for reply me @DavidMaze , so i have a 15 statefulset with many replicas, this replicas / pods are set in my 10 nodes and my all PVCs are set with ReadWriteOnce , so i have one Application who monitors my Statefulset use with nodes , so i dont want to changes in my application for differnt differnt statefulset so i want to use assign Pvcs in the node wise Pods in kubernets – om makwana Commented Mar 21 at 11:18
  • Usually the cluster can manage this on its own, though. In many environments PersistentVolumes can be moved between nodes, and the cluster manages this; if not, then the cluster will only schedule a Pod on a node where the storage is. Unless I had a really specific reason to do something different (maybe a shared NFS volume) I'd avoid manually creating PersistentVolumes/Claims. – David Maze Commented Mar 21 at 11:26
  • Thankyou for reply me @DavidMaze , But i have an reason i dont want to again and again changes in my application so i try to do this manually assign Pvcs in the node wise Pods so there are access any pod use with nodes – om makwana Commented Mar 21 at 11:53
Add a comment  | 

1 Answer 1

Reset to default 0

You've defined only one container, to be ran as 2 replicas.

 spec:
    containers:
      - name: busybox

And you've attached both PVCs to that container

volumeMounts:
  - name: busybox-storage-0
    mountPath: /data0
  - name: busybox-storage-1
    mountPath: /data1

To achieve what you want, you should define them separately. Something like:

spec:
    containers:
        - name: busybox-0
          volumeMounts:
              - name: busybox-storage-0
                mountPath: /data0
        - name: busybox-1
          volumeMounts:
              - name: busybox-storage-1
                mountPath: /data0

Same goes for the node affinity

发布评论

评论列表(0)

  1. 暂无评论