欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 游戏 > [CKS] K8S NetworkPolicy Set Up

[CKS] K8S NetworkPolicy Set Up

2025/6/17 8:00:12 来源:https://blog.csdn.net/agjllxchjy/article/details/143644688  浏览:    关键词:[CKS] K8S NetworkPolicy Set Up

最近准备花一周的时间准备CKS考试,在准备考试中发现有一个题目关于不安全项目修复的题目。

​ 专栏其他文章:

  • [CKS] Create/Read/Mount a Secret in K8S-CSDN博客
  • [CKS] Audit Log Policy-CSDN博客
    -[CKS] 利用falco进行容器日志捕捉和安全监控-CSDN博客
  • [CKS] K8S NetworkPolicy Set Up-CSDN博客
  • [CKS] K8S AppArmor Set Up-CSDN博客
  • [CKS] 利用Trivy对image进行扫描-CSDN博客
  • [CKS] kube-batch修复不安全项-CSDN博客
  • [CKS] K8S ServiceAccount Set Up-CSDN博客
  • [CKS] K8S Admission Set Up-CSDN博客
  • [CKS] K8S Dockerfile和yaml文件安全检测-CSDN博客
  • CKS真题
  • CKA真题

What’s the NetworkPolicy

关于network policy的介绍可以查看: https://kubernetes.io/docs/concepts/services-networking/network-policies/

Question 1

A default deny network policy is a policy that blocks all traffic in a namespace by default.

Create a network policy called default-deny that blocks all incoming traffic to all Pods in the mordor namespace.
本题的意思是需要创建一个default-deny的policy,他可以拒绝所有来自mordor命名空间下pod的访问流量

Practice

创建如下default-deny.yaml文件,文件内容为
在这里插入图片描述
创建资源

kubectl create -f default-deny.yml

Question 2

Create a network policy called ‘mtdoom-np’ that allows specific traffic on port 80 to reach the ‘mtdoom’ Pod in the mordor namespace.

The policy should allow incoming traffic:

  • From all Pods in the ‘frodo’ namespace.

  • From all Pods with the label ‘app=sam’, regardless of which namespace they are in.
    这个题的意思是创建一个名字叫mtdoom-np的network policy,它允许特定的流量到达mtdoom pod下。以下是规则

  • 所有来自frodo namespace下的流量

  • 所有带有label为app=sam的流量

对于目标pod可以使用podSelector的label进行筛选podSelector.matchLabels
通过下面的命令查看labels(我看到我的label是app=mtdoom):

kubectl get pod mtdoom -n mordor --show-labels

在这里对于namespace流量的筛选可以使用namespaceSelector下的label进行筛选namespaceSelector.matchLabels
通过下面的命令查看labels(我看到我的事app=frodo)

kubectl get ns frodo --show-labels

对于pod的流量可以通过podSelector的label进行筛选podSelector.matchLabels
所以创建mtdoom-np.yml文件,文件内容如下:
在这里插入图片描述
创建networkpolicy

kubectl create -f mtdoom-np.yml

Ingress和Egress的区别?

Ingress和Egress是网络中的两个术语,用来描述数据流动的方向。

Ingress(入口)是指数据从外部网络进入内部网络的方向。在网络中,Ingress点是数据从外部网络进入内部网络的地方。例如,当你从互联网上访问一个网站时,数据就是通过Ingress点从互联网进入网站的服务器。

Egress(出口)是指数据从内部网络流出到外部网络的方向。在网络中,Egress点是数据从内部网络流出的地方。例如,当你从一个网站下载文件时,数据就是通过Egress点从网站的服务器流向你的设备。

总结来说,Ingress是数据流动的入口方向,而Egress是数据流动的出口方向。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词