为何需要 Service

Kubernetes 中 Pod 是随时可以消亡的(节点故障、容器内应用程序错误等原因)。如果使用 Deployment 运行您的应用程序,Deployment 将会在 Pod 消亡后再创建一个新的 Pod 以维持所需要的副本数。每一个 Pod 有自己的 IP 地址,然而,对于 Deployment 而言,对应 Pod 集合是动态变化的。

这个现象导致了如下问题:

  • 如果某些 Pod(假设是 'backends')为另外一些 Pod(假设是 'frontends')提供接口,在 'backends' 中的 Pod 集合不断变化(IP 地址也跟着变化)的情况下,'frontends' 中的 Pod 如何才能知道应该将请求发送到哪个 IP 地址?

Service 存在的意义,就是为了解决这个问题。

# Kubernetes Service

Kubernetes 中 Service 是一个 API 对象,通过 kubectl + YAML 或者 Kuboard,定义一个 Service,可以将符合 Service 指定条件的 Pod 作为可通过网络访问的服务提供给服务调用者。

Service 是 Kubernetes 中的一种服务发现机制:

  • Pod 有自己的 IP 地址
  • Service 被赋予一个唯一的 dns name
  • Service 通过 label selector 选定一组 Pod
  • Service 实现负载均衡,可将请求均衡分发到选定这一组 Pod 中

例如,假设有一个无状态的图像处理后端程序运行了 3 个 Pod 副本。这些副本是相互可替代的(前端程序调用其中任何一个都可以)。在后端程序的副本集中的 Pod 经常变化(销毁、重建、扩容、缩容等)的情况下,前端程序不应该关注这些变化。

Kubernetes 通过引入 Service 的概念,将前端与后端解耦。

在 Kuboard 中使用 Service

从 Kuboard 工作负载编辑器的视角来看,Service 与其他重要的 Kubernetes 对象之间的关系如下图所示:

图中,Service 先连线到 Controller,Controller 在连线到容器组,这种表示方式只是概念上的,期望用户在使用 Kubernetes 的时候总是通过 Controller 创建 Pod,然后再通过 Service 暴露为网络服务,通过 Ingress 对集群外提供互联网访问。

事实上,Service 与 Controller 并没有直接联系,Service 通过 label selector 选择符合条件的 Pod,并将选中的 Pod 作为网络服务的提供者。从这个意义上来讲,您可以有很多种方式去定义 Service 的 label selector,然而,最佳的实践是,在 Service 中使用与 Controller 中相同的 label selector。如上图所示。

TIP

使用 Kubernetes 的最佳实践:

Service 与 Controller 同名

Service 与 Controller 使用相同的 label selector

在 Kuboard 中创建工作负载时,工作负载的名字(服务名称字段)将作为 Deployment(StatefulSet/DaemonSet)的名字,也将作为 Service、Ingress 的名字

[root@master ~]# kubectl get pods -n ocp -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
cloud-eureka-0 1/1 Running 1 22h 10.100.171.102 worker <none> <none>
cloud-eureka-1 1/1 Running 1 22h 10.100.171.97 worker <none> <none>
cloud-eureka-2 1/1 Running 1 22h 10.100.171.75 worker <none> <none>
cloud-redis-0 1/1 Running 1 24h 10.100.171.90 worker <none> <none>
db-auth-center-0 1/1 Running 1 24h 10.100.171.70 worker <none> <none>
db-log-center-0 1/1 Running 1 24h 10.100.171.99 worker <none> <none>
db-user-center-0 1/1 Running 1 24h 10.100.171.73 worker <none> <none>
gateway-api-55d8f876f5-wjbks 1/1 Running 1 22h 10.100.171.91 worker <none> <none>
svc-auth-server-6cd596f4cc-sslzj 1/1 Running 1 22h 10.100.171.103 worker <none> <none>
svc-user-center-5f8bb5c4c4-hq7sr 1/1 Running 1 22h 10.100.171.77 worker <none> <none>
web-back-center-665f59cbbc-vh7ck 1/1 Running 1 23h 10.100.171.72 worker <none> <none>
[root@master ~]# kubectl get service -n ocp -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
cloud-eureka NodePort 10.96.150.116 <none> 1111:31111/TCP 25h k8s.eip.work/layer=cloud,k8s.eip.work/name=cloud-eureka
cloud-redis ClusterIP 10.96.242.0 <none> 6379/TCP 24h k8s.eip.work/layer=cloud,k8s.eip.work/name=cloud-redis
db-auth-center ClusterIP 10.96.62.160 <none> 3306/TCP 24h k8s.eip.work/layer=db,k8s.eip.work/name=db-auth-center
db-log-center ClusterIP 10.96.70.39 <none> 3306/TCP 24h k8s.eip.work/layer=db,k8s.eip.work/name=db-log-center
db-user-center ClusterIP 10.96.71.151 <none> 3306/TCP 24h k8s.eip.work/layer=db,k8s.eip.work/name=db-user-center
gateway-api ClusterIP 10.96.10.43 <none> 9200/TCP 23h k8s.eip.work/layer=gateway,k8s.eip.work/name=gateway-api
svc-auth-server ClusterIP 10.96.238.226 <none> 8000/TCP 24h k8s.eip.work/layer=svc,k8s.eip.work/name=svc-auth-server
svc-user-center ClusterIP 10.96.209.141 <none> 7000/TCP 23h k8s.eip.work/layer=svc,k8s.eip.work/name=svc-user-center
web-back-center ClusterIP 10.96.7.72 <none> 80/TCP 23h k8s.eip.work/layer=web,k8s.eip.work/name=web-back-center
[root@master ~]# kubectl get ingress -n ocp -o wide
NAME CLASS HOSTS ADDRESS PORTS AGE
cloud-eureka <none> cloud-eureka.ocp.demo.kuboard.cn 80 25h
gateway-api <none> api-gateway.ocp.demo.kuboard.cn 80 23h
svc-auth-server <none> svc-auth-server.ocp.demo.kuboard.cn 80 24h
svc-user-center <none> svc-user-center.ocp.demo.kuboard.cn 80 24h
web-back-center <none> back-center.ocp.demo.kuboard.cn 80 23h

Service概述的更多相关文章

  1. Android---55---Web Service概述

    Web Service 是什么? /*w3school*/ Web Services 是应用程序组件 Web Services 使用开放协议进行通信 Web Services 是独立的(self-co ...

  2. Android中Service概述

    Service是Android中一种非常重要的组件,一般来说有两种用途:用Service执行长期执行的操作,而且与用户没有UI界面的交互:某个应用程序的Service能够被其它应用程序的组件调用以便提 ...

  3. Web Service概述 及 应用案例

    Web Service的定义  W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API,它们可以通过网络进行调用 ...

  4. Android四大核心组件之Service

    实验内容 启动Service 绑定Service 与Service进行通信 实验要求 启动Service 绑定Service 与Service进行通信 实验步骤 Service概述 Service通常 ...

  5. C#写Windows Service(windows服务程序)

    背景:        要学习使用一个新东西,我们必须知道他是个什么东西.对于我们此次研究的windows服务来说,他又是个什么东西,其实也没有什么高深的了. windows service概述: 一个 ...

  6. 使用mybatis完成通用dao和通用service

    使用mybatis完成通用dao和通用service 概述: 使用通用dao和通用service可以减少代码的开发.可以将常用的增删改查放到通用dao中.对不同的or框架,基本上都有自己的实现如Spr ...

  7. Web Service学习笔记(webservice、soap、wsdl、jws详细分析)

    Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...

  8. Web Service学习笔记

    Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...

  9. Web Service学习笔记(webservice、soap、wsdl、jws详细分析) (转)

    Web Service概述 Web Service的定义 W3C组织对其的定义如下,它是一个软件系统,为了支持跨网络的机器间相互操作交互而设计.Web Service服务通常被定义为一组模块化的API ...

随机推荐

  1. VS Code + GitHub

    来到博客园学着别人美化了一下自己的博客页面,蛮好看的,然后右上角有一个"Fork me on GitHub".之前就因为好奇而注册过GitHub,但一直不会使,现在正式开始编程学习 ...

  2. 自定义bean对象实现序列化接口

    上一个word count的案例中,我们为了理解mapreduce的流程,写了上面的代码.现在我们要把一个实体类序列化.比如现在有这么一个文件,里面的数据格式是这样的: 第一列是时间戳,第二列是手机号 ...

  3. 适合初学者的使用CNN的数字图像识别项目:Digit Recognizer with CNN for beginner

    准备工作 数据集介绍 数据文件 train.csv 和 test.csv 包含从零到九的手绘数字的灰度图像. 每张图像高 28 像素,宽 28 像素,总共 784 像素.每个像素都有一个与之关联的像素 ...

  4. 超全selenium元素定位XPath、CSS

    说明:在HTML页面中,<p> 是一个标签,<p>hello</p> 是一个元素,元素由一个开始的标签和结束的标签组成.<font color="r ...

  5. 我在Apache DolphinScheduler的心路历练

    摘要:Apache DolphinScheduler 目前是 Apache 孵化项目,目前正在快速发展中.加入Apache DolphinScheduler社区已一年多,已有 400+ 公司在生产上使 ...

  6. Luogu2375 [NOI2014]动物园 (KMP)

    写炸,上网,不同KMP形态. 无力,照该,一换写法就过. 横批:我是垃圾 求\(next\)时\(DP\)出\(num\),路径压缩防卡\(n^2\) AC #include <iostream ...

  7. LuoguP5390 [Cnoi2019]数学作业(数论)

    转进制,然后发现贡献只有\(1_{(2)}\),取奇数个的子集方案是\(2^{n-1}\) #include <iostream> #include <cstdio> #inc ...

  8. Luogu3855 [TJOI2008]Binary Land (BFS)

    #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> ...

  9. Node.js + Express + Knex 开发 API 接口

    安装依赖包 npm i express knex mysql2 这是 Knex 官方文档地址:Knex.js - SQL query builder. 搭建接口 config.js 新建一个 conf ...

  10. 【HTML】学习路径1-网页基本结构-标签基本语法

    本系列将学习最基础的web前端知识: HTML---CSS---JavaScripts---jQuery 四大部分学习完以后再进入到JavaWeb的知识.(后端) 然后再学习SpringBoot技术. ...