本文已参与「新人创作礼」活动, 一起开启掘金创作之路。
Service介绍
Service概述
在kubernetes中,pod是应用程序的载体,我们可以通过pod的ip来访问应用程序,但是pod的ip地址不是固定的,这也就意味着不方便直接采用pod的ip对服务进行访问。 为了解决这个问题,kubernetes提供了Service资源,Service会对提供同一个服务的多个pod进行聚合,并且提供一个统一的入口地址。通过访问Service的入口地址就能访问到后面的pod服务。
Service在很多情况下只是一个概念,真正起作用的其实是kube-proxy服务进程,每个Node节点上都运行着一个kube-proxy服务进程。当创建Service的时候会通过api-server向etcd写入创建的service的信息,而kube-proxy会基于监听的机制发现这种Service的变动,然后它会将最新的Service信息转换成对应的访问规则。
# 10.97.97.97:80 是service提供的访问入口
# 当访问这个入口的时候,可以发现后面有三个pod的服务在等待调用,
# kube-proxy会基于rr(轮询)的策略(ipvs的前提下),将请求分发到其中一个pod上去
# 这个规则会同时在集群内的所有节点上都生成,所以在任何一个节点上访问都可以。
[root@node1 ~]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.97.97.97:80 rr
-> 10.244.1.39:80 Masq 1 0 0
-> 10.244.1.40:80 Masq 1 0 0
-> 10.244.2.33:80 Masq 1 0 0
kube-proxy目前支持三种工作模式:
userspace 模式
userspace模式下,kube-proxy会为每一个Service创建一个监听端口,发向Cluster IP的请求被Iptables规则重定向到kube-proxy监听的端口上,kube-proxy根据LB算法选择一个提供服务的Pod并和其建立链接,以将请求转发到Pod上。 该模式下,kube-proxy充当了一个四层负责均衡器的角色。由于kube-proxy运行在userspace中,在进行转发处理时会增加内核和用户空间之间的数据拷贝,虽然比较稳定,但是效率比较低
iptables 模式
iptables模式下,kube-proxy为service后端的每个Pod创建对应的iptables规则,直接将发向Cluster IP的请求重定向到一个Pod IP。
该模式下kube-proxy不承担四层负责均衡器的角色,只负责创建iptables规则。该模式的优点是较userspace模式效率更高,但不能提供灵活的LB策略,当后端Pod不可用时也无法进行重试。
ipvs 模式
ipvs模式和iptables类似,kube-proxy监控Pod的变化并创建相应的ipvs规则。ipvs相对iptables转发效率更高。除此以外,ipvs支持更多的LB算法。
# 此模式必须安装ipvs内核模块,否则会降级为iptables
# 开启ipvs
[root@master modules]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
# mode模式改成ipvs-->mode="ipvs"
[root@master modules]# kubectl edit cm kube-proxy -n kube-system
configmap/kube-proxy edited
# 等待自动重建
[root@master modules]# kubectl delete pod -l k8s-app=kube-proxy -n kube-system
pod "kube-proxy-vxflz" deleted
pod "kube-proxy-w886d" deleted
pod "kube-proxy-xb7kp" deleted
[root@master modules]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.96.0.1:443 rr
-> 192.168.109.100:6443 Masq 1 0 0
TCP 10.96.0.10:53 rr
-> 10.244.0.2:53 Masq 1 0 0
-> 10.244.0.3:53 Masq 1 0 0
TCP 10.96.0.10:9153 rr
-> 10.244.0.2:9153 Masq 1 0 0
-> 10.244.0.3:9153 Masq 1 0 0
TCP 10.110.157.232:443 rr
-> 192.168.109.101:4443 Masq 1 0 0
UDP 10.96.0.10:53 rr
-> 10.244.0.2:53 Masq 1 0 0
-> 10.244.0.3:53 Masq 1 0 0
Service类型
Service的资源清单文件:
kind: Service # 资源类型
apiVersion: v1 # 资源版本
metadata: # 元数据
name: service # 资源名称
namespace: dev # 命名空间
spec: # 描述
selector: # 标签选择器,用于确定当前service代理哪些pod(通过标签获取对应ip,存入ipvs)
app: nginx
type: # Service类型,指定service的访问方式
clusterIP: # 虚拟服务的ip地址
sessionAffinity: # session亲和性,支持ClientIP、None两个选项(同一个请求ip到同一个pod)
ports: # 端口信息
- protocol: TCP
port: 3017 # service端口
targetPort: 5003 # pod端口
nodePort: 31122 # 主机端口
# type: Service类型,指定service的访问方式
- ClusterIP:默认值,它是Kubernetes系统自动分配的虚拟IP,只能在集群内部访问
- NodePort:将Service通过指定的Node上的端口暴露给外部,通过此方法,就可以在集群外部访问服务
- LoadBalancer:使用外接负载均衡器完成到服务的负载分发,注意此模式需要外部云环境支持
- ExternalName: 把集群外部的服务引入集群内部,直接使用
Service使用
实验环境准备
在使用service之前,首先利用Deployment创建出3个pod,注意要为pod设置app=nginx-pod的标签
创建deployment.yaml,内容如下:
apiVersion: apps/v1
kind: Deployment
metadata:
name: pc-deployment
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
# 创建deploy
[root@master k8sYamlForCSDN]# kubectl apply -f deployment.yaml
deployment.apps/pc-deployment created
# 查看pod详情
[root@master k8sYamlForCSDN]# kubectl get pods -n dev -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
pc-deployment-6d99999569-dxpzt 1/1 Running 0 40s 10.244.2.101 node2 <none> <none> app=nginx-pod,pod-template-hash=6d99999569
pc-deployment-6d99999569-m69zc 1/1 Running 0 40s 10.244.1.71 node1 <none> <none> app=nginx-pod,pod-template-hash=6d99999569
pc-deployment-6d99999569-wk9nz 1/1 Running 0 40s 10.244.2.102 node2 <none> <none> app=nginx-pod,pod-template-hash=6d99999569
# 为了方便后面的测试,修改下三台nginx的index.html页面(三台修改的IP地址不一致)
# kubectl exec -it pc-deployment-6d99999569-dxpzt -n dev /bin/sh
# echo "10.244.2.101" > /usr/share/nginx/html/index.html
# kubectl exec -it pc-deployment-6d99999569-m69zc -n dev /bin/sh
# echo "10.244.1.71" > /usr/share/nginx/html/index.html
# kubectl exec -it pc-deployment-6d99999569-wk9nz -n dev /bin/sh
# echo "10.244.2.102" > /usr/share/nginx/html/index.html
# 修改完毕之后,访问测试
[root@master k8sYamlForCSDN]# curl 10.244.2.101:80
10.244.2.101
[root@master k8sYamlForCSDN]# curl 10.244.1.71:80
10.244.1.71
[root@master k8sYamlForCSDN]# curl 10.244.2.102:80
10.244.2.102
ClusterIP类型的Service
它是Kubernetes系统自动分配的虚拟IP,缺点:==只能在集群内部访问==
创建service-clusterip.yaml文件
apiVersion: v1
kind: Service
metadata:
name: service-clusterip
namespace: dev
spec:
selector:
app: nginx-pod
clusterIP: 10.97.97.97 # service的ip地址,如果不写,默认会生成一个
type: ClusterIP
ports:
- port: 80 # Service端口
targetPort: 80 # pod端口
# 创建service
[root@master k8sYamlForCSDN]# kubectl apply -f service-clusterip.yaml
service/service-clusterip created
# 查看service
[root@master k8sYamlForCSDN]# kubectl get svc -n dev -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service-clusterip ClusterIP 10.97.97.97 <none> 80/TCP 17s app=nginx-pod
# 查看service的详细信息
# 在这里有一个Endpoints列表,里面就是当前service可以负载到的服务入口
[root@master k8sYamlForCSDN]# kubectl describe svc service-clusterip -n dev
Name: service-clusterip
Namespace: dev
Labels: <none>
Annotations: <none>
Selector: app=nginx-pod
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.97.97.97
IPs: 10.97.97.97
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.71:80,10.244.2.101:80,10.244.2.102:80
Session Affinity: None
Events: <none>
# 查看ipvs的映射规则
[root@master k8sYamlForCSDN]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.97.97.97:80 rr
-> 10.244.1.71:80 Masq 1 0 0
-> 10.244.2.101:80 Masq 1 0 0
-> 10.244.2.102:80 Masq 1 0 0
# rr->轮询
[root@master k8sYamlForCSDN]# curl 10.97.97.97:80
10.244.2.102
[root@master k8sYamlForCSDN]# curl 10.97.97.97:80
10.244.2.101
[root@master k8sYamlForCSDN]# curl 10.97.97.97:80
10.244.1.71
[root@master k8sYamlForCSDN]# curl 10.97.97.97:80
10.244.2.102
Endpoint Endpoint是kubernetes中的一个资源对象,存储在etcd中,用来记录一个service对应的所有pod的访问地址,它是根据service配置文件中selector描述产生的。 一个Service由一组Pod组成,这些Pod通过Endpoints暴露出来,Endpoints是实现实际服务的端点集合。换句话说,service和pod之间的联系是通过endpoints实现的。
图片仅仅演示,没有画全
负载分发策略
对Service的访问被分发到了后端的Pod上去,目前kubernetes提供了两种负载分发策略:
-
如果不定义,默认使用kube-proxy的策略,比如随机、轮询
-
基于客户端地址的会话保持模式,即来自同一个客户端发起的所有请求都会转发到固定的一个Pod上,此模式需要在spec中添加
sessionAffinity:ClientIP选项
# 查看ipvs的映射规则【rr 轮询】
[root@master k8sYamlForCSDN]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.97.97.97:80 rr
-> 10.244.1.71:80 Masq 1 0 0
-> 10.244.2.101:80 Masq 1 0 0
-> 10.244.2.102:80 Masq 1 0 0
# 循环访问测试
[root@master k8sYamlForCSDN]# while true;do curl 10.97.97.97:80; sleep 1; done;
10.244.1.71
10.244.2.102
10.244.2.101
10.244.1.71
10.244.2.102
10.244.2.101
# 修改分发策略----sessionAffinity:ClientIP
# 查看ipvs规则【persistent 代表持久】
[root@master ~]# ipvsadm -Ln
[root@master k8sYamlForCSDN]# ipvsadm -Ln
IP Virtual Server version 1.2.1 (size=4096)
Prot LocalAddress:Port Scheduler Flags
-> RemoteAddress:Port Forward Weight ActiveConn InActConn
TCP 10.97.97.97:80 rr persistent 10800
-> 10.244.1.71:80 Masq 1 0 4
-> 10.244.2.101:80 Masq 1 0 4
-> 10.244.2.102:80 Masq 1 0 3
# 循环访问测试
[root@master k8sYamlForCSDN]# while true;do curl 10.97.97.97:80; sleep 1; done;
10.244.2.102
10.244.2.102
10.244.2.102
10.244.2.102
# 删除service
[root@master k8sYamlForCSDN]# kubectl delete -f service-clusterip.yaml
service "service-clusterip" deleted
HeadLiness类型的Service
HeadLiness即无头服务,在某些场景中,开发人员可能不想使用Service提供的负载均衡功能,而希望自己来控制负载均衡策略,针对这种情况,kubernetes提供了HeadLiness Service,这类Service不会分配Cluster IP,如果想要访问service,只能通过service的域名进行查询。
创建service-headliness.yaml
apiVersion: v1
kind: Service
metadata:
name: service-headliness
namespace: dev
spec:
selector:
app: nginx-pod
clusterIP: None # 将clusterIP设置为None,即可创建headliness Service
type: ClusterIP
ports:
- port: 80
targetPort: 80
# 创建service
[root@master k8sYamlForCSDN]# kubectl apply -f service-headliness.yaml
service/service-headliness created
# 获取service, 发现CLUSTER-IP未分配
[root@master k8sYamlForCSDN]# kubectl get svc service-headliness -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service-headliness ClusterIP None <none> 80/TCP 38s
# 查看service详情
[root@master k8sYamlForCSDN]# kubectl describe svc service-headliness -n dev
Name: service-headliness
Namespace: dev
Labels: <none>
Annotations: <none>
Selector: app=nginx-pod
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: None
IPs: None
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.1.71:80,10.244.2.101:80,10.244.2.102:80
Session Affinity: None
Events: <none>
# 查看域名的解析情况
# 首先查找域名服务器的ip地址
[root@master k8sYamlForCSDN]# kubectl exec -it pc-deployment-6d99999569-dxpzt -n dev /bin/sh
# cat /etc/resolv.conf
nameserver 10.96.0.10
search dev.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
# 解析
[root@master k8sYamlForCSDN]# dig @10.96.0.10 service-headliness.dev.svc.cluster.local
...
;; ANSWER SECTION:
service-headliness.dev.svc.cluster.local. 30 IN A 10.244.2.102
service-headliness.dev.svc.cluster.local. 30 IN A 10.244.1.71
service-headliness.dev.svc.cluster.local. 30 IN A 10.244.2.101
...
NodePort类型的Service
在之前的样例中,创建的Service的ip地址只有集群内部才可以访问,如果希望将Service暴露给集群外部使用,那么就要使用到另外一种类型的Service,称为NodePort类型。NodePort的工作原理其实就是将service的端口映射到Node的一个端口上,然后就可以通过NodeIp:NodePort来访问service了。
创建service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
name: service-nodeport
namespace: dev
spec:
selector:
app: nginx-pod
type: NodePort # service类型
ports:
- port: 80
nodePort: 6872 # 指定绑定的node的端口(默认的取值范围是:30000-32767), 如果不指定,会默认分配
targetPort: 80
# 创建service
[root@master k8sYamlForCSDN]# kubectl apply -f service-nodeport.yaml
service/service-nodeport created
# 查看service
[root@master k8sYamlForCSDN]# kubectl get svc service-nodeport -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service-nodeport NodePort 10.99.95.186 <none> 80:30002/TCP 11s
# 接下来可以通过电脑主机的浏览器去访问集群中任意一个nodeip的30002端口,即可访问到pod
通过NodeIp:NodePort访问会映射到CLUSTER-IP:Port,而CLUSTER-IP:Port又会映射到其中的endpoint里面,而我这里集群有3台node,实际上就是3台node都开启了NodePort对应的端口。所以不论是100:30002,还是101,102,都可以成功访问
192.168.109.100:30002 --->映射到---> 10.99.95.186:80 --->映射+算法---> 对应的pod中
192.168.109.101:30002 --->映射到---> 10.99.95.186:80 --->映射+算法---> 对应的pod中
192.168.109.102:30002 --->映射到---> 10.99.95.186:80 --->映射+算法---> 对应的pod中
LoadBalancer类型的Service
LoadBalancer和NodePort很相似,目的都是向外部暴露一个端口,区别在于LoadBalancer会在集群的外部再来做一个负载均衡设备,而这个设备需要外部环境支持的,外部服务发送到这个设备上的请求,会被设备负载之后转发到集群中。
ExternalName类型的Service
ExternalName类型的Service用于引入集群外部的服务,它通过externalName属性指定外部一个服务的地址,然后在集群内部访问此service就可以访问到外部的服务了。
apiVersion: v1
kind: Service
metadata:
name: service-externalname
namespace: dev
spec:
type: ExternalName # service类型
externalName: www.baidu.com #改成ip地址也可以
# 创建service
[root@master k8sYamlForCSDN]# kubectl apply -f service-externalname.yaml
service/service-externalname created
# 查看service
[root@master k8sYamlForCSDN]# kubectl get svc service-externalname -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service-externalname ExternalName <none> www.baidu.com <none> 34s
# 域名解析
[root@master k8sYamlForCSDN]# dig @10.96.0.10 service-externalname.dev.svc.cluster.local
......
;; ANSWER SECTION:
service-externalname.dev.svc.cluster.local. 30 IN CNAME www.baidu.com.
www.baidu.com. 30 IN CNAME www.a.shifen.com.
www.a.shifen.com. 30 IN A 180.101.49.12
www.a.shifen.com. 30 IN A 180.101.49.11......