Is inject container id or container name to app at start time is best pratice?

when doing health check by using consul, when app start and register, we need container id or container name run the heath check script in container, but our engineer says that this is not a best pratice? what is the offical point about this, is this best pratice? in my opion, inject container id/container name or other meta data to pod/container is normal, and so could be best pratice?

I am assuming you are referring to this methodology:

containers:
  - env:
    - name: NODE_NAME
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: spec.nodeName
    - name: POD_NAME
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.name
    - name: POD_NAMESPACE
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: metadata.namespace
    - name: POD_IP
      valueFrom:
        fieldRef:
          apiVersion: v1
          fieldPath: status.podIP

There is nothing wrong in using this (see docs) but I would reconsider the use of service discovery mechanisms on K8s, what you should use is k8s services (see docs)

this is about pod info, pod is difference from container.

thanks for the awesome information.

1 Like