通过Docker启动多个Centos以模拟分布式环境

目的

有些项目没法通过本机启动多个端口号来模拟分布式集群,需要多台机器才能模拟的时候,不想用虚拟机或者不想购买机器的时候可以通过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容器去执行我们的命令

单机既然没有问题了,那么就可以很轻松地模拟分布式环境来进行开发测试了。

相关推荐
加油,旭杏2 分钟前
【go语言】grpc 快速入门
开发语言·后端·golang
brzhang22 分钟前
墙裂推荐一个在 Apple Silicon 上创建和管理虚拟机的轻量级开源工具:lume
前端·后端
沈韶珺2 小时前
Visual Basic语言的云计算
开发语言·后端·golang
沈韶珺2 小时前
Perl语言的函数实现
开发语言·后端·golang
美味小鱼2 小时前
Rust 所有权特性详解
开发语言·后端·rust
我的K84092 小时前
Spring Boot基本项目结构
java·spring boot·后端
慕璃嫣3 小时前
Haskell语言的多线程编程
开发语言·后端·golang
晴空๓3 小时前
Spring Boot项目如何使用MyBatis实现分页查询
spring boot·后端·mybatis
Hello.Reader7 小时前
深入浅出 Rust 的强大 match 表达式
开发语言·后端·rust