<p>How can egress from a Kubernetes pod be limited to only specific FQDN/DNS with Azure CNI Network Policies?</p>
<p>This is something that can be achieved with:</p>
<p>Istio</p>
<pre><code>apiVersion: config.istio.io/v1alpha2
kind: EgressRule
metadata:
  name: googleapis
  namespace: default
spec:
  destination:
      service: &quot;*.googleapis.com&quot;
  ports:
      - port: 443
        protocol: https
</code></pre>
<p>Cilium</p>
<pre><code>apiVersion: &quot;cilium.io/v2&quot;
kind: CiliumNetworkPolicy
metadata:
  name: &quot;fqdn&quot;
spec:
  endpointSelector:
    matchLabels:
      app: some-pod
  egress:
  - toFQDNs:
    - matchName: &quot;api.twitter.com&quot;  
  - toEndpoints:
    - matchLabels:
        &quot;k8s:io.kubernetes.pod.namespace&quot;: kube-system
        &quot;k8s:k8s-app&quot;: kube-dns
    toPorts:
    - ports:
      - port: &quot;53&quot;
        protocol: ANY
      rules:
        dns:
        - matchPattern: &quot;*&quot;
</code></pre>
<p>OpenShift</p>
<pre><code>apiVersion: network.openshift.io/v1
kind: EgressNetworkPolicy
metadata:
  name: default-rules 
spec:
  egress: 
  - type: Allow
    to:
      dnsName: www.example.com
  - type: Deny
    to:
      cidrSelector: 0.0.0.0/0
</code></pre>
<p>How can something similar be done with Azure CNI Network Policies?</p>

ALeX
<p>In case someone is hitting this page from google:</p>
<p>I found a solution that works nicely on my cloud provider (OpenTelekomCloud) and probably will on many other.</p>
<p>There is a project called <a href="https://github.com/GoogleCloudPlatform/gke-fqdnnetworkpolicies-golang" rel="nofollow noreferrer">gke-fqdnnetworkpolicies-golang</a></p>
<p>By defining a custom resource</p>
<pre><code>apiVersion: networking.gke.io/v1alpha3
kind: FQDNNetworkPolicy
metadata:
  name: allow-test
  namespace: test1
spec:
  podSelector: {}
  egress:
    - to:
      - fqdns:
        - heise.de
      ports:
      - port: 443
        protocol: TCP
      - port: 80
        protocol: TCP
</code></pre>
<p>it will resolve the FQDNs, produce the final NetworkPolicy and update the records every 30 seconds.
This is what the final Policy will look like</p>
<pre><code>apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: allow-test
  namespace: test1
  annotations:
    fqdnnetworkpolicies.networking.gke.io/owned-by: allow-test
spec:
  podSelector: {}
  egress:
    - ports:
        - protocol: TCP
          port: 443
        - protocol: TCP
          port: 80
      to:
        - ipBlock:
            cidr: 128.65.210.8/32
  policyTypes:
    - Ingress
    - Egress
</code></pre>
<p>I had to append following permissions to the clusterRole <strong>fqdnnetworkpolicies-manager-role</strong> in the yaml (downloaded from the release page) to make it work outside GKE</p>
<pre><code>apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: fqdnnetworkpolicies-manager-role
rules:

...

- apiGroups:
  - networking.k8s.io
  resources:
  - networkpolicies
  verbs:
  - create
  - delete
  - get
  - list
  - patch
  - update
  - watch
- apiGroups:
  - networking.k8s.io
  resources:
  - networkpolicies/status
  verbs:
  - get
  - patch
  - update
</code></pre>
