Kubernetes 中的服务是什么?
Service 支持通过网络访问 Kubernetes 中的一组 Pod。
服务根据标签选择 Pod。当向服务发出网络请求时,它会选择集群中与服务选择器匹配的所有 Pod,选择其中之一,并将网络请求转发给它。
Kubernetes 服务与部署
Kubernetes 中的服务和部署有什么区别?
部署负责保持一组 pod 运行。
服务负责启用对一组 pod 的网络访问。
我们可以使用没有服务的部署来保持一组相同的 Pod 在 Kubernetes 集群中运行。部署可以扩展和缩减,Pod 可以复制。每个 pod 都可以通过直接网络请求单独访问(而不是将它们抽象到服务后面),但是很难对许多 pod 进行跟踪。
我们也可以使用没有部署的服务。我们需要单独创建每个 pod(而不是像部署那样“一次性”创建)。然后我们的服务可以通过根据标签选择它们来将网络请求路由到这些 pod。
服务和部署是不同的,但它们可以很好地协同工作
Kubernetes 服务 NodePort 示例 YAML
此示例 YAML 创建了一个可用于外部网络请求的服务。我们已经指定了 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
ClusterIP、NodePort 和 LoadBalancer 是什么意思?
type服务规范中的属性决定了服务如何暴露给网络。它改变了可以从哪里访问服务。可能type的有 ClusterIP、NodePort 和 LoadBalancer
ClusterIP
: 默认值。该服务只能从 Kubernetes 集群内部访问——您不能从集群外部向您的 Pod 发出请求!NodePort
: 这使得服务可以在集群中每个节点的静态端口上访问。这意味着该服务可以处理来自集群外部的请求。LoadBalancer
: 可以通过云提供商的负载平衡器功能从外部访问该服务。GCP、AWS、Azure 和 OpenStack 提供此功能。云提供商将创建一个负载均衡器,然后它会自动将请求路由到您的 Kubernetes 服务
发表评论 取消回复