生成式AI领域的最新成果都在这里!抢 QCon 展区门票 了解详情
写点什么

Kubernetes 服务类型浅析:从概念到实践

  • 2020-11-06
  • 本文字数:2307 字

    阅读完需:约 8 分钟

Kubernetes服务类型浅析:从概念到实践


在 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-service
NAME 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
复制代码


原文链接:

https://medium.com/avmconsulting-blog/service-types-in-kubernetes-24a1587677d6


本文转载自公众号 RancherLabs(ID:RancherLabs)。


原文链接


Kubernetes服务类型浅析:从概念到实践


2020-11-06 10:112211

评论

发布
暂无评论
发现更多内容
Kubernetes服务类型浅析:从概念到实践_服务革新_Rancher_InfoQ精选文章