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


    本文转自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-world
    NAME          DESIRED   CURRENT   UP-TO-DATE   AVAILABLE       AGE
    hello-world    3          3            3        3              76s
    

    应用程序现在正在运行,如果你想要访问新创建的应用程序,我们需要创建ClusterIP类型的服务:

    • 为服务创建一个YAML manifest并应用它,或

    • 使用kubectl expose命令,这是一个更为简单的选项。因为这一命令可以无需创建YAML文件即可创建一个服务。

    $ kubectl expose deployment hello-world --type=ClusterIP --name=example-service
    service "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)    AGE
    example-service   ClusterIP   100.20.167.76   <none>        8080/TCP   1h
    
    $ kubectl port-forward service/example-service 8080:8080
    Forwarding 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-hostname
    ports:
        # 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
    
  • 相关阅读:
    Spark使用总结与分享【转】
    用实例讲解Spark Sreaming--转
    hbase RowFilter如何根据rowkey查询以及实例实现代码 habase模糊查询【转】
    Android OpenGL ES(十三)通用的矩阵变换指令 .
    Android OpenGL ES(十二):三维坐标系及坐标变换初步 .
    Android OpenGL ES(十一)绘制一个20面体 .
    Android OpenGL ES(十)绘制三角形Triangle .
    Android OpenGL ES(九)绘制线段Line Segment .
    Android OpenGL ES(八)绘制点Point ..
    Android OpenGL ES .介绍
  • 原文地址:https://www.cnblogs.com/rancherlabs/p/14009664.html
Copyright © 2020-2023  润新知