The DNS proxy feature in a Service Mesh allows the sidecar proxy to act as a caching DNS proxy. When an application sends a DNS query, the Service Mesh transparently intercepts and resolves the query. This topic describes how to enable and use the DNS proxy feature in ASM.
Prerequisites
-
You have an ASM instance of version 1.8.3.17 or later. For more information, see Create an ASM instance.
-
You have enabled automatic sidecar injection for the default namespace. For more information, see Enable automatic sidecar injection.
Background
By default, each ACK cluster deploys a DNS service to provide domain name resolution for workloads. This allows applications that run in a Kubernetes cluster to use DNS to discover other services in the cluster.
A DNS server runs in each Kubernetes cluster, and every pod uses this internal DNS server for domain name resolution. By default, DNS requests are not intercepted by the sidecar proxy, and each application attempts to resolve DNS names before it opens connections to other services. After you enable the DNS proxy feature in ASM, the sidecar proxy transparently intercepts and resolves DNS queries from applications, accelerating domain name resolution.
Enable DNS proxy
Scenario 1: Enable DNS proxy globally
-
Log on to the ASM console. In the left-side navigation pane, choose .
-
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose .
-
On the global tab, click DNS Proxy, turn on the Enable DNS Proxy Feature switch, and then click Update Settings.
-
Restart the pod to apply the DNS configuration.
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Pods page, find the target pod and choose in the Actions column.
-
In the Message dialog box, click Confirm.
The new configuration takes effect when the pod restarts.
Scenario 2: Enable DNS proxy for a specific namespace
-
Log on to the ASM console. In the left-side navigation pane, choose .
-
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose .
-
On the Configure the agent parameters of the injected Sidecar page, click the Namespaces tab.
-
Select a Namespaces, click DNS Proxy, turn on the Enable DNS Proxy Feature switch, and then click Update Settings.
-
Restart the pod to apply the DNS configuration.
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Pods page, find the target pod and choose in the Actions column.
-
In the Message dialog box, click Confirm.
The new configuration takes effect when the pod restarts.
Scenario 3: Enable DNS proxy for a specific pod
To enable the DNS proxy feature for a specific pod, add an annotation to the pod's YAML configuration.
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Deployments page, find the target application and choose from the Actions column.
-
In the Edit YAML dialog box, add the following annotation to spec.template.metadata.annotations, and then click Update.
annotations: proxy.istio.io/config: | proxyMetadata: ISTIO_META_DNS_CAPTURE: "true" ISTIO_META_DNS_AUTO_ALLOCATE: "true" -
Restart the pod to apply the DNS configuration.
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Pods page, find the target pod and choose in the Actions column.
-
In the Message dialog box, click Confirm.
The new configuration takes effect when the pod restarts.
Use DNS proxy
Step 1: Create a ServiceEntry
Create a ServiceEntry to add aliyun.com to the internal service registry of the Service Mesh.
-
Log on to the ASM console.
-
In the left-side navigation pane, choose .
-
On the Mesh Management page, find the ASM instance that you want to configure. Click the name of the ASM instance or click Manage in the Actions column.
-
On the details page of the ASM instance, choose . On the page that appears, click Create from YAML.
-
On the Create page, select a Namespaces, choose a Scenario Template, copy the following content into the text box, and then click Create.
apiVersion: networking.istio.io/v1beta1 kind: ServiceEntry metadata: name: test1-mydnsproxying spec: hosts: - aliyun.com location: MESH_EXTERNAL ports: - number: 443 name: https protocol: TLS resolution: DNS
Step 2: Deploy a sample application
-
Obtain the KubeConfig file of a cluster and use kubectl to connect to the cluster.
-
Create a file named sleep.yaml that contains the following content.
-
Run the following command to deploy the sleep application.
kubectl apply -f sleep.yaml -
Run the following command to check whether the sleep pod has started.
kubectl get pod |grep sleepExpected output:
NAME READY STATUS RESTARTS AGE sleep-66cd8f684f-nxw8v 2/2 Running 0 16m
Step 3: Enable DNS proxy for the sleep container
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Deployments page, find the target application and choose from the Actions column.
-
In the Edit YAML dialog box, add the following annotation to spec.template.metadata.annotations, and then click Update.
annotations: proxy.istio.io/config: | proxyMetadata: ISTIO_META_DNS_CAPTURE: "true" ISTIO_META_DNS_AUTO_ALLOCATE: "true" -
Restart the pod to apply the DNS configuration.
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Pods page, find the target pod and choose in the Actions column.
-
In the Message dialog box, click Confirm.
The new configuration takes effect when the pod restarts.
Step 4: Verify the DNS proxy
-
Check the logs of the istio-init container.
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Pods page, click the name of the sleep pod.
-
On the pod details page, click the Logs tab and set Containers to istio-init.
The following logs are displayed:
-A OUTPUT -p udp --dport 53 -d 192.168.0.10/32 -j REDIRECT --to-port 15053 -A ISTIO_OUTPUT -p tcp --dport 53 -d 192.168.0.10/32 -j REDIRECT --to-ports 15053The sidecar proxy intercepts all DNS queries from the application container by using iptables rules, just as it intercepts other types of traffic. Istio adds additional iptables rules to redirect all DNS packets on port 53 (TCP and UDP) that are destined for the Kubernetes DNS service, such as the CoreDNS service, to port 15053.
-
Check the port on which the pilot-agent process listens.
In the left navigation pane of the cluster management page, choose .
-
On the Pods page, find the sleep pod, click Terminal in the Actions column, and then select istio-proxy.
-
In the istio-proxy container, run the following command to check the port that the pilot-agent process listens on.
netstat -anp |grep 15053Expected output:
tcp 0 0 127.0.0.1:15053 0.0.0.0:* LISTEN 1/pilot-agent udp 0 0 127.0.0.1:15053 0.0.0.0:* 1/pilot-agentDNS queries are redirected to the pilot-agent process that runs in the sidecar proxy container. The output shows that the process is listening on port 15053.
-
Access aliyun.com.
In the left navigation pane of the cluster management page, choose .
-
On the Pods page, find the sleep pod, click Terminal in the Actions column, and then select sleep.
-
In the sleep container, run the following command to access aliyun.com.
curl -v https://aliyun.comExpected output:
* Trying 240.240.**.**:443... * Connected to aliyun.com (240.240.**.**) port 443 (#0)The returned IP address is 240.240.**.**. This is a Service Mesh automatically assigned by the Service Mesh, not the real public IP address. The service mesh uses iptables to hijack requests to kube-dns and routes them to the sidecar proxy that runs in the pod. When the application sends a request to this virtual IP address, the sidecar proxy translates it to the actual public IP address.
Enable debug logging for the DNS proxy
You can enable debug logging for the DNS proxy. When enabled, pilot-agent records every DNS query from the application container.
Log on to the ACK console. In the left navigation pane, click Clusters.
On the Clusters page, click the name of your cluster. In the left navigation pane, click .
-
On the Deployments page, find the target application and choose from the Actions column.
-
In the Edit YAML dialog box, add the following annotation under the spec parameter and click Update.
annotations: sidecar.istio.io/agentLogLevel: "dns:debug"spec: progressDeadlineSeconds: 600 replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: app: sleep strategy: rollingUpdate: maxSurge: 25% maxUnavailable: 25% type: RollingUpdate template: metadata: labels: app: sleep annotations: sidecar.istio.io/agentLogLevel: "dns:debug" -
Run the following command in the cluster to view the debug logs.
kubectl logs -n default sleep-85fdfd8896-2ctq4 -c istio-proxy | grep debugThe log entry contains
found=true, indicating that the domain name was resolved from the service mesh's internal registry.