Search code examples
proxymicroservicesdevopsload-balancingenvoyproxy

how to use envoyloadbalancer to route Multiple domains on same public IP


here is my architecture architecture

i want to bind multiple domains to same IP address by exemple when i enter foo.com in my browser i see webapp1 and when i type bar.com in my broswer i find webapp2 . for that i have two webapp : webapp1 on ip 1111:5000

webapp2 on ip 1111:6000

here is my envoy version

envoy  version: d362e791eb9e4efa8d87f6d878740e72dc8330ac/1.18.2/clean-getenvoy-76c310e-envoy/RELEASE/BoringSSL

and here is my config envoy.yaml :

static_resources:
  listeners:
  - address:
      socket_address:
        address: 0.0.0.0
        port_value: 80
    filter_chains:
    - filters:
      - name: envoy.http_connection_manager
        typed_config:
          "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
          codec_type: AUTO
          stat_prefix: ingress_http
          route_config:
            name: local_route
            virtual_hosts:
            - name: foo.com
              domains:
              - "foo.com"
              routes:
              - match:
                  prefix: "/"
                route:
                  cluster: service_foo
            - name: bar.com
              domains:
              - "bar.com"
              routes:
              - match:
                  prefix: "/"
                route:
                  cluster: service_bar
          http_filters:
          - name: envoy.router
            typed_config: {}
  clusters:
  - name: service_foo
    connect_timeout: 1.00s
    type: strict_dns
    lb_policy: round_robin
    load_assignment:
      cluster_name: service_foo
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 1.1.1.1
                port_value: 5000
                ipv4_compat: true
  - name: service_bar
    connect_timeout: 1.00s
    type: strict_dns
    lb_policy: round_robin
    load_assignment:
      cluster_name: service_bar
      endpoints:
      - lb_endpoints:
        - endpoint:
            address:
              socket_address:
                address: 1.1.1.1
                port_value: 6000
                ipv4_compat: true
  
admin:
  access_log_path: "/dev/null"
  address:
    socket_address:
      address: 0.0.0.0
      port_value: 8001

when i enter in my browser foo.com work but bar.com does not work. What is the issue please help me .


Solution

  • I did the same Test with a slight difference in the YAML config file.

    I think each service endpoint must be the internal Ip address (private). Here is an example: I have two web apps running on docker: start on port 3000 and blog on 8080. docker image here: https://hub.docker.com/r/ang67/blog and https://hub.docker.com/r/ang67/getting-started

    static_resources:
      listeners:
      - address:
          socket_address:
            address: 0.0.0.0
            port_value: 80
        filter_chains:
        - filters:
          - name: envoy.filters.network.http_connection_manager
            typed_config:
              "@type": type.googleapis.com/envoy.extensions.filters.network.http_connection_manager.v3.HttpConnectionManager
              codec_type: AUTO
              stat_prefix: ingress_http
              route_config:
                name: local_route
                virtual_hosts:
                - name: start.com
                  domains:
                  - "start.com"
                  routes:
                  - match:
                      prefix: "/"
                    route:
                      cluster: service_start
                - name: blog.com
                  domains:
                  - "blog.com"
                  routes:
                  - match:
                      prefix: "/"
                    route:
                      cluster: service_blog
              http_filters:
              - name: envoy.filters.http.router
                typed_config: {}
      clusters:
      - name: service_start
        connect_timeout: 1.00s
        type: strict_dns
        lb_policy: round_robin
        load_assignment:
          cluster_name: service_start
          endpoints:
          - lb_endpoints:
            - endpoint:
                address:
                  socket_address:
                    address: 172.17.0.1
                    port_value: 3000
                    ipv4_compat: true
      - name: service_blog
        connect_timeout: 1.00s
        type: strict_dns
        lb_policy: round_robin
        load_assignment:
          cluster_name: service_blog
          endpoints:
          - lb_endpoints:
            - endpoint:
                address:
                  socket_address:
                    address: 172.17.0.1
                    port_value: 8080
                    ipv4_compat: true
      
    admin:
      access_log_path: "/dev/null"
      address:
        socket_address:
          address: 0.0.0.0
          port_value: 8001
    
    

    Run the envoy on a container:

    docker run --rm -it \
        -v $(pwd)/envoy-custom.yaml:/envoy-custom.yaml \
        -p 9901:9901 \
        -p 80:80 \
        envoyproxy/envoy-dev:2e6db8378477a4a63740746c5bfeb264cd76bc34 \
        -c /envoy-custom.yaml
    

    Run:

    curl -H "Host: start.com" http://localhost
    curl -H "Host: blog.com" http://localhost

    or do mapping in your etc/hosts for start.com and blog.com in order to launch in a browser