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

deny-all-traffic-to-an-application

# DENY all traffic to an application

This NetworkPolicy will drop all traffic to pods of an application, selected using Pod Selectors.

Use Cases:

  • It’s very common: To start whitelisting the traffic using Network Policies, first you need to blacklist the traffic using this policy.
  • You want to run a Pod and want to prevent any other Pods communicating with it.
  • You temporarily want to isolate traffic to a Service from other Pods. Diagram for DENY all traffic to an application policy

# Example

Run a nginx Pod with labels app=web and expose it at port 80:

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

Run a temporary Pod and make a request to web Service:

$ kubectl run --rm -i -t --image=alpine test-$RANDOM -- sh
/ # wget -qO- http://web
<!DOCTYPE html>
<html>
<head>
...

It works, now save the following manifest to web-deny-all.yaml, then apply to the cluster:

kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
  name: web-deny-all
spec:
  podSelector:
    matchLabels:
      app: web
  ingress: []
1
2
3
4
5
6
7
8
9
$ kubectl apply -f web-deny-all.yaml
networkpolicy "web-deny-all" created
1
2

# Try it out

Run a test Pod again, and try to query web:

$ kubectl run --rm -i -t --image=alpine test-$RANDOM -- sh
/ # wget -qO- --timeout=2 http://web
wget: download timed out

Traffic dropped!


# Remarks

In the manifest above, we target Pods with app=web label to police the network. This manifest file is missing the spec.ingress field. Therefore it is not allowing any traffic into the Pod.

If you create another NetworkPolicy that gives some Pods access to this application directly or indirectly, this NetworkPolicy will be obsolete.

If there is at least one NetworkPolicy with a rule allowing the traffic, it means the traffic will be routed to the pod regardless of the policies blocking the traffic.

# Cleanup

kubectl delete pod web
kubectl delete service web
kubectl delete networkpolicy web-deny-all
1
2
3
上次更新: 2024/02/26, 10:14:04
create-cluster
allow-all-traffic-to-an-application

← create-cluster allow-all-traffic-to-an-application→

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