1. 创建Web集群
1.1 Web1
- 创建一个Nginx1
docker run -itd --name nginx1 centos /bin/bash
25b814928bb878f8aa2fa0834f60939d43500ca2e84279b1d5b06b5f5a91ccb9
- 进入 Nginx1 容器中
- 默认 centos 中是一个特别干净的环境(啥也没有)
- 使用 yum provides 查看需要使用命令由那些安装包提供并且安装
yum provides ip
yum provides scp
yum provides netstat
yum -y install iproute openssh-clients net-tools
- 拉取Nginx包
scp root@192.168.116.102:/root/nginx-* ./
- 解决 Nginx 依赖关系
yum -y install pcre* gcc zlib-devel openssl-devel
- 创建运行用户
useradd -M -s /sbin/nologin nginx
- 安装Nginx
[root@25b814928bb8 ~]# tar -zxf nginx-1.12.2.tar.gz -C /usr/src/
[root@25b814928bb8 ~]# cd /usr/src/nginx-1.12.2/
[root@25b814928bb8 nginx-1.12.2]# ./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre && make && make install
如果出现报错:
-bash: make: command not found
解决方法:
yum -y install make
- 优化路径
ln -s /usr/local/nginx/sbin/nginx /usr/sbin
- 编写测试网页
echo 'This is Nginx1' > /usr/local/nginx/html/index.html
- 启动
nginx
- 测试
[root@localhost ~]# curl 172.17.0.5
This is Nginx1
1.2 将 Nginx 打包
docker commit nginx1 nginx2/centos-ssh-nginx:latest
sha256:9e59ccb1785305865474699e1fcb0b2f1b733d8f9d5efb9b47c1d91e75a80d8e
docker commit nginx1 nginx3/centos-ssh-nginx:latest
sha256:846a3c6e9b449a71b45c4572697950e30e54d09eaca2db47770341dd02cbbb67
1.3 运行 Nginx2并对其进行修改
- 运行
[root@localhost ~]# docker run -itd --name nginx2 nginx2/centos-ssh-nginx /bin/bash
15660a29a25717c683171fa08adaeeca23eba328429ece9482f14a523facd80f
- 进入并进行修改
- IP地址修改
- 修改测试页面
[root@localhost ~]# docker exec -it nginx2 /bin/bash
[root@15660a29a257 /]# ip a
echo 'This is a Nginx2' > /usr/local/nginx/html/index.html
- 启动Nginx
nginx
1.3 运行 Nginx2并对其进行修改
- 运行
[root@localhost ~]# docker run -itd --name nginx2 nginx2/centos-ssh-nginx /bin/bash
a2f9ecb51596f0bcac3aae425dc7fbdda432068733b6c1ce4e45c115300e3051
- 进入并进行修改
- IP地址修改
- 修改测试页面
[root@localhost ~]# docker exec -it nginx2 /bin/bash
[root@15660a29a257 /]# ip a
echo 'This is a Nginx3' > /usr/local/nginx/html/index.html
- 启动Nginx
nginx
1.4 测试
[root@localhost ~]# curl 172.17.0.5
This is Nginx1
[root@localhost ~]# curl 172.17.0.6
This is a Nginx2
[root@localhost ~]# curl 172.17.0.7
This is a Nginx3
2. 本机部署Nginx
-
部署Nginx,实现反向代理,负载均衡
-
nginx安装跳过
-
修改其配置文件
http {
upstream nginxservers {
server 172.17.0.5 weight=1;
server 172.17.0.6 weight=1;
server 172.170.0.7 weight=1;
}
server {
location / {
proxy_pass http://nginxservers;
}
}
}
- 重启Nginx
killall nginx
nginx
- 测试
[root@localhost ~]# curl 192.168.116.102
This is Nginx1
[root@localhost ~]# curl 192.168.116.102
This is a Nginx2
[root@localhost ~]# curl 192.168.116.102
This is a Nginx3