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

12次阅读

共计 2912 个字符,预计需要花费 8 分钟才能阅读完成。

本文转自 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

正文完
 0