|
| 1 | +--- |
| 2 | +title: Using External Services Discovery In ApisixUpstream |
| 3 | +--- |
| 4 | + |
| 5 | +<!-- |
| 6 | +# |
| 7 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 8 | +# contributor license agreements. See the NOTICE file distributed with |
| 9 | +# this work for additional information regarding copyright ownership. |
| 10 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 11 | +# (the "License"); you may not use this file except in compliance with |
| 12 | +# the License. You may obtain a copy of the License at |
| 13 | +# |
| 14 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | +# |
| 16 | +# Unless required by applicable law or agreed to in writing, software |
| 17 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | +# See the License for the specific language governing permissions and |
| 20 | +# limitations under the License. |
| 21 | +# |
| 22 | +--> |
| 23 | + |
| 24 | +In this tutorial, we will introduce how to configure external services discovery in the ApisixUpstream resources. |
| 25 | + |
| 26 | +APISIX already supports various service discovery components, such as DNS, consul, nacos, etc. |
| 27 | +Please see [Integration service discovery registry](https://apisix.apache.org/docs/apisix/discovery/) for details. |
| 28 | + |
| 29 | +## Prerequisites |
| 30 | + |
| 31 | +- An available Kubernetes cluster |
| 32 | +- An available APISIX and APISIX Ingress Controller installation |
| 33 | + |
| 34 | +We assume that your APISIX is installed in the `apisix` namespace. |
| 35 | + |
| 36 | +## Introduction |
| 37 | + |
| 38 | +Integration of APISIX Ingress with service discovery components is configured through the ApisixUpstream resource. |
| 39 | +In this case, we don't configure the `backends` field in the ApisixRoute resource. |
| 40 | +Instead, we will use the `upstreams` field to refer to an ApisixUpstream resources with the `discovery` field configured. |
| 41 | + |
| 42 | +For example: |
| 43 | + |
| 44 | +```yaml |
| 45 | +# httpbin-route.yaml |
| 46 | +apiVersion: apisix.apache.org/v2 |
| 47 | +kind: ApisixRoute |
| 48 | +metadata: |
| 49 | + name: httpbin-route |
| 50 | +spec: |
| 51 | + http: |
| 52 | + - name: rule1 |
| 53 | + match: |
| 54 | + hosts: |
| 55 | + - local.httpbin.org |
| 56 | + paths: |
| 57 | + - /* |
| 58 | + # backends: # We won't use the `backends` field |
| 59 | + # - serviceName: httpbin |
| 60 | + # servicePort: 80 |
| 61 | + upstreams: |
| 62 | + - name: httpbin-upstream |
| 63 | +``` |
| 64 | +
|
| 65 | +This configuration tells the ingress controller not to resolve upstream hosts through the K8s services, but to use the configuration defined in the referenced ApisixUpstream. |
| 66 | +The referenced ApisixUpstream *MUST* have `discovery` field configured. For example: |
| 67 | + |
| 68 | +```yaml |
| 69 | +# httpbin-upstream.yaml |
| 70 | +apiVersion: apisix.apache.org/v2 |
| 71 | +kind: ApisixUpstream |
| 72 | +metadata: |
| 73 | + name: httpbin-upstream |
| 74 | +spec: |
| 75 | + discovery: |
| 76 | + type: dns |
| 77 | + serviceName: httpbin.default.svc.cluster.local |
| 78 | +``` |
| 79 | + |
| 80 | +In this yaml example, we configured `httpbin.default.svc.cluster.local` as the backend. |
| 81 | +The type of service discovery needs to be pre-configured in APISIX. For example: |
| 82 | + |
| 83 | +```yaml |
| 84 | +discovery: |
| 85 | + dns: |
| 86 | + servers: |
| 87 | + - "10.96.0.10:53" # default kube-dns cluster IP. |
| 88 | +``` |
| 89 | + |
| 90 | +After applying the above configuration, we can try to access `httpbin.default.svc.cluster.local` directly through APISIX. |
0 commit comments