Reputation: 6595
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.
Upvotes: 33
Views: 15648
Reputation: 181
Not a native Kubernetes solution but you can manually edit the .yaml
file 'on-the-fly' before applying it with kubectl
.
In your .yaml
file use a substitution that is not likely to become ambiguous in the volume
section:
volumes:
- name: vol_example
hostPath:
path: {{path}}/relative/path
type: Directory
Then to apply the manifest run:
cat deployment.yaml | sed s+{{path}}+$(pwd)+g | kubectl apply -f -
Note: sed
is used with the separator +
because $(pwd)
will result in a path that includes one or more /
which is the conventional sed
separator.
Upvotes: 2