Kubernetes – 5.1 Discovery and Load Balancing – Service

什么是Service

Service是为一组Pod提供入口调度服务,并可以在Pod之间实现负载均衡。由于Pod是有生命周期的,每当Pod进行销毁而启动时IP地址也会随之改变,这将无法被调用者发现到,而Service出新就是为了解决服务发现这个问题,提供一个稳定的入口已便于服务调用者,而不用去关心Pod的IP地址变化。

什么是EndPoint

Service会根据资源定义清单中的选择器选择与之绑定的Pod成员,EndPoint就是存储这些Pod成员的IP及端口信息。

Service类型

ClusterIP

将Service提供一个稳定的IP地址供Kubernetes内部的资源对象访问
kubectl apply -f service-clusterip.yaml

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
type: ClusterIP
ports:
- protocol: TCP
port: 80
targetPort: 80

查看service详细信息
kubectl get service nginx-service -oyaml
Kubernetes - 5.1 Discovery and Load Balancing - Service

kubectl describe service nginx-service
Kubernetes - 5.1 Discovery and Load Balancing - Service

NodePort

将Service的Port暴露到Node上,并可以指定Node上的暴露Port (端口范围30000-32767)
kubectl apply -f service-nodeport.yaml

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
type: NodePort
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30000

查看service详细信息
kubectl get service nginx-service -o yaml
Kubernetes - 5.1 Discovery and Load Balancing - Service

kubectl describe service nginx-service
Kubernetes - 5.1 Discovery and Load Balancing - Service

LoadBalancer

将Service暴露在云产商提供的负载均衡服务
kubectl apply -f service-loadbalance.yaml

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
type: LoadBalancer
ports:
- port: 80
targetPort: 80

kubectl get service nginx-service -o yaml
Kubernetes - 5.1 Discovery and Load Balancing - Service

kubectl describe service nginx-service
Kubernetes - 5.1 Discovery and Load Balancing - Service

ExternalName

将Service映射成外部的DNS名称,请求该Service时将会重定向到外部别名。
kubectl apply -f service-externalname.yaml

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: ExternalName
externalName: example.com

kubectl get service nginx-service -o yaml
Kubernetes - 5.1 Discovery and Load Balancing - Service

kubectl describe service nginx-service
Kubernetes - 5.1 Discovery and Load Balancing - Service

Headless

将Service不分配ClusterIP,直接将EndPoint列表返回,让客户端自行决定访问哪一个Pod
kubectl apply -f service-headless.yaml

apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
selector:
app: nginx
clusterIP: None
ports:
- protocol: TCP
port: 80
targetPort: 80

kubectl get service nginx-service -oyaml
Kubernetes - 5.1 Discovery and Load Balancing - Service

kubectl describe service nginx-service
Kubernetes - 5.1 Discovery and Load Balancing - Service

Service基本操作

查看Service列表
kubectl get service -o yaml | kubectl get service -o wide
Kubernetes - 5.1 Discovery and Load Balancing - Service

通过kubectl create创建Service
kubectl create service clusterip nginx--tcp=80:80

通过kubectl expose创建Service
kubectl expose deployment nginx-deployment --type=ClusterIP --name=nginx-service --port=80 --target-port=80

通过kubectl delete删除Service
kubectl delete service [serviceName]

kubectl delete -f service-clusterip.yaml

使用技巧

  1. Kubernetes Service中不建议使用NodePort去指定某一个IP,这样容易造成端口绑定冲突,最好是由系统自动去分配端口。
  2. Kubernetes 服务调用请通过Service去指定,直接通过Pod IP的方式将容易造成问题,因为Pod在重启后会丢失原有的状态。