Kubernetes: Choose volume dependent on namespace

12/6/2017

In a simple Postgres Deployment, I wish to choose the volume dependent on the namespace. The aim is to use the same Deployment configuration file to create Postgres deployments in different namespaces (e.g. production/staging).

What ways are there to achieve this?

Below my configuration file, I basically want to make MAKE_THIS_DEPENDENT_ON_NAMESPACE dependent on the environment (or namespace) this Deployment is used in.

kind: Deployment
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - image: postgres:9.6
          name: postgres
          volumeMounts:
            -name: postgres-storage
            mountPath: /var/lib/postgresql
      volumes:
        - name: postgres-persistent-storage
          gcePersistentDisk:
            pdName: MAKE_THIS_DEPENDENT_ON_NAMESPACE
-- hansonhill
kubernetes
postgresql

1 Answer

12/6/2017

You should try using a Persistent Volume Claim instead, PVCs are namespaced.

https://kubernetes.io/docs/concepts/storage/persistent-volumes/#claims-as-volumes

kind: Pod
apiVersion: v1
metadata:
  name: mypod
spec:
  containers:
    - name: myfrontend
      image: dockerfile/nginx
      volumeMounts:
      - mountPath: "/var/www/html"
        name: mypd
  volumes:
    - name: mypd
      persistentVolumeClaim:
        claimName: myclaim
-- jhernandez
Source: StackOverflow