Service概述
kubernetes提供了Service资源,Service对提供同一个服务的多个pod进行聚合,并且提供一个统一的入口地址。通过访问Service的入口地址就能访问到pod服务。
Service实现核心其实是kube-proxy服务进程,每个Node节点上都运行着一个kube-proxy服务进程。当创建Service的时候通过api-server向etcd写入创建的service信息,而kube-proxy会基于监听的机制发现Service的变动,然后将最新Service信息转换成对应的访问规则。
kube-proxy
kube-proxy支持三种工作模式: userspace模式
、iptables模式
、ipvs模式
userspace
kube-proxy为每一个Service创建一个监听端口,发向Cluster IP的请求被Iptables规则重定向到kube-proxy监听的端口上,kube-proxy根据LB算法选择一个提供服务的Pod并和其建立链接,以此将请求转发到Pod上。
kube-proxy充当一个四层负载均衡器角色。由于kube-proxy运行在userspace中,在进行转发处理时会增加内核和用户空间之间的数据拷贝,虽比较稳定,但效率比较低
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算法。
Service类型
ClusterIP
:默认值,它是Kubernetes系统自动分配的虚拟IP,只能在集群内部访问
NodePort
:将Service通过指定的Node上的端口暴露给外部,通过此方法,就可以在集群外部访问服务
HeadLiness
:不使用Service提供的负载均衡功能,通过自己来控制负载均衡策略
LoadBalancer
:使用外接负载均衡器完成到服务的负载分发,此模式需要外部环境支持
ExternalName
: 把集群外部的服务引入集群内部,直接使用
准备Deployment与Pod
准备Deployment
创建vim deployment-pod.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-pod
namespace: default
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx-name
image: nginx
ports:
- containerPort: 80
准备Pod
# 创建Pod
[root@administrator k8s]# kubectl create -f deployment-pod.yaml
deployment.apps/deployment-pod created
# 查看pod详情
[root@administrator k8s]# kubectl get pods -o wide --show-labels
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES LABELS
deployment-pod-6c568d58df-4cj2c 1/1 Running 0 56s 172.17.0.10 administrator <none> <none> app=nginx-pod,pod-template-hash=6c568d58df
deployment-pod-6c568d58df-bcxd5 1/1 Running 0 56s 172.17.0.8 administrator <none> <none> app=nginx-pod,pod-template-hash=6c568d58df
deployment-pod-6c568d58df-krnqb 1/1 Running 0 56s 172.17.0.9 administrator <none> <none> app=nginx-pod,pod-template-hash=6c568d58df
# 访问测试
[root@administrator k8s]# curl 172.17.0.10
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
ClusterIP类型
创建Service
创建vim service-clusterip.yaml
apiVersion: v1
kind: Service
metadata:
name: service-clusterip
namespace: default
spec:
selector:
app: nginx-pod
clusterIP: # 虚拟服务的ip地址、service的ip地址,如果不写,默认生成一个
type: ClusterIP # Service类型,指定service的访问方式
ports:
- port: 80 # Service端口
targetPort: 80 # pod端口
# 创建service
[root@administrator k8s]# kubectl create -f service-clusterip.yaml
service/service-clusterip created
# 查看service
[root@administrator k8s]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 51m <none>
service-clusterip ClusterIP 10.107.13.81 <none> 80/TCP 37s app=nginx-pod
# 查看service的详细信息
# Endpoints列表:当前service可以负载到的服务入口
[root@administrator k8s]# kubectl describe svc service-clusterip
Name: service-clusterip
Namespace: default
Labels: <none>
Annotations: <none>
Selector: app=nginx-pod
Type: ClusterIP
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.107.13.81
IPs: 10.107.13.81
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 172.17.0.10:80,172.17.0.8:80,172.17.0.9:80
Session Affinity: None
Events: <none>
测试Service
可进入pod中的容器: kubectl exec -it deployment-pod-6c568d58df-4cj2c /bin/sh
修改nginx的首页: echo "deployment-pod-6c568d58df-4cj2c" > /usr/share/nginx/html/index.html
[root@administrator k8s]# curl 10.107.13.81:80
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
Endpoint
Endpoint是kubernetes中的一个资源对象,存储在etcd中,用来记录一个service对应的所有pod的访问地址,它是根据service配置文件中selector描述产生的。
[root@administrator k8s]# kubectl get endpoints -o wide
NAME ENDPOINTS AGE
kubernetes 172.22.4.21:8443 58m
service-clusterip 172.17.0.10:80,172.17.0.8:80,172.17.0.9:80 7m19s
负载分发策略
对Service请求,就会将请求分发到Pod上去。kubernetes提供了两种负载分发策略:
1.如果不定义,默认使用kube-proxy的策略,比如随机、轮询
2.基于客户端地址的会话保持模式,即来自同一个客户端发起的所有请求都会转发到固定的一个Pod上。
第二种负载分发策略需要在spec中添加sessionAffinity:ClientIP
spec:
sessionAffinity:ClientIP # session亲和性,支持ClientIP、None两个选项
selector:
app: nginx-pod
clusterIP: # service的ip地址,如果不写,默认生成一个
NodePort类型
NodePort类型的Service用于将Service暴露给集群外部使用
工作原理:将service的端口映射到Node主机的一个端口上,然后通过
NodeIp:NodePort
来访问service
创建Service
创建service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:
name: service-nodeport
namespace: default
spec:
selector:
app: nginx-pod
type: NodePort # service类型
ports:
- port: 80
nodePort: 30001 # 指定绑定的node的端口(默认的取值范围是:30000-32767), 如果不指定,会默认分配
targetPort: 80
# 创建service
[root@administrator k8s]# kubectl create -f service-nodeport.yaml
service/service-nodeport created
# 查看service
[root@administrator k8s]# kubectl get svc -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 94m <none>
service-clusterip ClusterIP 10.107.13.81 <none> 80/TCP 43m app=nginx-pod
service-headliness ClusterIP None <none> 80/TCP 16m app=nginx-pod
service-nodeport NodePort 10.98.112.245 <none> 80:30001/TCP 11s app=nginx-pod
测试Service
[root@administrator k8s]# curl 127.0.0.1:30001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
body { width: 35em; margin: 0 auto;
font-family: Tahoma, Verdana, Arial, sans-serif; }
</style>
</head>
<body>
<h1>Welcome to nginx!</h1>
<p>If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.</p>
<p>For online documentation and support please refer to
<a href="http://nginx.org/">nginx.org</a>.<br/>
Commercial support is available at
<a href="http://nginx.com/">nginx.com</a>.</p>
<p><em>Thank you for using nginx.</em></p>
</body>
</html>
HeadLiness类型
若不想使用Service提供的负载均衡功能,而希望自己控制负载均衡策略,则可以使用HeadLiness。
kubernetes提供了HeadLiness Service,这类Service不会分配Cluster IP,如果想要访问service,只能通过service的域名进行查询。
创建Service
创建vim service-headliness.yaml
apiVersion: v1
kind: Service
metadata:
name: service-headliness
namespace: default
spec:
selector:
app: nginx-pod
clusterIP: None # 将clusterIP设置为None,即可创建headliness Service
type: ClusterIP
ports:
- port: 80
targetPort: 80
# 创建service
[root@administrator k8s]# kubectl create -f service-headliness.yaml
service/service-headliness created
# 获取service,CLUSTER-IP未分配IP
[root@administrator k8s]# kubectl get svc service-headliness -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service-headliness ClusterIP None <none> 80/TCP 17s app=nginx-pod
# 查看service详情
[root@administrator k8s]# kubectl describe svc service-headliness
Name: service-headliness
Namespace: default
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: 172.17.0.10:80,172.17.0.8:80,172.17.0.9:80
Session Affinity: None
Events: <none>
查看域名的解析
进入容器,查看域名的解析情况
[root@administrator k8s]# kubectl exec -it deployment-pod-6c568d58df-4cj2c /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@deployment-pod-6c568d58df-4cj2c:/# cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
测试Service
安装dig
dig用于从DNS域名服务器上获取主机地址信息
yum install bind-utils
[root@administrator ~]# dig @10.96.0.10 service-headliness.default.svc.cluster.local
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.9 <<>> @10.96.0.10 service-headliness.default.svc.cluster.local
; (1 server found)
;; 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: 15791
;; 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:
;service-headliness.default.svc.cluster.local. IN A
;; ANSWER SECTION:
service-headliness.default.svc.cluster.local. 30 IN A 172.17.0.8
service-headliness.default.svc.cluster.local. 30 IN A 172.17.0.10
service-headliness.default.svc.cluster.local. 30 IN A 172.17.0.9
;; Query time: 17 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Thu Mar 31 22:13:57 CST 2022
;; MSG SIZE rcvd: 253
LoadBalancer类型
LoadBalancer类型的Service用于向外部暴露一个端口,相比NodePort类型Service而言,LoadBalancer会在集群外部做一个负载均衡设备,而这个设备需要外部环境支持的,外部服务发送到这个设备上的请求,会被设备负载之后转发到集群中。
ExternalName类型
ExternalName类型的Service用于引入集群外部的服务,它通过
externalName
属性指定外部一个服务的地址,然后在集群内部访问此service就可以访问到外部的服务了。
创建Service
创建vim service-externalname.yaml
apiVersion: v1
kind: Service
metadata:
name: service-externalname
namespace: default
spec:
type: ExternalName # service类型
externalName: www.baidu.com # 域名或IP
# 创建service
[root@administrator k8s]# kubectl create -f service-externalname.yaml
service/service-externalname created
# 查看service
[root@administrator k8s]# kubectl describe svc service-externalname
Name: service-externalname
Namespace: default
Labels: <none>
Annotations: <none>
Selector: <none>
Type: ExternalName
IP Families: <none>
IP:
IPs: <none>
External Name: www.baidu.com
Session Affinity: None
Events: <none>
测试Service
# 进入容器,查看域名的解析情况
[root@administrator k8s]# kubectl exec -it deployment-pod-6c568d58df-4cj2c /bin/bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
root@deployment-pod-6c568d58df-4cj2c:/# cat /etc/resolv.conf
nameserver 10.96.0.10
search default.svc.cluster.local svc.cluster.local cluster.local
options ndots:5
# 域名解析
[root@administrator ~]# dig @10.96.0.10 service-externalname.default.svc.cluster.local
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.9 <<>> @10.96.0.10 service-externalname.default.svc.cluster.local
; (1 server found)
;; 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: 14408
;; 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:
;service-externalname.default.svc.cluster.local. IN A
;; ANSWER SECTION:
service-externalname.default.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 14.215.177.38
www.a.shifen.com. 30 IN A 14.215.177.39
;; Query time: 14 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Thu Mar 31 22:31:15 CST 2022
;; MSG SIZE rcvd: 255
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/136977.html