目的
有些项目没法通过本机启动多个端口号来模拟分布式集群,需要多台机器才能模拟的时候,不想用虚拟机或者不想购买机器的时候可以通过docker启动三个Centos来模拟分布式环境。
准备环境
docker即可
开始
创建docker网络
ini
docker network create --subnet=172.19.0.0/16 mynetwork
如果发生ip冲突,自行更换其他ip地址段,后续启动centos指定的ip需要在该地址段范围内。
启动三台centos7
shell
docker run -itd --name centos1 --net=mynetwork --ip 172.19.0.2 -v /home/loubens/project/docker/centos1:/var centos:7 /bin/bash
docker run -itd --name centos2 --net=mynetwork --ip 172.19.0.3 -v /home/loubens/project/docker/centos2:/var centos:7 /bin/bash
docker run -itd --name centos3 --net=mynetwork --ip 172.19.0.4 -v /home/loubens/project/docker/centos3:/var centos:7 /bin/bash
其中:
- -v参数可自定义挂载需求
- -ip需要在前面指定的ip地址段内
- -itd必须带上,否则会自动退出
查看是否启动成功
shell
docker ps
示意图:
配置centos环境
以容器centos1为例,其他容器需要执行相同的操作
- 进入centos
bash
docker exec -it centos1 /bin/bash
- 配置yum源
bash
# 删除之前的repo文件
rm -rf /etc/yum.repos.d/*
# 新建一个repo文件
vi /etc/yum.repos.d/Centos-Base.repo
- 将如下内容粘贴进去(源自阿里云镜像提供的repo文件),复制后不要急着退出
ini
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client. You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#
[base]
name=CentOS-$releasever - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/os/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#released updates
[updates]
name=CentOS-$releasever - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/updates/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/extras/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/centosplus/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
#contrib - packages by Centos Users
[contrib]
name=CentOS-$releasever - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/$releasever/contrib/$basearch/
http://mirrors.aliyuncs.com/centos/$releasever/contrib/$basearch/
http://mirrors.cloud.aliyuncs.com/centos/$releasever/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
- 由于docker创建的centos会存在着无法识别
$releasever
参数的情况,因此需要更换为自己的centos版本,比如我的版本是7,vi编辑的时候在普通模式下(按Esc)输入如下内容然后enter,把$releasever
全部更换为7即可。
ruby
:%s/\$releasever/7/g
- 直接无脑复制如下命令,安装基本环境
python
yum clean all
yum makecache
yum install -y openssh-server vim lrzsz wget gcc-c++ pcre pcre-devel zlib zlib-devel ruby openssl openssl-devel patch bash-completion zlib.i686 libstdc++.i686 lsof unzip zip
yum install -y openssh*
yum install -y net-tools
yum install -y initscripts
-
修改密码
passwd root
-
配置ssh,去掉一些必要的#号(Port、ListenAddress、PermitRootLogin前的#号)
bash
vim /etc/ssh/sshd_config
- 无脑复制
bash
mkdir -p /var/run/sshd
ssh-keygen -q -t rsa -b 2048 -f /etc/ssh/ssh_host_rsa_key -N ''
ssh-keygen -q -t ecdsa -f /etc/ssh/ssh_host_ecdsa_key -N ''
ssh-keygen -t dsa -f /etc/ssh/ssh_host_ed25519_key -N ''
/usr/sbin/sshd -D &
- 观察ssh是否启动成功
perl
netstat -antup | grep sshd
- 退出docker容器
bash
exit
- 测试ssh功能,为方便展示,我新建了第四个centos容器,ip为172.19.0.5
css
ssh root@172.19.0.5
展示结果如下:
测试单台centos是否可用
这里编写了一个go单元测试的方法,可以查看到成功地让指定的centos容器去执行我们的命令
单机既然没有问题了,那么就可以很轻松地模拟分布式环境来进行开发测试了。