基于docker-compose实现单机的Haproxy+Nginx+Tomcat

基于docker-compose实现单机的Haproxy+Nginx+Tomcat

一、安装docker  docker-compose  
    https://www.cnblogs.com/Yuanbangchen/p/16792323.html

二、使用dockerfile 制作镜像  1.nginx  2.haproxy  3.tomcat 
    https://www.cnblogs.com/Yuanbangchen/p/16836437.html  (参数第三 第四节)
    
三、安装harbor并把镜像上传
    https://www.cnblogs.com/Yuanbangchen/p/16872835.html


四、编写Dockerfile文件
[root@localhost7B web]# pwd
/data/docker-compose/web
[root@localhost7B web]# tree  
.
├── docker-compose.yml
├── haproxy
│   └── haproxy.cfg
└── nginx
    └── nginx.conf

4.1  nginx配置文件
[root@localhost7B web]# cat nginx/nginx.conf 
user  nginx;
worker_processes  1;

events {
    worker_connections  1024;
}

http {
    upstream tomcat {
        server service-tomcat-app1:8080; #service-tomcat-app1为docker-compse.yml指定的名称
        server service-tomcat-app2:8080;
    }

    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    server {
        listen       80;
        server_name  localhost;
        location / {
            root   html;
            index  index.html index.htm;
        }
        location /myapp {
            proxy_pass http://tomcat ;
        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

4.2 harpoxy配置文件
[root@localhost7B web]# cat haproxy/haproxy.cfg 
global
chroot /usr/local/haproxy
#stats socket /var/lib/haproxy/haproxy.sock mode 600 level admin
uid 99
gid 99
daemon
nbproc 1
pidfile /usr/local/haproxy/run/haproxy.pid 
log 127.0.0.1 local3 info
defaults
option http-keep-alive
option forwardfor
mode http
timeout connect 300000ms
timeout client 300000ms
timeout server 300000ms

listen stats
  mode http
  bind 0.0.0.0:9999
  stats enable
  log global
  stats uri   /haproxy-status
  stats auth  haadmin:123456 

listen web_port
  bind 0.0.0.0:80
  mode http
  log global
  balance roundrobin
  #说明:docker-compose使用关键字links:定义了地址。
  server web  service-nginx-web:80 check inter 3000 fall 2 rise 5  



4.3 docker-compose文件
[root@localhost7B web]# cat docker-compose.yml 
service-haproxy:
  image: harbor1.abc.com/web/centos-harpoxy:v1
  container_name: haproxy
  volumes:
    - /data/docker-compose/web/haproxy/haproxy.cfg:/etc/haproxy/haproxy.cfg
  expose:
    - 80
    - 9999
  ports:
    - "80:80"
    - "9999:9999"
  links:
    - service-nginx-web

service-nginx-web:
  image: harbor1.abc.com/web/nginx:v1
  container_name: nginx-web 
  volumes:
    - /data/docker-compose/web/nginx/nginx.conf:/usr/local/nginx/conf/nginx.conf
  expose:
    - 80 
    - 443
#  ports:  #haproxy中使用links,端口就不需要对外。
#    - "80:80"
#    - "443:443"
  links:
    - service-tomcat-app1
    - service-tomcat-app2

service-tomcat-app1:
  image: harbor1.abc.com/web/tomcat-web:app1
  container_name: tomcat-app1
  expose:
    - 8080
#  ports:
#    - "8081:8080"

service-tomcat-app2:
   image: harbor1.abc.com/web/tomcat-web:app1
   container_name: tomcat-app2
   expose:
     - 8080
#   ports:
#     - "8082:8080"

五:测试

[root@localhost7B web]# docker-compose up  -d
[root@localhost7B web]# 
[root@localhost7B web]# 
[root@localhost7B web]# docker ps
CONTAINER ID        IMAGE                                   COMMAND                  CREATED             STATUS              PORTS                                        NAMES
c4a95abbbb4c        harbor1.abc.com/web/centos-harpoxy:v1   "/usr/bin/run_haprox…"   About an hour ago   Up About an hour    0.0.0.0:80->80/tcp, 0.0.0.0:9999->9999/tcp   haproxy
510a9f05c614        harbor1.abc.com/web/nginx:v1            "nginx -g 'daemon of…"   About an hour ago   Up About an hour    80/tcp, 443/tcp                              nginx-web
33e478bbe786        harbor1.abc.com/web/tomcat-web:app1     "/apps/tomcat/bin/ru…"   About an hour ago   Up About an hour    8009/tcp, 8080/tcp                           tomcat-app1
1fa94ef12cee        harbor1.abc.com/web/tomcat-web:app2     "/apps/tomcat/bin/ru…"   About an hour ago   Up About an hour    8009/tcp, 8080/tcp                           tomcat-app2

 [root@localhost7C ~]# curl 192.168.80.110/myapp/
  echo Tomcat Page in app2
  [root@localhost7C ~]# curl 192.168.80.110/myapp/
  tomcat web page app1


[root@localhost7C ~]# 
[root@localhost7C ~]# curl 192.168.80.110
dockerfile nginx test

 

posted @ 2022-10-18 16:57  yuanbangchen  阅读(78)  评论(0编辑  收藏  举报