|
| 1 | +# traefik 快速入门 |
| 2 | + |
| 3 | +```yaml |
| 4 | +version: '3.6' |
| 5 | +services: |
| 6 | + traefik: |
| 7 | + image: traefik:2.2.1 |
| 8 | + # Enables the web UI and tells Traefik to listen to docker |
| 9 | + # 启用 webUI 并告诉 Traefik 去监听docker的容器实例 |
| 10 | + command: --api.insecure=true --providers.docker |
| 11 | + ports: |
| 12 | + # Traefik暴露的http端口 |
| 13 | + - "80:80" |
| 14 | + # webUI暴露的端口(必须指定--api.insecure=true才可以访问) |
| 15 | + - "8080:8080" |
| 16 | + volumes: |
| 17 | + # 指定docker的sock文件来让traefik获取docker的事件,从而实现动态负载均衡 |
| 18 | + - /var/run/docker.sock:/var/run/docker.sock |
| 19 | +``` |
| 20 | +
|
| 21 | +```bash |
| 22 | +$ docker-compose -f traefik.yaml up |
| 23 | +``` |
| 24 | + |
| 25 | +访问traefik管理面板 http://127.0.0.1:8080 |
| 26 | + |
| 27 | +部署应用,并使用traefik进行测试 |
| 28 | + |
| 29 | +```yaml |
| 30 | +version: '3.6' |
| 31 | +services: |
| 32 | + whoami: |
| 33 | + image: containous/whoami |
| 34 | + labels: |
| 35 | + - "traefik.http.routers.whoami.rule=Host(`whoami.me`)" |
| 36 | +``` |
| 37 | +
|
| 38 | +```bash |
| 39 | +$ docker-compose -f whoami.yaml up |
| 40 | +``` |
| 41 | + |
| 42 | +命令行测试访问 |
| 43 | + |
| 44 | +```bash |
| 45 | +$ curl -H Host:whoami.me http://localhost |
| 46 | + Hostname: ed8c4aa14f3c |
| 47 | + IP: 127.0.0.1 |
| 48 | + IP: 172.26.0.2 |
| 49 | + RemoteAddr: 172.26.0.3:47906 |
| 50 | + GET / HTTP/1.1 |
| 51 | + Host: whoami.me |
| 52 | + User-Agent: curl/7.64.1 |
| 53 | + Accept: */* |
| 54 | + Accept-Encoding: gzip |
| 55 | + X-Forwarded-For: 172.26.0.1 |
| 56 | + X-Forwarded-Host: whoami.me |
| 57 | + X-Forwarded-Port: 80 |
| 58 | + X-Forwarded-Proto: http |
| 59 | + X-Forwarded-Server: c1c0e9cba5a6 |
| 60 | + X-Real-Ip: 172.26.0.1 |
| 61 | +``` |
| 62 | + |
| 63 | +切记`-H Host:whoami.me`参数,不加该参数是访问不到的。 |
| 64 | + |
| 65 | +```bash |
| 66 | +$ curl -i http://localhost |
| 67 | + HTTP/1.1 404 Not Found |
| 68 | + Content-Type: text/plain; charset=utf-8 |
| 69 | + X-Content-Type-Options: nosniff |
| 70 | + Date: Fri, 08 May 2020 06:31:27 GMT |
| 71 | + Content-Length: 19 |
| 72 | + |
| 73 | + 404 page not found |
| 74 | +``` |
| 75 | + |
| 76 | +Traefik 可用的路由规则: |
| 77 | + |
| 78 | +| **规则** | **描述** | |
| 79 | +| ------------------------------------------------------------ | ------------------------------------------------------------ | |
| 80 | +| Headers(`key`, `value`) | 检查 headers 中是否有一个键为 key`值为`value`的键值对 | |
| 81 | +| HeadersRegexp(`key`, `regexp`) | 检查 headers 中是否有一个键为 `key`,值匹配正则表达式 `regexp`的键值对 | |
| 82 | +| Host(`example.com`, ...) | 检查请求的域名是否包含在给定的 `domains` 域名中 | |
| 83 | +| HostRegexp(`example.com`, `{subdomain:[a-z]+}.example.com`, ...) | 检查请求的域名是否匹配给定的 `regexp`正则表达式 | |
| 84 | +| Method(`GET`, ...) | 检查请求的方法是否包含在给定的 `methods (GET, POST, PUT, DELETE, PATCH)` 中 | |
| 85 | +| Path(`/path`, `/articles/{cat:[a-z]+}/{id:[0-9]+}`, ...) | 匹配确定的请求路径,它接受一系列文字和正则表达式路径 | |
| 86 | +| PathPrefix(`/products/`, `/articles/{cat:[a-z]+}/{id:[0-9]+}`) | 匹配请求前缀路径,它接受一系列文字和正则表达式前缀路径 | |
| 87 | +| Query(`foo=bar`, `bar=baz`) | 匹配查询字符串参数,接受key=value的键值对序列 | |
| 88 | + |
| 89 | +使用 docker-compose up --scale命令来对容器横向扩容,将单机扩容成2台: |
| 90 | + |
| 91 | +```bash |
| 92 | +$ docker-compose -f whoami.yaml up -d --scale whoami=2 |
| 93 | +Starting k8s-example_whoami_1 ... done |
| 94 | +Creating k8s-example_whoami_2 ... done |
| 95 | +``` |
| 96 | + |
| 97 | + |
| 98 | + |
| 99 | +此时再访问`curl -H Host:whoami.me http://localhost`就会自动负载均衡到2个不同的实例上去了。 |
| 100 | + |
| 101 | +```bash |
| 102 | +$ curl -H Host:whoami.me http://localhost |
| 103 | + Hostname: 9052df7a5a4b |
| 104 | + IP: 127.0.0.1 |
| 105 | + IP: 172.26.0.4 |
| 106 | + RemoteAddr: 172.26.0.3:38558 |
| 107 | + GET / HTTP/1.1 |
| 108 | + Host: whoami.me |
| 109 | + User-Agent: curl/7.64.1 |
| 110 | + Accept: */* |
| 111 | + Accept-Encoding: gzip |
| 112 | + X-Forwarded-For: 172.26.0.1 |
| 113 | + X-Forwarded-Host: whoami.me |
| 114 | + X-Forwarded-Port: 80 |
| 115 | + X-Forwarded-Proto: http |
| 116 | + X-Forwarded-Server: c1c0e9cba5a6 |
| 117 | + X-Real-Ip: 172.26.0.1 |
| 118 | +``` |
| 119 | + |
| 120 | +```bash |
| 121 | +$ curl -H Host:whoami.me http://localhost |
| 122 | + Hostname: ed8c4aa14f3c |
| 123 | + IP: 127.0.0.1 |
| 124 | + IP: 172.26.0.2 |
| 125 | + RemoteAddr: 172.26.0.3:51362 |
| 126 | + GET / HTTP/1.1 |
| 127 | + Host: whoami.me |
| 128 | + User-Agent: curl/7.64.1 |
| 129 | + Accept: */* |
| 130 | + Accept-Encoding: gzip |
| 131 | + X-Forwarded-For: 172.26.0.1 |
| 132 | + X-Forwarded-Host: whoami.me |
| 133 | + X-Forwarded-Port: 80 |
| 134 | + X-Forwarded-Proto: http |
| 135 | + X-Forwarded-Server: c1c0e9cba5a6 |
| 136 | + X-Real-Ip: 172.26.0.1 |
| 137 | +``` |
| 138 | + |
| 139 | +可见已具备负载均衡的能力,在`172.26.0.4`和`172.26.0.2`之间进行负载。 |
0 commit comments