您现在的位置是:首页 > 文章详情

Kubernetes - 5.1 Discovery and Load Balancing - Service

日期:2020-03-01点击:380

什么是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
image

kubectl describe service nginx-service
image

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
image

kubectl describe service nginx-service
image

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
image

kubectl describe service nginx-service
image

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
image

kubectl describe service nginx-service
image

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
image

kubectl describe service nginx-service
image

Service基本操作

查看Service列表
kubectl get service -o yaml | kubectl get service -o wide
image

通过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在重启后会丢失原有的状态。
原文链接:https://yq.aliyun.com/articles/747558
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章