简体   繁体   中英

Creating "hostPath" volume mount dynamically

I have a use case where I need to mount the hostPath to a Pod, now the use case is to mount the container environment's sock file and as in the newer version of kubernetes, we have containerd instead of docker, I want to make this socket mounting dynamic, ie check if docker socket file exists, mount that otherwise check for containerd socket file and mount that.

currently, I have this

spec:
      volumes:
      - name: ctd-sock
        hostPath:
        path: "/run/containerd/containerd1.sock"
        type: Socket

I am not sure how to make it dynamic. I checked the Official Documentation , didn't find anything there.

I don't think there is a way to conditionally mount a volume using only the pod spec, like you found. Well there is type: FileOrCreate which is sort of conditional, but I don't think that would apply in your case because it is a socket.

However, if you're willing to do some shell scripting, you might be able to dynamically create your yaml based on what container runtime you detect.

For example:

cr=$(kubectl get node -o=jsonpath={.items[0].status.nodeInfo.containerRuntimeVersion})
if [[ $cr =~ "containerd" ]]; then 
  echo 'Cluster uses containerd'
  # TODO: Create your pod with containerd socket
elif [[ $cr =~ "docker" ]]; then
  echo 'Cluster uses docker'
  # TODO: Create your pod with docker socket
else 
  echo 'Cluster uses something else'
  # TODO: Handle however you want
fi

Maybe this isn't the pure Kubernetes answer you were hoping for, but practically, it might be your best bet.

If you are looking for something more advanced, you could create a CRD and controller for this, and instead of creating a pod directly, you would create your CRD. Then, in your controller, you would make it inspect the cluster and create a pod the way you need it based on the cluster's container runtime.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM