Service
一句话解释
Service 是 Kubernetes resource 那个 provides stable network identity for 设置的 Pods, 偶数 as Pods 是创建 and destroyed.
用大白话说
In Kubernetes, Pods are ephemeral — they are created and destroyed, and each time they get a new IP address. This makes it hard to connect to them directly. A Service solves this problem by providing a stable IP address and DNS name that stays the same even as the underlying Pods change.
A Service acts as a load balancer: it receives traffic on a stable address and distributes it across all the Pods that match its selector. When a Pod dies and a new one is created, the Service automatically starts routing to the new Pod.
There are several types of Services: ClusterIP (internal only), NodePort (exposed on a node's port), and LoadBalancer (provisioned by a cloud load balancer).
示例
apiVersion: v1
kind: Service
metadata:
name: my-app-service
spec:
type: ClusterIP
selector:
app: my-app
ports:
- port: 80
targetPort: 8080这个 Service routes traffic 从 port 80 到 port 8080 on 所有 Pods labeled app: my-app.
工作原理
- You define a Service with a selector that matches certain Pods.
- Kubernetes assigns the Service a stable ClusterIP and DNS name.
- Traffic sent to the Service is load-balanced across matching Pods.
- As Pods are created or destroyed, the Service updates its endpoints automatically.
常见混淆
容易混淆的对象: Ingress
关键区别: Service provides 内部 routing and 加载 balancing; Ingress manages 外部 HTTP/HTTPS routing and host-based 规则.
相关术语
解读错误信息
用大白话解释常见错误——什么意思、为什么发生、如何修复。