一、四层负载均衡Service基本介绍
Service 是 Kubernetes 中的一个资源对象,它定义了一组具有相同标签的 Pod 的逻辑集合,并为 这组 Pod 分配了一个虚拟 IP(Cluster IP)。Service 充当了网络终结点,客户端可以通过访问 Service 的虚拟 IP 来访问后端的 Pod。k8s集群中的IP地址
二、创建service资源
1.创建ClusterIP类型的Service
简介ClusterIP(默认模式):使用Kubernetes内部代理(kube-proxy)来将连接路由到Service的后端Pod。这是最常用的模式。---apiVersion: apps/v1kind: Deploymentmetadata:name: nginxlabels:test_pod: nginxspec:replicas: 3selector:matchLabels:run: nginxtemplate:metadata:name: nginxlabels:run: nginxspec:containers:image: nginx:latestimagePullPolicy: IfNotPresentports:protocol: TCP创建ClusterIP类型的Service---apiVersion: v1kind: Servicemetadata:name: clusterip-testlabels:app: svcspec:type: ClusterIPports:port: 80protocol: TCPtargetPort: 80selector:run: nginx检查:查看podIPNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES#查看service信息NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR#查看负载均衡ipvsadm -LnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler FlagsTCP 10.109.26.178:80 rr#查看服务端点信息NAME ENDPOINTS AGE
2.创建 NodePort 类型的 Service
NodePort:将Service公开为集群节点上的特定端口。在每个节点上监听Service的特定端口,并将流量转发到后端Pod。可以通过任何集群节点的IP地址和指定的端口访问Service。创建nginx Pod---apiVersion: apps/v1kind: Deploymentmetadata:name: node-portspec:replicas: 3selector:matchLabels:run: NodePort-nginxtemplate:metadata:labels:run: NodePort-nginxspec:containers:image: nginx:latestimagePullPolicy: IfNotPresentports:创建NodePort类型的Service---apiVersion: v1kind: Servicemetadata:name: nginx-nodeportspec:type: NodePortselector:run: NodePort-nginxports:port: 80nodePort: 30180targetPort: 80protocol: TCPNAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES查看service信息NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGEkubernetes ClusterIP 10.96.0.1 443/TCP 28d查看负载均衡ipvsadm -LnIP Virtual Server version 1.2.1 (size=4096)Prot LocalAddress:Port Scheduler FlagsTCP 172.17.0.1:30180 rrTCP 192.168.1.100:30180 rrTCP 192.168.122.1:30180 rrTCP 10.246.73.128:30180 rr#查看服务端点信息NAME ENDPOINTS AGEkubernetes 192.168.1.100:6443 28d集群内访问:curl http://10.105.50.230:80集群外访问(192.168.1.100为K8S节点IP,所有节点都可以):
3.创建 ExternalName 类型的 Service
要实现在不同命名空间下的 Pod 之间进行跨命名空间访问
ExternalName:将Service映射到集群外部的某个外部域名。不会为Service分配Cluster IP或任何负载均衡器IP。只会返回由ExternalName指定的外部域名的CNAME记录。1、在nginx命名空间创建pod和service资源创建名为nginx的命名空间kubectl create ns nginx编写nginx命名空间pod的yaml---apiVersion: apps/v1kind: Deploymentmetadata:name: nginx-podnamespace: nginxspec:replicas: 3selector:matchLabels:name: nginxnginxtemplate:metadata:labels:name: nginxnginxspec:containers:- name: nginximage: nginx:latestimagePullPolicy: IfNotPresentports:- containerPort: 80编写nginx命名空间service的yaml文件---apiVersion: v1kind: Servicemetadata:name: nginx-svcnamespace: nginxspec:selector:name: nginxnginxports:- port: 80protocol: TCPtargetPort: 80在default命名空间下创建pod和service创建pod:---apiVersion: apps/v1kind: Deploymentmetadata:name: busyboxnamespace: defaultspec:replicas: 1selector:matchLabels:app: busyboxtemplate:metadata:labels:app: busyboxspec:containers:- name: busyboximage: busyboximagePullPolicy: IfNotPresentcommand: ["/bin/sh","-c","sleep 3600"]创建serviceapiVersion: v1kind: Servicemetadata:name: client-svcspec:type: ExternalNameexternalName: nginx-svc.nginx.svc.cluster.localports:- name: httpport: 80targetPort: 80验证:进入default命名空间中的pod[root@lx100 svc]NAME READY STATUS RESTARTS AGEbusybox-6bb56cf754-sx2dt 1/1 Running 0 6m12s[root@lx100 svc]/返回nginx页面则成功
三、代理外部服务
在节点上安装MariaDB数据库yum install mariadb-server.x86_64 -ysystemctl start mariadb创建service---apiVersion: v1kind: Servicemetadata:name: mysqlspec:type: ClusterIPports:- port: 3306创建Endpoint---apiVersion: v1kind: Endpointsmetadata:name: mysqlsubsets:- addresses:- ip: 192.168.1.110ports:- port: 3306查看service详细信息kubectl describe svc mysqlName: mysqlNamespace: defaultLabels: none>Annotations: none>Selector: none>Type: ClusterIPIP Family Policy: SingleStackIP Families: IPv4IP: 10.105.22.22IPs: 10.105.22.22Port: 3306/TCPTargetPort: 3306/TCPEndpoints: 192.168.1.110:3306 #已经加入Session Affinity: NoneEvents: none>创建一个空的pod尝试连接---apiVersion: apps/v1kind: Deploymentmetadata:name: busyboxnamespace: defaultspec:replicas: 1selector:matchLabels:app: busyboxtemplate:metadata:labels:app: busyboxspec:containers:- name: busyboximage: busyboximagePullPolicy: IfNotPresentcommand: ["/bin/sh","-c","sleep 3600"]进入podkubectl exec -it busybox-6bb56cf754-sx2dt /bin/sh/ # telnet mysql 3306Connected to mysql@Host 'lx120' is not allowed to connect to this MariaDB serverConnection closed by foreign host连接成功;输出信息为@Host 'lx120'不允许连接到被外部主机关闭的MariaDB服务器
