Reputation: 2121
I have a K8s deployment yaml definition where few env variables are declared. I want to declare an environment variable whose value to be referenced from a specific volume's mountpath.
For example, I want it as shown in below yaml from line #42 to #45. (Note: I have used it as an example only. I know it is not correct). Is it possible to achieve this and how?
37 spec:
38 containers:
39 - name: appplugin
40 image: {{APP_VERSION}}
41 env:
42 - name: INFRA_ACCESS_IP
43 valueFrom:
44 fieldRef:
45 fieldPath: status.hostIP
42 - name: LOG_BASE_DIR
43 valueFrom:
44 fieldRef:
45 fieldPath: volumeMounts.app-logs.mountPath.subPath
46 envFrom:
47 - configMapRef:
48 name: infra-config
49 - configMapRef:
50 name: core-config
51 ports:
52 - containerPort: 9095
53 volumeMounts:
54 - name: app-certs
55 mountPath: /etc/secrets/certs
56 readOnly: true
57 - name: app-logs
58 mountPath: /var/log/toHost/
59 subPath: app-logs
Upvotes: 0
Views: 3479
Reputation: 158812
Not directly. If you look at the API documentation for the EnvVarSource object, you can see that a limited number of fields are supported for the downward API; generally only the metadata fields, the service-account name, the dynamic IP and node information, and the resource limits.
In the context of the file you show, the file path is fixed and you don't need a dynamic lookup. Since each container has an isolated filesystem, it's a little unlikely you'll actually change this path in different deployments, and it will work to just specify that path directly:
env:
- name: LOG_BASE_DIR
value: app-logs
volumeMounts:
- name: app-logs
mountPath: /var/log/toHost/
subPath: app-logs
If you're using a templating tool like Helm you can make this value configurable at deploy time. Helm has the notion of "values" that are configurable at deployment time, and can inject those values (or do much more complex manipulation) when it installs things. You could use this to set the path inside the container if you had a reason to:
image: {{ .Values.appVersion }}
env:
- name: LOG_BASE_DIR
value: {{ .Values.logBaseDir | default "app-logs" }}
volumeMounts:
- name: app-logs
mountPath: /var/log/toHost/
subPath: {{ .Values.logBaseDir | default "app-logs" }}
(For logs specifically, it might be better to skip this configuration entirely and just send logs to your process's stdout. Then kubectl logs
can retrieve them later. You can also deploy a log collector as a DaemonSet that will capture these logs to some other system.)
Upvotes: 2