需要镜像的下载
zookeeper
docker pull zookeeper:latest
kafka
docker pull wurstmeister/kafka:latest
docker-compose安装
下载
curl -L https://github.com/docker/compose/releases/download/1.23.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
测试版本
docker-compose --version
出现下面结果
docker-compose version 1.23.2, build 1110ad01
使用docker-compose启动集群
启动集群
COMPOSE_PROJECT_NAME=kafkatest docker-compose -f docker-compose.yml up -d
查看集群状态
COMPOSE_PROJECT_NAME=kafkatest docker-compose -f docker-compose.yml ps
docker-compose.yml
version: '2'
services:
zoo1:
image: zookeeper
restart: always
container_name: zoo1
ports:
- "2181:2181"
environment:
ZOO_MY_ID: 1
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888 server.4=zoo4:2888:3888:observer
zoo2:
image: zookeeper
restart: always
container_name: zoo2
ports:
- "2182:2181"
environment:
ZOO_MY_ID: 2
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888 server.4=zoo4:2888:3888:observer
zoo3:
image: zookeeper
restart: always
container_name: zoo3
ports:
- "2183:2181"
environment:
ZOO_MY_ID: 3
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888 server.4=zoo4:2888:3888:observer
zoo4:
image: zookeeper
restart: always
container_name: zoo4
ports:
- "2184:2181"
environment:
ZOO_MY_ID: 4
PEER_TYPE: observer
ZOO_SERVERS: server.1=zoo1:2888:3888 server.2=zoo2:2888:3888 server.3=zoo3:2888:3888 server.4=zoo4:2888:388:observer
broker1:
image: wurstmeister/kafka
restart: always
container_name: broker1
ports:
- "9091:9092"
depends_on:
- zoo1
- zoo2
- zoo3
- zoo4
environment:
KAFKA_BROKER_ID: 1
KAFKA_ADVERTISED_HOST_NAME: 192.168.204.128
KAFKA_ADVERTISED_PORT: 9091
KAFKA_HOST_NAME: 192.168.204.128
KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181,zoo4:2181
KAFKA_LISTENERS: PLAINTEXT://192.168.204.128:9091
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.204.128:9091
volumes:
- /var/run/docker.sock:/var/run/docker.sock
broker2:
image: wurstmeister/kafka
restart: always
container_name: broker2
ports:
- "9092:9092"
depends_on:
- zoo1
- zoo2
- zoo3
- zoo4
environment:
KAFKA_BROKER_ID: 2
KAFKA_ADVERTISED_HOST_NAME: 192.168.204.128
KAFKA_ADVERTISED_PORT: 9092
KAFKA_HOST_NAME: 192.168.204.128
KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181,zoo4:2181
KAFKA_LISTENERS: PLAINTEXT://192.168.204.128:9092
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.204.128:9092
volumes:
- /var/run/docker.sock:/var/run/docker.sock
broker3:
image: wurstmeister/kafka
restart: always
container_name: broker3
ports:
- "9093:9092"
depends_on:
- zoo1
- zoo2
- zoo3
- zoo4
environment:
KAFKA_BROKER_ID: 3
KAFKA_ADVERTISED_HOST_NAME: 192.168.204.128
KAFKA_ADVERTISED_PORT: 9093
KAFKA_HOST_NAME: 192.168.204.128
KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181,zoo4:2181
KAFKA_LISTENERS: PLAINTEXT://192.168.204.128:9093
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://192.168.204.128:9093
volumes:
- /var/run/docker.sock:/var/run/docker.sock