Docker Swarm + Traefik - master not discovered when open ports 9200 and 9300

Once I open ports on es03 the elasticsearch node can't communicate with others.

Here is the compose file:

version: '3.9'

services:
  setup:
    image: ${ELASTIC_IMAGE}:${STACK_VERSION}
    networks:
      - esnet
    volumes:
      - ${ELASTIC_CONFIG_PATH}/certs:/usr/share/elasticsearch/config/certs
    deploy:
      # set to 0 to not run again
      replicas: 0
      placement:
        constraints:
          - node.hostname == test1
          
    user: "0"
    command: >
      bash -c '
        if [ x${ELASTIC_PASSWORD} == x ]; then
          echo "Set the ELASTIC_PASSWORD environment variable in the .env file";
          exit 1;
        elif [ x${KIBANA_PASSWORD} == x ]; then
          echo "Set the KIBANA_PASSWORD environment variable in the .env file";
          exit 1;
        fi;
        if [ ! -f config/certs/ca.zip ]; then
          echo "Creating CA";
          bin/elasticsearch-certutil ca --silent --pem -out config/certs/ca.zip;
          unzip config/certs/ca.zip -d config/certs;
        fi;


        if [ ! -f config/certs/certs.zip ]; then
          echo "Creating certs";
          echo -ne \
          "instances:\n"\
          "  - name: es01\n"\
          "    dns:\n"\
          "      - es01\n"\
          "    ip:\n"\
          "      - 169.42.xxx.1\n"\
          "  - name: es02\n"\
          "    dns:\n"\
          "      - es02\n"\
          "    ip:\n"\
          "      - 169.42.xxx.2\n"\
          "  - name: es03\n"\
          "    dns:\n"\
          "      - es03\n"\
          "    ip:\n"\
          "      - 169.42.xxx.3\n"\
          "  - name: kibana\n"\
          "    dns:\n"\
          "      - kibana\n"\
          "      - localhost\n"\
          "    ip:\n"\
          "      - 127.0.0.1\n"\
          > config/certs/instances.yml;
      
          bin/elasticsearch-certutil cert --silent --pem -out config/certs/certs.zip --in config/certs/instances.yml --ca-cert config/certs/ca/ca.crt --ca-key config/certs/ca/ca.key
          unzip config/certs/certs.zip -d config/certs;
        fi;
        echo "Setting file permissions"
        chown -R root:root config/certs;
        find . -type d -exec chmod 750 \{\} \;;
        find . -type f -exec chmod 640 \{\} \;;
        echo "Waiting for Elasticsearch availability";
        until curl -s --cacert config/certs/ca/ca.crt https://es01:9200 | grep -q "missing authentication credentials"; do sleep 30; done;
        echo "Setting kibana_system password ${KIBANA_PASSWORD} with elastic ${ELASTIC_PASSWORD} on es01";
        until curl -s -X POST --cacert config/certs/ca/ca.crt -u "elastic:${ELASTIC_PASSWORD}" -H "Content-Type: application/json" https://es01:9200/_security/user/kibana_system/_password -d "{\"password\":\"${KIBANA_PASSWORD}\"}" | grep -q "^{}"; do sleep 3; done;

        echo "All done!";
      '
    healthcheck:
      test: ["CMD-SHELL", "[ -f config/certs/es01/es01.crt ]"]
      interval: 1s
      timeout: 5s
      retries: 120

  es01:
    image: ${ELASTIC_IMAGE}:${STACK_VERSION}
    networks:
      - traefik_proxy
      - esnet
    environment:
      - node.name=es01
      - cluster.name=${CLUSTER_NAME}
      - cluster.initial_master_nodes=es01,es02,es03
      - discovery.seed_hosts=es02,es03
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms2g -Xmx2g"
      # SSL/TLS Configuration
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=true
      - xpack.security.http.ssl.key=certs/es01/es01.key
      - xpack.security.http.ssl.certificate=certs/es01/es01.crt
      - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.enabled=true
      - xpack.security.transport.ssl.key=certs/es01/es01.key
      - xpack.security.transport.ssl.certificate=certs/es01/es01.crt
      - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.verification_mode=certificate
      # - xpack.security.transport.ssl.verification_mode=none
      # - xpack.security.enrollment.enabled=true
      - xpack.license.self_generated.type=basic
      - network.host=0.0.0.0
      - ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - ${ELASTIC_DATA_PATH}:/usr/share/elasticsearch/data
      - ${ELASTIC_CONFIG_PATH}/certs:/usr/share/elasticsearch/config/certs
    deploy:
      # replicas: 0
      placement:
        constraints:
          - node.hostname == test1
      labels:
        - traefik.enable=true
        - traefik.docker.network=traefik_proxy
        - traefik.http.routers.es01.rule=Host(`es01.${ELASTIC_BASE_DOMAIN}`)
        - traefik.http.services.es01.loadbalancer.server.port=9200
        - traefik.http.services.es01.loadbalancer.server.scheme=https
        - traefik.http.routers.es01.entrypoints=websecure
        - traefik.http.routers.es01.tls.certresolver=le

  es02:
    image: ${ELASTIC_IMAGE}:${STACK_VERSION}
    networks:
      - traefik_proxy
      - esnet
    environment:
      - node.name=es02
      - cluster.name=${CLUSTER_NAME}
      - cluster.initial_master_nodes=es01,es02,es03
      - discovery.seed_hosts=es01,es03
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms2g -Xmx2g"
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=true
      - xpack.security.http.ssl.key=certs/es02/es02.key
      - xpack.security.http.ssl.certificate=certs/es02/es02.crt
      - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.enabled=true
      - xpack.security.transport.ssl.key=certs/es02/es02.key
      - xpack.security.transport.ssl.certificate=certs/es02/es02.crt
      - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.verification_mode=certificate
      # - xpack.security.transport.ssl.verification_mode=none
      - xpack.security.enrollment.enabled=true
      - xpack.license.self_generated.type=basic
      - network.host=0.0.0.0
      - ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - ${ELASTIC_DATA_PATH}:/usr/share/elasticsearch/data
      - ${ELASTIC_CONFIG_PATH}/certs:/usr/share/elasticsearch/config/certs
    deploy:
      # replicas: 0
      placement:
        constraints:
          - node.hostname == test2
      labels:
        - traefik.enable=true
        - traefik.docker.network=traefik_proxy
        - traefik.http.routers.es02.rule=Host(`es02.${ELASTIC_BASE_DOMAIN}`)
        - traefik.http.services.es02.loadbalancer.server.port=9200
        - traefik.http.services.es02.loadbalancer.server.scheme=https
        - traefik.http.routers.es02.entrypoints=websecure
        - traefik.http.routers.es02.tls.certresolver=le

      
  es03:
    image: ${ELASTIC_IMAGE}:${STACK_VERSION}
    ports:
      - 9200:9200
      - 9300:9300
    networks:
      - traefik_proxy
      - esnet
    environment:
      - node.name=es03
      - cluster.name=${CLUSTER_NAME}
      - cluster.initial_master_nodes=es01,es02,es03
      - discovery.seed_hosts=es01,es02
      - bootstrap.memory_lock=true
      - "ES_JAVA_OPTS=-Xms2g -Xmx2g"
      - xpack.security.enabled=true
      - xpack.security.http.ssl.enabled=true
      - xpack.security.http.ssl.key=certs/es03/es03.key
      - xpack.security.http.ssl.certificate=certs/es03/es03.crt
      - xpack.security.http.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.enabled=true
      - xpack.security.transport.ssl.key=certs/es03/es03.key
      - xpack.security.transport.ssl.certificate=certs/es03/es03.crt
      - xpack.security.transport.ssl.certificate_authorities=certs/ca/ca.crt
      - xpack.security.transport.ssl.verification_mode=certificate
      # - xpack.security.transport.ssl.verification_mode=none
      - xpack.security.enrollment.enabled=true
      - network.host=0.0.0.0
      - xpack.license.self_generated.type=basic
      - ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
    ulimits:
      memlock:
        soft: -1
        hard: -1
    volumes:
      - ${ELASTIC_DATA_PATH}:/usr/share/elasticsearch/data
      - ${ELASTIC_CONFIG_PATH}/certs:/usr/share/elasticsearch/config/certs
    deploy:
      # replicas: 0
      placement:
        constraints:
          - node.hostname == test3
      labels:
        - traefik.enable=true
        - traefik.docker.network=traefik_proxy
        - traefik.http.routers.es03.rule=Host(`es03.${ELASTIC_BASE_DOMAIN}`)
        - traefik.http.services.es03.loadbalancer.server.port=9200
        - traefik.http.services.es03.loadbalancer.server.scheme=https
        - traefik.http.routers.es03.entrypoints=websecure
        - traefik.http.routers.es03.tls.certresolver=le

  kibana:
    image: ${KIBANA_IMAGE}:${STACK_VERSION}
    networks:
      - traefik_proxy
      - esnet
    ports:
      - 5601:5601
    environment:
      - SERVERNAME=kibana
      - SERVER_PUBLICBASEURL=https://${KIBANA_HOST}
      # - ELASTICSEARCH_HOSTS=${ELASTIC_HOSTS}
      - ELASTICSEARCH_HOSTS=https://es01:9200
      - ELASTICSEARCH_USERNAME=${KIBANA_USERNAME}
      - ELASTICSEARCH_PASSWORD=${KIBANA_PASSWORD} 
      # SSL/TLS Configuration
      - ELASTICSEARCH_SSL_CERTIFICATEAUTHORITIES=config/certs/ca/ca.crt
      - XPACK_SECURITY_ENCRYPTIONKEY=${XPACK_SECURITY_ENCRYPTIONKEY}
      - XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY=${XPACK_ENCRYPTEDSAVEDOBJECTS_ENCRYPTIONKEY}
      - XPACK_REPORTING_ENCRYPTIONKEY=${XPACK_REPORTING_ENCRYPTIONKEY}
      # other
      - XPACK_REPORTING_ROLES_ENABLED=false
      - xpack.reporting.roles.enabled=false
      - xpack.reporting.queue.timeout=10m
      # - xpack.monitoring.elasticsearch.ssl.verificationMode=none
    volumes:
      - kibanadata:/usr/share/kibana/data
      - ${ELASTIC_CONFIG_PATH}/certs:/usr/share/kibana/config/certs
    deploy:
      # replicas: 0
      placement:
        constraints:
          - node.hostname == test1
      labels:
        - traefik.enable=true
        - traefik.docker.network=traefik_proxy
        - traefik.http.routers.kibana.rule=Host(`${KIBANA_HOST}`)
        - traefik.http.services.kibana.loadbalancer.server.port=5601
        - traefik.http.routers.kibana.entrypoints=websecure

  metricbeat:
    user: "0"
    image: ${BEATS_IMAGE}:${STACK_VERSION}
    networks:
      - traefik_proxy
      - esnet
    labels:
      co.elastic.logs/module: metricbeat
    deploy:
      # replicas: 0
      placement:
        constraints:
          - node.hostname == test1
      resources:
        limits:
          memory: 1gb
    environment:
      - SERVERNAME=metricbeat
      - ELASTIC_HOSTS=${ELASTIC_HOSTS}
      - ELASTIC_USERNAME=${ELASTIC_USERNAME}
      - ELASTIC_PASSWORD=${ELASTIC_PASSWORD}
      - KIBANA_HOSTS=${KIBANA_HOSTS}
      - KIBANA_USERNAME=${KIBANA_USERNAME}
      - KIBANA_PASSWORD=${KIBANA_PASSWORD}
      - CA_CERT=config/certs/ca/ca.crt
      # - xpack.security.transport.ssl.verification_mode=none
    volumes:
      - metricbeatdata:/usr/share/metricbeat/data
      - ${ELASTIC_CONFIG_PATH}/metricbeat.yml:/usr/share/metricbeat/metricbeat.yml:ro
      - ${ELASTIC_CONFIG_PATH}/certs:/usr/share/metricbeat/config/certs
      - /var/run/docker.sock:/var/run/docker.sock:ro
      - /sys/fs/cgroup:/hostfs/sys/fs/cgroup:ro
      - /proc:/hostfs/proc:ro
      - /:/hostfs:ro


networks:
  traefik_proxy:
    external: true
  esnet:
    driver: overlay
    
volumes:
  kibanadata:
    driver: local
  metricbeatdata:
    driver: local

Here is the error:

Otherwise, the es01, es02 and kibana works fine. The subdomains works fine.

What's wrong?

Sorry, it's almost impossible to help using a screenshot of some fragment of a few logs. There's a troubleshooting guide in the manual that should help, or at least it tells you what you'd need to share in order to get a more useful answer here.

I found the issue - the ports need to be published in host mode

    ports:
      - target: 9200
        published: 9200
        protocol: tcp
        mode: host
      - target: 9300
        published: 9300
        protocol: tcp
        mode: host
1 Like