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 [email protected]:/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