Docker 是一個(gè)用于開(kāi)發(fā),交付和運(yùn)行應(yīng)用程序的開(kāi)放平臺(tái)。Docker 使您能夠?qū)?yīng)用程序與基礎(chǔ)架構(gòu)分開(kāi),從而可以快速交付軟件。今天來(lái)為大家演示一下docker部署nginx負(fù)載均衡集群。
Docker 是一個(gè)用于開(kāi)發(fā),交付和運(yùn)行應(yīng)用程序的開(kāi)放平臺(tái)。Docker 使您能夠?qū)?yīng)用程序與基礎(chǔ)架構(gòu)分開(kāi),從而可以快速交付軟件。今天來(lái)為大家演示一下docker部署nginx負(fù)載均衡集群。
環(huán) 境
centos7
安裝Docker
移除舊版本Docker
[root@chaols ~]# sudo yum remove docker \
> docker-client \
> docker-client-latest \
> docker-common \
> docker-latest \
> docker-latest-logrotate \
> docker-logrotate \
> docker-engine
安裝yum-utils包并設(shè)置穩(wěn)定存儲(chǔ)庫(kù)
[root@chaols ~]# yum install -y yum-utils
安裝Docker
[root@chaols ~]# yum install docker-ce docker-ce-cli containerd.io
啟動(dòng)Docker
[root@chaols ~]# systemctl start docker
查看Docker鏡像
剛剛安裝docker是沒(méi)有鏡像的
[root@chaols ~]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
下載Nginx鏡像
[root@chaols ~]# docker pull nginx
本地創(chuàng)建3個(gè)Nginx目錄
[root@chaols nginx]# pwd
/home/nginx
[root@chaols nginx]# ls
nginx01 nginx02 nginx03
[root@chaols nginx]# ls nginx01/ && ls nginx02 && ls nginx03
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
conf.d fastcgi_params mime.types modules nginx.conf scgi_params uwsgi_params
創(chuàng)建網(wǎng)絡(luò)
docker network create --driver bridge --subnet 172.16.0.0/16 --gateway 172.16.0.1 mynet
chao_nginx01配置文件
[root@chaols ~]# cat /home/nginx/nginx01/conf.d/default.conf
upstream chao_test {
server 172.16.0.2 weight=5;
server 172.16.0.3 weight=5;
}
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://chao_test;
}
chao_nginx02、chao_nginx03配置文件相同
[root@chaols ~]# cat /home/nginx/nginx02/conf.d/default.conf
server {
listen 80;
listen [::]:80;
server_name localhost;
#access_log /var/log/nginx/host.access.log main;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
}
配置頁(yè)面展示
chao_nginx02
[root@chaols ~]# cat /home/html02/index.html
02020202020202
chao_nginx03
[root@chaols ~]# cat /home/html03/index.html
030303030303
創(chuàng)建虛擬機(jī)
-d 后臺(tái)運(yùn)行 --name 指定名字 --net 指定剛剛創(chuàng)建的網(wǎng)絡(luò) -p 指定宿主機(jī)的映射端口 -v 指定和宿主機(jī)共享的目錄 nginx 鏡像名稱
[root@chaols ~]# docker run -d --name chao_nginx01 --net mynet -p 8001:80 -v /home/nginx/nginx01:/etc/nginx -v /home/html01/:/usr/share/nginx/html nginx
bf4a1a593e0908e383ade9f0b893a324e3f95cb251844c58a352f4d070ed253d
[root@chaols ~]# docker run -d --name chao_nginx02 --net mynet -p 8002:80 -v /home/nginx/nginx02:/etc/nginx -v /home/html02/:/usr/share/nginx/html nginx
6177bb3461b8e8e912eacef161b3619d612e1e51136b324aacd6e888ec805b23
[root@chaols ~]# docker run -d --name chao_nginx03 --net mynet -p 8003:80 -v /home/nginx/nginx03:/etc/nginx -v /home/html03/:/usr/share/nginx/html nginx
abbef89fc891d06de4b055a316eb11c4a938e2033b15ce1528acb858643df8fd
驗(yàn)證
訪問(wèn)chao_nginx01:172.16.0.2循環(huán)顯示chao_nginx02:172.16.0.3、chao_nginx03:172.16.0.4的頁(yè)面
[root@chaols ~]# curl 172.16.0.2
030303030303
[root@chaols ~]# curl 172.16.0.2
02020202020202
[root@chaols ~]# curl 172.16.0.2
030303030303
[root@chaols ~]# curl 172.16.0.2
02020202020202
[root@chaols ~]# curl 172.16.0.2
030303030303
[root@chaols ~]# curl 172.16.0.2
02020202020202
[root@chaols ~]# curl 172.16.0.2
030303030303
[root@chaols ~]# curl 172.16.0.2
02020202020202