本文描述了如何在 Kubernetes 集群中通过创建 NetworkPolicy 的方式来声明网络策略,以管理 Pod 之间的网络通信流量。

前提条件

您已经安装了 Kubernetes 集群,如果没有,请参考文档 安装Kubernetes单Master节点

您可以使用 kubectl 访问您的集群,请参考文档 安装Kubectl

请确保您使用的网络插件支持 Network Policy,如下的网络插件都是可以的:

如果您按照 安装Kubernetes单Master节点 安装的集群,默认是 calico 网络插件

Cilium

Kube-routerRomanaWeave Net

排序

按字母顺序排序,不代表推荐顺序。本文中的例子对上述所有网络插件都有效

创建一个Deployment并配置Service

  • 创建一个 nginx Deployment 用于演示 Kubernetes 的 NetworkPolicy:
kubectl create deployment nginx --image=nginx

输出结果

deployment.apps/nginx created

通过Service暴露该Deployment

kubectl expose deployment nginx --port=80

输出结果

service/nginx exposed

查询结果

kubectl get svc,pod

输出结果

NAME                        CLUSTER-IP    EXTERNAL-IP   PORT(S)    AGE
service/kubernetes 10.100.0.1 <none> 443/TCP 46m
service/nginx 10.100.0.16 <none> 80/TCP 33s NAME READY STATUS RESTARTS AGE
pod/nginx-701339712-e0qfq 1/1 Running 0 35s

从另外一个pod访问Service

默认是可以从另外一个Pod访问 nginx Service 的。下面的方法可以执行此测试:

default 名称空间中创建 busybox 容器,并执行 wget 命令:

kubectl run --generator=run-pod/v1 busybox --rm -ti --image=busybox -- /bin/sh

请按照下面的例子,在命令行中执行 wget --spider --timeout=1 nginx

Waiting for pod default/busybox-472357175-y0m47 to be running, status is Pending, pod ready: false

Hit enter for command prompt

/ # wget --spider --timeout=1 nginx
Connecting to nginx (10.100.0.16:80)
remote file exists
/ #

限制对nginx的访问

下面的 NetworkPolicy 可以声明:只有带 access=true 标签的 Pod 可以访问 nginx 服务:

network-policy.yaml文件内容

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: access-nginx
spec:
podSelector:
matchLabels:
app: nginx
ingress:
- from:
- podSelector:
matchLabels:
access: "true"

执行命令以创建该 NetworkPolicy:

kubectl apply -f network-policy.yaml

输出结果如下:

networkpolicy.networking.k8s.io/access-nginx created

从不带标签的Pod访问nginx服务

如果从不带标签的 Pod 访问该 nginx 服务,请求将超时:

kubectl run --generator=run-pod/v1 busybox --rm -ti --image=busybox -- /bin/sh

请按照下面的例子在命令行中执行 wget --spider --timeout=1 nginx

Waiting for pod default/busybox-472357175-y0m47 to be running, status is Pending, pod ready: false

Hit enter for command prompt

/ # wget --spider --timeout=1 nginx
Connecting to nginx (10.100.0.16:80)
wget: download timed out
/ #

从带有标签的Pod访问nginx服务

从带有 access=true 标签的 Pod 中访问 nginx 服务,将能够执行成功:

kubectl run --generator=run-pod/v1 busybox --rm -ti --labels="access=true" --image=busybox -- /bin/sh

请按照下面的例子在命令行中执行 wget --spider --timeout=1 nginx

Waiting for pod default/busybox-472357175-y0m47 to be running, status is Pending, pod ready: false

Hit enter for command prompt

/ # wget --spider --timeout=1 nginx
Connecting to nginx (10.100.0.16:80)
remote file exists
/ #

举例:Network Policies的更多相关文章

  1. routing decisions based on paths, network policies, or rule-sets configured by a network administrator

    https://en.wikipedia.org/wiki/Border_Gateway_Protocol Border Gateway Protocol (BGP) is a standardize ...

  2. Improving Network Management with Software Defined Networking

    Name of article:Improving Network Management with  Software Defined Networking Origin of the article ...

  3. 边界网关协议BGP

    Border Gateway Protocol (BGP) is a standardized exterior gateway protocol designed to exchange routi ...

  4. (zhuan) Deep Reinforcement Learning Papers

    Deep Reinforcement Learning Papers A list of recent papers regarding deep reinforcement learning. Th ...

  5. Attacks for RL

    1. http://rll.berkeley.edu/adversarial/   Adversarial Attacks on Neural Network Policies 就是对test时候的p ...

  6. Software-Defined Networking A Comprehensive Survey --阅读_day2

    3. 什么是SDN?(WHAT IS SOFTWARE-DEFINED NETWORKING?) The term SDN was originally coined to represent the ...

  7. Software-Defined Networking A Comprehensive Survey --阅读_day1

    The Internet has led to the creation of a digital society, where (almost) everything is connected an ...

  8. 在windows中安装OpenSSH,无密码登录,永远不断线

    到这里下载最新安装包:https://github.com/PowerShell/Win32-OpenSSH/releases下载下来解压,然后用管理员打开服务器自带的PowerShell,运行下列命 ...

  9. ospf 提升 二 ---LSA

    ospf ABR和ASBR的区别 官方建议中大型网络的规模参考   根据spf算法   而不是路由器的硬件性能强弱 a ABR最多关联3个区域 b 单区域内路由器最多50台 c 一台运行ospf的路由 ...

随机推荐

  1. 从Python到水一篇AI论文(核心 or Sci三区+)

    博客配套视频链接: https://space.bilibili.com/383551518?spm_id_from=333.1007.0.0 b 站直接看 配套 github 链接:https:// ...

  2. windows配置skywalking集群

    一.zookeeper 准备配置三个zookeeper,因为我是单台模拟,所以需要使用不同的端口,使用版本是apache-zookeeper-3.6.3-bin (必须是3.5+) 1.第1个zook ...

  3. ajax04_实现关键字联想和自动补全

    用ajax实现关键字联想和自动补全 遇到的小坑 回调函数相对window.onload的摆放位置 给回调函数addData传数据时,如何操作才能将数据传进去 代码实现 前端代码 <!DOCTYP ...

  4. Dubbo源码(一) - SPI使用

    为什么学SPI Dubbo 的可扩展性是基于 SPI 去实现的,而且Dubbo所有的组件都是通过 SPI 机制加载. 什么是SPI SPI 全称为 (Service Provider Interfac ...

  5. YII 技巧

    大部分来源于  https://getyii.com/topic/47#comment24 获取当前Controller name和action name(在控制器里面使用) echo $this-& ...

  6. linux学习随笔2之防火墙

    centos7默认使用的防火墙是firewalld 查看所有打开的端口: firewall-cmd --zone=public --list-ports 更新防火墙规则: firewall-cmd - ...

  7. 论文解读(JKnet)《Representation Learning on Graphs with Jumping Knowledge Networks》

    论文信息 论文标题:Representation Learning on Graphs with Jumping Knowledge Networks论文作者:Keyulu Xu, Chengtao ...

  8. 手把手教你定位线上MySQL慢查询问题,包教包会

    1. 慢查询日志的作用 慢查询日志默认不开启,建议手动开启,方便我们定位线上问题. 执行时间超过阈值的SQL会被写入到慢查询日志当中,这样可以帮助我们记录执行时间过长的SQL语句,定位线上慢SQL问题 ...

  9. 技术分享 | 浅谈MySQL闪回的实现

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 1.闪回实现原理 2.binlog文件格式初探 3.闪回实现过程 1.闪回实现原 ...

  10. Apache DolphinScheduler 的持续集成方向实践

    今天给大家带来的分享是基于 Apache DolphinScheduler 的持续集成方向实践,分享的内容主要为以下六点: " 研发效能 DolphinScheduler CI/CD 应用案 ...