运维八一 运维八一
首页
运维杂记
编程浅尝
周积跬步
专栏
生活
关于
收藏
  • 分类
  • 标签
  • 归档
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
    • 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
    目录

    allow-traffic-from-all-namespaces

    # ALLOW traffic to an application from all namespaces

    This NetworkPolicy will allow traffic from all pods in all namespaces to a particular application.

    Use Case:

    • You have a common service or a database which is used by deployments in different namespaces.

    You do not need this policy unless there is already a NetworkPolicy blocking traffic to the application or a NetworkPolicy blocking non-whitelisted traffic to all pods in the namespace.

    Diagram of  ALLOW traffic to an application from all namespaces policy

    # Example

    Start a web service on default namespace:

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

    Save the following manifest to web-allow-all-namespaces.yaml and apply to the cluster:

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

    Note a few things about this NetworkPolicy manifest:

    • Applies the policy only to app:web pods in default namespace.
    • Selects all pods in all namespaces (namespaceSelector: {}).
    • By default, if you omit specifying a namespaceSelector it does not select any namespaces, which means it will allow traffic only from the namespace the NetworkPolicy is deployed to.

    Note: Dropping all selectors from the spec.ingress.from item has the same effect of matching all pods in all namespaces. e.g.:

    ...
       ingress:
         - from:
    

    However, prefer the syntax in the full manifest clear expression of intent.

    # Try it out

    Create a new namespace called secondary and query this web service in the default namespace:

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

    Similarly, it also works if you query it from any pod deployed to bar.

    # Cleanup

    kubectl delete pod web -n default
    kubectl delete service web -n default
    kubectl delete networkpolicy web-allow-all-namespaces -n default
    kubectl delete namespace secondary
    
    上次更新: 2024/02/26, 10:14:04
    deny-traffic-from-other-namespaces
    allow-traffic-from-a-namespace

    ← deny-traffic-from-other-namespaces allow-traffic-from-a-namespace→

    最近更新
    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
    • 跟随系统
    • 浅色模式
    • 深色模式
    • 阅读模式