docker版本
[root@localhost opt]# docker --version
Docker version 19.03.1, build 74b1e89
https://blog.csdn.net/qq_39680564/article/details/97920064
由于是给hadoop准备的,所以取名hadoop_net
docker network create --subnet=172.10.0.0/16 hadoop_net
查看网络
[root@localhost ~]# docker network ls
NETWORK ID NAME DRIVER SCOPE
d05f84a8746f bridge bridge local
398d73e5b1ed hadoop_net bridge local
da5e746c4d2e host host local
bfa3adcc971d none null local
docker run -d \
--add-host master:172.10.0.2 \
--add-host slave1:172.10.0.3 \
--add-host slave2:172.10.0.4 \
--net hadoop_net \
--ip 172.10.0.2 \
-h master \
-p 10022:22 \
-p 2181:2181 \
-p 2887:2888 \
-p 3887:3888 \
--restart always \
--name master \
192.168.0.20:5000/centos
docker run -d \
--add-host master:172.10.0.2 \
--add-host slave1:172.10.0.3 \
--add-host slave2:172.10.0.4 \
--net hadoop_net \
--ip 172.10.0.3 \
-h slave1 \
-p 20022:22 \
-p 2182:2181 \
-p 2888:2888 \
-p 3888:3888 \
--restart always \
--name slave1 \
192.168.0.20:5000/centos
docker run -d \
--add-host master:172.10.0.2 \
--add-host slave1:172.10.0.3 \
--add-host slave2:172.10.0.4 \
--net hadoop_net \
--ip 172.10.0.4 \
-h slave2 \
-p 30022:22 \
-p 2183:2181 \
-p 2889:2888 \
-p 3889:3888 \
--restart always \
--name slave2 \
192.168.0.20:5000/centos
开启三个终端,分别进入这三个容器
每台终端执行下面的命令,生成公共密钥
ssh-keygen -t rsa -P ''
每台终端创建authorized_keys文件
touch ~/.ssh/authorized_keys
将这三台服务器的id_rsa.pub内容复制到每台服务的authorized_keys内,类似下面格式
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQD4gNi3PuQybl3I7yZNISSB8KTmpsRs2oZ9sYqJCxMAVnUIFTtGzXQqJ8joG8gHj4t/QA5YwIYM3bW6WKky7L3wHWwqmutfCnSQIuttNE0r79fMV9JoEqMDyZu++9orIF6jfspe3h6sILSF8OrcMr0x03bpHmLc82AqeKsU5wcNZFnjzqAaJPPT71jxEgOJ5LxMvzxDAagsGnyHMIo9Be20xAgUID6W/gPpv+7AAKS6hflgqWqubgFerh/mNL0u4F17zter3ny6OHe8sugobDchOxHiNc+sxxgrwPPgOgkb3Wi3veAjHe7HJ3kDSOsaOhYkRW1aLU9F7oWDcGAtPVad root@master
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC+OOSahGWs6aAqEYtcqHiSwmvxHkYlLsiqpgcaoIMYkBxoE321q0uV2ZoWidWSnsuotX0kdwIxhS3/X29FkHOlVeViCdqCwMvkDfwugRVvQiMgfMnultkIEpGf91ARtCX2pCcFulXun0UOy7SOjZmKl7KZpPp3DGqftSR2ZpLiCdKAVIPMYuEQpWMbYGWwiWlwPRHvbsoJHGhFVuKlHoHSQ5uT4T+qz4kbOipJ9flTTSAGkHf4/wQyrOAO9Bw+1Hesr500r24JmFmuqwRzuXMFOki7d8wHiPSnM/thX2YuIAw0jogLVbD+UTa9pu0UhAKV3meEK4ljfRnqib/YOiIl root@slave1
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC69S37Fdx7TzOs0AMZmZn+/hUQuKBkYJye7sLsYfORnGAkLgtxov0aU6YgkpE1LARp5LKpi+xnf7aklnFdkmpauLU2lH+hWUd2py79d1fVYwvXrmNL2YKpJiAnlh3gTAgj61CWMAsp7tjiYpGgVSWR5wgFRoe7omxIi1c/JdsNujzfZEB2Eq7GgVOKc0+sro+/JNMR0Eh91iy9N64Xs7DAk2rpbO/TtRmRSTVVU8oc95Au/jIkaILp3XvsGjOyPsTCGHVuD+m2YTXawbVfyIntiNLYBT2Bm78mqyA9j0NbS2ihVJeOKdzys3mxh1yK94E7r3V1YmMlIVZSmp6yETrF root@slave2
ssh验证,已经不需要密码了
ssh master
ssh slave1
ssh slave2
将jdk1.8包复至容器中
docker cp jdk-1.8 master:/opt/
docker cp jdk-1.8 slave1:/opt/
docker cp jdk-1.8 slave2:/opt/
每个容器执行相同操作,确保每台服务器有Java环境
vi ~/.bashrc
追加以下内容
# java
export JAVA_HOME=/opt/jdk-1.8
export JRE_HOME=$JAVA_HOME/jre
export PATH=$JAVA_HOME/bin:$PATH
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
刷新生效
source ~/.bashrc
将zookeeper包复至容器中
docker cp zookeeper-3.4.10/ master:/opt/
docker cp zookeeper-3.4.10/ slave1:/opt/
docker cp zookeeper-3.4.10/ slave2:/opt/
每个容器执行相同操作
mkdir -p /data/zookeeper/data
mkdir -p /data/zookeeper/log
master执行
echo 1 > /data/zookeeper/data/myid
slave1执行
echo 2 > /data/zookeeper/data/myid
slave2执行
echo 3 > /data/zookeeper/data/myid
先将每台服服务器的zoo_sample.cfg文件改为zoo.cfg
每台服务器执行相同操作,编辑zoo.cfg文件
vim /opt/zookeeper-3.4.10/conf/zoo.cfg
追加以下内容
dataDir=/data/zookeeper/data
dataLogDir=/data/zookeeper/log
server.1=master:2887:3887
server.2=slave1:2888:3888
server.3=slave2:2889:3889
每个容器执行相同操作
vim ~/.bashrc
追加以下内容
# zookeeper
export ZOOKEEPER_HOME=/opt/zookeeper-3.4.10
export PATH=$PATH:$ZOOKEEPER_HOME/bin
刷新生效
source ~/.bashrc
启动
/opt/zookeeper-3.4.10/bin/zkServer.sh start
查看状态
/opt/zookeeper-3.4.10/bin/zkServer.sh status
保存镜像
docker commit master 192.168.0.20:5000/zookeeper:master
docker commit slave1 192.168.0.20:5000/zookeeper:slave1
docker commit slave2 192.168.0.20:5000/zookeeper:slave2
上传至镜像仓库
docker push 192.168.0.20:5000/zookeeper:slave2
docker push 192.168.0.20:5000/zookeeper:slave1
docker push 192.168.0.20:5000/zookeeper:master
启动方法
master
docker run -d \
--add-host master:172.10.0.2 \
--add-host slave1:172.10.0.3 \
--add-host slave2:172.10.0.4 \
--net hadoop_net \
--ip 172.10.0.2 \
-h master \
-p 10022:22 \
-p 2181:2181 \
-p 2887:2888 \
-p 3887:3888 \
--restart always \
--name master \
192.168.0.20:5000/zookeeper:master
slave1
docker run -d \
--add-host master:172.10.0.2 \
--add-host slave1:172.10.0.3 \
--add-host slave2:172.10.0.4 \
--net hadoop_net \
--ip 172.10.0.3 \
-h slave1 \
-p 20022:22 \
-p 2182:2181 \
-p 2888:2888 \
-p 3888:3888 \
--restart always \
--name slave1 \
192.168.0.20:5000/zookeeper:slave1
slave2
docker run -d \
--add-host master:172.10.0.2 \
--add-host slave1:172.10.0.3 \
--add-host slave2:172.10.0.4 \
--net hadoop_net \
--ip 172.10.0.4 \
-h slave2 \
-p 30022:22 \
-p 2183:2181 \
-p 2889:2888 \
-p 3889:3888 \
--restart always \
--name slave2 \
192.168.0.20:5000/zookeeper:slave2
https://blog.csdn.net/qq_39680564/article/details/89500281