
# 容器服务访问虚拟机服务
启动ASM-PROXY后，容器内的服务可以访问虚拟机上的服务，如下图所示。
![](https://support.huaweicloud.com/bestpractice-asm/zh-cn_image_0000001543410793.png)
验证流程如下：
1. [部署虚拟机服务]：在虚拟机中部署httptest应用。
2. [部署容器服务]：在CCE集群中部署容器服务tomcat。
3. [添加虚拟机服务到网格]：不同于容器服务有自动注册能力，当把一个虚拟机服务加入网格时，需要用户手动注册服务的基础信息。因此需要手动创建如下资源：Service、WorkloadEntry、ServiceEntry、DestinationRule和VirtualService。
4. [访问虚拟机服务]：容器服务通过服务名访问虚拟机服务。
 #### 部署虚拟机服务
此处以httptest测试程序作为示例。httptest为go http应用，监听在8080端口，访问正常返回"hello, http protocol server"消息。
1. 上传httptest应用到虚拟机。
2. 修改权限。 
   **chmod +x httptest**
   
   
3. 启动httptest应用。 
   **./httptest**
   
   
 
 #### 部署容器服务
1. 登录云容器引擎控制台，在"集群管理"页面单击集群名称，进入集群详情页。
2. 在左侧导航栏选择"资源 \> 工作负载"，单击右上角"镜像创建"。
3. 设置工作负载参数。 
   - 负载类型：选择"无状态负载"。
   
   - 命名空间：选择在[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间"vmns"。
   
   - 镜像：选择"tomcat"。
   
   - 服务配置：添加服务，服务端口和容器端口设置为8080。
   
   
   其余参数按需填写。
   ![](https://support.huaweicloud.com/bestpractice-asm/public_sys-resources/note_3.0-zh-cn.png)
   如果是TCP协议服务，需要服务监听在127.0.0.1或0.0.0.0。
   
   
4. 单击"创建工作负载"。
 
 #### 添加虚拟机服务到网格
1. 创建Service。 
   1. 登录云容器引擎控制台，在"集群管理"页面单击集群名称，进入集群详情页。
   
   2. 在左侧导航栏选择"资源 \> 服务发现"，选择命名空间"vmns"，单击右上角"YAML创建"。
   
   3. 输入以下内容：
      ```
      apiVersion: v1
      kind: Service
      metadata:
        name: <vm-server1>
        namespace: "<NS>"
        labels:
          app: <vm-server1>
      spec:
        ports:
        - port: 8080
          name: http-<vm-server1>
          targetPort: 8080
        selector:
          app: <vm-server1>
      ```
      其中，\<vm-server1\>为虚拟机服务名称，\<NS\>为[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间。
      示例：
      ![](https://support.huaweicloud.com/bestpractice-asm/zh-cn_image_0000001543530601.png)
      
   
   4. 单击"确定"完成创建。
   
   
   
   
2. 创建WorkloadEntry。 
   使用WorkloadEntry定义虚拟机服务的工作负载属性。
   1. 登录[应用服务网格控制台](https://console.huaweicloud.com/asm/?locale=zh-cn)，单击服务网格的名称，进入网格详情页面。
   
   2. 在左侧导航栏选择"网格配置"，单击"istio资源管理"页签。
   
   3. 选择"istio资源：workloadentries"，命名空间选择[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间"vmns"，单击"创建"。
      ![](https://support.huaweicloud.com/bestpractice-asm/public_sys-resources/note_3.0-zh-cn.png)
      这里的"vmns"命名空间中必须存在服务在网格中正常使用，否则选择不到该命名空间。你可以将[部署容器服务]中的tomcat服务加入网格，并确保配置诊断通过。
      
   
   4. 输入以下内容：
      ```
      apiVersion: networking.istio.io/v1alpha3
      kind: WorkloadEntry
      metadata:
        name: "<vm-server1>"
        namespace: "<NS>"
      spec:
        address: "<VM_IP>"
        labels:
          app: <vm-server1>
          version: v1
        serviceAccount: "<SERVICE_ACCOUNT>"
      ```
      其中，\<vm-server1\>为虚拟机服务名称，\<NS\>为[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间，\<VM_IP\>为虚拟机的私有IP，\<SERVICE_ACCOUNT\>为[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的服务账号名称。
      示例：
      ![](https://support.huaweicloud.com/bestpractice-asm/zh-cn_image_0000001492570760.png)
      
   
   5. 单击"确定"完成创建。
   
   
   
   
3. 创建ServiceEntry。 
   使用ServiceEntry将虚拟机服务添加到网格内部维护的服务注册表，以便网格中自动发现的服务可以访问或路由到这些手动指定的服务。
   1. 登录[应用服务网格控制台](https://console.huaweicloud.com/asm/?locale=zh-cn)，单击服务网格的名称，进入网格详情页面。
   
   2. 在左侧导航栏选择"网格配置"，单击"istio资源管理"页签。
   
   3. 选择"istio资源：serviceentries"，命名空间选择[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间"vmns"，单击"创建"。
   
   4. 输入以下内容：
      ```
      apiVersion: networking.istio.io/v1alpha3
      kind: ServiceEntry
      metadata:
        name: "<vm-server1>"
        namespace: "<NS>"
      spec:
        hosts:
        - <vm-server1>.<NS>.svc.cluster.local
        location: MESH_INTERNAL
        ports:
        - number: 8080
          name: http
          protocol: HTTP
        resolution: STATIC
        workloadSelector:
          labels:
            app: <vm-server1>
      ```
      其中，\<vm-server1\>为虚拟机服务名称，\<NS\>为[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间。
      示例：
      ![](https://support.huaweicloud.com/bestpractice-asm/zh-cn_image_0000001492730652.png)
      
   
   5. 单击"确定"完成创建。
   
   
   
   
4. 创建DestinationRule。
   
   1. 登录[应用服务网格控制台](https://console.huaweicloud.com/asm/?locale=zh-cn)，单击服务网格的名称，进入网格详情页面。
   
   2. 在左侧导航栏选择"网格配置"，单击"istio资源管理"页签。
   
   3. 选择"istio资源：destinationrules"，命名空间选择[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间"vmns"，单击"创建"。
   
   4. 输入以下内容：
      ```
      apiVersion: networking.istio.io/v1alpha3
      kind: DestinationRule
      metadata:
        name: <vm-server1>
        namespace: "<NS>"
      spec:
        host: <vm-server1>
        subsets:
        - labels:
            version: v1
          name: v1
      ```
      其中，\<vm-server1\>为虚拟机服务名称，\<NS\>为[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间。
      示例：
      ![](https://support.huaweicloud.com/bestpractice-asm/zh-cn_image_0000001492410872.png)
      
   
   5. 单击"确定"完成创建。
   
   
   
   
5. 创建VirtualService。
   
   1. 登录[应用服务网格控制台](https://console.huaweicloud.com/asm/?locale=zh-cn)，单击服务网格的名称，进入网格详情页面。
   
   2. 在左侧导航栏选择"网格配置"，单击"istio资源管理"页签。
   
   3. 选择"istio资源：virtualservices"，命名空间选择[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间"vmns"，单击"创建"。
   
   4. 输入以下内容：
      ```
      apiVersion: networking.istio.io/v1alpha3
      kind: VirtualService
      metadata:
        name: <vm-server1>-8080
        namespace: "<NS>"
      spec:
        hosts:
        - <vm-server1>
        http:
        - match:
          - gateways:
            - mesh
            port: 8080
          route:
          - destination:
              host: <vm-server1>.<NS>.svc.cluster.local
              port:
                number: 8080
              subset: v1
      ```
      其中，\<vm-server1\>为虚拟机服务名称，8080为服务端口，\<NS\>为[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间。
      示例：
      ![](https://support.huaweicloud.com/bestpractice-asm/zh-cn_image_0000001543770669.png)
      
   
   5. 单击"确定"完成创建。
   
   
   
   
 
 #### 访问虚拟机服务
1. 在kubectl节点执行以下命令，获取tomcat工作负载的Pod名称。 
   **kubectl get pods -n \<vmns\>\|grep tomcat**
   回显示例：
   ```
   tomcat-75cbb4b948-nzrfs       2/2      Running   0      19h
   ```
   
   
2. 执行以下命令，测试访问虚拟机服务。 
   **kubectl exec -it \<tomcat-xxx\> -n \<vmns\> -- curl \<vm-server1\>.\<vmns\>.svc.cluster.local:8080**
   其中，\<vm-server1\>为虚拟机服务名称，\<vmns\>为[3](https://support.huaweicloud.com/bestpractice-asm/asm_bestpractice_0007.html#asm_bestpractice_0007__zh-cn_topic_0000001494658309_li8573193914511)中填写的命名空间。
   若回显"hello, http protocol server"，则容器服务访问虚拟机服务成功。
   
   
 
