安装Docker:
虚拟机网卡配置成桥接模式:
配置Yum:
[root@192 ~]# vim /etc/yum.repos.d/ali.repo
https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/ //阿里云docker的Yum库
配置yum源
[root@localhost ~]# vim /etc/yum.repos.d/Ali-Docker.repo
添加
[AliDocker]
name=ali.yum.docker
baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/docker-ce/linux/centos/gpg
使用配置号的Yum安装Docker:
[root@192 ~]# yum -y install docker-ce
启动国内镜像加速器:
[root@192 ~]# curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://f1361db2.m.daocloud.io
启动Docker:
[root@192 ~]# systemctl start docker
下载Docker所需的镜像环境:
[root@192 ~]# docker pull centos
[root@192 ~]# docker pull httpd
[root@192 ~]# docker pull busybox
首先在虚拟机中安装Nginx服务:
[root@192 ~]# yum -y install pcre-devel zlib-devel openssl-devel gcc gcc-c++
[root@192 ~]# tar xf nginx-1.15.4.tar.gz -C /usr/src/
[root@192 ~]# cd /usr/src/nginx-1.15.4/
[root@192 nginx-1.15.4]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx
[root@192 nginx-1.15.4]# make && make install
[root@192 nginx-1.15.4]# useradd -M -s /sbin/nologin nginx
[root@192 nginx-1.15.4]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@192 nginx-1.15.4]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
创建一个新容器,在里面安装Nginx服务:
[root@192 ~]# docker run -itd --name web1 centos /bin/bash
4ba6d36fe69dd16c25c01525bedc404eb7edcbefa831d0f9db275ff596171a08
[root@192 ~]# docker ps
进入web1容器的操作终端:
[root@192 ~]# docker exec -it web1 /bin/bash
在容器内安装vim、iifconfig等相关应用:
[root@4ba6d36fe69d ~]# yum -y install vim
[root@4ba6d36fe69d ~]# yum provides ifconfig //查询需要安装应用的包
所需要的包是net-tools
[root@4ba6d36fe69d ~]# yum -y install net-tools
安装号查看IP的命令后,查询到web1的IP为
[root@4ba6d36fe69d ~]# ifconfig
在容器终端,反向把Nginx的软件包,复制到容器内,首先在容器中安装openssl-clients包:
[root@4ba6d36fe69d ~]# yum provides scp
[root@4ba6d36fe69d ~]# yum -y install openssh-clients
[root@4ba6d36fe69d ~]# scp root@172.17.0.1:/root/nginx-1.15.4.tar.gz /root/
在web1容器中,安装Nginx:
[root@4ba6d36fe69d ~]# yum -y install gcc gcc-c++
[root@4ba6d36fe69d ~]# yum -y install pcre-devel openssl-devel zlib-devel
[root@4ba6d36fe69d nginx-1.15.4]# yum -y install make-3.82-23.el7.x86_64
[root@4ba6d36fe69d ~]# tar xf nginx-1.15.4.tar.gz -C /usr/src/
[root@4ba6d36fe69d ~]# cd /usr/src/nginx-1.15.4/
[root@4ba6d36fe69d nginx-1.15.4]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx && make && make install
[root@4ba6d36fe69d nginx-1.15.4]# useradd -M -s /sbin/nologin nginx
[root@4ba6d36fe69d nginx-1.15.4]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
[root@4ba6d36fe69d nginx-1.15.4]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
退出web1容器:
[root@4ba6d36fe69d ~]# exit
logout
把创建好的web1容器,做成镜像:
[root@192 ~]# docker commit 4ba6d36fe69d zhy/nginx:latest
sha256:f541a5eacebc276ead4ef4272b39d8ebdb12da6ab233fcde414b9c89885c3fcb
用刚刚创建好的镜像,开启三个容器:
[root@192 ~]# docker run -itd --name web2 zhy/nginx /bin/bash
b130ba06baf8225f38c99ab6d1b586dabe23c3a9cdad0aad7cb0b65505ec2ca4
[root@192 ~]# docker run -itd --name web3 zhy/nginx /bin/bash
1d8ffec4c71cdc63847ca1e0fb584fbfe49b9b06e899a5706f0c5c554bc19194
[root@192 ~]# docker run -itd --name web4 zhy/nginx /bin/bash
c0cb22cf6f2a55c6cab8748148ac7751fc8b7e67b8065cdd568a36e77a72886d
分别进入三个容器,进行查看IP,并修改网页内容,进行验证:
第一个:
[root@192 ~]# docker exec -it web2 /bin/bash
[root@b130ba06baf8 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.3 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:03 txqueuelen 0 (Ethernet)
RX packets 8 bytes 648 (648.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@b130ba06baf8 /]# echo 2 > /usr/local/nginx/html/index.html
[root@b130ba06baf8 /]# nginx
[root@b130ba06baf8 /]# exit
Exit
第二个:
[root@192 ~]# docker exec -it web3 /bin/bash
[root@1d8ffec4c71c /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.4 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:04 txqueuelen 0 (Ethernet)
RX packets 8 bytes 648 (648.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@1d8ffec4c71c /]# echo 3 > /usr/local/nginx/html/index.html
[root@1d8ffec4c71c /]# nginx
[root@1d8ffec4c71c /]# exit
exit
第三个:
[root@c0cb22cf6f2a /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.5 netmask 255.255.0.0 broadcast 172.17.255.255
ether 02:42:ac:11:00:05 txqueuelen 0 (Ethernet)
RX packets 8 bytes 648 (648.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
loop txqueuelen 1 (Local Loopback)
RX packets 0 bytes 0 (0.0 B)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@c0cb22cf6f2a /]# echo 4 > /usr/local/nginx/html/index.html
[root@c0cb22cf6f2a /]# nginx
[root@c0cb22cf6f2a /]# exit
exit
在虚拟机配置Nginx的反向代理和轮询:
[root@192 ~]# vim /usr/local/nginx/conf/nginx.conf
启动虚拟机上的Nginx服务:
[root@192 ~]# nginx
然后访问虚拟机的IP,进行验证:
[root@192 ~]# curl 192.168.0.4
2
[root@192 ~]# curl 192.168.0.4
3
[root@192 ~]# curl 192.168.0.4
4
[root@192 ~]# curl 192.168.0.4
2
[root@192 ~]# curl 192.168.0.4
3
[root@192 ~]# curl 192.168.0.4
4