Service

Service

Service 的概念

Kubernetes Service 定义了这样一种抽象:一个Pod的逻辑分组,一种可以访问它们的策略 —— 通常称为微服务。这一组 Pod 能够被 Service 访问到,通常是通过 Label Selector

Service 是由 kube-proxy 组件,加上 iptables 来共同实现的。

Service能够提供负载均衡的能力,但是在使用上有以下限制:只提供 4 层负载均衡能力,而没有 7 层功能,但有时我们可能需要更多的匹配规则来转发请求,这点上 4 层负载均衡是不支持的

svc

service 类型

ServiceK8s 中 有以下四种类型

  1. ClusterIp:默认类型,自动分配一个仅 Cluster 内部可以访问的虚拟 IP
  2. NodePort:在 ClusterIP 基础上为 Service 在每台机器上绑定一个端口,这样就可以通过: NodePort 来访问该服务
  3. LoadBalancer:在 NodePort 的基础上,借助 cloud provider 创建一个外部负载均衡器,并将请求转发到: NodePort
  4. ExternalName:把集群外部的服务引入到集群内部来,在集群内部直接使用。没有任何类型代理被创建

代理模式的分类

当你通过 Service 的域名去访问时,会先通过 CoreDNS 解析出 Service 对应的 Cluster IP,即虚拟 IP。然后请求到达宿主机的网络后,就会被kube-proxy 所配置的 iptables 规则所拦截,之后请求会被转发到每一个实际的后端 Pod 上面去,这样就实现了负载均衡

  1. userspace 代理模式 (效率较低)
  1. iptables 代理模式 (默认)
  1. Ipvs 代理模式

ClusterIp

clusterIP

clusterIP2
service-example.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: v1
kind: Service
metadata:
name: myapp
namespace: default
spec:
type: ClusterIP
selector:
app: myapp
release: stabel
ports:
- name: http
port: 80
targetPort: 80

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
[root@k8s01 ~]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-deploy-75bf6f6cd-jnf96 1/1 Running 0 14s 172.18.236.167 k8s02 <none> <none>
myapp-deploy-75bf6f6cd-m8ffb 1/1 Running 0 14s 172.18.236.168 k8s02 <none> <none>
myapp-deploy-75bf6f6cd-njzj2 1/1 Running 0 14s 172.18.235.168 k8s03 <none> <none>
[root@k8s01 ~]# curl 172.18.236.167
## 省略


## 只有处于 Running 状态,且 readinessProbe 检查通过的 Pod,才会出现在 Service 的 Endpoints 列表里。并且,当某一个 Pod 出现问题时,Kubernetes 会自动把它从 Service 里摘除掉。
[root@k8s01 ~]# kubectl get endpoints myapp # 获取 endpoints
NAME ENDPOINTS AGE
hostnames 172.18.236.167:80,172.18.236.168:80,172.18.235.168:80 19m


## service 的 hostname : myapp.default.svc.cluster.local

Headless Service

Headless Service : 用于为Pod资源标识符生成可解析的DNS资源记录
(一个典型、完整可用的 StatefulSet 通常由三个组件构成: Headless ServiceStatefulSetvolumeClaimTemplate 。其中,Headless Service 用于为 Pod 资源标识符生成可解析的 DNS 资源记录,StatefulSet 用于管控 Pod 资源,volumeClaimTemplate 则基于静态或动态的PV供给方式为Pod资源提供专有且固定的存储。)

有时不需要或不想要负载均衡,以及单独的 Service IP 。遇到这种情况,可以通过指定 ClusterIP(spec.clusterIP) 的值为 None 来创建 Headless Service 。这类 Service 并不会分配 Cluster IP, kube-proxy 不会处理它们,而且平台也不会为它们进行负载均衡和路由.

svc-headless-example.yaml

1
2
3
4
5
6
7
8
9
10
11
12
apiVersion: v1
kind: Service
metadata:
name: myapp-headless
namespace: default
spec:
clusterIP: "None"
selector:
app: myapp
ports:
port: 80
targetPort: 80

DNS 记录格式 $(pod_name).$(service_name).$(namespace).svc.cluster.local

普通的 pod DNS 记录格式 eg: 172-20-1-125.default.pod.cluster.local

172.20.1.125 是 pod 的 ip 会解析到 172-20-1-125.svc_name.default.svc.cluster.local

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
[root@k8s01 ~]# kubectl apply -f svc-headless-example.yaml
service/myapp-headless created

[root@k8s01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 26d
myapp ClusterIP 10.96.221.214 <none> 80/TCP 26m
myapp-headless ClusterIP None <none> 80/TCP 57s
[root@k8s01 ~]# kubectl get pod -n kube-system -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
calico-kube-controllers-76d4774d89-rltpc 1/1 Running 12 21d 172.18.73.104 k8s01 <none> <none>
calico-node-5xqsz 1/1 Running 13 26d 192.168.43.103 k8s03 <none> <none>
calico-node-cwv6n 1/1 Running 86 26d 192.168.43.101 k8s01 <none> <none>
calico-node-pfpr4 1/1 Running 11 26d 192.168.43.102 k8s02 <none> <none>
coredns-7ff77c879f-7frh7 1/1 Running 14 26d 172.18.73.103 k8s01 <none> <none>
coredns-7ff77c879f-hlzpn 1/1 Running 13 26d 172.18.73.105 k8s01 <none> <none>
etcd-k8s01 1/1 Running 13 26d 192.168.43.101 k8s01 <none> <none>
kube-apiserver-k8s01 1/1 Running 15 26d 192.168.43.101 k8s01 <none> <none>
kube-controller-manager-k8s01 1/1 Running 14 26d 192.168.43.101 k8s01 <none> <none>
kube-proxy-hmlxx 1/1 Running 12 26d 192.168.43.103 k8s03 <none> <none>
kube-proxy-nh96k 1/1 Running 11 26d 192.168.43.102 k8s02 <none> <none>
kube-proxy-x57zq 1/1 Running 13 26d 192.168.43.101 k8s01 <none> <none>
kube-scheduler-k8s01 1/1 Running 14 26d 192.168.43.101 k8s01 <none> <none>
[root@k8s01 ~]# dig -t A myapp-headless.default.svc.cluster.local. @172.18.73.103

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-16.P2.el7_8.6 <<>> -t A myapp-headless.default.svc.cluster.local. @172.18.73.103
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 49368
;; flags: qr aa rd; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;myapp-headless.default.svc.cluster.local. IN A

;; ANSWER SECTION:
myapp-headless.default.svc.cluster.local. 30 IN A 172.18.235.168 # 含有
myapp-headless.default.svc.cluster.local. 30 IN A 172.18.236.167
myapp-headless.default.svc.cluster.local. 30 IN A 172.18.236.168

;; Query time: 1 msec
;; SERVER: 172.18.73.103#53(172.18.73.103)
;; WHEN: Sun Aug 23 11:24:29 EDT 2020
;; MSG SIZE rcvd: 237

NodePort

nodePort 的原理在于在 node 上开了一个端口,将向该端口的流量导入到 kube-proxy,然后由 kube-proxy 进一步到给对应的 pod

nodePort

myapp-noode-service.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
apiVersion: v1
kind: Service
metadata:
name: myapp
namespace: default
spec:
type: NodePort
selector:
app: myapp
release: stabel
ports:
- name: http
port: 80
targetPort: 80

测试

1
2
3
4
5
6
7
8
9
10
11
[root@k8s01 ~]#  kubectl apply -f  myapp-noode-service.yaml
service/myapp configured
[root@k8s01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 26d
myapp NodePort 10.96.221.214 <none> 80:30260/TCP 32m
myapp-headless ClusterIP None <none> 80/TCP 6m31s
[root@k8s01 ~]# curl 172.18.236.167:80 # 略 pod 的 ip + port
[root@k8s01 ~]# curl 10.96.221.214:80 # 略 service 的ip + port
[root@k8s01 ~]# curl 192.168.43.101:30260 # 略 nodeport 的 ip + port
[root@k8s01 ~]# curl 192.168.43.102:30260 # 略

同一网段下

LoadBalancer

loadBalancernodePort 其实是同一种方式。区别在于 loadBalancernodePort 多了一步,就是可以调用 cloud provider 去创建 LB 来向节点导流

myapp-loadbalance.yaml 示例文件

1
2
3
4
5
6
7
8
9
10
11
12
13
apiVersion: v1
kind: Service
metadata:
name: myapp-loadbalance
namespace: default
spec:
selector:
app: myapp
release: stabel
ports:
- port: 8765
targetPort: 80
type: LoadBalancer

LoadBalance

LoadBalance2

ExternalName

这种类型的 Service 通过返回 CNAME 和它的值,可以将服务映射到 externalName 字段的内容( 例如:www.baidu.com )。
ExternalName ServiceService 的特例,它没有 selector,也没有定义任何的端口和Endpoint。相反的,对于运行在集群外部的服务,它通过返回该外部服务的别名这种方式来提供服务

ExternalName
externalname-example.yaml

1
2
3
4
5
6
7
8
kind: Service
apiVersion: v1
metadata:
name: my-service-1
namespace: default
spec:
type: ExternalName
externalName: www.baidu.com

当查询主机 my-service-1.defalut.svc.cluster.local ( SVC_NAME.NAMESPACE.svc.cluster.local )时,集群的 DNS 服务将返回一个值 www.baidu.comCNAME 记录。
访问这个服务的工作方式和其他的相同,唯一不同的是重定向发生在 DNS 层,而且不会进行代理或转发

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
[root@k8s01 ~]# kubectl apply -f  externalname-example.yaml
service/my-service-1 unchanged
[root@k8s01 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 26d
my-service-1 ExternalName <none> www.baidu.com <none> 3m27s
myapp NodePort 10.96.221.214 <none> 80:30260/TCP 47m
myapp-headless ClusterIP None <none> 80/TCP 21m
[root@k8s01 ~]# dig -t A my-service-1.default.svc.cluster.local. @172.18.73.103

; <<>> DiG 9.11.4-P2-RedHat-9.11.4-16.P2.el7_8.6 <<>> -t A my-service-1.default.svc.cluster.local. @172.18.73.103
;; global options: +cmd
;; Got answer:
;; WARNING: .local is reserved for Multicast DNS
;; You are currently testing what happens when an mDNS query is leaked to DNS
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 4010
;; flags: qr aa rd; QUERY: 1, ANSWER: 4, AUTHORITY: 0, ADDITIONAL: 1
;; WARNING: recursion requested but not available

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
;; QUESTION SECTION:
;my-service-1.default.svc.cluster.local. IN A

;; ANSWER SECTION:
my-service-1.default.svc.cluster.local. 1 IN CNAME www.baidu.com. ## 相当于做域名解析
www.baidu.com. 1 IN CNAME www.a.shifen.com.
www.a.shifen.com. 1 IN A 163.177.151.110
www.a.shifen.com. 1 IN A 163.177.151.109

;; Query time: 0 msec
;; SERVER: 172.18.73.103#53(172.18.73.103)
;; WHEN: Sun Aug 23 11:44:59 EDT 2020
;; MSG SIZE rcvd: 239

比如引用外部数据库到集群

1
2
3
4
5
6
7
8
9
# cat mysql_service.yaml 
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
type: ClusterIP
ports:
- port: 3306

定义 endpoint

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# cat mysql_endpoint.yaml 
apiVersion: v1
kind: Endpoints
metadata:
name: mysql
subsets:
- addresses:
- ip: 192.168.43.101
ports:
- port: 3306

# kubectl apply -f mysql_service.yaml -f mysql_endpoint.yaml

# mysql -u root -h mysql.default.svc.cluster.local ## 也可以访问 MySQL 数据库

myapp

myapp-deploy.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
apiVersion: apps/v1
kind: Deployment
metadata:
name: myapp-deploy
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: myapp
release: stabel
template:
metadata:
labels:
app: myapp
release: stabel
env: test
spec:
containers:
- name: myapp
image: nginx:1.7.9
imagePullPolicy: IfNotPresent
ports:
- name: http
containerPort: 80

更多

https://blog.csdn.net/xixihahalelehehe/article/details/107993811