docker基于haproxy实现容器(nginx、apache)的负载均衡

####第一步:导入haproxy镜像

[root@foundation23 docker]# docker load -i haproxy.tar 
917c0fc99b35: Loading layer 130.9 MB/130.9 MB
5f70bf18a086: Loading layer 1.024 kB/1.024 kB
c205bb11f213: Loading layer 4.684 MB/4.684 MB
ffef890bdf7b: Loading layer 9.549 MB/9.549 MB
3ec368642ee3: Loading layer 2.048 kB/2.048 kB
Loaded image: haproxy:latest

docker基于haproxy实现容器(nginx、apache)的负载均衡_第1张图片
####第二步:安装docker-compose命令

[root@foundation23 docker]# cd /usr/local/bin/
[root@foundation23 bin]# ls
rht-vmctl  rht-vmicons  rht-vmsetkeyboard
[root@foundation23 bin]# ls
docker-compose-Linux-x86_64-1.22.0  rht-vmicons
rht-vmctl                           rht-vmsetkeyboard
[root@foundation23 bin]# chmod +x docker-compose-Linux-x86_64-1.22.0 
[root@foundation23 bin]# ln -s docker-compose-Linux-x86_64-1.22.0 docker-compose

docker基于haproxy实现容器(nginx、apache)的负载均衡_第2张图片

####配置haproxy的文件,实现负载均衡
这里的rhel7:v1是之前创建的镜像,具体的查看创建apache镜像博客

[root@foundation23 bin]# cd /tmp/docker/
[root@foundation23 docker]# mkdir compose
[root@foundation23 docker]# cd compose/
[root@foundation23 compose]# docker inspect haproxy
[
"Cmd": [
                "haproxy",
                "-f",
                "/usr/local/etc/haproxy/haproxy.cfg"
[root@foundation23 compose]# vim docker-compose.yml
apache:
     image: rhel7:v1
     expose:
        - 80
     volumes:
        - ./web:/var/www/html
nginx:
   image: nginx
   expose:
        - 80

haproxy:
    image: haproxy
    volumes:
        - ./haproxy:/usr/local/etc/haproxy
    links:
        - apache
        - nginx
    ports:
        - "8080:80"
    expose:
        - 80

[root@foundation23 compose]# cd ..
[root@foundation23 docker]# cp -r web/ compose/
[root@foundation23 docker]# cd compose/
[root@foundation23 compose]# ls
docker-compose.yml  web

docker基于haproxy实现容器(nginx、apache)的负载均衡_第3张图片
####编辑haproxy的配置文件

[root@foundation23 compose]# mkdir haproxy
[root@foundation23 compose]# cd haproxy/
[root@foundation23 haproxy]# vim haproxy.cfg

[root@foundation23 haproxy]# cd ..
[root@foundation23 compose]# docker-compose -v
docker-compose version 1.22.0, build f46880fe
[root@foundation23 compose]# docker-compose up
Creating compose_nginx_1  ... done
Creating compose_apache_1 ... done
Creating compose_haproxy_1 ... done
Attaching to compose_nginx_1, compose_apache_1, compose_haproxy_1
apache_1   | AH00558: httpd: Could not reliably determine the server's fully qualified domain name, using 172.17.0.4. Set the 'ServerName' directive globally to suppress this message
haproxy_1  | <7>haproxy-systemd-wrapper: executing /usr/local/sbin/haproxy -p /run/haproxy.pid -f /usr/local/etc/haproxy/haproxy.cfg -Ds 
nginx_1    | 2018/08/22 03:49:51 [error] 7#7: *1 open() "/usr/

docker基于haproxy实现容器(nginx、apache)的负载均衡_第4张图片

[root@foundation23 compose]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                            NAMES
90b268360c70        haproxy             "/docker-entrypoin..."   2 minutes ago       Exited (0) 15 seconds ago                                       compose_haproxy_1
ecbbfe401523        rhel7:v1            "/usr/sbin/httpd -..."   2 minutes ago       Exited (0) 15 seconds ago                                       compose_apache_1
e56353584938        nginx               "nginx -g 'daemon ..."   2 minutes ago       Exited (0) 15 seconds ago                                       compose_nginx_1

[root@foundation23 compose]# docker-compose start
Starting apache  ... done
Starting nginx   ... done
Starting haproxy ... done
[root@foundation23 compose]# docker ps -a
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                         PORTS                            NAMES
90b268360c70        haproxy             "/docker-entrypoin..."   3 minutes ago       Up 4 seconds                   0.0.0.0:8080->80/tcp             compose_haproxy_1
ecbbfe401523        rhel7:v1            "/usr/sbin/httpd -..."   3 minutes ago       Up 4 seconds                   80/tcp                           compose_apache_1
e56353584938        nginx               "nginx -g 'daemon ..."   3 minutes ago       Up 4 s

docker基于haproxy实现容器(nginx、apache)的负载均衡_第5张图片
docker基于haproxy实现容器(nginx、apache)的负载均衡_第6张图片

###测试:
浏览器输入172.25.254.23:8080
docker基于haproxy实现容器(nginx、apache)的负载均衡_第7张图片
这里是将web映射到apache的默认发布目录,呈现出来的是web目录里面的内容
docker基于haproxy实现容器(nginx、apache)的负载均衡_第8张图片
监控界面
docker基于haproxy实现容器(nginx、apache)的负载均衡_第9张图片

你可能感兴趣的:(docker)