kubernetes网络通信

需要解决的问题

  • 同一个pod内部的不同容器间通信, local
  • Pod间的通信
  • pod与Service的通信: PodIP<--->ClusterIP
  • Service 与集群外部通信

CNI:

常见的解决方案

  • flannel
  • calico
  • canel
  • kube-router

解决方案:

虚拟网桥:brige方案

多路复用:MacVLAN(二层方案)

硬件交换:SR-IOV

flannel

查看集群的flannel 配置文件

将配置文件放到/etc/cni/net.d目录下, kubelet 会加载该文件。

cat /etc/cni/net.d/10-flannel.conflist

flannel不支持网络策略

不同namespace 的pod 可以相互通信

支持多种方式后端

  • Vxlan 工作原理

    • vxlan
    • Directrouting # 同网段使用host-gw 不同网段使用vxlan
  • host-gw: Host Gateway 工作原理
  • UDP: 效率很低

flannel 的配置参数:

  • network

    使用CIRD格式的网络地址:

    10.244.0.0/16 ->

    master: 10.244.0.0/24

    ​ node1: 10.244.1.0/24

    ​ ...

    node255: 10.244.255.0/24

  • SubnetLen

    在node上使用多长的掩码 默认 24位

  • SubnetMin 与SubnetMax

    网段中最小的子网网段与最大的子网网段。

  • Backend: 选择flannel的类型

修改flannel 类型

修改配置文件 kube-flannel.yaml

  net-conf.json: |
{
"Network": "172.20.0.0/16",
"Backend": {
"Type": "vxlan"
"Direcrouting": true
}
}

Calico/Cannel

可以提供网络策略

https://docs.projectcalico.org/v3.5/getting-started/kubernetes/installation/other

networkpolicy.spec

   egress	<[]Object>
List of egress rules to be applied to the selected pods. Outgoing traffic
is allowed if there are no NetworkPolicies selecting the pod (and cluster
policy otherwise allows the traffic), OR if the traffic matches at least
one egress rule across all of the NetworkPolicy objects whose podSelector
matches the pod. If this field is empty then this NetworkPolicy limits all
outgoing traffic (and serves solely to ensure that the pods it selects are
isolated by default). This field is beta-level in 1.8 ingress <[]Object>
List of ingress rules to be applied to the selected pods. Traffic is
allowed to a pod if there are no NetworkPolicies selecting the pod OR if
the traffic source is the pod's local node, OR if the traffic matches at
least one ingress rule across all of the NetworkPolicy objects whose
podSelector matches the pod. If this field is empty then this NetworkPolicy
does not allow any traffic (and serves solely to ensure that the pods it
selects are isolated by default). podSelector <Object> -required-
Selects the pods to which this NetworkPolicy object applies. The array of
ingress rules is applied to any pods selected by this field. Multiple
network policies can select the same set of pods. In this case, the ingress
rules for each are combined additively. This field is NOT optional and
follows standard label selector semantics. An empty podSelector matches all
pods in this namespace. policyTypes <[]string>
List of rule types that the NetworkPolicy relates to. Valid options are
Ingress, Egress, or Ingress,Egress. If this field is not specified, it will
default based on the existence of Ingress or Egress rules; policies that
contain an Egress section are assumed to affect Egress, and all policies
(whether or not they contain an Ingress section) are assumed to affect
Ingress. If you want to write an egress-only policy, you must explicitly
specify policyTypes [ "Egress" ]. Likewise, if you want to write a policy
that specifies that no egress is allowed, you must specify a policyTypes
value that include "Egress" (since such a policy would not include an
Egress section and would otherwise default to just [ "Ingress" ]). This
field is beta-level in 1.8

注意:如果是定义egress-only 策略,则需要显式的声明Egress。如果需要一个拒绝所有出流量的策略,需要在value中包含Egress,因为如果不包含egress,默认只包含ingress。

阻止所有ingress, 允许所有Egress

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-ingress
spec:
podSelector: {}
policyTypes:
- Ingress

允许所有ingress,允许所有Egress

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-ingress
spec:
podSelector: {}
ingress:
- {}
policyTypes:
- Ingress

阻止所有Egress

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: deny-all-ingress
spec:
podSelector: {}
policyTypes:
- Egress

案例:

apiVersion: networking.k8s.io/v1
kind: NetworkPolicy
metadata:
name: test-network-policy
namespace: default
spec:
podSelector:
matchLabels:
role: db
policyTypes:
- Ingress
- Egress
ingress:
- from:
- ipBlock:
cidr: 172.17.0.0/16
except:
- 172.17.1.0/24
- namespaceSelector:
matchLabels:
project: myproject
- podSelector:
matchLabels:
role: frontend
ports:
- protocol: TCP
port: 6379
egress:
- to:
- ipBlock:
cidr: 10.0.0.0/24
ports:
- protocol: TCP
port: 5978

总结

  1. NetworkPolicy 针对 namespace 中的 pod 生效。
  2. 默认ingress 禁止, egreess 放行。要默认禁止 egress 需要在 policyTypes 的 value 中加入 egress。
  3. NetworkPolicy 在定义流量匹配上与防火墙类似。
  4. 一般来说对于 提供服务的pod 应该按一下规则定义
    • 默认关闭所有出入栈
    • 放行本namespace 中的所有
    • 定义外部流量

04-kubernetes网络通信的更多相关文章

  1. 04 . kubernetes资源清单YAML入门

    YAML 通过k8s操作yaml配置文件在node上创建资源,yaml配置文件就像船垛,用来操控docker这艘大船 yam是专门用来写配置文件的语言,非常简洁和强大.而实际上使用yaml配置文件创建 ...

  2. kubernetes之flannel

    kubernetes网络通信 容器间的通信   pod内的容器通信(lo) Pod之间的通信   pod IP <-----> pod IP Pod与Service之间的通信 podIP ...

  3. Docker Kubernetes Service 网络服务代理模式详解

    Docker Kubernetes  Service 网络服务代理模式详解 Service service是实现kubernetes网络通信的一个服务 主要功能:负载均衡.网络规则分布到具体pod 注 ...

  4. Kubernetes中的垃圾回收机制

    本文所讨论垃圾回收(GC,Garbage Collection)机制针对Kubernetes1.1.7,docker容器. 一.Tips 01. Kubernetes的垃圾回收由kubelet进行管理 ...

  5. Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列之集群部署环境规划(一)

    0.前言 整体架构目录:ASP.NET Core分布式项目实战-目录 k8s架构目录:Kubernetes(k8s)集群部署(k8s企业级Docker容器集群管理)系列目录 一.环境规划 软件 版本 ...

  6. Kubernetes lxcfs

    容器实现的基础是NameSpace和Cgroups. NameSpace实现了对容器(进程)的隔离,NameSpace技术实际上修改了应用进程看待整个计算机“视图”,也就是作用域,即它的“视线”被操作 ...

  7. Linux 系统化学习系列文章总目录(持续更新中)

    本页内容都是本人系统化学习Linux 时整理出来的.这些文章中,绝大多数命令类内容都是翻译.整理man或info文档总结出来的,所以相对都比较完整. 本人的写作方式.风格也可能会让朋友一看就恶心到直接 ...

  8. 18.flannel的配置

    Kubernetes网络通信: (1) 容器间通信:同一个Pod内的多个容器间的通信, lo (2) Pod通信:Pod IP <--> Pod IP (3) Pod与Service通信: ...

  9. k8s的flannel网络插件配置

    flannel的网络插件配置 Kubernetes网络通信需要解决以下问题:            (1)容器间通信:同一个Pod内的多个容器间的通信,lo            (2)Pod通信:P ...

随机推荐

  1. vue.js实现单选框、复选框和下拉框

    Vue.js可以很方便的实现数据双向绑定,所以在处理表单,人机交互方面具有很大的优势.下边以单选框.复选框和下拉框为例介绍他们在HTML和Vue.js中的具体实现方式. 一.单选框   在传统的HTM ...

  2. Android短视频中如何实现720P磨皮美颜录制?

    视频中磨皮.美颜功能已成为刚需,那么如何在Android短视频中实现720P磨皮美颜录制?本篇文章中,网易云信资深开发工程师将向大家介绍具体的操作方法. 相关阅读推荐 <短视频技术详解:Andr ...

  3. LBXF时间管理法2.0

    前言 LBXF是柳比歇夫的中文拼音缩写,下文中会着重介绍柳比歇夫 时间管理法主要帮助我们提高生产性时间,以及提高生产效率.我会按照柳比歇夫.执行步骤.时间管理工具Toggle三个模块来讲解LBXF时间 ...

  4. Qt之股票组件-股票检索--支持预览框、鼠标、键盘操作

    目录 一.感慨一下 二.效果展示 三.搜索编辑框 1.编辑框 2.预览框 四.相关文章 原文链接:Qt之股票组件-股票检索--支持预览框.鼠标.键盘操作 一.感慨一下 之前做过一款炒股软件,个人觉着是 ...

  5. scikit-learn算法选择路径图

     原文链接:https://blog.csdn.net/guang_mang/article/details/73658496

  6. Maven项目读取resources下文件的路径

    要取编译后的路径,而不是你看到的src/main/resources的路径.如下: URL url = 类名.class.getClassLoader().getResource("conf ...

  7. 使用字蛛教程以及遇到的bug

    前言: 前段时间刚完成一个外项目,歇了几天,老大让我看看公司的官网,优化一下,发现移动端的字体下载特别慢,才发现引用了字体包,一个字体包就达到了11M,想着既然有了图片压缩,那么应该有字体压缩,所以百 ...

  8. vue组件间通信六种方式(完整版)

    本文总结了vue组件间通信的几种方式,如props. $emit/ $on.vuex. $parent / $children. $attrs/ $listeners和provide/inject,以 ...

  9. Java 将Word转为PDF、PNG、SVG、RTF、XPS、TXT、XML

    同一文档在不同的编译或阅读环境中,需要使用特定的文档格式来打开,通常需要通过转换文档格式的方式来实现.下面将介绍在Java程序中如何来转换Word文档为其他几种常见文档格式,如PDF.图片png.sv ...

  10. MyBatis从入门到精通(十二):使用collection标签实现嵌套查询

    最近在读刘增辉老师所著的<MyBatis从入门到精通>一书,很有收获,于是将自己学习的过程以博客形式输出,如有错误,欢迎指正,如帮助到你,不胜荣幸! 本篇博客主要讲解使用collectio ...