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

    # ALLOW traffic only to a port of an application

    This NetworkPolicy lets you define ingress rules for specific ports of an application. If you do not specify a port in the ingress rules, the rule applies to all ports.

    A port may be either a numerical or named port on a pod.

    Use Cases

    • Allow monitoring system to collect the metrics by querying the diagnostics port of your application, without giving it access to the rest of the application.

    Diagram of ALLOW traffic only to a port of an application policy

    # Example

    Run a web server pod called apiserver:

    kubectl run apiserver --image=ahmet/app-on-two-ports --labels="app=apiserver"
    

    This application returns a hello response to requests on http://:8000/ and a monitoring metrics response on http://:5000/metrics.

    Expose the pod as Service, map 8000 to 8001, map 5000 to 5001.

    kubectl create service clusterip apiserver \
        --tcp 8001:8000 \
        --tcp 5001:5000
    

    #c5f015 NOTE: Network Policies will not know the port numbers you exposed the application, such as 8001 and 5001. This is because they control inter-pod traffic and when you expose Pod as Service, ports are remapped like above. Therefore, you need to use the Pod port numbers (such as 8000 and 5000) in the NetworkPolicy specification. An alternative less error prone is to refer to the port names (such as metrics and http).

    Save this Network Policy as api-allow-5000.yaml and apply to the cluster.

    kind: NetworkPolicy
    apiVersion: networking.k8s.io/v1
    metadata:
      name: api-allow-5000
    spec:
      podSelector:
        matchLabels:
          app: apiserver
      ingress:
      - ports:
        - port: 5000
        from:
        - podSelector:
            matchLabels:
              role: monitoring
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    $ kubectl apply -f api-allow-5000.yaml
    networkpolicy "api-allow-5000" created
    
    1
    2

    This network policy will:

    • Drop all non-whitelisted traffic to app=apiserver.
    • Allow traffic on port 5000 from pods with label role=monitoring in the same namespace.

    # Try it out

    Run a pod with no custom labels, observe the traffic to ports 5000 and 8000 are blocked:

    $ kubectl run test-$RANDOM --rm -i -t --image=alpine -- sh
    / # wget -qO- --timeout=2 http://apiserver:8001
    wget: download timed out
    
    / # wget -qO- --timeout=2 http://apiserver:5001/metrics
    wget: download timed out
    
    1
    2
    3
    4
    5
    6

    Run a pod with role=monitoring label, observe the traffic to port 5000 is allowed, but port 8000 is still not accessible:

    $ kubectl run test-$RANDOM --labels="role=monitoring" --rm -i -t --image=alpine -- sh
    / # wget -qO- --timeout=2 http://apiserver:8001
    wget: download timed out
    
    / # wget -qO- --timeout=2 http://apiserver:5001/metrics
    http.requests=3
    go.goroutines=5
    go.cpus=1
    
    1
    2
    3
    4
    5
    6
    7
    8

    # Cleanup

    kubectl delete pod apiserver
    kubectl delete service apiserver
    kubectl delete networkpolicy api-allow-5000
    
    上次更新: 2024/02/26, 10:14:04
    allow-external-traffic
    allowing-traffic-with-multiple-selectors

    ← allow-external-traffic allowing-traffic-with-multiple-selectors→

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