can I give hostname for each container in kubernetes

12/2/2021
agent {
        kubernetes {
            yaml """
spec:
  
  containers:
  - name: abc
    image: myrepo/abc:v1.0
    tty: true
    command:
     - cat
    resources:
      requests:
        memory: "16Gi"
        cpu: "8000m"
      limits:
        memory: "64Gi"
        cpu: "8000m"
    securityContext:
      runAsGroup: 500
      runAsUser: 500
  - name: mssqldb
    image: myrepo/mssql:2019-latest
    tty: true
    readinessProbe:
      initialDelaySeconds: 10
      failureThreshold: 10
      periodSeconds: 3
      tcpSocket:
        port: 1433
    securityContext:
      runAsUser: 11001
      runAsGroup: 0
    resources:
      requests:
        memory: "4Gi"
        cpu: "4000m"
      limits:
        memory: "8Gi"
        cpu: "8000m"
"""
            defaultContainer 'abc'
            
        }
    }

I am running this in my pipeline, but what I am trying to do is to have a hostname for each container, because container to be able to communicate with each other, plus I want to add another container as a client that will try to connect to the server which is the abc container. when I added hostname before containers part, i get same hostname for all the container which I don't really get, unless it is giving a hostname for the host luanching the containers (ps: when trying the hostname command it gives a random name that changes each time, unless I define the hostname)

-- anonymous
containers
docker
jenkins
kubernetes

1 Answer

12/2/2021

Containers in the same pod can use localhost to talk to each other

This is a fixed local address from within the pod, it will not change and does not have to be set

So in your example, the address "localhost:1443" could be used to talk to the container mssqldb from any of the other containers

-- Vorsprung
Source: StackOverflow