<p>I have a few questions regarding Kubernetes: How to secure Kubernetes cluster?</p>

<p>My plan is to develop an application that secures Kubernetes cluster by default. I have written and tested a few Network Policy successfully. 
As a second step I want to set these information dynamically in my application based on cloud provider and so one.</p>

<p>1.) I want to block access the host network as well as the meta data services (my cluster runs on AWS):</p>

<pre><code>  egress:
  - to:
    - ipBlock:
        cidr: 0.0.0.0/0
        except:
        - 10.250.0.0/16     # host network
        - 169.254.169.254/32    # metadata service
</code></pre>

<p>Does anyone know how I can access the host network dynamically?
I found an issue that says that you must use the Meta Data Service: <a href="https://github.com/kubernetes/kubernetes/issues/24657" rel="nofollow noreferrer">https://github.com/kubernetes/kubernetes/issues/24657</a> </p>

<p>Does anyone know how I can find out on which cloud provider I am currently running?
Based on that information, I want to set the meta data service IP.</p>

<p>2.) I want to block access to the "kube-system" namespace:</p>

<pre><code>  egress:
  - to:
    - podSelector:
        matchExpressions:
        - key: namespace
          operator: NotIn
          values:
          - kube-system
</code></pre>

<p>Does anyone know how I can enforce the actual denied access?
As far as I understood, the key labled "namespace" is just a name that I choosed. How does Kubernetes know that I actually mean the namespace and nothing else?</p>

<p>3.) I want to block Internet access:</p>

<pre><code>spec:
  podSelector: {}
  policyTypes:
  - Egress
</code></pre>

<p>Does anyone know, if something like the DNS server in the DMZ zone is still reachable?</p>

<p>4.) I want to block communication to pods with a different namespace:</p>

<pre><code>  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          project: default
</code></pre>

<p>Here, I developed a controller that set the namespace dynamically.</p>

<p>Your ideas are good in terms of a least-privilege policy but the implementation is problematic due to the following reasons.</p>

<ol>
<li><p>The logic you are trying to achieve it beyond the capabilities of Kubernetes network policies. It is very difficult to combine multiple block and allow policies in k8s without them conflicting with each other. For example, your first snippet allows access to any IP outside of the cluster and then your 3rd question is about blocking access to the internet - these two policies can't work simultaneously.  </p></li>
<li><p>You shouldn't block access to the kube-system namespace because that's where the k8s DNS service is deployed and blocking access to it will prevent all communications in the cluster.</p></li>
<li><p>To answer your 1st question specifically:  </p>

<ul>
<li>How I can access the host network dynamically?<br>
The cluster subnet is defined when you deploy it on AWS - you should store it during creation and inject it to your policies. Alternatively, you may be able to get it by calling an AWS API. 
You can also get the cluster node IPs from Kubernetes: <code>kubectl get nodes -o wide</code>  </li>
<li>How I can find out on which cloud provider I am currently running?<br>
Kubernetes doesn't know which platform it is running on, but you can guess it based on the node name prefix, for example: aks-nodepool1-18370774-0 or gke-...</li>
</ul></li>
<li><p>Your 4th point about blocking access between namespaces is good but it would be better to do it with an ingress policy like this:</p></li>
</ol>

<pre><code>apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: deny-all
  namespace: default
spec:
  podSelector: {}
  policyTypes:
  - Ingress
</code></pre>

<p>For more details, I recommend this blog post that explains the complexities of k8s network policies: <a href="https://medium.com/@reuvenharrison/an-introduction-to-kubernetes-network-policies-for-security-people-ba92dd4c809d" rel="nofollow noreferrer">https://medium.com/@reuvenharrison/an-introduction-to-kubernetes-network-policies-for-security-people-ba92dd4c809d</a></p>
