Service详解
k8s Service详解
Service详解
Pod是程序的载体,通过Pod的IP可以访问程序,但Pod的IP地址并不固定,不方便直接对Pod IP直接进行访问。
k8s提供Service资源,能对提供同一个服务的多个Pod进行聚合,并提供一个统一的入口地址。通过访问Service的入口地址就能访问Pod服务。
Service很多情况是概念,kube-proxy服务进程真正起作用,每个Node节点上运行kube-proxy。创建Service通过api-server向etcd写入创建的service信息,kube-proxy基于监听发现Service变动,将最新的Service信息转换成对应访问规则。
一般常用ipvs模式
#开启ipvs
#把mode变成ipvs
[root@master ~]# kubectl edit cm kube-proxy -n kube-system
configmap/kube-proxy edited
[root@master ~]# kubectl delete pod -l k8s-app=kube-proxy -n kube-system
pod "kube-proxy-lqdks" deleted
pod "kube-proxy-wcn2x" deleted
pod "kube-proxy-x78bv" deleted
#查看规则
[root@master ~]# 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.126.132:6443 Masq 1 0 0
TCP 10.96.0.10:53 rr
-> 10.244.0.4:53 Masq 1 0 0
-> 10.244.0.5:53 Masq 1 0 0
TCP 10.96.0.10:9153 rr
-> 10.244.0.4:9153 Masq 1 0 0
-> 10.244.0.5:9153 Masq 1 0 0
TCP 10.100.107.200:443 rr
-> 192.168.126.134:443 Masq 1 0 10
UDP 10.96.0.10:53 rr
-> 10.244.0.4:53 Masq 1 0 0
-> 10.244.0.5:53 Masq 1 0 0
资源清单
kind: Service # 资源类型
apiVersion: v1 # 资源版本
metadata: # 元数据
name: service # 资源名称
namespace: dev # 命名空间
spec: # 描述
selector: # 标签选择器,用于确定当前service代理哪些pod
app: nginx
type: # Service类型,指定service的访问方式
clusterIP: # 虚拟服务的ip地址
sessionAffinity: # session亲和性,支持ClientIP、None两个选项
ports: # 端口信息
- protocol: TCP
port: 3017 # service端口
targetPort: 5003 # pod端口
nodePort: 31122 # 主机端口
- clusterIP:默认值,k8s系统自动分配,只能在集群内部使用
- nodePort:将Service通过指定Node上端口暴露给外部
- LoadBalancer:使用外接负载均衡器完成到服务的负载分发,注意此模式需要外部云环境支持
- ExternalName: 把集群外部的服务引入集群内部,直接使用
使用
环境准备
首先利用Deployment创建出三个Pod,为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:1.17.1
ports:
- containerPort: 80
#这里我创建了一个临时文件夹把yaml文件放入这里执行 防止与其他文件混淆
[root@master ~]# mkdir tmp
[root@master ~]# cd tmp/
#创建名称空间dev
[root@master tmp]# kubectl create ns dev
namespace/dev created
#创建deployment
[root@master tmp]# kubectl create -f deployment.yaml
deployment.apps/pc-deployment created
#查看Pod详情
[root@master ~]# kubectl get pod -n dev -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pc-deployment-6696798b78-7l646 1/1 Running 0 33m 10.244.2.75 node2 <none> <none>
pc-deployment-6696798b78-dp9t4 1/1 Running 0 33m 10.244.2.77 node2 <none> <none>
pc-deployment-6696798b78-llzpc 1/1 Running 0 33m 10.244.2.76 node2 <none> <none>
#方便测试,修改三台nginx的index.html页面
# kubectl exec -it pc-deployment-自动生成的后缀 -n dev /bin/sh
# echo [IP地址] > /usr/share/nginx/html/index.html
[root@master ~]# kubectl exec -it pc-deployment-6696798b78-7l646 -n dev /bin/sh
# echo "10.244.2.75" > /usr/share/nginx/html/index.html
# exit
[root@master ~]# kubectl exec -it pc-deployment-6696798b78-dp9t4 -n dev /bin/sh
# echo "10.244.2.77" > /usr/share/nginx/html/index.html
# exit
[root@master ~]# kubectl exec -it pc-deployment-6696798b78-llzpc -n dev /bin/sh
# echo "10.244.2.76" > /usr/share/nginx/html/index.html
# exit
[root@master ~]# curl 10.244.2.75:80
10.244.2.75
[root@master ~]# curl 10.244.2.77:80
10.244.2.77
[root@master ~]# curl 10.244.2.76:80
10.244.2.76
如果无法ping通node节点或Pod节点,很有可能是虚拟机IP地址改变请设置静态IP地址
#BOOTPROTO=static 设置静态模式
IPADDR= #IP地址 你自己虚拟机的IP地址
NETMASK=255.255.255.0 #掩码
GATEWAY= #网关
DNS1=8.8.8.8
DNS2=114.114.114.114
ClusterIP
ClusterIP
通过集群的内部 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 tmp]# kubectl create -f service-clusterip.yaml
service/service-clusterip created
[root@master tmp]# 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 25s app=nginx-pod
[root@master tmp]# kubectl describe svc service-clusterip -n dev
Name: service-clusterip
Namespace: dev
Labels: <none>
Annotations: <none>
Selector: app=nginx-pod
Type: ClusterIP
IP: 10.97.97.97
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.2.75:80,10.244.2.76:80,10.244.2.77:80
Session Affinity: None
Events: <none>
上述详细信息里的Endpoint是k8s一个资源对象,存储在etcd中,用来记录Service对应的所以Pod访问地址,根据Service的selector描述产生。
一个Service由一组Pod组成,这些Pod通过Endpoints暴露,Endpoints是实现实际服务的端点集合。
Endpoints: 10.244.2.75:80,10.244.2.76:80,10.244.2.77:80
#查看endpoints
[root@master tmp]# kubectl get endpoints -n dev -o wide
NAME ENDPOINTS AGE
service-clusterip 10.244.2.75:80,10.244.2.76:80,10.244.2.77:80 23m
#查看ipvs规则
[root@master tmp]# ipvsadm -Ln
...
TCP 10.97.97.97:80 rr#rr 轮询策略
-> 10.244.2.75:80 Masq 1 0 0
-> 10.244.2.76:80 Masq 1 0 0
-> 10.244.2.77:80 Masq 1 0 0
...
#循环访问10.97.97.97:80查看效果
[root@master tmp]# while true;do curl 10.97.97.97:80; sleep 2; done;
10.244.2.76
10.244.2.75
10.244.2.77
10.244.2.76
...
负载分发策略
对Service的访问被分发到了后端的Pod上去,目前kubernetes提供了两种负载分发策略:
-
如果不定义,默认使用kube-proxy的策略,比如随机、轮询
-
基于客户端地址的会话保持模式,即来自同一个客户端发起的所有请求都会转发到固定的一个Pod上
此模式可以使在spec中添加
sessionAffinity:ClientIP
选项
#首先停止service
[root@master tmp]# kubectl delete -f service-clusterip.yaml
service "service-clusterip" deleted
#修改
[root@master tmp]# vim service-clusterip.yaml
[root@master tmp]# cat service-clusterip.yaml
apiVersion: v1
kind: Service
metadata:
name: service-clusterip
namespace: dev
spec:
sessionAffinity: ClientIP
selector:
app: nginx-pod
clusterIP: 10.97.97.97 # service的ip地址,如果不写,默认会生成一个
type: ClusterIP
ports:
- port: 80 # Service端口
targetPort: 80 # pod端口
#查看ipvs策略
[root@master tmp]# ipvsadm -Ln
...
TCP 10.97.97.97:80 rr persistent 10800
-> 10.244.2.75:80 Masq 1 0 0
-> 10.244.2.76:80 Masq 1 0 0
-> 10.244.2.77:80 Masq 1 0 0
...
#循环访问测试
[root@master tmp]# while true;do curl 10.97.97.97; sleep 2; done;
10.244.2.77
10.244.2.77
10.244.2.77
10.244.2.77
...
#删除service
[root@master tmp]# kubectl delete -f service-clusterip.yaml
service "service-clusterip" deleted
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
#创建svc
[root@master tmp]# kubectl create -f service-headliness.yaml
service/service-headliness created
[root@master tmp]# kubectl get svc -n dev
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service-headliness ClusterIP None <none> 80/TCP 0s
[root@master tmp]# kubectl describe svc service-headliness -n dev
Name: service-headliness
Namespace: dev
Labels: <none>
Annotations: <none>
Selector: app=nginx-pod
Type: ClusterIP
IP: None
Port: <unset> 80/TCP
TargetPort: 80/TCP
Endpoints: 10.244.2.75:80,10.244.2.76:80,10.244.2.77:80
Session Affinity: None
Events: <none>
#查看域名解析
[root@master tmp]# kubectl exec -it pc-deployment-6696798b78-7l646 -n dev /bin/sh
#
# cat /etc/resolv.conf
nameserver 10.96.0.10
search dev.svc.cluster.local svc.cluster.local cluster.local localdomain
options ndots:5
# exit
#访问域名
[root@master tmp]# dig @10.96.0.10 service-headliness.dev.svc.cluster.local
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> @10.96.0.10 service-headliness.dev.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: 22352
;; 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.dev.svc.cluster.local. IN A
;; ANSWER SECTION:
service-headliness.dev.svc.cluster.local. 30 IN A 10.244.2.77
service-headliness.dev.svc.cluster.local. 30 IN A 10.244.2.75
service-headliness.dev.svc.cluster.local. 30 IN A 10.244.2.76
;; Query time: 39 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Sun Jun 04 05:58:29 EDT 2023
;; MSG SIZE rcvd: 237
#删除服务
[root@master tmp]# kubectl delete -f service-headliness.yaml
service "service-headliness" deleted
NodePort
如果希望将Service暴露给集群外部使用,那么就要使用到另外一种类型的Service,称为NodePort类型。NodePort的工作原理其实就是将service的端口映射到Node的一个端口上,然后就可以通过NodeIp:NodePort
来访问service了。
如果你将 type
字段设置为 NodePort
,则 Kubernetes 控制平面将在 --service-node-port-range
标志指定的范围内分配端口(默认值:30000-32767)。 每个节点将那个端口(每个节点上的相同端口号)代理到你的服务中。 你的服务在其 .spec.ports[*].nodePort
字段中报告已分配的端口。
创建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: 30002 # 指定绑定的node的端口(默认的取值范围是:30000-32767), 如果不指定,会默认分配
targetPort: 80
#创建service
[root@master tmp]# kubectl create -f service-nodeport.yaml
service/service-nodeport created
#查看
[root@master tmp]# kubectl get svc -n dev -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service-nodeport NodePort 10.101.18.114 <none> 80:30002/TCP 21s app=nginx-pod
#通过电脑主机浏览器访问集群中任意一个nodeip:端口,即可访问pod
[root@master tmp]# kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
master Ready master 5d4h v1.17.4 192.168.126.132 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 docker://24.0.2
node1 Ready <none> 5d4h v1.17.4 192.168.126.133 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 docker://18.6.3
node2 Ready <none> 5d4h v1.17.4 192.168.126.134 <none> CentOS Linux 7 (Core) 3.10.0-957.el7.x86_64 docker://18.6.3
LoadBalancer
LoadBalancer和NodePort很相似,目的都是向外部暴露一个端口,区别在于LoadBalancer会在集群的外部再来做一个负载均衡设备,而这个设备需要外部环境支持的,外部服务发送到这个设备上的请求,会被设备负载之后转发到集群中。
ExternalName
ExternalName类型的Service用于引入集群外部的服务,它通过externalName
属性指定外部一个服务的地址,然后在集群内部访问此service就可以访问到外部的服务了。
创建service-externalname.yaml
apiVersion: v1
kind: Service
metadata:
name: service-externalname
namespace: dev
spec:
type: ExternalName # service类型
externalName: www.baidu.com #改成ip地址也可以
#创建
[root@master tmp]# kubectl create -f service-externalname.yaml
service/service-externalname created
#解析域名
[root@master tmp]# dig @10.96.0.10 service-externalname.dev.svc.cluster.local
; <<>> DiG 9.11.4-P2-RedHat-9.11.4-26.P2.el7_9.13 <<>> @10.96.0.10 service-externalname.dev.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: 59479
;; 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.dev.svc.cluster.local. IN A
;; ANSWER SECTION:
service-externalname.dev.svc.cluster.local. 5 IN CNAME www.baidu.com.
www.baidu.com. 5 IN CNAME www.a.shifen.com.
www.a.shifen.com. 5 IN A 110.242.68.3
www.a.shifen.com. 5 IN A 110.242.68.4
;; Query time: 51 msec
;; SERVER: 10.96.0.10#53(10.96.0.10)
;; WHEN: Sun Jun 04 07:08:19 EDT 2023
;; MSG SIZE rcvd: 247
更多推荐
所有评论(0)