skip to Main Content

My project needs to deploy a PV on the current master node, so this involves a resource scheduling problem. I use hostPath, so I must specify the scheduling to the current node.

apiVersion: v1
kind: PersistentVolume
metadata:
  name: local-pv-redis-0
spec:
  capacity:
    storage: 8Gi
  volumeMode: Filesystem
  accessModes:
  - ReadWriteOnce
  storageClassName: local-storage-redis
  persistentVolumeReclaimPolicy: Retain
  local:
    path: /home/lab/redis/0
  nodeAffinity:
    required:
      nodeSelectorTerms:
      - matchExpressions:
        - key: kubernetes.io/hostname
          operator: In
          values:
          - node1

But I need a common way to implement this method for any cluster. What I’m thinking of is getting the nodeName for the current node, which by default is the same as the hostname which can be obtained with the shell’s hostname.

But I want to do it in a more generic way to prevent it from being overridden by --hostname-override.

I would also like to be able to make the host directory settings more generic, But kss does not seem to support relative paths and the ${PWD} setting

I would appreciate it if you could tell me how to solve these two problems which include resource scheduling and mount directory questions?

2

Answers


  1. Chosen as BEST ANSWER

    envsubst < redis.yaml | kubectl apply -f -

    # redis.yaml
    apiVersion: apps/v1
    kind: List
    items:
    - kind: StorageClass
      apiVersion: storage.k8s.io/v1
      metadata:
        name: local-storage-redis
        namespace: test-system
      provisioner: kubernetes.io/no-provisioner
      volumeBindingMode: WaitForFirstConsumer
    
    - apiVersion: v1
      kind: PersistentVolume
      metadata:
        name: local-pv-redis-0
        namespace: test-system
      spec:
        capacity:
          storage: 8Gi
        volumeMode: Filesystem
        accessModes:
        - ReadWriteOnce
        storageClassName: local-storage-redis
        persistentVolumeReclaimPolicy: Retain
        local:
          path: ${PWD}/redis-0
        nodeAffinity:
          required:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/hostname
                operator: In
                values:
                - ${HOST_NAME}
    

    That's how I did it.


  2. This is a community wiki answer posted for better visibility. Feel free to expand it.

    As already mentioned by moluzhui: the Node Affinity can be used in order to get it done.

    Node affinity is conceptually similar to nodeSelector — it allows
    you to constrain which nodes your pod is eligible to be scheduled on,
    based on labels on the node.

    There are currently two types of node affinity, called
    requiredDuringSchedulingIgnoredDuringExecution and
    preferredDuringSchedulingIgnoredDuringExecution. You can think of
    them as "hard" and "soft" respectively, in the sense that the former
    specifies rules that must be met for a pod to be scheduled onto a node
    (just like nodeSelector but using a more expressive syntax), while
    the latter specifies preferences that the scheduler will try to
    enforce but will not guarantee. The "IgnoredDuringExecution" part of
    the names means that, similar to how nodeSelector works, if labels
    on a node change at runtime such that the affinity rules on a pod are
    no longer met, the pod will still continue to run on the node. In the
    future we plan to offer
    requiredDuringSchedulingRequiredDuringExecution which will be just
    like requiredDuringSchedulingIgnoredDuringExecution except that it
    will evict pods from nodes that cease to satisfy the pods’ node
    affinity requirements.

    More details with some examples can be found in the linked docs.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search