Using External Services Discovery In ApisixUpstream
In this tutorial, we will introduce how to configure external services discovery in the ApisixUpstream resources.
APISIX already supports various service discovery components, such as DNS, consul, nacos, etc. Please see Integration service discovery registry for details.
#
Prerequisites- An available Kubernetes cluster
- An available APISIX and APISIX Ingress Controller installation
We assume that your APISIX is installed in the apisix
namespace.
#
IntroductionIntegration of APISIX Ingress with service discovery components is configured through the ApisixUpstream resource.
In this case, we don't configure the backends
field in the ApisixRoute resource.
Instead, we will use the upstreams
field to refer to an ApisixUpstream resources with the discovery
field configured.
For example:
# httpbin-route.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: httpbin-route
spec:
http:
- name: rule1
match:
hosts:
- local.httpbin.org
paths:
- /*
# backends: # We won't use the `backends` field
# - serviceName: httpbin
# servicePort: 80
upstreams:
- name: httpbin-upstream
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.
The referenced ApisixUpstream MUST have discovery
field configured. For example:
# httpbin-upstream.yaml
apiVersion: apisix.apache.org/v2
kind: ApisixUpstream
metadata:
name: httpbin-upstream
spec:
discovery:
type: dns
serviceName: httpbin.default.svc.cluster.local
In this yaml example, we configured httpbin.default.svc.cluster.local
as the backend.
The type of service discovery needs to be pre-configured in APISIX. For example:
discovery:
dns:
servers:
- "10.96.0.10:53" # default kube-dns cluster IP.
After applying the above configuration, we can try to access httpbin.default.svc.cluster.local
directly through APISIX.
note
The above discovery configuration needs to be configured at the time of installation and cannot be edited later. For example, if you're installing via helm chart, make sure that you use the below configuration to override default helm values.
apisix:
discovery:
enabled: true
registry:
dns:
servers:
- "172.17.0.11:53" # replace with your server addresses