温馨提示:学习本篇文章需要有一定docker基础和shell基础。

第一步:真实机安装ansible

curl -o /etc/yum.repos.d/CentOS7-Base-163.repo http://mirrors.163.com/.help/CentOS7-Base-163.repo
yum clean && yum makecache
yum -y install ansible

第二步:快速构建ansible控制的centos集群

A.安装docker 和 配置docker国内镜像加速器
yum -y install docker
cat /etc/docker/daemon.json<<EOF
{“registry-mirrors”: [“http://f1361db2.m.daocloud.io”]}
EOF
systemctl restart docker
B.快速构建有安装centos7的测试环境
a.先使用Dockerfile构建7的sshd环境
FROM centos:7
MAINTAINER zhangqijun “qijundada01@163.com” “qq:68327349”
RUN yum update -y
RUN yum repolist
RUN yum -y install vim iproute net-tools psmisc openssh-server initscripts openssh-clients wget
RUN ssh-keygen -N “” -f /root/.ssh/id_rsa
RUN sshd-keygen
RUN cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys
RUN sed -ir ‘/StrictHostKeyChecking/cStrictHostKeyChecking no’ /etc/ssh/ssh_config
RUN echo “123456”|passwd –stdin root
ENV EnvironmentFile=/etc/sysconfig/sshd
ADD jdk-8u201-linux-x64.tar.gz /usr/local/
ENV JAVA_HOME /usr/local/jdk1.8.0_201
ENV PATH $PATH:$JAVA_HOME/bin
RUN useradd zqj
RUN echo “123456”|passwd –stdin zqj
EXPOSE 22
CMD [“/usr/sbin/sshd”, “-D”]
然后根据编写的dockerfile执行命令构建镜像
docker build -t myos:sshd .
b.构建6台带有sshd服务的centos7容器
创建主机网络
docker network create –subnet=192.168.1.0/24 -o com.docker.network.bridge.name=ansible ansible
然后shell命令行执行下面命令
for i in {51..56}
do
docker run -d –ip 192.168.1.${i} –network ansible –name ansible${i} -h ansible${i} –privileged myos:sshd init
done
命令执行后,就利用docker模拟生成了6台带有sshd的centos服务器。
1.主机名是ansible[51-56]
2.ip地址是192.168.1.51-56
3.集群之间已经做了ssh相互免密登陆

发表评论

邮箱地址不会被公开。 必填项已用*标注