docker-compose部署zk集群、kafka集群以及kafka-manager,及其遇到的问题和解决


zk集群docker-compose.yml

1、新建网络

docker network create --driver bridge --subnet 172.23.0.0/25 --gateway 172.23.0.1  zookeeper_network

2、zk集群

version: '3.4'

services:
  zoo1:
    image: zookeeper
    restart: always
    hostname: zoo1
    container_name: zoo1
    ports:
    - 2181:2181
    volumes:
    - "./zoo1/data:/data"
    - "./zoo1/datalog:/datalog"
    environment:
      ZOO_MY_ID: 1
      ZOO_SERVERS: server.1=0.0.0.0:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888
    networks:
      default:
        ipv4_address: 172.23.0.11

  zoo2:
    image: zookeeper
    restart: always
    hostname: zoo2
    container_name: zoo2
    ports:
    - 2180:2181
    volumes:
    - "./zoo2/data:/data"
    - "./zoo2/datalog:/datalog"
    environment:
      ZOO_MY_ID: 2
      ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=0.0.0.0:2888:3888 server.3=zoo3:2888:3888
    networks:
      default:
        ipv4_address: 172.23.0.12

  zoo3:
    image: zookeeper
    restart: always
    hostname: zoo3
    container_name: zoo3
    ports:
    - 2179:2181
    volumes:
    - "./zoo3/data:/data"
    - "./zoo3/datalog:/datalog"
    environment:
      ZOO_MY_ID: 3
      ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=0.0.0.0:2888:3888
    networks:
      default:
        ipv4_address: 172.23.0.13

networks:
  default:
    external:
      name: zookeeper_network

3、kafka集群以及kafka-manager

version: '2'

services:
  kafka1:
    image: wurstmeister/kafka
    restart: always
    hostname: kafka1
    container_name: kafka1
    ports:
    - "9092:9092"
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.0.6:9092
      KAFKA_ADVERTISED_HOST_NAME: 192.168.0.6
      KAFKA_ADVERTISED_PORT: 9092
      KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
      JMX_PORT: 9999 
    volumes:
    - ./kafka1/logs:/kafka
    external_links:
    - zoo1
    - zoo2
    - zoo3
    networks:
      default:
        ipv4_address: 172.23.0.14

  kafka2:
    image: wurstmeister/kafka
    restart: always
    hostname: kafka2
    container_name: kafka2
    ports:
    - "9093:9092"
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.0.6:9093 
      KAFKA_ADVERTISED_HOST_NAME: 192.168.0.6
      KAFKA_ADVERTISED_PORT: 9093
      KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
      JMX_PORT: 9988
    volumes:
    - ./kafka2/logs:/kafka
    external_links:  # 连接本compose文件以外的container
    - zoo1
    - zoo2
    - zoo3
    networks:
      default:
        ipv4_address: 172.23.0.15

  kafka3:
    image: wurstmeister/kafka
    restart: always
    hostname: kafka3
    container_name: kafka3
    ports:
    - "9094:9092"
    environment:
      KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.0.6:9094
      KAFKA_ADVERTISED_HOST_NAME: 192.168.0.6
      KAFKA_ADVERTISED_PORT: 9094
      KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
      JMX_PORT: 9977
    volumes:
    - ./kafka3/logs:/kafka
    external_links:  # 连接本compose文件以外的container
    - zoo1
    - zoo2
    - zoo3
    networks:
      default:
        ipv4_address: 172.23.0.16

  kafka-manager:
    image: sheepkiller/kafka-manager:latest
    restart: always
    container_name: kafa-manager
    hostname: kafka-manager
    ports:
      - "9002:9000"
    links:            # 连接本compose文件创建的container
      - kafka1
      - kafka2
      - kafka3
    external_links:   # 连接本compose文件以外的container
      - zoo1
      - zoo2
      - zoo3
    environment:
      ZK_HOSTS: zoo1:2181,zoo2:2181,zoo3:2181
      KAFKA_BROKERS: kafka1:9092,kafka2:9093,kafka3:9094
      APPLICATION_SECRET: letmein
      KM_ARGS: -Djava.net.preferIPv4Stack=true
    networks:
      default:
        ipv4_address: 172.23.0.10

networks:
  default:
    external:   # 使用已创建的网络
      name: zookeeper_network

错误:

1、kafka使用external_links仍连不上zookeeper

INFO Opening socket connection to server zk2.zookeeper_default/172.22.0.3:2180. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)
docker-kafka     | [2019-01-19 05:22:45,206] WARN Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect (org.apache.zookeeper.ClientCnxn)
docker-kafka     | java.net.ConnectException: Connection refused
docker-kafka     |     at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
docker-kafka     |     at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:717)
docker-kafka     |     at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
docker-kafka     |     at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1141)
docker-kafka     | [2019-01-19 05:22:45,311] INFO Opening socket connection to server zk3.zookeeper_default/172.22.0.2:2179. Will not attempt to authenticate using SASL (unknown error) (org.apache.zookeeper.ClientCnxn)

解决办法:

两个docker-compose使用相同的网络

2、kafka节点相互连不上

kafka3    | [2019-01-19 05:49:11,086] WARN [Controller id=1001, targetBrokerId=1003] Connection to node 1003 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
kafka3    | [2019-01-19 05:49:11,146] WARN [Controller id=1001, targetBrokerId=1001] Connection to node 1001 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)
kafka3    | [2019-01-19 05:49:11,187] WARN [Controller id=1001, targetBrokerId=1003] Connection to node 1003 could not be established. Broker may not be available. (org.apache.kafka.clients.NetworkClient)

找了挺多靠谱和不靠谱的解决方案,都没能解决问题,但也给我提示。

这个问题无非就是kafka集群,这里是通过宿主机连接节点的,

  KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka3:9094
  KAFKA_ADVERTISED_HOST_NAME: kafka3

这两个配置可以配置宿主机的IP或者容器的hostname都可,但是有两点:1、端口需要暴露出来 2、并且可以通过宿主机访问进去
我是第二点被坑了,fire-wall开发端口,就没有上面这个错误了。

3、kafka-manager报jmx相关错误

然后点击链接开启jmx

于是就出现下面的错误

[error] k.m.j.KafkaJMX$ - Failed to connect to service:jmx:rmi:///jndi/rmi://9.11.8.48:-1/jmxrmi java.lang.IllegalArgumentException: requirement failed: No jmx port but jmx polling enabled!

 解决办法是:

在每一个kafka节点加上环境变量  JMX_PORT=端口

加上之后发现连不上,又是网络连接的问题,于是又把每个jmx端口暴露出来,然后fire-wall放行, 解决问题。

KAFKA_ADVERTISED_HOST_NAME: 

这个最好设置宿主机的ip,宿主机以外的代码或者工具来连接,后面的端口也需要设置暴露的端口。