本篇内容介绍了“Traefik怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
traefik是一个使你把微服务暴露出来变的更容易的http反向代理和负载均衡软件。traefik支持K8S、docker swarm、mesos、consul、etcd、zookeeper等基础设施组件,个人认为更适合容器化的微服务,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集群
下载traefik
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"]
编译image
docker build -t traefik .
上传image
docker tag traefik 172.31.68.241/library/traefik
docker push 172.31.68.241/library/traefik
部署traefik
创建网络
docker network create --driver=overlay traefik-net
ps:traefik和app要在同一个网络内,否则traefik识别不到app
部署traefik
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
查看web
http://172.31.68.241:8080/dashboard/
轮训访问app
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
会话粘滞访问app
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
查看cookies
“Traefik怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注天达云网站,小编将为大家输出更多高质量的实用文章!