Skip to Content
Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition
book

Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition

by Benjamin Muschko
January 2026
Intermediate to advanced
392 pages
4h 44m
Chinese
O'Reilly Media, Inc.
Content preview from Kubernetes 认证管理员 (CKA) 学习指南 (Chinese Edition), 2nd Edition

第17章 服务

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

《使用 Pod 的 IP 地址进行网络通信》中,我们了解到可以通过 Pod 的 IP 地址与其通信 。当 Pod 重启时,系统会自动分配新的虚拟集群 IP 地址 。因此,系统中其他组件在相互通信时不能依赖 Pod 的 IP 地址。

构建微服务架构( )时,各组件在独立Pod中运行并需通过稳定网络接口通信,此时需要另一种基础组件——服务Service)

Service 在 Pod 之上实现抽象层,为所有标签匹配的 Pod 分配固定虚拟 IP,该虚拟 IP 称为集群 IP(ClusterIP)。本章将重点解析 Service 的工作原理,尤其是基于 Pod 声明类型实现集群内外暴露机制的核心机制。

在 minikube 中访问服务

访问 minikube 中的NodePortLoadBalancer类型服务 需要特殊处理。具体操作请参阅官方文档

服务操作

简而言之 ,服务为一组Pod提供可发现名称和负载均衡功能。借助Kubernetes DNS控制平面组件 ,服务能够实现与IP地址的解耦——这将在"通过DNS查询发现服务"章节中详细讨论与Deployment类似,服务通过标签选择机制 确定其作用对象。

图17-1展示了该功能机制:Pod 1因其标签与服务定义的标签选择器匹配而接收流量;Pod 2因定义了不匹配的标签而未接收流量。

Diagram illustrating service traffic routing where Pod 1 receives traffic due to matching label selection, while Pod 2 does not.
图17-1. 基于标签选择的服务流量 路由

需注意:为支持其他场景,可创建不含标签选择器的服务。更多信息请参阅相关Kubernetes文档

服务与部署

服务是部署的补充概念 。服务将网络流量路由至一组 Pod,而部署通过 ReplicaSet 管理这组 Pod(即副本)。虽然两者可独立使用,但建议配合使用部署与服务。主要原因是:既能扩展副本数量,又能暴露端点将网络流量汇聚至这些 Pod。

服务类型

每个服务都定义了一种类型 。该类型负责在集群内部和/或外部暴露服务。表17-1列出了与考试相关的服务类型。

表 17-1. 服务 类型
类型 描述

集群IP

在集群内部 IP 上暴露服务。仅可在集群内部访问。Kubernetes 使用轮询算法将流量均匀分配到目标 Pod。

NodePort

在每个节点的静态端口上暴露服务。可从集群外部访问。该服务类型不提供跨多节点的负载均衡功能。

LoadBalancer

通过Cloud服务商的负载均衡器对外暴露服务。

其他服务类型(如ExternalName或无头服务)虽可定义,但本书不作讨论,因其超出考试范围。更多信息请参阅Kubernetes 文档

服务类型继承

前述的ClusterIPNodePort和LoadBalancer服务类型,使 服务能够以不同暴露范围访问。必须理解这些服务类型之间存在层叠关系。图17-2展示了不同服务类型的关联性。

图 17-2. 服务类型的网络可访问性特征

例如创建NodePort类型的服务,意味着该服务同时具备 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Kubernetes 上的生成式人工智能 (Chinese Edition)

Kubernetes 上的生成式人工智能 (Chinese Edition)

Roland Huß, Daniele Zonca
游戏化头脑风暴

游戏化头脑风暴

Dave Gray, Sunni Brown, James Macanufo

Publisher Resources

ISBN: 0642572314712