Use relative paths in Kubernetes config

4/16/2018

The goal is to orchestrate both production and local development environments using Kubernetes. The problem is that hostPath doesn't work with relative path values. This results in slightly differing configuration files on each developer's machine to accommodate for the different project locations (i.e. "/my/absolute/path/to/the/project"):

apiVersion: v1
kind: Service
metadata:
  name: some-service
  labels:
    app: app
spec:
  type: LoadBalancer
  ports:
  - port: 80
  selector:
    app: app
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: some-deploy
spec:
  selector:
    matchLabels:
      app: app
  replicas: 1
  template:
    metadata:
      labels:
        app: app
    spec:
      containers:
      - name: app
        image: nginx:1.13.12-alpine
        ports:
        - containerPort: 80
        volumeMounts:
        - name: vol_example
          mountPath: /var/www/html
      volumes:
        - name: vol_example
          hostPath:
            path: "/my/absolute/path/to/the/project"
            type: Directory

How can relative paths be used in Kubernetes config files? Variable replacements (such as $(PWD)/project) have been tried, but didn't seem to work. If config variables can work with volumes, this might help but unsure of how to achieve this.

-- willsquire
kubernetes

1 Answer

4/17/2018

As mentioned here kubectl will never support variable substitution.

You can create a helm chart for your app (yaml). It supports yaml template variables (among various other features). So you'll be able to pass hostPath parameter based on development or production.

-- bits
Source: StackOverflow