docker运行springboot+haproxy+mysql+redis

发布于 2020-04-15  395 次阅读


首先新建以下文件和目录,为运行docker做准备,docker内数据要关联到这些文件夹/文件下。

由于我项目是前后端分离,所以需要两个nginx服务器,一个负责前端页面,一个负责反向代理后端SpringBoot,虽然这里都只用了单节点,但还使用了haproxy,方便扩展,要添加服务器时,直接修改haproxy里的配置就行。

docker-compose.yml文件内容,里面的各个容器都指定了IP,因为在haproxy内需要用到, 映射的端口看自己需要进行变更(其实不需要在宿主机映射端口,因为都是通过haproxy来访问各个容器内的服务了,只需要将haproxy的端口暴漏出来即可,但是为了方便自己测试、检查排错,还是需要将该服务的端口暴漏出来)

docker-compose.yml

version: '3.7'
services:
  lkAdminHaproxy:
    image: haproxy
    restart: always
    container_name: lkAdminHaproxy
    networks:
      lkAdmin:
        ipv4_address: 172.54.0.100
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "18888:8888"  #监控页面
      - "180:80" #前端页面
      - "18080:8080" #后端接口
      - "13306:3306" #mysql
      - "16379:6379" #redis
    volumes:
      - ./haproxy/haproxy.cfg:/usr/local/etc/haproxy/haproxy.cfg
  lkAdminMysql:
    image: mysql:8
    container_name: lkAdminMysql
    networks:
      lkAdmin:
        ipv4_address: 172.54.0.5
    restart: always
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
    environment:
      - TZ=Asia/Shanghai
      - MYSQL_ROOT_PASSWORD=123456
    volumes:
      - ./mysql:/var/lib/mysql
    ports:
      - 33066:3306
  redis:
    container_name: lkAdminRedis
    image: redis
    networks:
      lkAdmin:
        ipv4_address: 172.54.0.6
    ports:
      - 6666:6379
    restart: always
    volumes:
      - ./redis/data:/data
    environment:
      - TZ=Asia/Shanghai
      - LANG=en_US.UTF-8
  #springboot打包的docker镜像
  lkAdmin:
    image: loneking/lkadmin
    restart: always
    container_name: lkAdmin
    networks:
      lkAdmin:
        ipv4_address: 172.54.0.2
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "18003:8080"
  lkAdminNginxBackend:
    image: nginx
    restart: always
    container_name: lkAdminNginxBackend
    networks:
      lkAdmin:
        ipv4_address: 172.54.0.3
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "18002:80"
      # - "443:443"
    volumes:
      - ./backend/nginx/conf.d:/etc/nginx/conf.d
      - ./backend/nginx/log:/var/log/nginx
      - ./backend/nginx/www:/etc/nginx/html
      # - ./backend/letsencrypt:/etc/letsencrypt
  lkAdminNginxFrontend:
    image: nginx
    restart: always
    container_name: lkAdminNginxFrontend
    networks:
      lkAdmin:
        ipv4_address: 172.54.0.4
    environment:
      - TZ=Asia/Shanghai
    ports:
      - "18001:80"
      # - "443:443"
    volumes:
      - ./frontend/nginx/conf.d:/etc/nginx/conf.d
      - ./frontend/nginx/log:/var/log/nginx
      - ./frontend/nginx/www:/etc/nginx/html
      # - ./frontend/etc/letsencrypt:/etc/letsencrypt
networks:
  lkAdmin:
    driver: bridge
    ipam:
      driver: default
      config:
        - subnet: 172.54.0.0/16
  

后端nginx配置文件backend.conf

server {
        listen       80;
        server_name  localhost;
        #access_log  logs/host.access.log  main;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    location = /favicon.ico {
           log_not_found off;
            access_log off;
    }

    location / {
            root   html;
            index  index.html index.htm;
            #springboot的docker所在的ip及开放端口
            proxy_pass  http://172.54.0.2:8080/;
    }
}

前端nginx配置文件frontend.conf,前端程序直接放在www目录下即可

server {
        listen       80;
        server_name  localhost;
        #access_log  logs/host.access.log  main;

    location = /favicon.ico {
           log_not_found off;
            access_log off;
    }

    location / {
            root   html;
            index  index.html index.htm;
    }

}

haproxy配置文件haproxy.cfg

global
        daemon                         #以后台形式运行haproxy
        chroot /usr/local/etc/haproxy  #工作目录
        #nbproc 1                       #进程数量(可以设置多个进程提高性能)
        #pidfile /var/run/haproxy.pid    #haproxy的pid存放路径,启动进程的用户必须有权限访问此文件
defaults   #默认配置,如果下面的frontend或者backend没配置,会采取这里的
        log 127.0.0.1 local0 err #[err warning info debug]
        #mode http                       #所处理的类别 (TCP4层,HTTP7层 )
        retries 3                #3次连接失败就认为是服务器不可用,也可以通过后面设置
        balance roundrobin       #默认的负载均衡的方式,轮询方式
        option redispatch        #当serverId对应的服务器挂掉后,强制定向到其他健康的服务器
        option abortonclose      #当服务器负载很高的时候,自动结束掉当前队列处理比较久的链接
        option dontlognull       #日志中不记录负载均衡的心跳检测记录
        option httplog           #日志类别http日志格式
        option forwardfor        #如果后端服务器需要获得客户端真实ip需要配置的参数,可以从Http Header中获得客户端ip
        maxconn 4096             #最大连接数
        timeout connect 5000ms   #连接超时
        timeout client 30000ms   #客户端超时
        timeout server 30000ms   #服务器超时

######## 监控界面配置 #################
listen admin_status
        bind *:8888                     # 监控界面访问信息
        mode http
        stats refresh 5s                #统计页面刷新间隔
        stats uri /haproxy              # 监控页面URI
        stats realm Global\ statistics  # 统计报告格式
        stats auth admin:123456         # 登录账户信息
#######后端接口##############
frontend lkadmin_backend
        bind *:8080
        default_backend lkadmin_backends
backend lkadmin_backends
          mode http
          option httpchk GET /editor      #http检查
          balance static-rr
          server  backend1        172.54.0.3:80  check  weight 1 inter 2000 rise 1 fall 2 maxconn 1000
          #inter 2000 心跳检测间隔2000ms
          #rise 1 检测1次正确表明服务器可用
          #fall 2 检测2次错误表明服务器不可用
 ######前端页面##############
frontend lkadmin_frontend
         bind *:80
         log global
         default_backend lkadmin_frontends
backend lkadmin_frontends
         mode http
         balance static-rr
         option httpchk GET /
         server  frontend1        172.54.0.4:80 check   weight 1 inter 2000 rise 1 fall 2 maxconn 1000
 ######## mysql###############
frontend mysql
        bind *:3306
        mode tcp
        log global
        option tcplog
        default_backend mysql_servers
backend mysql_servers
        balance leastconn #最少连接者先处理
        # 在 mysql 创建一个没有权限的haproxy用户,密码为空。 haproxy用户
        # create user 'haproxy'@'%' identified by ''; FLUSH PRIVILEGES;
        option mysql-check user haproxy
        server mysql_1 172.54.0.5:3306 check inter 2000 rise 1 fall 2  maxconn 300

#############redis######################
frontend redis
        bind *:6379
        mode tcp
        log global
        option tcplog
        default_backend redis_servers
backend redis_servers
        balance leastconn
        server redis_1 172.54.0.6:6379 check inter 2000 rise 1 fall 2  maxconn 300
#########################################

都编辑好后,将其上传到服务器,前提是服务器已经装好docker了,本博其他文章写有docker相关安装教程,记得给这些文件夹加个权限chmod -R 777 deploy ,我是习惯777了,给读写权限应该就够了,666

putty连接上服务器,执行下面命令,-d代表后台执行

docker-compose up -d

如果安装完成,可以在docker管理面板中查看

docker面板portainer
haproxy面板