9.2 KiB
| title | description | keywords | weight | aliases | owner | test | |||
|---|---|---|---|---|---|---|---|---|---|
| Egress using Wildcard Hosts | Describes how to enable egress traffic for a set of hosts in a common domain, instead of configuring each and every host separately. |
|
50 |
|
istio/wg-networking-maintainers | yes |
The Accessing External Services task and
the Configure an Egress Gateway example
describe how to configure egress traffic for specific hostnames, like edition.cnn.com.
This example shows how to enable egress traffic for a set of hosts in a common domain, for
example *.wikipedia.org, instead of configuring each and every host separately.
Background
Suppose you want to enable egress traffic in Istio for the wikipedia.org sites in all languages.
Each version of wikipedia.org in a particular language has its own hostname, e.g., en.wikipedia.org and
de.wikipedia.org in the English and the German languages, respectively.
You want to enable egress traffic by common configuration items for all the Wikipedia sites,
without the need to specify every language's site separately.
Before you begin
-
Install Istio using the
democonfiguration profile and with the blocking-by-default outbound traffic policy:{{< text bash >}} $ istioctl install --set profile=demo --set meshConfig.outboundTrafficPolicy.mode=REGISTRY_ONLY {{< /text >}}
{{< tip >}} You can run this task on an Istio configuration other than the
demoprofile as long as you make sure to deploy the Istio egress gateway, enable Envoy’s access logging, and apply the blocking-by-default outbound traffic policy in your installation. You will also need to add the second gateway using your ownIstioOperatorCR instead of the one shown in setup egress gateway with SNI proxy. {{< /tip >}} -
Deploy the [sleep]({{< github_tree >}}/samples/sleep) sample app to use as a test source for sending requests. If you have automatic sidecar injection enabled, run the following command to deploy the sample app:
{{< text bash >}} $ kubectl apply -f @samples/sleep/sleep.yaml@ {{< /text >}}
Otherwise, manually inject the sidecar before deploying the
sleepapplication with the following command:{{< text bash >}} $ kubectl apply -f <(istioctl kube-inject -f @samples/sleep/sleep.yaml@) {{< /text >}}
{{< tip >}} You can use any pod with
curlinstalled as a test source. {{< /tip >}} -
Set the
SOURCE_PODenvironment variable to the name of your source pod:{{< text bash >}}
export SOURCE_POD=(kubectl get pod -l app=sleep -o jsonpath={.items..metadata.name}) {{< /text >}}
Configure direct traffic to a wildcard host
The first, and simplest, way to access a set of hosts within a common domain is by configuring
a simple ServiceEntry with a wildcard host and calling the services directly from the sidecar.
When calling services directly (i.e., not via an egress gateway), the configuration for
a wildcard host is no different than that of any other (e.g., fully qualified) host,
only much more convenient when there are many hosts within the common domain.
{{< warning >}} Note that the configuration below can be easily bypassed by a malicious application. For a secure egress traffic control, direct the traffic through an egress gateway. {{< /warning >}}
{{< warning >}}
Note that the DNS resolution cannot be used for wildcard hosts. This is why the NONE resolution (omitted since it is
the default) is used in the service entry below.
{{< /warning >}}
-
Define a
ServiceEntryfor*.wikipedia.org:{{< text bash >}} $ kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: ServiceEntry metadata: name: wikipedia spec: hosts:
- "*.wikipedia.org" ports:
- number: 443 name: https protocol: HTTPS EOF {{< /text >}}
-
Send HTTPS requests to https://en.wikipedia.org and https://de.wikipedia.org:
{{< text bash >}} $ kubectl exec "$SOURCE_POD" -c sleep -- sh -c 'curl -s https://en.wikipedia.org/wiki/Main_Page | grep -o ""; curl -s https://de.wikipedia.org/wiki/Wikipedia:Hauptseite | grep -o ""'
{{< /text >}}
Cleanup direct traffic to a wildcard host
{{< text bash >}} $ kubectl delete serviceentry wikipedia {{< /text >}}
Wildcard configuration for a single hosting server
When all wildcard hosts are served by a single server, the configuration for egress gateway-based access to a wildcard host is very similar to that of any host, with one exception: the configured route destination will not be the same as the configured host, i.e., the wildcard. It will instead be configured with the host of the single server for the set of domains.
-
Create an egress
Gatewayfor *.wikipedia.org, a destination rule and a virtual service to direct the traffic through the egress gateway and from the egress gateway to the external service.{{< text bash >}} $ kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: Gateway metadata: name: istio-egressgateway spec: selector: istio: egressgateway servers:
- port:
number: 443
name: https
protocol: HTTPS
hosts:
- "*.wikipedia.org" tls: mode: PASSTHROUGH
apiVersion: networking.istio.io/v1alpha3 kind: DestinationRule metadata: name: egressgateway-for-wikipedia spec: host: istio-egressgateway.istio-system.svc.cluster.local subsets: - name: wikipedia
apiVersion: networking.istio.io/v1alpha3 kind: VirtualService metadata: name: direct-wikipedia-through-egress-gateway spec: hosts:
- "*.wikipedia.org" gateways:
- mesh
- istio-egressgateway tls:
- match:
- gateways:
- mesh port: 443 sniHosts:
- "*.wikipedia.org" route:
- destination: host: istio-egressgateway.istio-system.svc.cluster.local subset: wikipedia port: number: 443 weight: 100
- gateways:
- match:
- gateways:
- istio-egressgateway port: 443 sniHosts:
- "*.wikipedia.org" route:
- destination: host: www.wikipedia.org port: number: 443 weight: 100 EOF {{< /text >}}
- gateways:
- port:
number: 443
name: https
protocol: HTTPS
hosts:
-
Create a
ServiceEntryfor the destination server, www.wikipedia.org.{{< text bash >}} $ kubectl apply -f - <<EOF apiVersion: networking.istio.io/v1alpha3 kind: ServiceEntry metadata: name: www-wikipedia spec: hosts:
- www.wikipedia.org ports:
- number: 443 name: https protocol: HTTPS resolution: DNS EOF {{< /text >}}
-
Send HTTPS requests to https://en.wikipedia.org and https://de.wikipedia.org:
{{< text bash >}} $ kubectl exec "$SOURCE_POD" -c sleep -- sh -c 'curl -s https://en.wikipedia.org/wiki/Main_Page | grep -o ""; curl -s https://de.wikipedia.org/wiki/Wikipedia:Hauptseite | grep -o ""'
{{< /text >}} -
Check the statistics of the egress gateway's proxy for the counter that corresponds to your requests to *.wikipedia.org. If Istio is deployed in the
istio-systemnamespace, the command to print the counter is:{{< text bash >}}
kubectl exec "(kubectl get pod -l istio=egressgateway -n istio-system -o jsonpath='{.items[0].metadata.name}')" -c istio-proxy -n istio-system -- pilot-agent request GET clusters | grep '^outbound|443||www.wikipedia.org.*cx_total:' outbound|443||www.wikipedia.org::208.80.154.224:443::cx_total::2 {{< /text >}}
Cleanup wildcard configuration for a single hosting server
{{< text bash >}} $ kubectl delete serviceentry www-wikipedia $ kubectl delete gateway istio-egressgateway $ kubectl delete virtualservice direct-wikipedia-through-egress-gateway $ kubectl delete destinationrule egressgateway-for-wikipedia {{< /text >}}
Cleanup
-
Shutdown the [sleep]({{< github_tree >}}/samples/sleep) service:
{{< text bash >}} $ kubectl delete -f @samples/sleep/sleep.yaml@ {{< /text >}}
-
Uninstall Istio from your cluster:
{{< text bash >}} $ istioctl uninstall --purge -y {{< /text >}}