Say I have the following pod spec.
apiVersion: apps/v1beta1
kind: Deployment
metadata:
# Unique key of the Deployment instance
name: deployment-example
spec:
# 3 Pods should exist at all times.
replicas: 3
template:
metadata:
labels:
# Apply this label to pods and default
# the Deployment label selector to this value
app: nginx
spec:
containers:
- name: nginx
# Run this image
image: nginx:1.10
Here, the name of the container is nginx
. Is there a way to get the "nginx" string from within the running container?
I mean, once I exec into the container with something like
kubectl exec -it <pod-name> -c nginx bash
Is there a programmatic way to get to the given container name in the pod spec ?
Note that this is not necessarily the docker container name that gets printed in docker ps
. Kubernetes composes a longer name for the spawned docker container.
The downward api looks promising in this regard. However container name
is not mentioned in the Capabilities of the Downward API
section.
How about using the container hostname then chopping off the generated components?
$ kubectl exec alpine-tools-645f786645-vfp82 hostname | cut -d- -f1,2
alpine-tools
Although this is very dependent on how you name Pods/containers..
$ kubectl exec -it alpine-tools-645f786645-vfp82 /bin/sh
/ # hostname
alpine-tools-645f786645-vfp82
/ # hostname | cut -d- -f1,2
alpine-tools
The container name is not available trough the downward api. You can use yaml anchors and aliases (references). Unfortunately they are not scoped so you will have to come up with unique names for the anchors - it does not matter what they are as they are not present in the parsed document.
Subsequent occurrences of a previously serialized node are presented as alias nodes. The first occurrence of the node must be marked by an anchor to allow subsequent occurrences to be presented as alias nodes.
An alias node is denoted by the “*” indicator. The alias refers to the most recent preceding node having the same anchor. It is an error for an alias node to use an anchor that does not previously occur in the document. It is not an error to specify an anchor that is not used by any alias node.
First occurrence: &anchor Foo Second occurrence: *anchor Override anchor: &anchor Bar Reuse anchor: *anchor
Here is a full working example:
apiVersion: v1
kind: Pod
metadata:
name: reftest
spec:
containers:
- name: &container1name first
image: nginx:1.10
env:
- name: MY_CONTAINER_NAME
value: *container1name
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: &container2name second
image: nginx:1.10
env:
- name: MY_CONTAINER_NAME
value: *container2name
- name: MY_POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
Not sure container name within the deployment is available somehow.
For the deployment name, one way that works in OpenShift for deployment configs (and so presumably Kubernetes deployments), is to take the value of the HOSTNAME
environment variable, which will be of the form <deployment-name>-<deployment-number>-<random-string>
.
Drop from second last -
onwards and the lead component is the deployment name.
Would be a fair bit of mucking around, but one could maybe then infer the container name somehow by querying the REST API for deployment resource object based on that deployment name.
What specifically are you after the container name for? If knew what you need it for, may be able to suggest other options.