当前位置:网站首页>Security assurance is based on software life cycle - networkpolicy application
Security assurance is based on software life cycle - networkpolicy application
2022-07-28 13:58:00 【taoli-qiao】
NetworkPolicy It can be downloaded from IP Address or port level controls network traffic , Network policy is implemented through network plug-ins , If you want to use network policy , Support... Must be used networkPolicy The network solution . Create a NetworkPolicy Resource objects , If there is no controller to use , So this NetworkPolicy It won't work . When the network strategy selects some Pod after , These are the selected ones Pod Will follow NetworkPolicy Rules defined in . in addition , Network testing will not conflict , If one or more strategies choose one Pod, Then the Pod A sum set that uses multiple policies . In defining NetworkPolicy There are some key fields in the rule , The specific field meanings are as follows :
spec: Included in a namespace All the information needed to define a specific network policy .
podSelector: Used to select a group Pod, If podSelector Field is empty , For choice namespace All under Pod
policyTypes:policyType contain Ingress perhaps Egress Or both , If policyTypes Is not specified , So the default is Ingress.
Ingress:Ingress Rule white list , Each allows simultaneous matching from and ports Part of the flow
Egress:Egress White list of rules , Each rule allows matching to and port Part of the flow
For example, the following rule is : Choose to label role=db Of pod.
podSelector:
matchLabels:
role:dbThe following rule is to allow all traffic , If ingress There is no -{}, Is to reject all traffic .
spec:
podSelector:{}
policyType:
- Ingress
spec:
podSelector:{}
ingress:
-{}
policyTypes:
-Ingress NetworkPolicy It's a namespace level resource , The rule applies the endpoint Set .GlobalNetworkPolicy And NetworkPolicy Function as , It is a resource at the whole cluster level ,GlobalNetworkPolicy All in the cluster Namespace take effect , And can limit the host (HostEndpoint).
Next, a practical example is given to demonstrate NetworkPolicy working process , The network plug-in used by the cluster is Calico. First deploy two applications , The deployment of yaml The documents are as follows ,toolbox Is used to access deployment calico-demo Of pod Of .
apiVersion: v1
kind: Namespace
metadata:
name: calico-demo
---
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: calico-demo
name: calico-demo
labels:
app: calico-demo
spec:
replicas: 1
selector:
matchLabels:
app: calico-demo
template:
metadata:
labels:
app: calico-demo
access: "true"
spec:
containers:
- name: calico-demo
image: nginx
ports:
- containerPort: 80apiVersion: apps/v1
kind: Deployment
metadata:
# namespace: default
name: toolbox
spec:
replicas: 1
selector:
matchLabels:
app: toolbox
template:
metadata:
labels:
app: toolbox
access: "true"
spec:
containers:
- name: toolbox
image: centos
command:
- tail
- -f
- /dev/nulltoolbox One is deployed at calico-demo Of namespace Next , One is deployed at default Of namespace below , After deployment , Log in to toolbox pod Inside , use curl and ping Command access calico-demo service , You can see that all the visits are successful . Similarly, enter in default namespace Under the toolbox,curl and ping All can succeed .


At this time let NetworkPolicy take effect ,NetworkPolicy The rules are as follows , Indicates that no traffic is allowed .NetworkPolicy Come into force after , In two namespace Under the toolbox pod Reuse inside curl and ping command , It doesn't work
kind: NetworkPolicy
apiVersion: networking.k8s.io/v1
metadata:
name: default-deny
namespace: calico-demo
spec:
podSelector: {}Then let the following GlobalNetworkPolicy take effect , This rule is open ping command . the reason being that Global Of policy, So in two namespace Next use ping Orders can succeed ,curl The command still fails .
apiVersion: projectcalico.org/v3
kind: GlobalNetworkPolicy
metadata:
name: allow-ping-in-cluster
spec:
selector: all()
types:
- Ingress
ingress:
- action: Allow
protocol: ICMP
source:
selector: all()
icmp:
type: 8 # Ping request
- action: Allow
protocol: ICMPv6
source:
selector: all()
icmp:
type: 128 # Ping requestThen open 80 port , But the traffic of this port should come from default namespace Medium pod Talent . So the following NetworkPolicy Come into force after , stay default namespace Under the toolbox of use curl Command access calico-demo The service is accessible .
apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: allow-http
namespace: calico-demo
spec:
podSelector: {}
ingress:
- from:
- namespaceSelector:
matchLabels:
kubernetes.io/metadata.name: default
ports:
- protocol: TCP
port: 80It shows NetworkPolicy working process , and NetworkPolicy In fact, it is controlled by firewall rules , and Iptables The project was first launched to solve firewall rule configuration . So check Calico Of Iptables You can know the logic behind the control . see Calico The firewall rules of will see cali-INPUT Of chain.
Through the command ip a Command to view the network configuration , You can see that there is cali Configuration at the beginning . Actually, if cali Network plug-in , Then the request through the network plug-in , with cali start .

Here is the first added NetworkPolicy after Iptables Main content . Here to cali The initial request is labeled 0x0/0x20000, And for the request with these labels ,-j The treatment of is DROP, That is, abandoned , Don't deal with . In this way, it shows that the request is rejected NetworkPolicy.

It's opening ICMP after , Firewall rules have been changed , The change points are as follows . about matchle icmp-type 8 Your request is called 0x10000 label , And for the request with such a label ,-j Operation is RETURN, That is to say, return directly to , Rather than being DROP fall .

That's all NetworkPolicy working principle , You can see through configuration NetworkPolicy You can customize the control of requests at the network level , Ensure cluster security .
边栏推荐
- Li Kou sword finger offer 51. reverse order pairs in the array
- No swagger, what do I use?
- 7. Dependency injection
- Strict mode -- let and const -- arrow function -- Deconstruction assignment -- string template symbol -- set and map -- generator function
- Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
- 面经整理,助力秋招,祝你称为offer收割机
- 【LVGL事件(Events)】事件代码
- 最强分布式锁工具:Redisson
- 使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
- 30天刷题计划(三)
猜你喜欢

性能超群!牛津&上海AI Lab&港大&商汤&清华强强联手,提出用于引用图像分割的语言感知视觉Transformer!代码已开源...

How to check if the interface cannot be adjusted? I didn't expect that the old bird of the 10-year test was planted on this interview question

Algorithm --- different paths (kotlin)

Children's programming electronic society graphical programming level examination scratch Level 2 real problem analysis (judgment question) June 2022

在 Kubernetes 中部署应用交付服务(第 1 部分)
![[security] read rfc6749 and understand the authorization code mode under oauth2.0](/img/dc/e6d8626195b2e09a6c06050a9b552e.jpg)
[security] read rfc6749 and understand the authorization code mode under oauth2.0

30天刷题训练(一)

The domestic API management tool eolink is very easy to use, creating an efficient research and development tool

30天刷题计划(三)

Qt5开发从入门到精通——第一篇概述
随机推荐
My friend sent me some interview questions
Implementation of StrCmp, strstr, memcpy, memmove
POJ3259虫洞题解
Machine learning (Zhou Zhihua) Chapter 6 notes on Support Vector Learning
Tutorial on the principle and application of database system (062) -- MySQL exercise questions: operation questions 32-38 (6)
Excellent performance! Oxford, Shanghai, AI Lab, Hong Kong University, Shangtang, and Tsinghua have joined forces to propose a language aware visual transformer for reference image segmentation! Open
Socket类关于TCP字符流编程的理解学习
DOJP1520星门跳跃题解
Tutorial on the principle and application of database system (058) -- MySQL exercise (2): single choice question
R语言因子数据的表格和列联表(交叉表)生成:使用summay函数分析列表查看卡方检验结果判断两个因子变量是否独立(使用卡方检验验证独立性)
图的遍历(BFS&&DFS基础)
Customized template in wechat applet
安全保障基于软件全生命周期-Istio的认证机制
To build agile teams, these methods are indispensable
Dojnoip201708 cheese solution
数据库系统原理与应用教程(058)—— MySQL 练习题(二):单选题
7. Dependency injection
在 Kubernetes 中部署应用交付服务(第 1 部分)
使用 Fail2ban 保护 Web 服务器免受 DDoS 攻击
多线程与高并发(三)—— 源码解析 AQS 原理