Kubernetes 简明教程
Kubernetes - Service
服务可以定义为 pod 的逻辑集合。它可以定义为 pod 上的抽象,它提供一个单个 IP 地址和 DNS 名称,可以通过它访问 pod。使用服务,非常容易管理负载均衡配置。它帮助 pod 非常容易地扩展。
A service can be defined as a logical set of pods. It can be defined as an abstraction on the top of the pod which provides a single IP address and DNS name by which pods can be accessed. With Service, it is very easy to manage load balancing configuration. It helps pods to scale very easily.
在 Kubernetes 中,服务是一个 REST 对象,它的定义可以发布到 Kubernetes master 上的 Kubernetes apiServer 以创建一个新实例。
A service is a REST object in Kubernetes whose definition can be posted to Kubernetes apiServer on the Kubernetes master to create a new instance.
Service without Selector
apiVersion: v1
kind: Service
metadata:
name: Tutorial_point_service
spec:
ports:
- port: 8080
targetPort: 31999
上面的配置将创建一个名为 Tutorial_point_service 的服务。
The above configuration will create a service with the name Tutorial_point_service.
Service Config File with Selector
apiVersion: v1
kind: Service
metadata:
name: Tutorial_point_service
spec:
selector:
application: "My Application" -------------------> (Selector)
ports:
- port: 8080
targetPort: 31999
在这个示例中,我们有一个选择器;所以为了传输流量,我们需要手动创建一个端点。
In this example, we have a selector; so in order to transfer traffic, we need to create an endpoint manually.
apiVersion: v1
kind: Endpoints
metadata:
name: Tutorial_point_service
subnets:
address:
"ip": "192.168.168.40" -------------------> (Selector)
ports:
- port: 8080
在上面的代码中,我们创建了一个端点,它将路由流量到定义为“192.168.168.40:8080”的端点。
In the above code, we have created an endpoint which will route the traffic to the endpoint defined as “192.168.168.40:8080”.
Multi-Port Service Creation
apiVersion: v1
kind: Service
metadata:
name: Tutorial_point_service
spec:
selector:
application: “My Application” -------------------> (Selector)
ClusterIP: 10.3.0.12
ports:
-name: http
protocol: TCP
port: 80
targetPort: 31999
-name:https
Protocol: TCP
Port: 443
targetPort: 31998
Types of Services
ClusterIP − 这有助于在集群内限制服务。它在定义的 Kubernetes 集群内公开该服务。
ClusterIP − This helps in restricting the service within the cluster. It exposes the service within the defined Kubernetes cluster.
spec:
type: NodePort
ports:
- port: 8080
nodePort: 31999
name: NodeportService
NodePort − 它将在已部署节点上的静态端口公开服务。一个服务, NodePort 服务将路由到的服务自动创建。使用 NodeIP:nodePort 可以从集群外访问该服务。
NodePort − It will expose the service on a static port on the deployed node. A ClusterIP service, to which NodePort service will route, is automatically created. The service can be accessed from outside the cluster using the NodeIP:nodePort.
spec:
ports:
- port: 8080
nodePort: 31999
name: NodeportService
clusterIP: 10.20.30.40
Load Balancer − 它使用云服务商的负载均衡器。 NodePort 和 ClusterIP 服务自动创建,外部负载均衡器将路由到这些服务。
Load Balancer − It uses cloud providers’ load balancer. NodePort and ClusterIP services are created automatically to which the external load balancer will route.
一个完整服务 yaml 文件,其服务类型为节点端口。尝试自己创建一个。
A full service yaml file with service type as Node Port. Try to create one yourself.
apiVersion: v1
kind: Service
metadata:
name: appname
labels:
k8s-app: appname
spec:
type: NodePort
ports:
- port: 8080
nodePort: 31999
name: omninginx
selector:
k8s-app: appname
component: nginx
env: env_name