什么是Kubernetes Service?
Kubernetes Service 是一种抽象,可以理解为负载均衡的端口转发服务,用于将网络流量路由到一组 Pod。它的主要作用包括:
-
服务发现:在 Kubernetes 中,Pod 的 IP 地址是动态分配的,可能会随着 Pod 的创建和销毁而变化。Service 提供了一个稳定的访问点,使客户端可以通过固定的 IP 地址或 DNS 名称访问服务。
-
负载均衡:Service 可以将流量分发到多个后端 Pod,确保服务的高可用性和性能。
-
网络代理:通过 kube-proxy 实现流量的转发和路由,支持多种代理模式(如 iptables 和 IPVS)。
Kubernetes Service 与 API Gateway 有什么区别和联系
Kubernetes Service 与 API Gateway 有相似之处,但它们的核心功能和使用场景有所不同。
-
相似点:
-
它们都用于流量管理。
-
都可以作为客户端访问后端服务的入口点。
-
-
不同点:
-
Kubernetes Service:更侧重于在 Kubernetes 集群内的网络流量路由和负载均衡,解决服务发现问题。它是一个集群内的概念,主要负责将流量分发到后端的 Pod,并确保服务之间的通信。
-
API Gateway:通常用于微服务架构,具备更多高级功能,比如身份验证、安全控制、请求/响应转换、限流等。API Gateway 适用于更复杂的流量管理场景,尤其是在对外暴露 API 或需要统一入口时。
-
简单来说,Kubernetes Service 是一种基础网络功能组件,而 API Gateway 是一种高层次的应用组件。
Kubernetes Service 类型及适用场景
-
ClusterIP(默认类型):
-
描述:仅在集群内部访问。
-
适用场景:用于服务间通信(如微服务架构)或集群内的内部应用。
-
-
NodePort:
-
描述:在每个节点的特定端口上公开服务。
-
适用场景:适合本地开发或调试,或者简单的集群外部访问。
-
-
LoadBalancer:
-
描述:通过云提供商配置一个外部负载均衡器。
-
适用场景:生产环境中,用于外部客户端的访问。
-
-
ExternalName:
-
描述:将服务映射到外部 DNS 名称,而不是路由流量到 Pod。
-
适用场景:需要访问外部服务(如数据库或 API)。
-
Service之YAML 模板
LoadBalancer
apiVersion: v1
kind: Service
metadata:name: lmd-api-service-lblabels:app: lmd-apitype: back-end
spec:selector:app: lmd-apitype: back-endports:- protocol: TCPport: 12666targetPort: 12666type: LoadBalancer
ClusterIP
apiVersion: v1
kind: Service
metadata:name: lmd-api-servicelabels:app: lmd-apitype: back-end
spec:selector:app: lmd-apitype: back-endports:- protocol: TCPport: 12666targetPort: 12666type: ClusterIP
Service练习
Udemy Labs - Certified Kubernetes Administrator with Practice Tests Course | KodeKloud
Welcome to the KodeKloud Hands-On lab __ ______ ____ ________ __ __ ____ __ ______ / //_/ __ \/ __ \/ ____/ //_// / / __ \/ / / / __ \/ ,< / / / / / / / __/ / ,< / / / / / / / / / / / // /| / /_/ / /_/ / /___/ /| |/ /___/ /_/ / /_/ / /_/ /
/_/ |_\____/_____/_____/_/ |_/_____/\____/\____/_____/ All rights reserved controlplane ~ ➜ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 15mcontrolplane ~ ➜ kubectl describe service kubernetes
Name: kubernetes
Namespace: default
Labels: component=apiserverprovider=kubernetes
Annotations: <none>
Selector: <none>
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.43.0.1
IPs: 10.43.0.1
Port: https 443/TCP
TargetPort: 6443/TCP
Endpoints: 192.168.243.182:6443
Session Affinity: None
Internal Traffic Policy: Cluster
Events: <none>controlplane ~ ➜ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
simple-webapp-deployment 4/4 4 4 20scontrolplane ~ ➜ kubectl describe deployment
Name: simple-webapp-deployment
Namespace: default
CreationTimestamp: Sun, 30 Mar 2025 05:36:26 +0000
Labels: <none>
Annotations: deployment.kubernetes.io/revision: 1
Selector: name=simple-webapp
Replicas: 4 desired | 4 updated | 4 total | 4 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:Labels: name=simple-webappContainers:simple-webapp:Image: kodekloud/simple-webapp:redPort: 8080/TCPHost Port: 0/TCPEnvironment: <none>Mounts: <none>Volumes: <none>Node-Selectors: <none>Tolerations: <none>
Conditions:Type Status Reason---- ------ ------Available True MinimumReplicasAvailableProgressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: simple-webapp-deployment-8555484b96 (4/4 replicas created)
Events:Type Reason Age From Message---- ------ ---- ---- -------Normal ScalingReplicaSet 36s deployment-controller Scaled up replica set simple-webapp-deployment-8555484b96 from 0 to 4controlplane ~ ➜ kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
simple-webapp-deployment 4/4 4 4 2m41scontrolplane ~ ✖ ls
service-definition-1.yamlcontrolplane ~ ➜ vim service-definition-1.yaml controlplane ~ ➜ cat service-definition-1.yaml
---
apiVersion: v1
kind: Service
metadata:name: webapp-servicenamespace: default
spec:ports:- nodePort: 30080port: 8080targetPort: 8080 selector:name: simple-webapptype: NodePortcontrolplane ~ ➜ kubectl create -f service-definition-1.yaml
service/webapp-service createdcontrolplane ~ ➜ kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 23m
webapp-service NodePort 10.43.81.40 <none> 8080:30080/TCP 9scontrolplane ~ ➜ kubectl describe service webapp-service
Name: webapp-service
Namespace: default
Labels: <none>
Annotations: <none>
Selector: name=simple-webapp
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.43.81.40
IPs: 10.43.81.40
Port: <unset> 8080/TCP
TargetPort: 8080/TCP
NodePort: <unset> 30080/TCP
Endpoints: 10.22.0.9:8080,10.22.0.11:8080,10.22.0.12:8080 + 1 more...
Session Affinity: None
External Traffic Policy: Cluster
Internal Traffic Policy: Cluster
Events: <none>controlplane ~ ➜ Powered by Moshow@https://zhengkai.blog.csdn.net/