本文转自Rancher Labs

在Kubernetes中,服务总是能使其网络拜访到一个或一组Pod上。服务将会依据标签抉择Pod并且当对这些服务建设网络时,它会抉择集群中所有与服务的selector相匹配的Pod,并抉择其中的一个,而后将网络申请转发给它。

Kubernetes 服务vs Deployment

在K8S中咱们应该如何辨别Deployment和服务呢?

  • Deployment次要负责让一组pod在集群中放弃运行
  • 服务次要负责在集群中启用对一组pod的网络拜访

咱们能够应用deployment而不应用服务,所以咱们能够放弃几个雷同的Pod在K8S集群中运行。此外,Deployment的规模能够扩充和放大,pod也能够复制。在Kubernetes中,单个pod能够间接通过网络申请独自拜访,因而要跟踪pod会有些艰难。

咱们也能够应用一个服务类型而不须要deployment。如果咱们这样做,将创立一个繁多的pod,而不是像咱们在deployment中那样一起创立所有pod。不过,咱们还有另一种代替计划,即咱们的服务可能依据调配给它们的标签进行抉择,从而将网络申请路由到这些Pod。

咱们如何发现Kubernetes服务呢?

在Kubernetes中,有两种形式能够发现服务:

  • DNS类型。DNS server被增加到集群中,以便察看Kubernetes API为每个新服务创立DNS record set。当整个集群启用DNS后,所有的Pod都应该可能主动进行服务名称解析。
  • ENV变量。在这一发现办法中,一个pod运行在一个节点上,所以 kubelet为每个active服务增加环境变量。

ClusterIP、NodePort和LoadBalancer是什么?

服务标准中的类型属性决定了服务如何裸露在网络中。比方,ClusterIP、NodePort和LoadBalancer。

  • ClusterIP—默认值。该服务只能从Kubernetes集群内拜访。
  • NodePort—这使得服务能够通过集群中每个节点上的动态端口拜访。
  • LoadBalancer—服务通过云提供商的负载均衡器性能能够从内部拜访。阿里云、AWS、Azure都提供了这一性能。

如何创立一个服务

通过deployment kind的帮忙,以“Hello World” App模式的简略示例将会帮忙你更好地了解如何创立服务。

咱们的操作流程是,当咱们看到应用程序曾经部署实现并且以up状态运行的时候,咱们将创立服务(Cluster IP)来拜访Kubernetes中的应用程序。

当初,让咱们创立一个正在运行的deployment

“kubectl run hello-world –replicas=3 –labels=”run=load-balancer-example” –image=gcr.io/google-samples/node-hello:1.0 –port=8080”. 

这里,这个命令在Kubernetes中创立了一个有两个应用程序正本的deployment。

接下来,

run "kubectl get deployment hello-world" so see that the deployment is running.Now we can check the replicaset and pods that the deployment created.$ kubectl get deployments hello-worldNAME          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE       AGEhello-world    3          3            3        3              76s

应用程序当初正在运行,如果你想要拜访新创建的应用程序,咱们须要创立ClusterIP类型的服务:

  • 为服务创立一个YAML manifest并利用它,或
  • 应用kubectl expose命令,这是一个更为简略的选项。因为这一命令能够无需创立YAML文件即可创立一个服务。
$ kubectl expose deployment hello-world --type=ClusterIP --name=example-serviceservice "example-service" exposed

在这里,咱们将创立一个名为example-service的服务,类型为ClusterIP。

那么,当初咱们将拜访咱们的应用程序:

run “kubectl get service example-service” to get our port number.

而后,咱们须要执行port-forward命令。因为咱们的服务类型是ClusterIP,所以只能在集群内拜访,因而咱们必须通过转发端口到集群中的本地端口能力拜访咱们的应用程序。

咱们能够应用其余类型,如LoadBalancer,这将在AWS或GCP中创立一个LB,而后咱们能够应用给LB的DNS地址和咱们端口号来拜访应用程序。

$ kubectl get service example-serviceNAME              TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGEexample-service   ClusterIP   100.20.167.76   <none>        8080/TCP   1h$ kubectl port-forward service/example-service 8080:8080Forwarding from 127.0.0.1:8080 -> 8080

当初咱们能够从工作站浏览http://localhost:8080,并且咱们应该会看到:

Hello Kubernetes!

Kubernetes 服务 NodePort YAML示例

此示例YAML创立了可用于内部网络申请的服务。在这里,咱们提到了带Value的NodePort,因而服务被映射到集群中每个节点上的端口。

上面是一个yaml的例子,它将展现咱们如何在Kubernetes中应用NodePort服务类型。

kind: Service apiVersion: v1 metadata:  name: hostname-service spec:  # Expose the service on a static port on each node  # so that we can access the service from outside the cluster   type: NodePort# When the node receives a request on the static port (30163)  # "select pods with the label 'app' set to 'echo-hostname'"  # and forward the request to one of them  selector:    app: echo-hostnameports:    # Three types of ports for a service    # nodePort - a static port assigned on each the node    # port - port exposed internally in the cluster    # targetPort - the container port to send requests to    - nodePort: 30163      port: 8080       targetPort: 80