Difference between revisions of "K8s network policy"

From UVOO Tech Wiki
Jump to navigation Jump to search
Line 49: Line 49:
 
spec:
 
spec:
 
   podSelector: {}
 
   podSelector: {}
 +
  policyTypes:
 +
    - Egress
 +
    # - Ingress
 
   egress:
 
   egress:
 
   - to:
 
   - to:
 
     - namespaceSelector:
 
     - namespaceSelector:
 
         matchLabels:
 
         matchLabels:
           networking/namespace: kube-system
+
           kubernetes.io/metadata.name: kube-system
 
       podSelector:
 
       podSelector:
 
         matchLabels:
 
         matchLabels:
Line 62: Line 65:
 
     - protocol: UDP
 
     - protocol: UDP
 
       port: 53
 
       port: 53
  policyTypes:
 
  - Egress
 
 
```
 
```
  

Revision as of 12:34, 8 July 2023

https://loft.sh/blog/kubernetes-network-policies-for-isolating-namespaces/

https://kubernetes.io/docs/concepts/services-networking/network-policies/

https://editor.networkpolicy.io/?id=u7ZyunLd9YSsf9Da

https://cloud.redhat.com/blog/guide-to-kubernetes-egress-network-policies

k8s core DNS example egress

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: foo
spec:
  podSelector:
    matchLabels:
      run: nginx
  policyTypes:
    - Egress
  egress:
    - to:
        - ipBlock:
            cidr: 192.168.0.0/16
      ports:
        - protocol: TCP
          port: 80
          endPort: 81
    - to:
        - namespaceSelector: {}
          podSelector:
            matchLabels:
              k8s-app: kube-dns
      ports:
        - port: 53
          protocol: UDP
        - port: 53
          protocol: TCP

Only k8s dns

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: default-deny-all-egress
spec:
  podSelector: {}
  policyTypes:
    - Egress
    # - Ingress
  egress:
  - to:
    - namespaceSelector:
        matchLabels:
          kubernetes.io/metadata.name: kube-system
      podSelector:
        matchLabels:
          k8s-app: kube-dns
    ports:
    - protocol: TCP
      port: 53
    - protocol: UDP
      port: 53

Internet

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
  name: internet-egress
spec:
  podSelector:
    matchLabels:
      networking/allow-internet-egress: "true"
  egress:
  - {}
  policyTypes:
  - Egress