service
k8s中的service是一组具有相同lable pod集合的抽象,集群内外的各个服务都可以通过service进行通信.
service资源主要为pod对象提供了一个固定,统一的访问接口和负载均衡的能力.
endpoint
endpoint 是k8s集群中的一个资源对象,存储在etcd中,用来记录一个service对应的所有pod的访问地址
endpoint controller 负责维护endpoint对象
负责监听server和pod的变化,及时更新endpoint对象
Service有哪些类型
- ClusterIP
- 默认,它将创建一个虚拟的ip地址,用于连接客户端和pod。这个ip地址只能在集群内部使用,无法通过集群外访问
apiVersion: v1 kind: Service metadata: name: service-python spec: ports: - port: 3000 protocol: TCP targetPort: 443 selector: run: pod-python type: ClusterIP
- NodePort
- NodePort 允许从集群外部访问service,通过提供端口号映射到Pod的ip地址上,下面的30080就是对外暴露的端口
apiVersion: v1
kind: Service
metadata:
name: service-python
spec:
ports:
- port: 3000
protocol: TCP
targetPort: 443
nodePort: 30080
selector:
run: pod-python
type: NodePort
- LoadBalancer
- LoadBalancer类型的service 是可以实现集群外部访问服务的另外一种解决方案。不过并不是所有的k8s集群都会支持,大多是在公有云托管集群中会支持该类型。负载均衡器是异步创建的。
apiVersion: v1
kind: Service
metadata:
name: service-python
spec:
ports:
- port: 3000
protocol: TCP
targetPort: 443
nodePort: 30080
selector:
run: pod-python
type: LoadBalancer
可以看到external-ip。我们就可以通过该ip来访问了。
- ExternalName
- 类型为 ExternalName 的service将服务映射到 DNS 名称,而不是典型的选择器,例如my-service或者cassandra。 您可以使用spec.externalName参数指定这些服务。
kind: Service
apiVersion: v1
metadata:
name: service-python
spec:
ports:
- port: 3000
protocol: TCP
targetPort: 443
type: ExternalName
externalName: remote.server.url.com
Service的三种工作模式
- userspace
- iptables
- ipvs