场景:在一个拥有多个 Pods 的服务中,需要创建一个 EndpointSlice 来更有效地管理网络端点。
创建 Service:首先创建一个服务。
apiVersion: v1
kind: Service
metadata:
name: example-service
spec:
selector:
app: example
ports:
- protocol: TCP
port: 80
查看自动生成的 EndpointSlice:Kubernetes 会自动为服务创建 EndpointSlice。
kubectl get endpointslice
这将显示类似于 example-service-abcde
的 EndpointSlice。
场景:手动创建和管理 EndpointSlice,以直接控制网络端点。
创建 EndpointSlice:
apiVersion: discovery.k8s.io/v1
kind: EndpointSlice
metadata:
name: custom-endpointslice
labels:
kubernetes.io/service-name: example-service
addressType: IPv4
ports:
- name: http
protocol: TCP
port: 80
endpoints:
- addresses: ["192.0.2.42"]
conditions:
ready: true
hostname: example-hostname
应用 EndpointSlice:
kubectl apply -f custom-endpointslice.yaml
在这些案例中,EndpointSlice 被用于管理服务的网络端点,无论是通过 Kubernetes 自动管理还是用户自定义。这提高了大型集群中的网络效率和可扩展性。