概述
docker安装traefik网关
- 什么是traefik
- 编写docker-compose.yml
- 执行docker-compose
- 在windows上配置域名映射
- 浏览器访问
- 修改之前的tomcat项目
什么是traefik
Traefik is an open-source Edge Router that makes publishing your services a fun and easy experience. It receives requests on behalf of your system and finds out which components are responsible for handling them.
编写docker-compose.yml
version: '3'
services:
traefik:
image: traefik:2.4.0
container_name: traefik
labels:
- "traefik.enable=true" #开启traefikf代理
- "traefik.http.services.traefik.loadbalancer.server.scheme=http"
- "traefik.http.routers.traefik.rule=Host(`www.traefik.com.cn`)" #拦截条件,当host为www.traefik.com.cn的时候,会转发到此服务
- "traefik.http.services.traefik.loadbalancer.server.port=8080" #转发端口为8080,traefik 8080端口是dashboard
- "traefik.http.routers.traefik.entrypoints=websecure" #暴露服务为https
- "traefik.http.routers.traefik.tls=true" #开启tls支持,如果不开启,https会失效
- "traefik.http.middlewares.ssl.redirectscheme.scheme=https" #定义中间件,使用的是重定向中间件,保证访问http的时候自动重定向到https
- "traefik.http.middlewares.ssl.redirectscheme.permanent=false"
- "traefik.http.middlewares.ssl.redirectscheme.port=443"
- "traefik.http.routers.myrouter.middlewares=ssl@docker" #使用中间件
ports:
- 80:80
- 443:443
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- ./config/traefik.yaml:/etc/traefik/traefik.yml
networks:
- traefik
networks:
traefik: #定义网卡,以后所有被代理的服务都必须使用此网卡,不然会导致网络无法通讯
driver: bridge
ipam:
driver: default
config:
- subnet: "10.100.0.0/16"
traefik配置文件
global:
checkNewVersion: true
sendAnonymousUsage: true
entryPoints:
web: #同docker-compose里面携带entrypoints里面使用的必须一致
address: :80
websecure:
address: :443
log:
level: DEBUG
filePath: /opt/traefik/logs/traefik.log
format: json
accessLog:
filePath: /opt/traefik/logs/access.log
format: json
api:
dashboard: true
insecure: true
providers:
docker:
endpoint: unix:///var/run/docker.sock
执行docker-compose
docker-compose up -d
在windows上配置域名映射
添加对应的域名,因为上面写的是hostname匹配模式
浏览器访问
浏览器输入地址https://www.traefik.com.cn
修改之前的tomcat项目
实现使用traefik转发到tomcat
tomcat博客地址: docker篇-(docker安装tomcat2-基于Dockerfile构建tomcat)
tomcat目录里面的docker-compose.yml修改如下
version: '3'
services:
tomcat:
build: .
image: tomcat:9.0.44-alpine
container_name: tomcat
labels: # 添加traefik需要的标签信息
- "traefik.enable=true"
- "traefik.http.routers.tomcat.rule=Host(`www.tomcat.com.cn`)"
- "traefik.http.services.tomcat.loadbalancer.server.scheme=http"
- "traefik.http.services.tomcat.loadbalancer.server.port=8080"
- "traefik.http.routers.tomcat.entrypoints=websecure"
- "traefik.http.routers.tomcat.tls=true"
- "traefik.http.routers.myrouter.middlewares=ssl@docker" #因为traefik服务上面定义了中间件,这里直接使用
networks:
- tomcat
# tomcat
# ipv4_address: "10.10.10.80" #固定ip
networks: #配置网卡
tomcat: #网卡名称
external:
name: traefik_traefik #这里使用traefik那张网卡
#driver: bridge #网卡驱动
#ipam:
# driver: default
# config:
# - subnet: 10.10.0.0/16 #配置子网
查看traefik控制台里面的服务信息
配置tomcat的域名映射
通过浏览器输入https://www.tomcat.com.cn
访问
最后
以上就是尊敬荷花为你收集整理的docker篇-(docker-compose安装web网关traefik)什么是traefik编写docker-compose.yml执行docker-compose在windows上配置域名映射浏览器访问修改之前的tomcat项目的全部内容,希望文章能够帮你解决docker篇-(docker-compose安装web网关traefik)什么是traefik编写docker-compose.yml执行docker-compose在windows上配置域名映射浏览器访问修改之前的tomcat项目所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复