商城首页欢迎来到中国正版软件门户

您的位置: 首页 > 文章列表 > 系统应用 > Kubernetes服务类型浅析:从概念到实践

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

  发布于2026-08-20 阅读(0)

扫一扫,手机访问

本文转自Rancher Labs

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

Kubernetes 服务vs Deployment

在K8S中我们应该如何区分Deployment和服务呢?

  • Deployment主要负责让一组pod在集群中保持运行

  • 服务主要负责在集群中启用对一组pod的网络访问

在K8S集群中,我们其实可以只使用deployment,而不依赖服务,这样就能让几个相同的Pod保持运行。而且,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 A VAILABLE 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 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
本文转载于:https://apiv1.oschina.net/oschinapi/blog/detail?id=4727825 如有侵犯,请联系zhengruancom@outlook.com删除。
免责声明:正软商城发布此文仅为传递信息,不代表正软商城认同其观点或证实其描述。

热门关注