文章目录
1、services简介
- Service可以看作是一组提供相同服务的Pod对外的访问接口。借助Service,应用可以方便地实现服务发现和负载均衡。
service默认只支持4层负载均衡能力,没有7层功能。(可以通过Ingress实现)
service的类型:(前三种是集群外部访问内部资源)
ClusterIP:默认值,k8s系统给service自动分配的虚拟IP,只能在集群内部访问。
NodePort:将Service通过指定的Node上的端口暴露给外部,访问任意一个NodeIP:nodePort都将路由到ClusterIP。
LoadBalancer:在 NodePort 的基础上,借助 cloud provider 创建一个外部的负载均衡器,并将请求转发到 <NodeIP>:NodePort,此模式只能在云服务器上使用。
ExternalName:将服务通过 DNS CNAME 记录方式转发到指定的域名(通过 spec.externlName 设定)。[集群内部访问外部,通过内部调用外部资源]
2、IPVS模式的service
- Service 是由 kube-proxy 组件,加上 iptables 来共同实现的.
kube-proxy 通过 iptables 处理 Service 的过程,需要在宿主机上设置相当多的 iptables 规则,如果宿主机有大量的Pod,不断刷新iptables规则,会消耗大量的CPU资源。
PVS模式的service,可以使K8s集群支持更多量级的Pod。
2.1 查看没有设置ipvs模式时候的ipvs
[root@server2 ~]# lsmod | grep ip
ip6_udp_tunnel 12755 1 vxlan
ip_vs_sh 12688 0
ip_vs_wrr 12697 0
ip_vs_rr 12600 0
ip_vs 145458 6 ip_vs_rr,ip_vs_sh,ip_vs_wrr
2.2 部署ipvs模式
[root@server2 ~]# yum install -y ipvsadm ##安装ipvsadm,每个节点都需要安装。每个节点操作一样
[root@server3 ~]# yum install -y ipvsadm
[root@server4 ~]# yum install -y ipvsadm
[root@server2 ~]# kubectl get cm -n kube-system
[root@server2 ~]# kubectl get pod -n kube-system | grep kube-proxy ##部署之前查看一下
[root@server2 ~]# kubectl -n kube-system edit cm kube-proxy ##进入修改mode为ipvs
mode: "ipvs"
[root@server2 ~]# kubectl get pod -n kube-system |grep kube-proxy | awk '{system("kubectl delete pod "$1" -n kube-system")}' ##更新kube-proxy pod
[root@server2 ~]# kubectl get pod -n kube-system | grep kube-proxy ##部署之后查看是否发生变化
#IPVS模式下,kube-proxy会在service创建后,在宿主机上添加一个虚拟网卡:kube-ipvs0,并分配service IP。
[root@server2 ~]# ip addr ##查看ip
[root@server2 ~]# kubectl expose deployment deployment --port=80
service/deployment exposed
[root@server2 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
deployment ClusterIP 10.98.150.51 <none> 80/TCP 5s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 28d
[root@server2 ~]# kubectl create -f service.yml
service/myservice created
[root@server2 ~]# ipvsadm -ln
[root@server2 ~]# iptables -t nat -nL | grep :80
[root@server2 ~]#
2.3 测试(观察是否是动态负载均衡变化)
[root@server2 ~]# cat service.yml rs.yml > demo.yml
[root@server2 ~]# vim demo.yml
[root@server2 ~]# kubectl get pod -L app
[root@server2 ~]# vim demo.yml
---
apiVersion: v1
kind: Service
metadata:
name: myservice
spec:
selector:
app: myapp
ports:
- protocol: TCP
port: 80
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: demo2
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v2
[root@server2 ~]# kubectl apply -f demo.yml ##创建services和pod
service/myservice created
deployment.apps/demo2 created
[root@server2 ~]# kubectl get svc ##查看服务
[root@server2 ~]# kubectl describe svc myservice
[root@server2 ~]# ip addr
[root@server2 ~]# ipvsadm -ln ##查看对应的负载均衡
[root@server2 ~]# curl 10.108.244.104/hostname.html
demo2-67f8c948cf-qv8cl
[root@server2 ~]# curl 10.108.244.104/hostname.html
demo2-67f8c948cf-wcrlp
[root@server2 ~]# curl 10.108.244.104/hostname.html
demo2-67f8c948cf-f2zxw
3、k8s提供的dns服务插件
[root@server2 ~]# kubectl get services kube-dns --namespace=kube-system
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kube-dns ClusterIP 10.96.0.10 <none> 53/UDP,53/TCP,9153/TCP 28d
[root@server2 ~]# kubectl attach demo -it ##如果有demo就直接进
[root@server2 ~]# kubectl run demo --image=busyboxplus -it ##没有demo创建demo
[root@server2 ~]# yum install bind-utils -y ##安装dig工具
[root@server2 ~]# dig myservice.default.svc.cluster.local. @10.96.0.10 ##通过dig进行测试
4、Headless Service “无头服务”
- Headless Service不需要分配一个VIP,而是直接以DNS记录的方式解析出被代理Pod的IP地址。
域名格式: ( s e r v i c e n a m e ) . (servicename). (servicename).(namespace).svc.cluster.local
[root@server2 ~]# vim demo.yml
clusterIP: None ##无头服务
replicas: 6
[root@server2 ~]# kubectl apply -f demo.yml
service/myservice created
deployment.apps/demo2 created
[root@server2 ~]# kubectl get pod
[root@server2 ~]# kubectl delete -f rs.yml
deployment.apps "deployment" deleted
[root@server2 ~]# kubectl delete svc deployment
service "deployment" deleted
[root@server2 ~]# kubectl get svc ##CLUSTER-IP没有分配ip
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 28d
myservice ClusterIP None <none> 80/TCP 107s
[root@server2 ~]# kubectl describe svc myservice ##有对应的Endpoints:
[root@server2 ~]# kubectl get pod
[root@server2 ~]# kubectl attach demo -it
5、从外部访问service的三种方式
5.1 NodePort方式
[root@server2 ~]# kubectl delete -f demo.yml
service "myservice" deleted
deployment.apps "demo2" deleted
[root@server2 ~]# vim demo.yml
#clusterIP: None
type: NodePort
replicas: 3
[root@server2 ~]# kubectl apply -f demo.yml
service/myservice created
deployment.apps/demo2 created
[root@server2 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 28d
myservice NodePort 10.105.12.247 <none> 80:30942/TCP 25s
[root@server2 ~]# netstat -antlp| grep :30942
tcp 0 0 0.0.0.0:30942 0.0.0.0:* LISTEN 30455/kube-proxy
[root@foundation50 yum.repos.d]# curl 192.168.0.2:30942/hostname.html
[root@server2 ~]# ipvsadm -ln
5.2 LoadBalancer
[root@server2 ~]# vim demo.yml
[root@server2 ~]# kubectl apply -f demo.yml
service/myservice configured
deployment.apps/demo2 unchanged
#type: NodePort
type: LoadBalancer
[root@server2 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 28d
myservice LoadBalancer 10.105.12.247 <pending> 80:30942/TCP 7m40s
[root@server2 ~]# ipvsadm -ln
[root@server2 ~]# kubectl describe svc myservice
[root@server2 ~]# kubectl delete svc myservice
service "myservice" deleted
[root@server2 ~]# kubectl apply -f demo.yml
service/myservice created
deployment.apps/demo2 unchanged
[root@server2 ~]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 28d
myservice LoadBalancer 10.100.19.229 <pending> 80:30105/TCP 2s