
# 虚拟机服务添加网关和路由
网关（Gateway）定义了在网格出入口操作的负载均衡器，用于接收传入或传出的HTTP/TCP连接。
#### 前提条件
- 已执行[添加虚拟机服务到网格](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0010.html#asm_bestpractice_0010__zh-cn_topic_0000001494538437_section1481518471114)，即已创建v1版本的WorkloadEntry、ServiceEntry，已创建VirtualService、DestinationRule。
- 已创建负载均衡实例，要求所属VPC为集群所在的VPC。
 
 #### 创建Loadbalancer服务
1. 登录云容器引擎控制台，在"集群管理"页面单击集群名称，进入集群详情页。
2. 在左侧导航栏选择"资源 \> 服务发现"，选择命名空间"istio-system"，单击右上角"YAML创建"。
3. 输入以下内容： 
   ```
   apiVersion: v1
   kind: Service
   metadata:
     name: <vm-server1>-gw-svc
     namespace: istio-system
     annotations:
       kubernetes.io/elb.class: <ELB_TYPE>
       kubernetes.io/elb.id: <ELB_ID>
       service.protal.kubernetes.io/type: LoadBalancer
   spec:
     ports:
       - name: http
         protocol: TCP
         port: <345>
         targetPort: <2050>
     selector:
       app: istio-ingressgateway
       istio: ingressgateway
     type: LoadBalancer
     sessionAffinity: None
     loadBalancerIP: <ELB_IP>
     externalTrafficPolicy: Cluster
   ```
   其中，
   - \<vm-server1\>为虚拟机服务名称
   
   - \<ELB_TYPE\>为负载均衡类型，可设置为union（共享型）或performance（独享型）
   
   - \<ELB_ID\>为ELB实例ID
   
   - \<345\>为服务port
   
   - \<2050\>为服务targetPort 如果port小于1025，targetPort需要设置为**未被占用**且范围为1025\~65535的值；如果port大于等于1025，则targetPort可直接设置为port值。
     ![](https://support.huaweicloud.com/bestpractice-asm/public_sys-resources/note_3.0-zh-cn.png)
     判断targetPort是否被占用的方法：
     1. 登录[应用服务网格控制台](https://console.huaweicloud.com/asm/?locale=zh-cn)，单击服务网格的名称，进入网格详情页面。
     
     2. 在左侧导航栏选择"网格配置"，单击"istio资源管理"页签。
     
     3. 选择"istio资源：gateways"，命名空间选择"istio-system"，查看Gateway资源的YAML文件中server的port.number值。
       
   
   - \<ELB_IP\>为ELB实例的弹性IP
   
   
   
   
4. 单击"确定"完成创建。
 
#### 创建Gateway资源
1. 登录[应用服务网格控制台](https://console.huaweicloud.com/asm/?locale=zh-cn)，单击服务网格的名称，进入网格详情页面。
2. 在左侧导航栏选择"网格配置"，单击"istio资源管理"页签。
3. 选择"istio资源：gateways"，命名空间选择"istio-system"，单击"创建"。
4. 输入以下内容： 
   ```
   apiVersion: networking.istio.io/v1alpha3
   kind: Gateway
   metadata:
     annotations:
       asm/elbPort: '<345>'
       asm/gatewaySvc: <vm-server1>-gw-svc.istio-system
     name: <vm-server1>-gw
     namespace: istio-system
   spec:
     selector:
       istio: ingressgateway
     servers:
       - hosts:
           - <ELB_IP>
         port:
           name: http
           number: <2050>
           protocol: http
   ```
   其中，\<vm-server1\>为虚拟机服务名称，\<345\>为对外端口（同[创建Loadbalancer服务]中的服务port），\<ELB_IP\>为ELB实例的公网IP或域名，\<2050\>同[创建Loadbalancer服务]中的服务targetPort。
   
   
5. 单击"确定"完成创建。
 
#### 创建VirtualService资源
1. 登录[应用服务网格控制台](https://console.huaweicloud.com/asm/?locale=zh-cn)，单击服务网格的名称，进入网格详情页面。
2. 在左侧导航栏选择"网格配置"，单击"istio资源管理"页签。
3. 选择"istio资源：virtualservices"，命名空间选择"istio-system"，单击"创建"，输入以下内容： 
   ```
   apiVersion: networking.istio.io/v1alpha3
   kind: VirtualService
   metadata:
     name: <vm-server1>-root-vs
     namespace: istio-system
   spec:
     gateways:
       - istio-system/<vm-server1>-gw
     hosts:
       - <ELB_IP>
     http:
       - delegate:
           name: <vm-server1-8080>
           namespace: <vmns>
         match:
           - uri:
               prefix: /test
       - delegate:
           name: <vm-server1-8080>
           namespace: <vmns>
         match:
           - uri:
               prefix: /
   ```
   其中，\<vm-server1\>为虚拟机服务名称，\<ELB_IP\>为ELB实例的公网IP或域名，\<vm-server1-8080\>同[创建VirtualService](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0010.html#asm_bestpractice_0010__zh-cn_topic_0000001494538437_li865313575122)中的VirtualService name，\<vmns\>同[创建VirtualService](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0010.html#asm_bestpractice_0010__zh-cn_topic_0000001494538437_li865313575122)中的VirtualService namespace。
   ![](https://support.huaweicloud.com/bestpractice-asm/public_sys-resources/note_3.0-zh-cn.png)
   上述http.delegate列表配置了"/"和"/test"两个路由，请根据需要添加或者删除。
   
   
4. 选择"istio资源：virtualservices"，命名空间选择"istio-system"，单击"创建"再创建一个VirtualService资源，输入以下内容： 
   ```
   apiVersion: networking.istio.io/v1alpha3
   kind: VirtualService
   metadata:
     name: <vm-server1>-mesh-root-vs
     namespace: istio-system
   spec:
     hosts:
       - <vm-server1>.<vmns>.svc.cluster.local
     http:
       - delegate:
           name: <vm-server1-8080>
           namespace: <vmns>
         match:
           - uri:
               prefix: /test
       - delegate:
           name: <vm-server1-8080>
           namespace: <vmns>
         match:
           - uri:
               prefix: /
   ```
   其中，\<vm-server1\>为虚拟机服务名称，\<vm-server1-8080\>同[创建VirtualService](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0010.html#asm_bestpractice_0010__zh-cn_topic_0000001494538437_li865313575122)中的VirtualService name，\<vmns\>同[创建VirtualService](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0010.html#asm_bestpractice_0010__zh-cn_topic_0000001494538437_li865313575122)中的VirtualService namespace。
   ![](https://support.huaweicloud.com/bestpractice-asm/public_sys-resources/note_3.0-zh-cn.png)
   上述http.delegate列表配置了"/"和"/test"两个路由，请根据需要添加或者删除。
   
   
 
#### 更新VirtualService资源
1. 登录[应用服务网格控制台](https://console.huaweicloud.com/asm/?locale=zh-cn)，单击服务网格的名称，进入网格详情页面。
2. 在左侧导航栏选择"网格配置"，单击"istio资源管理"页签。
3. 选择"istio资源：virtualservices"，命名空间选择"vmns"，选择[创建VirtualService](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0010.html#asm_bestpractice_0010__zh-cn_topic_0000001494538437_li865313575122)中创建的VirtualService，单击操作列的"编辑"，进行如下修改。
   
   1. 移除hosts字段配置
   
   2. 复制spec.http\[0\]后，在下方并列位置添加如下内容：
      ```
      - match:
              - gateways:
                  - istio-system/<vm-server1>-gw
                port: <2050>
            route:
              - destination:
                  host: <vm-server1>.<vmns>.svc.cluster.local
                  port:
                    number: 8080
                  subset: v1
      ```
      其中，\<vm-server1\>为虚拟机服务名称，\<2050\>同[创建Loadbalancer服务]中的服务targetPort，\<vmns\>为[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间。
      示例：下图红框中为所添加的内容，在结构上需要与spec.http\[0\]对齐。
      ![](https://support.huaweicloud.com/bestpractice-asm/zh-cn_image_0000001492251016.png)
      
   
   
   
   
4. 单击"确定"完成修改。
![](https://support.huaweicloud.com/bestpractice-asm/public_sys-resources/note_3.0-zh-cn.png)
如果虚拟机服务需要移除网关，请删除以上创建的资源对象，移除添加的字段即可。
#### 结果验证
登录ASM管理控制台，在"网关管理"页面查看网关状态为"配置正常"，在浏览器打开访问地址，返回"hello, http protocol server"。
