运维八一 运维八一
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
Source (opens new window)

运维八一

运维,运维!
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
Source (opens new window)
  • Devops笔记

  • Kubernetes

  • 运维监控系统

  • go分布式爬虫

  • Linux性能优化

  • 夜莺(nightingale)开源观测平台

  • kubernetes-network-policy-recipes

    • create-cluster
    • deny-all-traffic-to-an-application
    • allow-all-traffic-to-an-application
    • limit-traffic-to-an-application
    • deny-all-non-whitelisted-traffic-in-the-namespace
    • deny-traffic-from-other-namespaces
      • DENY all traffic from other namespaces
        • Example
      • Try it out
        • Cleanup
    • allow-traffic-from-all-namespaces
    • allow-traffic-from-a-namespace
    • allow-traffic-from-some-pods-in-another-namespace
    • allow-external-traffic
    • allow-traffic-only-to-a-port
    • allowing-traffic-with-multiple-selectors
    • deny-egress-traffic-from-an-application
    • deny-all-non-whitelisted-traffic-from-the-namespace
    • deny-external-egress-traffic
    • README
  • 专栏
  • kubernetes-network-policy-recipes
lyndon
2024-02-26
目录

deny-traffic-from-other-namespaces

# DENY all traffic from other namespaces

(a.k.a LIMIT access to the current namespace)

You can configure a NetworkPolicy to deny all the traffic from other namespaces while allowing all the traffic coming from the same namespace the pod deployed to.

Use Cases

  • You do not want deployments in test namespace to accidentally send traffic to other services or databases in prod namespace.
  • You host applications from different customers in separate Kubernetes namespaces and you would like to block traffic coming from outside a namespace.

Diagram of DENY all traffic from other namespaces policy

# Example

Start a web service in namespace default:

$ kubectl run web --namespace=default --image=nginx --labels="app=web" --expose --port=80
1

Save the following manifest to deny-from-other-namespaces.yaml and apply to the cluster:

kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  namespace: default
  name: deny-from-other-namespaces
spec:
  podSelector:
    matchLabels:
  ingress:
  - from:
    - podSelector: {}
1
2
3
4
5
6
7
8
9
10
11
$ kubectl apply -f deny-from-other-namespaces.yaml
networkpolicy "deny-from-other-namespaces" created"
1
2

Note a few things about this manifest:

  • namespace: default deploys it to the default namespace.
  • it applies the policy to ALL pods in default namespace as the spec.podSelector.matchLabels is empty and therefore selects all pods.
  • it allows traffic from ALL pods in the default namespace, as spec.ingress.from.podSelector is empty and therefore selects all pods.

# Try it out

Query this web service from the foo namespace:

$ kubectl create namespace foo
$ kubectl run test-$RANDOM --namespace=foo --rm -i -t --image=alpine -- sh
/ # wget -qO- --timeout=2 http://web.default
wget: download timed out
1
2
3
4

It blocks the traffic from foo namespace!

Any pod in default namespace should work fine:

$ kubectl run test-$RANDOM --namespace=default --rm -i -t --image=alpine -- sh
/ # wget -qO- --timeout=2 http://web.default
<!DOCTYPE html>
<html>
1
2
3
4

# Cleanup

$ kubectl delete pod web -n default
$ kubectl delete service web -n default
$ kubectl delete networkpolicy deny-from-other-namespaces -n default
$ kubectl delete namespace foo
1
2
3
4
上次更新: 2024/02/26, 10:14:04
deny-all-non-whitelisted-traffic-in-the-namespace
allow-traffic-from-all-namespaces

← deny-all-non-whitelisted-traffic-in-the-namespace allow-traffic-from-all-namespaces→

最近更新
01
ctr和crictl显示镜像不一致
03-13
02
alpine镜像集成常用数据库客户端
03-13
03
create-cluster
02-26
更多文章>
Theme by Vdoing | Copyright © 2015-2024 op81.com
苏ICP备18041258号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式