Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use relative paths in Kubernetes config

Tags:

kubernetes

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.

like image 665
Will Squire Avatar asked Apr 16 '18 22:04

Will Squire


People also ask

How do you specify relative paths?

Relative paths make use of two special symbols, a dot (.) and a double-dot (..), which translate into the current directory and the parent directory. Double dots are used for moving up in the hierarchy. A single dot represents the current directory itself.

How do you use absolute and relative paths?

An absolute path is defined as specifying the location of a file or directory from the root directory(/). In other words,we can say that an absolute path is a complete path from start of actual file system from / directory. Relative path is defined as the path related to the present working directly(pwd).

What is Mountpath in Kubernetes?

The mount path is always the destination inside the Pod a volume gets mounted to. I think the documentation is pretty clear on what hostPath does: A hostPath volume mounts a file or directory from the host node's filesystem into your Pod.

Does chdir work with relative path?

Yes. The current working directory is a property of the process.


1 Answers

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.

like image 149
bits Avatar answered Oct 10 '22 10:10

bits