安装docker

  1. yum install docker -y
  2. systemctl enable docker
  3. systemctl start docker

拉去zookeeper和kafka镜像

docker pull wurstmeister/zookeeper
docker pull wurstmeister/kafka

安装docker-compose编排工具

yum install docker-compose -y

mkdir -p /data/scripts/kakfa

配置kafka编排yaml文件

vim docker-compose.yml
version: '2'
services:
  zookeeper:
    image: wurstmeister/zookeeper
    volumes:
      - ./data:/data
    ports:
      - "2181:2181"

  kafka:
    image: wurstmeister/kafka
    restart: always
    hostname: kafka1
    privileged: true
    ports:
      - "9092:9092"
    environment:
      KAFKA_ADVERTISED_HOST_NAME: kafka1
      KAFKA_LISTENERS: PLAINTEXT://kafka1:9092
      KAKFA_ADVERTISED_LISTENERS: PLAINTEXT://kafka1:9092
      KAFKA_ZOOKEEPER_CONNECT: 192.168.0.15:2181
    volumes:
      - ./kafka-logs:/kafka
      - /var/run/docker.sock:/var/run/docker.sock

后台启动

docker-compose up -d

客户端测试

/etc/hosts 新增192.168.0.15 kafka1

创建kafka主题:

cd /opt/kafka_2.13-2.7.0
bin/kafka-topics.sh --create --zookeeper 192.168.0.15:2181 --replication-factor 1 --partitions 1 --topic test

显示kafka所有主题:

bin/kafka-topics.sh -list -zookeeper 192.168.0.15:2181

创建kafka生产者,输入数据:

bin/kafka-console-producer.sh --broker-list 192.168.0.15:9092 --topic test

image-20210310101415204-1615365263946.png
#创建kafka消费,消费数据:

bin/kafka-console-consumer.sh --bootstrap-server 192.168.0.15:9092 --topic test --from-beginning

image-20210310101431797-1615365258278.png