|
1 | 1 | ---
|
2 |
| -title: Deploy a self-hosted Azure API Management gateway to Kubernetes | Microsoft Docs |
3 |
| -description: Learn how to deploy a self-hosted Azure API Management gateway to Kubernetes |
| 2 | +title: Deploy Azure API Management self-hosted gateway to Kubernetes | Microsoft Docs |
| 3 | +description: Learn how to deploy self-hosted gateway to Kubernetes |
4 | 4 | services: api-management
|
5 |
| -documentationcenter: '' |
6 |
| -author: miaojiang |
| 5 | +author: vladvino |
7 | 6 | manager: gwallace
|
8 |
| -editor: '' |
9 |
| - |
10 | 7 | ms.service: api-management
|
11 | 8 | ms.workload: mobile
|
12 |
| -ms.tgt_pltfrm: na |
13 | 9 | ms.topic: article
|
14 |
| -ms.date: 03/31/2020 |
15 | 10 | ms.author: apimpm
|
16 |
| - |
| 11 | +ms.date: 04/23/2020 |
17 | 12 | ---
|
| 13 | +# Deploy Azure API Management self-hosted gateway to Kubernetes |
18 | 14 |
|
19 |
| -# Deploy a self-hosted Azure API Management gateway to Kubernetes |
20 |
| - |
21 |
| -This article provides the steps for deploying self-hosted Azure API Management gateway into a Kubernetes Cluster. |
| 15 | +This article describes the steps for deploying self-hosted gateway to a Kubernetes cluster. |
22 | 16 |
|
23 | 17 | ## Prerequisites
|
24 | 18 |
|
25 | 19 | - Complete the following quickstart: [Create an Azure API Management instance](get-started-create-service-instance.md)
|
26 |
| -- Create a Kubernetes cluster. [Minikube](https://kubernetes.io/docs/tasks/tools/install-minikube/) is a good option for development and evaluation purposes. For production workloads, you can use [Azure Kubernetes Service](https://azure.microsoft.com/services/kubernetes-service/) or a Kubernetes cluster in a foreign cloud. |
27 |
| -- [Provision a gateway resource in your API Management instance](api-management-howto-provision-self-hosted-gateway.md). |
| 20 | +- Create a Kubernetes cluster. |
| 21 | +> [!TIP] |
| 22 | +> [Single-node clusters](https://kubernetes.io/docs/setup/#learning-environment) work well for development and evaluation purposes. Use [Kubernetes Certified](https://kubernetes.io/partners/#conformance) multi-node clusters on-premises or in the cloud for production workloads. |
| 23 | +- [Provision a self-hosted gateway resource in your API Management instance](api-management-howto-provision-self-hosted-gateway.md). |
28 | 24 |
|
29 |
| -## Deploy the gateway to Kubernetes |
| 25 | +## Deploy the self-hosted gateway to Kubernetes |
30 | 26 |
|
31 |
| -1. Select **Gateways** from under **Settings**. |
32 |
| -2. Select the gateway resource you intend to deploy. |
| 27 | +1. Select **Gateways** from under **Deployment and infrastructure**. |
| 28 | +2. Select the self-hosted gateway resource you intend to deploy. |
33 | 29 | 3. Select **Deployment**.
|
34 |
| -4. Note that a new token in the **Token** text box was autogenerated for you using the default **Expiry** and **Secret Key** values. Adjust either or both if desired and select **Generate** to create a new token. |
35 |
| -5. Make sure **Kubernetes** is selected under **Deployment scripts**. |
36 |
| -6. Select **<gateway-name>.yml** file link next to **Deployment** to download the file. |
37 |
| -7. Adjust the port mappings and container name in the yml file as needed. |
38 |
| -8. Select the **copy** icon located at the right end of the **Deploy** text box to save the `kubectl` command to clipboard. |
39 |
| -9. Paste the command to the terminal (or command) window. Note that the command expects the downloaded environment file to be present in the current directory. |
40 |
| -```console |
41 |
| - kubectl apply -f <gateway-name>.yaml |
42 |
| -``` |
43 |
| -10. Execute the command. The command instructs your Kubernetes cluster to run the container, using self-hosted gateway's image downloaded from the Microsoft Container Registry, and to configure the container to expose HTTP (8080) and HTTPS (443) ports. |
44 |
| -11. Run the below command to check the gateway pod is running. Note that your pod name will be different. |
| 30 | +4. Note that an access token in the **Token** text box was autogenerated for you using the default **Expiry** and **Secret key** values. If needed, pick desired values in either or both controls to generate a new token. |
| 31 | +5. Select **Kubernetes** tab under **Deployment scripts**. |
| 32 | +6. Click on the **<gateway-name>.yml** file link and download the YAML file. |
| 33 | +7. Select **copy** icon located at the bottom right corner of the **Deploy** text box to save the `kubectl` commands to the clipboard. |
| 34 | +8. Paste commands to the terminal (or command) window. The first command creates a Kubernetes secret containing access token generated in step 4. Second command applies the configuration file downloaded in step 6 to the Kubernetes cluster and expects the the file to be present in the current directory. |
| 35 | +9. Execute the commands to create the necessary Kubernetes objects in the [default namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/) and start self-hosted gateway pod(s) from [container image](https://aka.ms/apim/sputnik/repo) downloaded from the Microsoft Container Registry. |
| 36 | +10. Execute command shown bellow to check if the deployment succeeded . Note that it might take a little time for all the objects to be created and for pod(s) to initialize. |
45 | 37 | ```console
|
46 |
| -kubectl get pods |
47 |
| -NAME READY STATUS RESTARTS AGE |
48 |
| -local-gateway-55f774f844-bv9wt 1/1 Running 0 1m |
| 38 | +kubectl get deployments |
| 39 | +NAME READY UP-TO-DATE AVAILABLE AGE |
| 40 | +<gateway-name> 1/1 1 1 18s |
49 | 41 | ```
|
50 |
| -12. Run the below command to check the gateway service is running. Note that your service name will be different. |
| 42 | +11. Execute command shown bellow to check if the service was successfully created. Note that your service IPs and ports will be different. |
51 | 43 | ```console
|
52 | 44 | kubectl get services
|
53 |
| -NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
54 |
| -localgateway NodePort 10.110.230.87 <none> 80:32504/TCP,443:30043/TCP 1m |
| 45 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 46 | +<gateway-name> LoadBalancer 10.99.236.168 <pending> 80:31620/TCP,443:30456/TCP 9m1s |
55 | 47 | ```
|
56 |
| -13. Go back to the Azure portal and confirm that gateway node you just deployed is reporting healthy status. |
| 48 | +12. Go back to the Azure portal and select **Overview**. |
| 49 | +13. **Status** showing a green checkmark icon followed by the node count matching the number of replicas specified in the YAML file confirms that deployed self-hosted gateway pods are successfully communicating with the API Management service and have a regular "heartbeat". |
57 | 50 |
|
58 | 51 | 
|
59 | 52 |
|
60 | 53 | > [!TIP]
|
61 |
| -> Use <code>kubectl logs <gateway-pod-name></code> command to view a snapshot of self-hosted gateway log. |
| 54 | +> Execute <code>kubectl logs deployment/<gateway-name></code> command to view logs from a randomly selected pod if there are more than one. |
| 55 | +> Execute <code>kubectl logs -h</code> for a complete set of command options, e.g. how to view logs for a specific pod or container. |
62 | 56 |
|
63 |
| -## Next steps |
| 57 | +## Production deployment considerations |
| 58 | + |
| 59 | +### Access token |
| 60 | +Without a valid access token self-hosted gateway is unable to access and download configuration from configuration data endpoint of the associated API Management service. Access token can be valid for a maximum of 30 days. It must be regenerated and the cluster configured with a fresh token either manually or via automation before it expires. When automating token refresh use this management API [operation](https://docs.microsoft.com/rest/api/apimanagement/2019-12-01/gateway/generatetoken) to generated a new token. Follow this [link](https://kubernetes.io/docs/concepts/configuration/secret) for information on managing Kubernetes secrets. |
| 61 | + |
| 62 | +### Namespace |
| 63 | +Kubernetes [namespaces](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/) help with dividing a single cluster among multiple teams, projects, or applications. Namespaces provide a scope for resources and names and can be associated with a resource quota and access control policies. |
| 64 | +Commands provided in the Azure portal create self-hosted gateway resources in the **default** namespace which is automatically created, exists in every cluster, and can't be deleted. |
| 65 | +Consider [creating and deploying](https://kubernetesbyexample.com/ns/) self-hosted gateway into a separate namespace in production. |
| 66 | + |
| 67 | +### Number of replicas |
| 68 | +Minimal number of replicas suitable in production is two. |
| 69 | + |
| 70 | +By default, self-hosted gateway is deployed with a **RollingUpdate** deployment [strategy](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#strategy). Review the default values and consider explicitly setting [**maxUnavailable**](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#max-unavailable) and [**maxSurge**](https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#max-surge) fields, especially when using a high replica count. |
| 71 | + |
| 72 | +### Container resources |
| 73 | +By default, the YAML file provided in the Azure portal doesn't specify container resource requests. |
64 | 74 |
|
65 |
| -* To learn more about the self-hosted gateway, see [Azure API Management self-hosted gateway overview](self-hosted-gateway-overview.md) |
66 |
| -* Learn more about [Azure Kubernetes Service](https://docs.microsoft.com/azure/aks/intro-kubernetes) |
| 75 | +It's impossible to reliably predict and recommend the amount of per-container CPU and memory resources and the number of replicas required for supporting a specific workload due to many factors at play, e.g.: |
67 | 76 |
|
| 77 | +- Specific hardware the cluster is running on |
| 78 | +- Presence and type of virtualization |
| 79 | +- Number and rate of concurrent client connections |
| 80 | +- Request rate |
| 81 | +- Kind and number of configured policies |
| 82 | +- Payload size and if payloads are buffered or streamed |
| 83 | +- Backend service latency |
| 84 | + |
| 85 | +We recommend setting resource request to 2 cores and 2 GiB as a starting point, performing a load test and scaling up/out or down/in based the results. |
| 86 | + |
| 87 | +### Container image tag |
| 88 | +The YAML file provided in the Azure portal uses the **latest** tag which always references the most recent version of the self-hosted gateway container image. |
| 89 | + |
| 90 | +Consider using a specific version tag in production to avoid unintentional upgrade to a newer version. |
| 91 | + |
| 92 | +Follow this [link](https://mcr.microsoft.com/v2/azure-api-management/gateway/tags/list) to see the full list of available tags. |
| 93 | + |
| 94 | +### DNS policy |
| 95 | +DNS name resolution plays a critical role in self-hosted gateway's ability to connect to dependencies in Azure and dispatch API calls to backend services. |
| 96 | + |
| 97 | +The YAML file provided in the Azure portal applies the default [**ClusterFirst**](https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy) policy which causes name resolution requests not resolved by the cluster DNS to be forwarded to the upstream DNS server inherited from the node. |
| 98 | + |
| 99 | +Follow this [link](https://kubernetes.io/docs/concepts/services-networking/dns-pod-service) to learn about name resolution in Kubernetes and consider customizing [DNS policy](https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-policy) or D[NS configuration](https://kubernetes.io/docs/concepts/services-networking/dns-pod-service/#pod-s-dns-config) as appropriate for your setup. |
| 100 | + |
| 101 | +### Configuration backup |
| 102 | +Follow this [link](self-hosted-gateway-overview.md#connectivity-to-azure) to learn about self-hosted gateway behavior in the presence of a temporary Azure connectivity outage. |
| 103 | +Configure local storage volume for the self-hosted gateway container, so it can persist a backup copy of the latest downloaded configuration and, if connectivity is down, use it upon restart. The volume mount path must be <code>/apim/config</code>. See an example [here](https://github.com/Azure/api-management-self-hosted-gateway/blob/master/examples/self-hosted-gateway-with-configuration-backup.yaml). |
| 104 | +To learn about storage in Kubernetes follow this [link](https://kubernetes.io/docs/concepts/storage/volumes/). |
| 105 | + |
| 106 | +### Local logs and metrics |
| 107 | +Self-hosted gateway sends telemetry to [Azure Monitor](api-management-howto-use-azure-monitor.md) and [Azure Application Insights](api-management-howto-app-insights.md) per configuration settings in the associated API Management service. |
| 108 | +When [connectivity to Azure](self-hosted-gateway-overview.md#connectivity-to-azure) is temporarily lost, the flow of telemetry to Azure is interrupted and the data is lost for the duration of the outage. |
| 109 | +Consider [setting up local monitoring](api-management-howto-configure-local-metrics-and-logs.md) to ensure ability to observe the API traffic and prevent telemetry loss during Azure connectivity outages. |
| 110 | + |
| 111 | +## Next steps |
68 | 112 |
|
| 113 | +* To learn more about the self-hosted gateway, see [Azure API Management self-hosted gateway overview](self-hosted-gateway-overview.md) |
0 commit comments