我们准备部署6个容器,分别安装3个zookeeper和3个Kafka,总共6个服务下表为各个服务的IP和端口映射
容器名称 | 容器IP | 容器内端口 | 容器外端口 |
---|---|---|---|
zoo1 | 172.19.0.11 | 2181 | 2184 |
zoo2 | 172.19.0.12 | 2181 | 2185 |
zoo3 | 172.19.0.13 | 2181 | 2186 |
kafka1 | 172.19.0.14 | 9092 | 9092 |
kafka2 | 172.19.0.15 | 9093 | 9093 |
kafka3 | 172.19.0.16 | 9094 | 9094 |
FinalShell是一体化的的服务器,网络管理软件,不仅是ssh客户端,还是功能强大的开发,运维工具,充分满足开发,运维需求。
下载地址: http://www.hostbuf.com
移除旧版本的Docker
$ sudo yum remove docker \
docker-client \
docker-client-latest \
docker-common \
docker-latest \
docker-latest-logrotate \
docker-logrotate \
docker-selinux \
docker-engine-selinux \
docker-engine
安装一些必要的系统工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
更新 yum 缓存
sudo yum makecache fast
安装 Docker-ce
sudo yum -y install docker-ce
启动 Docker 后台服务
sudo systemctl start docker
测试运行 hello-world
docker run hello-world
由于国内网络问题,拉取Docker镜像十分缓慢,我们可以通过更改镜像地址的方式来解决,我们使用的是华为云所提供的镜像地址
https://5cs1uaxp.mirror.swr.myhuaweicloud.com
找到daemon配置文件,找不到就创建一个
sudo mkdir -p /etc/docker
向daemon配置文件插入加速器地址
sudo tee /etc/docker/daemon.json <<-'EOF'
{
"registry-mirrors": ["https://5cs1uaxp.mirror.swr.myhuaweicloud.com"]
}
EOF
加载配置文件
sudo systemctl daemon-reload
重启docker服务
sudo systemctl restart docker
到此docker的镜像拉取地址已经被我们修改为华为云提供的镜像地址了
我们这里需要拉取的镜像有两个
第一个是zookeeper
docker pull wurstmeister/zookeeper
第二个是kafka
docker pull wurstmeister/kafka
拉取完成后可以通过命令,查看刚才所拉取的镜像
docker images
这里我们用到的服务编排工具是docker-compose,通过该工具能够大幅降低我们配置的成本
下载Docker Compose的当前稳定版本
sudo curl -L "https://github.com/docker/compose/releases/download/1.24.0/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
对二进制文件应用可执行权限
sudo chmod +x /usr/local/bin/docker-compose
测试安装是否成功,从下图可以看到docker-compose当前的版本号和权限状态
docker-compose --version
创建网络环境
docker network create --subnet=172.19.0.0/24 br17219
查看添加状况,从下图可以看到网络已经添加成功了
docker ntework ls
创建一个目录,刷新后可以看到刚刚创建的目录结构
mkdir -p /data/program/kafka
在刚刚创建的目录下点击右键,新建文件,命名为:docker-compose.yml
双击新建的文件,进入编辑页面,开始编辑
如果使用的不是FinalShell就没有这个功能
在编辑框内编写服务编排脚本
version: '2'
services:
zoo1:
image: wurstmeister/zookeeper
restart: always
hostname: zoo1
container_name: zoo1
ports:
- 2184:2181
volumes:
- "/Users/shaozhipeng/Development/volume/zkcluster/zoo1/data:/data"
- "/Users/shaozhipeng/Development/volume/zkcluster/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:
br17219:
ipv4_address: 172.19.0.11
zoo2:
image: wurstmeister/zookeeper
restart: always
hostname: zoo2
container_name: zoo2
ports:
- 2185:2181
volumes:
- "/Users/shaozhipeng/Development/volume/zkcluster/zoo2/data:/data"
- "/Users/shaozhipeng/Development/volume/zkcluster/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:
br17219:
ipv4_address: 172.19.0.12
zoo3:
image: wurstmeister/zookeeper
restart: always
hostname: zoo3
container_name: zoo3
ports:
- 2186:2181
volumes:
- "/Users/shaozhipeng/Development/volume/zkcluster/zoo3/data:/data"
- "/Users/shaozhipeng/Development/volume/zkcluster/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:
br17219:
ipv4_address: 172.19.0.13
kafka1:
image: wurstmeister/kafka
restart: always
hostname: kafka1
container_name: kafka1
ports:
- 9092:9092
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka1
KAFKA_ADVERTISED_PORT: 9092
KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka1:9092
KAFKA_LISTENERS: PLAINTEXT://kafka1:9092
volumes:
- /Users/shaozhipeng/Development/volume/kfkluster/kafka1/logs:/kafka
external_links:
- zoo1
- zoo2
- zoo3
networks:
br17219:
ipv4_address: 172.19.0.14
kafka2:
image: wurstmeister/kafka
restart: always
hostname: kafka2
container_name: kafka2
ports:
- 9093:9093
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka2
KAFKA_ADVERTISED_PORT: 9093
KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka2:9093
KAFKA_LISTENERS: PLAINTEXT://kafka2:9093
volumes:
- /Users/shaozhipeng/Development/volume/kfkluster/kafka2/logs:/kafka
external_links:
- zoo1
- zoo2
- zoo3
networks:
br17219:
ipv4_address: 172.19.0.15
kafka3:
image: wurstmeister/kafka
restart: always
hostname: kafka3
container_name: kafka3
ports:
- 9094:9094
environment:
KAFKA_ADVERTISED_HOST_NAME: kafka3
KAFKA_ADVERTISED_PORT: 9094
KAFKA_ZOOKEEPER_CONNECT: zoo1:2181,zoo2:2181,zoo3:2181
KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://kafka3:9094
KAFKA_LISTENERS: PLAINTEXT://kafka3:9094
volumes:
- /Users/shaozhipeng/Development/volume/kfkluster/kafka3/logs:/kafka
external_links:
- zoo1
- zoo2
- zoo3
networks:
br17219:
ipv4_address: 172.19.0.16
networks:
br17219:
external:
name: br17219
将上面的脚本放入编辑框内,通过快捷键(Ctrl+S)保存脚本,脚本会自动将脚本保存到服务器上,当显示如下图的时候则说明已经保存成功了
移动到当前文件夹
cd /data/program/kafka
执行docker-compose的启动命令
docker-compose up -d
稍等一会(几秒钟吧),查看启动情况
docker ps