• 前端代理
    • 运行沙箱
  • 返回

    前端代理

    为让大家尽快了解Envoy如何作为前端代理,我们发布了一个docker compose沙箱,这个沙箱部署了一个前端Envoy代理和几个后端服务(简单的flask应用),并与一个正在运行的合作的Envoy服务。这三个容器将部署在名为envoymesh的虚拟网格中。

    该Docker compose的部署图如下所示:

    部署图

    所有传入的请求都通过前端Envoy进行路由,该Envoy充当位于envoymesh网络边缘的反向代理。端口80通过docker compose映射到端口8000(请参阅/examples/front-proxy/docker-compose.yml)。此外,请注意,由前端Envoy路由到容器内的服务,实际上所有流量都是路由到服务的Envoy代理(在/examples/front-proxy/front-envoy.json中设置的路由)。反过来,服务的Envoy通过环回地址(/examples/front-proxy/service-envoy.json中的路由设置)将请求路由到flask应用程序。此阐述了Envoy与您服务搭配的优势:所有请求都由Envoy代理,并有效地路由到您的服务。

    运行沙箱

    以下文档将按照上图中所述的envoy集群进行运行设置。

    第1步:安装Docker工具集

    请您确保已经安装了最新版本的dockerdocker-composedocker-machine

    Docker工具箱提供了简单的方法来获取这些工具。

    第2步:设置Docker Machine

    首先让我们创建一个新的机器来容纳容器:

    1. $ docker-machine create --driver virtualbox default
    2. $ eval $(docker-machine env default)

    第3步:建立本地Envoy克隆仓库,并启动所有的容器

    如果你还没有克隆Envoy仓库,请用git克隆git clone git@github.com:envoyproxy/envoy或者git clone https://github.com/envoyproxy/envoy.git

    1. $ pwd
    2. envoy/examples/front-proxy
    3. $ docker-compose up --build -d
    4. $ docker-compose ps
    5. Name Command State Ports
    6. -------------------------------------------------------------------------------------------------------------
    7. example_service1_1 /bin/sh -c /usr/local/bin/ ... Up 80/tcp
    8. example_service2_1 /bin/sh -c /usr/local/bin/ ... Up 80/tcp
    9. example_front-envoy_1 /bin/sh -c /usr/local/bin/ ... Up 0.0.0.0:8000->80/tcp, 0.0.0.0:8001->8001/tcp

    第4步:测试Envoy的路由功能

    您现在可以通过前端Envoy向两个服务发送请求。

    对于service1:

    1. $ curl -v $(docker-machine ip default):8000/service/1
    2. * Trying 192.168.99.100...
    3. * Connected to 192.168.99.100 (192.168.99.100) port 8000 (#0)
    4. > GET /service/1 HTTP/1.1
    5. > Host: 192.168.99.100:8000
    6. > User-Agent: curl/7.43.0
    7. > Accept: */*
    8. >
    9. < HTTP/1.1 200 OK
    10. < content-type: text/html; charset=utf-8
    11. < content-length: 89
    12. < x-envoy-upstream-service-time: 1
    13. < server: envoy
    14. < date: Fri, 26 Aug 2016 19:39:19 GMT
    15. < x-envoy-protocol-version: HTTP/1.1
    16. <
    17. Hello from behind Envoy (service 1)! hostname: f26027f1ce28 resolvedhostname: 172.19.0.6
    18. * Connection #0 to host 192.168.99.100 left intact

    对于service2:

    1. $ curl -v $(docker-machine ip default):8000/service/2
    2. * Trying 192.168.99.100...
    3. * Connected to 192.168.99.100 (192.168.99.100) port 8000 (#0)
    4. > GET /service/2 HTTP/1.1
    5. > Host: 192.168.99.100:8000
    6. > User-Agent: curl/7.43.0
    7. > Accept: */*
    8. >
    9. < HTTP/1.1 200 OK
    10. < content-type: text/html; charset=utf-8
    11. < content-length: 89
    12. < x-envoy-upstream-service-time: 2
    13. < server: envoy
    14. < date: Fri, 26 Aug 2016 19:39:23 GMT
    15. < x-envoy-protocol-version: HTTP/1.1
    16. <
    17. Hello from behind Envoy (service 2)! hostname: 92f4a3737bbc resolvedhostname: 172.19.0.2
    18. * Connection #0 to host 192.168.99.100 left intact

    请注意,每个请求在发送给前端Envoy时,已正确路由到相应的应用程序。

    第5步:测试Envoy的负载均衡能力

    现在扩展我们的service1节点来演示Envoy的集群能力。

    1. $ docker-compose scale service1=3
    2. Creating and starting example_service1_2 ... done
    3. Creating and starting example_service1_3 ... done

    现在,如果我们多次向service1发送请求,前端Envoy会将请求通过负载均衡发给三个service1服务:

    1. $ curl -v $(docker-machine ip default):8000/service/1
    2. * Trying 192.168.99.100...
    3. * Connected to 192.168.99.100 (192.168.99.100) port 8000 (#0)
    4. > GET /service/1 HTTP/1.1
    5. > Host: 192.168.99.100:8000
    6. > User-Agent: curl/7.43.0
    7. > Accept: */*
    8. >
    9. < HTTP/1.1 200 OK
    10. < content-type: text/html; charset=utf-8
    11. < content-length: 89
    12. < x-envoy-upstream-service-time: 1
    13. < server: envoy
    14. < date: Fri, 26 Aug 2016 19:40:21 GMT
    15. < x-envoy-protocol-version: HTTP/1.1
    16. <
    17. Hello from behind Envoy (service 1)! hostname: 85ac151715c6 resolvedhostname: 172.19.0.3
    18. * Connection #0 to host 192.168.99.100 left intact
    19. $ curl -v $(docker-machine ip default):8000/service/1
    20. * Trying 192.168.99.100...
    21. * Connected to 192.168.99.100 (192.168.99.100) port 8000 (#0)
    22. > GET /service/1 HTTP/1.1
    23. > Host: 192.168.99.100:8000
    24. > User-Agent: curl/7.43.0
    25. > Accept: */*
    26. >
    27. < HTTP/1.1 200 OK
    28. < content-type: text/html; charset=utf-8
    29. < content-length: 89
    30. < x-envoy-upstream-service-time: 1
    31. < server: envoy
    32. < date: Fri, 26 Aug 2016 19:40:22 GMT
    33. < x-envoy-protocol-version: HTTP/1.1
    34. <
    35. Hello from behind Envoy (service 1)! hostname: 20da22cfc955 resolvedhostname: 172.19.0.5
    36. * Connection #0 to host 192.168.99.100 left intact
    37. $ curl -v $(docker-machine ip default):8000/service/1
    38. * Trying 192.168.99.100...
    39. * Connected to 192.168.99.100 (192.168.99.100) port 8000 (#0)
    40. > GET /service/1 HTTP/1.1
    41. > Host: 192.168.99.100:8000
    42. > User-Agent: curl/7.43.0
    43. > Accept: */*
    44. >
    45. < HTTP/1.1 200 OK
    46. < content-type: text/html; charset=utf-8
    47. < content-length: 89
    48. < x-envoy-upstream-service-time: 1
    49. < server: envoy
    50. < date: Fri, 26 Aug 2016 19:40:24 GMT
    51. < x-envoy-protocol-version: HTTP/1.1
    52. <
    53. Hello from behind Envoy (service 1)! hostname: f26027f1ce28 resolvedhostname: 172.19.0.6
    54. * Connection #0 to host 192.168.99.100 left intact

    第6步:进入容器开启curl服务

    除了使用主机上的curl外,您还可以自己输入容器并从里面curl。要输入一个容器镜像,你可以使用docker-compose exec <container_name> /bin/bash。例如,我们可以进入front-envoy容器,并在执行本地的curl服务:

    1. $ docker-compose exec front-envoy /bin/bash
    2. root@81288499f9d7:/# curl localhost:80/service/1
    3. Hello from behind Envoy (service 1)! hostname: 85ac151715c6 resolvedhostname: 172.19.0.3
    4. root@81288499f9d7:/# curl localhost:80/service/1
    5. Hello from behind Envoy (service 1)! hostname: 20da22cfc955 resolvedhostname: 172.19.0.5
    6. root@81288499f9d7:/# curl localhost:80/service/1
    7. Hello from behind Envoy (service 1)! hostname: f26027f1ce28 resolvedhostname: 172.19.0.6
    8. root@81288499f9d7:/# curl localhost:80/service/2
    9. Hello from behind Envoy (service 2)! hostname: 92f4a3737bbc resolvedhostname: 172.19.0.2

    第7步:进入容器和使用curl管理

    当Envoy运行时,它也将admin连接到所需的端口。在示例配置admin被绑定到8001端口.我们可以curl它获得有用的信息。例如,您可以curl /server_info来获取有关您正在运行的Envoy版本信息。另外,你可以·curl· ·/stats·得到统计数据。例如在frontenvoy里面我们可以得到:

    1. $ docker-compose exec front-envoy /bin/bash
    2. root@e654c2c83277:/# curl localhost:8001/server_info
    3. envoy 10e00b/RELEASE live 142 142 0
    4. root@e654c2c83277:/# curl localhost:8001/stats
    5. cluster.service1.external.upstream_rq_200: 7
    6. ...
    7. cluster.service1.membership_change: 2
    8. cluster.service1.membership_total: 3
    9. ...
    10. cluster.service1.upstream_cx_http2_total: 3
    11. ...
    12. cluster.service1.upstream_rq_total: 7
    13. ...
    14. cluster.service2.external.upstream_rq_200: 2
    15. ...
    16. cluster.service2.membership_change: 1
    17. cluster.service2.membership_total: 1
    18. ...
    19. cluster.service2.upstream_cx_http2_total: 1
    20. ...
    21. cluster.service2.upstream_rq_total: 2
    22. ...

    请注意,我们还可以获得上游群集的成员数量,完成的请求数量,有关http入站的信息以及其他大量有用的统计信息。

    返回

    • 上一级
    • 首页目录