通过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容器去执行我们的命令

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

相关推荐
SoniaChen3312 分钟前
Rust基础-part3-函数
开发语言·后端·rust
全干engineer17 分钟前
Flask 入门教程:用 Python 快速搭建你的第一个 Web 应用
后端·python·flask·web
William一直在路上37 分钟前
SpringBoot 拦截器和过滤器的区别
hive·spring boot·后端
小马爱打代码1 小时前
Spring Boot 3.4 :@Fallback 注解 - 让微服务容错更简单
spring boot·后端·微服务
曾曜2 小时前
PostgreSQL逻辑复制的原理和实践
后端
豌豆花下猫2 小时前
Python 潮流周刊#110:JIT 编译器两年回顾,AI 智能体工具大爆发(摘要)
后端·python·ai
轻语呢喃2 小时前
JavaScript :事件循环机制的深度解析
javascript·后端
ezl1fe2 小时前
RAG 每日一技(四):让AI读懂你的话,初探RAG的“灵魂”——Embedding
后端
经典19922 小时前
spring boot 详解以及原理
java·spring boot·后端
Aurora_NeAr2 小时前
Apache Iceberg数据湖高级特性及性能调优
大数据·后端