Kubernetes NetworkPolicy provides L3/L4 network isolation but cannot enforce application-level access control. Service Mesh (ASM) uses a zero-trust model: you create authorization policies that dynamically allow or deny egress traffic from services in a namespace to external websites, without code changes.
This walkthrough blocks HTTP egress from all services in the demo-frontend namespace to www.aliyun.com. The same pattern applies to any namespace and external host.
Prerequisites
Before you begin, make sure that you have:
A cluster added to the ASM instance. For more information, see Add a cluster to an ASM instance
A namespace named
demo-frontendwith automatic sidecar proxy injection enabled. For more information, see Manage global namespaces
Step 1: Deploy a test service
Deploy a sleep service in the demo-frontend namespace to generate egress traffic for testing.
Connect to the cluster with kubectl. For more information, see Obtain the kubeconfig file of a cluster and use kubectl to connect to the cluster.
Save the following YAML as sleep.yaml:
Apply the manifest:
kubectl apply -f sleep.yaml -n demo-frontendVerify that the sidecar proxy is injected:
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, click the name of the target cluster. In the left-side navigation pane, choose Workloads > Pods.
Select demo-frontend from the Namespace drop-down list, then click the pod name of the sleep service.
On the Container tab, confirm that a container named istio-proxy exists. This indicates that the sidecar proxy is running.
Step 2: Create an egress gateway
An egress gateway centralizes outbound traffic from the mesh so that authorization policies can enforce access control at a single point. Create an egress gateway named egressgateway. For detailed instructions, see Create an egress gateway.
Step 3: Restrict outbound traffic and register the external host
By default, services in an ASM instance can reach any external endpoint. Switch the mesh-level outbound traffic policy to REGISTRY_ONLY to block access to hosts not registered as service entries.
Set the outbound traffic policy
Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Dataplane Component Management > Sidecar Proxy Setting.
On the global tab, set Outbound Traffic Policy to REGISTRY_ONLY, then click Update Settings.
Register www.aliyun.com as a service entry
On the ASM instance details page, in the left-side navigation pane, choose Cluster & Workload Management > External Service(ServiceEntry).
Click Create from YAML. Select istio-system from the Namespace drop-down list and paste the following YAML:
apiVersion: networking.istio.io/v1beta1 kind: ServiceEntry metadata: name: aliyuncom-ext namespace: istio-system spec: hosts: - www.aliyun.com location: MESH_EXTERNAL ports: - name: http number: 80 protocol: HTTP - name: tls number: 443 protocol: TLS resolution: DNSClick Create.
Step 4: Route egress traffic through the gateway
Create three Istio resources to route traffic from the demo-frontend namespace through the egress gateway to www.aliyun.com. The Gateway and DestinationRule enforce mutual TLS (mTLS) between the sidecar proxy and the egress gateway.
Istio gateway
Create the following gateway in the istio-system namespace. For more information, see Manage Istio gateways.
apiVersion: networking.istio.io/v1beta1
kind: Gateway
metadata:
name: istio-egressgateway
namespace: istio-system
spec:
selector:
istio: egressgateway
servers:
- port:
number: 80
name: http
protocol: HTTPS
tls:
mode: ISTIO_MUTUAL # Requires mTLS for all egress traffic
hosts:
- '*'Setting mode to ISTIO_MUTUAL means that services must pass mesh-managed mTLS authentication before reaching any external host.
Destination rule
Create the following destination rule in the demo-frontend namespace. For more information, see Manage destination rules.
apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
name: target-egress-gateway
namespace: demo-frontend
spec:
host: istio-egressgateway.istio-system.svc.cluster.local
subsets:
- name: target-egress-gateway-mTLS
trafficPolicy:
loadBalancer:
simple: ROUND_ROBIN
tls:
mode: ISTIO_MUTUAL # mTLS to the egress gatewayVirtual service
Create the following virtual service in the demo-frontend namespace. For more information, see Manage virtual services.
The http section defines two routing rules:
| Rule | Gateway match | Behavior |
|---|---|---|
| Sidecar-to-gateway | mesh | Forwards traffic from pods in demo-frontend to the egress gateway |
| Gateway-to-external | istio-system/istio-egressgateway | Forwards traffic from the egress gateway to www.aliyun.com |
Step 5: Create a DENY authorization policy
Create an authorization policy that instructs the egress gateway to deny all traffic originating from the demo-frontend namespace.
Log on to the ASM console. In the left-side navigation pane, choose Service Mesh > Mesh Management.
On the Mesh Management page, click the name of the ASM instance. In the left-side navigation pane, choose Mesh Security Center > AuthorizationPolicy. Click Create.
Configure the following parameters:
Parameter Value Name A descriptive name for the policy Policy Type DENY ASM Gateway (on the Gateway Scope tab) egressgateway Request Matching Rules In Add Request Source, enable Namespaces and set the value to demo-frontendClick Create.
Step 6: Verify the policy
Confirm that services in the demo-frontend namespace can no longer reach www.aliyun.com.
Log on to the ACK console. In the left-side navigation pane, click Clusters.
On the Clusters page, click the cluster name. In the left-side navigation pane, choose Workloads > Pods.
Select demo-frontend from the Namespace drop-down list. Find the sleep pod and click Terminal > sleep in the Actions column.
Run the following command:
curl -I http://www.aliyun.comExpected output:
HTTP/1.1 403 Forbidden content-length: 19 content-type: text/plain date: Thu, 12 Oct 2023 07:14:09 GMT server: envoy x-envoy-upstream-service-time: 4The
403 Forbiddenresponse confirms that the authorization policy blocks egress traffic from thedemo-frontendnamespace towww.aliyun.com. Theserver: envoyheader indicates that the Envoy-based egress gateway intercepted the request, not the external host.