0

In Openshift when writing $HOSTNAME I do not get the Hostname/Pod name. However doing echo $HOSTNAME, then the pod name is returned.

I need to use $HOSTNAME in my Deployment.yaml as seen below but it does not work and I wonder how come $HOSTNAME doesn't exist?

kind: Deployment
apiVersion: apps/v1
metadata:
  namespace: my-namespace
  labels:
    app: my-service
spec:
  selector:
    matchLabels:
      ...
  template:
    metadata:
      ...
    spec:
      containers:
        - name: my-service
          image: 'myservice.myrepo.com/my-service:latest'
          env:
            - name: USERS
              value: >-
                -javaagent:/opt/project/javaagent.jar
                -Users.exist=true
                -Users.first=John
                -Users.specialone=$(HOSTNAME)
                ...

I tried using $(echo $HOSTNAME) in deployment yaml but it does not work. I believe there is also ways to achieve this result but i.e. Python commands to get the $HOSTNAME but I wish for a more simplified method.

SEBDA
  • 11
  • 2
  • I'm pretty sure you can't use `$(VARIABLE)` references at that particular point; but at the same time, the actual per-pod hostname will be random and it doesn't make sense to use it as a ConfigMap key. What's actually in the ConfigMap? – David Maze Dec 11 '22 at 11:48
  • @DavidMaze Hello. I updated the YAML. Not to use it as the ConfigMap key. That was my mistake. But the `$HOSTNAME` via POD terminal appearantly should be accessable just by typing `hostname` in terminal but it doesnt appear until i type `echo $HOSTNAME`. The point is to be able to access the hostname/pod name as in the yaml above. – SEBDA Dec 11 '22 at 11:58
  • 1
    Are you trying to [Programmatically get the name of the pod that a container belongs to in Kubernetes?](https://stackoverflow.com/questions/37253068/programmatically-get-the-name-of-the-pod-that-a-container-belongs-to-in-kubernet) If you inject the Pod name using the downward API as described there, then you can use that variable in other variables. – David Maze Dec 11 '22 at 12:04
  • @DavidMaze I believe there are no ways to make this possible without scripts or other APIs? – SEBDA Dec 11 '22 at 12:16
  • The question I linked to in the previous comment can get you a `POD_NAME` environment variable, which you can then include using the syntax you show here. It wouldn't surprise me if `$HOSTNAME` is special here. – David Maze Dec 11 '22 at 18:05
  • @DavidMaze And how to access MY_POD_NAME variable inside that string? By `$(MY_POD_NAME)` so `-Users.specialone=$(MY_POD_NAME)`? – SEBDA Dec 11 '22 at 19:44
  • Correct. Since it's a "normal" environment variable that Kubernetes knows about, it can use the Kubernetes variable interpolation syntax. – David Maze Dec 11 '22 at 21:46

0 Answers0