Kubernetes 服务
服务可以定义为一组逻辑上的 pod。它可以被定义为 Pod 顶部的抽象,它提供了一个单一的 IP 地址和 DNS 名称,通过它可以访问 Pod。使用 Service,可以非常轻松地管理负载平衡配置。它有助于 Pod 非常轻松地扩展。
服务是 Kubernetes 中的一个 REST 对象,其定义可以发布到 Kubernetes master 上的 Kubernetes apiServer 以创建新实例。
无选择器的服务
apiVersion: v1
kind: Service
metadata:
name: Tutorial_point_service
spec:
ports:
-port: 8080
targetPort: 31999
以上配置将创建一个名为 Tutorial_point_service 的服务。
带有选择器的服务配置文件
apiVersion: v1
kind: Service
metadata:
name: Tutorial_point_service
spec:
selector:
application: "My Application"-------------------> (Selector)
ports:
-port: 8080
targetPort: 31999
在这个例子中,我们有一个选择器;所以为了传输流量,我们需要手动创建一个端点。
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"的端点。
多端口服务创建
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
服务类型
ClusterIP-这有助于限制集群内的服务。它在定义的 Kubernetes 集群中公开服务。
spec:
type: NodePort
ports:
-port: 8080
nodePort: 31999
name: NodeportService
NodePort-它将在已部署节点的静态端口上公开服务。自动创建
NodePort 服务将路由到的
ClusterIP 服务。可以使用
NodeIP:nodePort 从集群外部访问该服务。
spec:
ports:
-port: 8080
nodePort: 31999
name: NodeportService
clusterIP: 10.20.30.40
负载均衡器-它使用云提供商的负载均衡器。
NodePort 和
ClusterIP 服务是自动创建的,外部负载均衡器将路由到这些服务。
一个完整的服务
yaml 文件,服务类型为节点端口。尝试自己创建一个。
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