Expose an ACK application through an HTTP API

更新时间:
复制 MD 格式

This tutorial shows you how to expose an application running in Container Service for Kubernetes (ACK) to the internet. You will learn how to create an HTTP API and configure a route in Cloud-native API Gateway to allow external access to your microservice.

Prerequisites

  1. You have created a container cluster. For more information, see Create an ACK managed cluster or Create an ACK Swarm cluster.

  2. You have deployed a sample microservice to your ACK cluster.

    Deploy a sample microservice

    Log on to the Container Service for Kubernetes console. In the navigation pane on the left, click Clusters.

    On the Clusters page, click the name of your target cluster. In the navigation pane on the left, choose Workloads > Deployments.

    In the upper-right corner of the Deployments page, click Create from YAML. Use the following YAML to deploy a Deployment named httpbin and a Service named httpbin.

    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: httpbin
      namespace: default
    spec:
      replicas: 1
      selector:
        matchLabels:
          app: httpbin
      template:
        metadata:
          labels:
            app: httpbin
            version: v1
        spec:
          containers:
            - image: registry.cn-hangzhou.aliyuncs.com/mse-ingress/go-httpbin
              args:
                - "--version=v1"
              imagePullPolicy: Always
              name: httpbin
    ---
    apiVersion: v1
    kind: Service
    metadata:
      name: httpbin
      namespace: default
    spec:
      ports:
        - port: 8080
          protocol: TCP
      selector:
        app: httpbin

    On the Deployments page, click the httpbin deployment to confirm that the application is deployed successfully.

    The application is deployed successfully if its Pod status is Running.

Solution overview

This solution involves creating a Cloud-native API Gateway instance and associating it with your ACK cluster. You will then configure routes in the gateway to direct traffic to your containerized application, allowing clients to access it through the API gateway.

  1. Create a Cloud-native API Gateway instance.

    Create a Cloud-native API Gateway instance based on your existing microservice environment.

  2. Create an HTTP API.

    Create an HTTP API.

  3. Create a route.

    Add and publish a route for the service.

    1. Create a service source.

      Add a service source in Cloud-native API Gateway and select ACK as the source type.

    2. Add a service.

      Cloud-native API Gateway can retrieve the namespace of the service from the ACK source and add the existing service as a backend service.

  4. Debug the route.

    Test the route for the microservice.

Step 1: Create a Cloud-native API Gateway instance

  1. Log on to the Cloud-native API Gateway console. If you are using Cloud-native API Gateway for the first time, perform the following steps:

    Note

    The first time you access Cloud-native API Gateway, the system prompts you to authorize it to create a service-linked role.

    1. Click Authorize Now. In the dialog box that appears, click OK.

    2. Click Experience Cloud-native API Gateway Now to go to the console.

  2. In the navigation pane on the left, choose Instance. In the top menu bar, select a region, and then click Create Instance.

    Important

    The Cloud-native API Gateway instance must be in the same region as your ACK cluster.

  3. On the Cloud-native API Gateway buy page, configure the parameters for your instance, and then click Buy Now.

    Configure key parameters, including Region, Gateway Name, Gateway Specification, VPC, and VSwitch.

  4. On the Confirm Order page, select the I have read and agree to the Cloud-native API Gateway (Pay-As-You-Go) Terms of Service checkbox, and then click Activate Now.

    Note

    Instance creation may take 2 to 3 minutes.

  5. Click Management Console and return to the Instance page to view the information and status of the gateway.

    The gateway is created successfully if its Status is Running.

Step 2: Create an HTTP API

  1. In the navigation pane on the left, choose APIs and click Create API.

  2. On the HTTP API card, click Create. In the Create HTTP API panel, enter an API Name and click Create.

Step 3: Create a route

  1. In the navigation pane on the left, navigate to the APIs page and click the name of your API.

    Click Create Route. In the Create Route panel, configure the parameters.

Basic route information

On the Basic Information & Match Rules page, set Route Name to httpbin-demo. Select httpbin.com for Domain. For the Path, select Prefix as the matching method, enter / for the path, and select the Case Sensitive checkbox.

  • Domain: You can click Add Domain on the right to create a domain name in the Add Domain panel.

  • Path: Matches HTTP requests where the path starts with /.

Instance and backend service

Note

When you create a gateway instance, a default environment is automatically created and associated with the instance.

  1. From the Instance drop-down list, select the instance that you created in Step 1.

    In the Backend Service section, configure the Service Name, Service Port, and Protocol.

  2. Scenarios: From the Scenario list, select Single Service.

  3. In the Backend Services section, click Associated Service from the Service Name drop-down list.

  4. Create a service source and a service.

    1. For Source Type, select ACK.

    2. Click Add Source and select the cluster that you created in the Prerequisites section.

    3. From the Services, select the httpbin service and click OK.

    4. In the Backend Services section, select the httpbin service that you added from the Service Name drop-down list.

  5. Click Save and Publish.

Step 4: Debug the route

  1. In the navigation pane on the left, navigate to the APIs page and click the name of your API.

  2. In the route list, find the httpbin-demo route and click Debug in the Actions column.

  3. Enter version as the request parameter and click Send to view the service response.

    The response includes version: v1 and hostname: httpbin-xxx, where xxx is the suffix of the actual Pod name.

Related documents