k8s 网络基础

k8s 网络基础

pod

Field Description
containerPort integer Number of port to expose on the pod's IP address. This must be a valid port number, 0 < x < 65536.
hostIP string What host IP to bind the external port to.
hostPort integer Number of port to expose on the host. If specified, this must be a valid port number, 0 < x < 65536. If HostNetwork is specified, this must match ContainerPort. Most containers do not need this.
name string If specified, this must be an IANA_SVC_NAME and unique within the pod. Each named port in a pod must have a unique name. Name for the port that can be referred to by services.
protocol string Protocol for port. Must be UDP or TCP. Defaults to "TCP".

重点关注containerPort,他与Pod IP组成了endpoint,Pod IP就是docker0网桥分配的IP,有pause容器拥有,Pod其他容器与pause容器公用一个network namespace。

关于containerPort有个问题,如果一个pod中有2个容器,2个容器暴露相同的port,为什么可以?两个容器应该是公用pause容器的网络协议栈,如果tcp来了,到底是访问哪个容器的里面的进程?

如果简单点想尽快从外部访问pod,就可以直接指定hostPort,一般不推荐。

这个问题需要理解pause,待解决

serivce

Field Description
name string The name of this port within the service. This must be a DNS_LABEL. All ports within a ServiceSpec must have unique names. This maps to the 'Name' field in EndpointPort objects. Optional if only one ServicePort is defined on this service.
nodePort integer The port on each node on which this service is exposed when type=NodePort or LoadBalancer. Usually assigned by the system. If specified, it will be allocated to the service if unused or else creation of the service will fail. Default is to auto-allocate a port if the ServiceType of this Service requires one. More info: https://kubernetes.io/docs/concepts/services-networking/service/#type-nodeport
port integer The port that will be exposed by this service.
protocol string The IP protocol for this port. Supports "TCP" and "UDP". Default is TCP.
targetPort Number or name of the port to access on the pods targeted by the service. Number must be in the range 1 to 65535. Name must be an IANA_SVC_NAME. If this is a string, it will be looked up as a named port in the target Pod's container ports. If this is not specified, the value of the 'port' field is used (an identity map). This field is ignored for services with clusterIP=None, and should be omitted or set equal to the 'port' field. More info: https://kubernetes.io/docs/concepts/services-networking/service/#defining-a-service
  • targetPort,将外部流量映射到对应Pod的Container Port
  • port就是service port,与cluster ip组合就是k8s内部服务互相通信的地址
  • nodePort:暴露给外部程序使用,需要与node ip一起使用,配合type: NodePort

service的网络信息早期采用ENV管理,后期通过DNS系统,直接使用service name访问cluster ip。

cluster ip

为什么cluster ip不能ping?

Headless Service

设置clusterIP: None,自己做LB,而非k8s做LB,提供cluster IP,减少网络损耗。直接返回对应ep。

endpoints

service通过label selector筛选合适的pod,然后post到同名的Endpoints对象,Endpoints对象保存Endpoint,即Pod IP + Container Port
[图片上传失败...(image-83ad0d-1536978807340)]

endpoints用法:

  • 使用外部非k8s服务,比如外部还没有容器化的数据库服务
  • 把一个service指向其他的service

具体流程:

  1. 创建一个没有label selector的service,因为没有选择器,所以就没有对应的Endpoints对象
  2. 手动创建Endpoints对象,重点是想要代理的服务地址

比如这里有个场景,batch服务的task多副本,一个task对应一个service,一个pod;那么如何对这些task多副本做一个统一的入口呢?我们可以在这些services上面再加一个serivce,然后手动创建对应的Endpoints,这里注意endpoint必须是pod ip,而不能是cluster ip,至于为什么暂时不清楚。

详见services

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 196,165评论 5 462
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 82,503评论 2 373
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 143,295评论 0 325
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,589评论 1 267
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,439评论 5 358
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,342评论 1 273
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,749评论 3 387
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,397评论 0 255
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,700评论 1 295
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,740评论 2 313
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,523评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,364评论 3 314
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,755评论 3 300
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,024评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,297评论 1 251
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,721评论 2 342
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,918评论 2 336

推荐阅读更多精彩内容