Traefik 实战(traefik+docker swarm) - 好脑袋和烂笔头 - OSCHINA
- - traefik是一个使你把微服务暴露出来变的更容易的http反向代理和负载均衡软件. traefik支持K8S、docker swarm、mesos、consul、etcd、zookeeper等基础设施组件,个人认为更适合容器化的微服务,traefik的配置会自动的、动态的配置更新自己. traefik的原理在另一篇讲解,本章直接实战看效果.
traefik是一个使你把微服务暴露出来变的更容易的http反向代理和负载均衡软件。traefik支持K8S、docker swarm、mesos、consul、etcd、zookeeper等基础设施组件,个人认为更适合容器化的微服务,traefik的配置会自动的、动态的配置更新自己。traefik的原理在另一篇讲解,本章直接实战看效果。
本篇主要模拟的是traefik+docker swarm mode的场景,由traefik自动发现swarm mode下的service
ubuntu16.04-1 | swarm manager | traefik |
ubuntu16.04-2 | swarm works | |
ubuntu16.04-3 | swarm works |
ps:还是之前文章做实验的swarm集群
wget 'https://github-production-release-asset-2e65be.s3.amazonaws.com/42408804/b7288f00-a48e-11e8-817e-298aa1a8bae9?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20180903%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180903T065922Z&X-Amz-Expires=300&X-Amz-Signature=4d4a6a61122a5e0ac8aaae5da30883d555db307c2a9dbe1e300fb1dc2decb0a2&X-Amz-SignedHeaders=host&actor_id=12913767&response-content-disposition=attachment%3B%20filename%3Dtraefik_linux-amd64&response-content-type=application%2Foctet-stream'
ps:把下载完的traefik_linux-amd64二进制文件重命名成traefik,上传到镜像制作服务器。
Dockfile
FROM scratch
COPY ./traefik /
EXPOSE 80
ENTRYPOINT ["/traefik"]
docker build -t traefik .
docker tag traefik 172.31.68.241/library/traefik
docker push 172.31.68.241/library/traefik
docker network create --driver=overlay traefik-net
ps:traefik和app要在同一个网络内,否则traefik识别不到app
docker service create \
--name traefik \
--constraint=node.role==manager \
--publish 8090:80 --publish 8080:8080 \
--mount type=bind,source=/var/run/docker.sock,target=/var/run/docker.sock \
--network traefik-net \
172.31.68.241/library/traefik \
--docker \
--docker.swarmMode \
--docker.domain=example.org \
--docker.watch \
--logLevel=DEBUG \
--web
http://172.31.68.241:8080/dashboard/
docker service create --replicas 2 --network traefik-net --label traefik.port=80 --label traefik.frontend.rule=Host:test.example.org --name hello 172.31.68.241/library/friendlyhello
curl -H Host:test.example.org http://172.31.68.241:8090
docker service create --replicas 2 --network traefik-net --label traefik.port=80 --label traefik.frontend.rule=Host:test.example.org --label traefik.backend.loadbalancer.sticky=true --name hello 172.31.68.241/library/friendlyhello
curl -c cookies.txt -H Host:test.example.org http://172.31.68.241:8090
curl -b cookies.txt -H Host:test.example.org http://172.31.68.241:8090