深圳幻海软件技术有限公司 欢迎您!

Docker 初体验:Docker 部署 Nginx 负载均衡集群

2023-02-28

Docker是一个用于开发,交付和运行应用程序的开放平台。Docker使您能够将应用程序与基础架构分开,从而可以快速交付软件。今天来为大家演示一下docker部署nginx负载均衡集群。环 境centos7安装Docker移除旧版本Docker复制[root@chaols~]#sudoyu

Docker 是一个用于开发,交付和运行应用程序的开放平台。Docker 使您能够将应用程序与基础架构分开,从而可以快速交付软件。今天来为大家演示一下docker部署nginx负载均衡集群。

环 境

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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

安装yum-utils包并设置稳定存储库

[root@chaols ~]# yum install -y yum-utils
  • 1.

安装Docker

[root@chaols ~]# yum install docker-ce docker-ce-cli containerd.io
  • 1.

启动Docker

[root@chaols ~]# systemctl start docker
  • 1.

查看Docker镜像

刚刚安装docker是没有镜像的

[root@chaols ~]# docker images
REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
  • 1.
  • 2.

下载Nginx镜像

[root@chaols ~]# docker pull nginx
  • 1.

本地创建3个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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

创建网络

docker network create --driver bridge --subnet 172.16.0.0/16 --gateway 172.16.0.1 mynet
  • 1.

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;
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

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;
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.

配置页面展示

chao_nginx02

[root@chaols ~]# cat /home/html02/index.html 
02020202020202
chao_nginx03
[root@chaols ~]# cat /home/html03/index.html 
030303030303
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.

创建虚拟机

-d 后台运行 --name 指定名字 --net 指定刚刚创建的网络 -p 指定宿主机的映射端口 -v 指定和宿主机共享的目录 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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

验证

访问chao_nginx01:172.16.0.2循环显示chao_nginx02:172.16.0.3、chao_nginx03:172.16.0.4的页面

[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
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.