TCP负载均衡器
无论是
iptables
还是ipvs
模型的Service
资源都配置于Linux
内核中的Netfilter
之上进行四层调度,是一种类型更为通用的调度器,支持调度HTTP
、MySQL
等应用层服务。不过,也正是由于工作于传输层从而使得它无法做到类似卸载HTTPS
中的SSL
会话等一类操作,也不支持基于URL
的请求调度机制,而且,Kubernetes
也不支持为此类负载均衡器配置任何类型的健康状态检查机制。HTTP(S)负载均衡器
HTTP(S)
负载均衡器是应用层负载均衡机制的一种,支持根据环境做出更好的调度决策。与传输层调度器相比,它提供了诸如可自定义URL
映射和TLS
卸载等功能,并支持多种类型的后端服务器健康状态检查机制。
internet | ------------ [ Services ]Ingress是授权入站连接到达集群服务的规则集合。
internet | [ Ingress ] --|-----|-- [ Services ]
GCE/GKE
会在master
节点上部署一个Ingress Controller
。你可以在一个Pod
中部署任意个自定义的Ingress Controller
。你必须正确的annotate
每个Ingress
,比如运行多个Ingress Controller
和关闭glbc
。
apiVersion: extensions/v1beta1 kind: Ingress metadata: name: ingress-demo namespace: default annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: www.ilinux.io http: paths: - backend: serviceName: myapp-svc servicePort: 80 #说明:上面资源清单文件中的annotations用于识别其所属的Ingress控制器的类别,这一点在集群上部署多个Ingress控制器时尤为重要。
rules <[]Object>:用于定义当前
Ingress
资源的转发规则列表;未由rules
定义规则,或者没有匹配到任何规则时,所有流量都会转发到由backend
定义的默认后端。backend <Object>:默认的后端用于服务那些没有匹配到任何规则的请求;定义
Ingress
资源时,至少应该定义backend
或rules
两者之一;此字段用于让负载均衡器指定一个全局默认的后端。tls <[]Object>:
TLS
配置,目前仅支持通过默认端口443
提供服务;如果要配置指定的列表成员指向了不同的主机,则必须通过SNI TLS
扩展机制来支持此功能。
ingress.spec.rules.http.paths.backend
对象的定义由两个必须的内嵌字段组成:serviceName
和servicePort
,分别用于指定流量转发的后端目标Service
资源的名称和端口。
Ingress
控制器其实就是托管于Kubernetes
系统之上的用于实现在应用层发布服务的Pod
资源,跟踪Ingress
资源并实时生成配置规则。
Ingress-nginx官网
Ingress-nginx GitHub仓库地址
Ingress安装文档
[root@k8s-master ~]# mkdir ingress-nginx #这里创建一个目录专门用于ingress-nginx(可省略) [root@k8s-master ~]# cd ingress-nginx/ [root@k8s-master ingress-nginx]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml #下载配置清单yaml文件 [root@k8s-master ingress-nginx]# ls #查看下载的文件 mandatory.yaml [root@k8s-master ingress-nginx]# kubectl apply -f mandatory.yaml #创建Ingress namespace/ingress-nginx created configmap/nginx-configuration created configmap/tcp-services created configmap/udp-services created serviceaccount/nginx-ingress-serviceaccount created clusterrole.rbac.authorization.k8s.io/nginx-ingress-clusterrole created role.rbac.authorization.k8s.io/nginx-ingress-role created rolebinding.rbac.authorization.k8s.io/nginx-ingress-role-nisa-binding created clusterrolebinding.rbac.authorization.k8s.io/nginx-ingress-clusterrole-nisa-binding created deployment.apps/nginx-ingress-controller created
2)验证
[root@k8s-master ingress-nginx]# kubectl get pods -n ingress-nginx #查看生成的pod,注意这里在ingress-nginx名称空间 NAME READY STATUS RESTARTS AGE nginx-ingress-controller-79f6884cf6-5fb6v 1/1 Running 0 18m [root@k8s-master ingress-nginx]# kubectl describe pod nginx-ingress-controller-79f6884cf6-5fb6v -n ingress-nginx 查看该pod的详细信息 Name: nginx-ingress-controller-79f6884cf6-5fb6v Namespace: ingress-nginx Priority: 0 Node: k8s-node2/192.168.1.33 Start Time: Fri, 27 Sep 2019 17:53:07 +0800 Labels: app.kubernetes.io/name=ingress-nginx app.kubernetes.io/part-of=ingress-nginx pod-template-hash=79f6884cf6 Annotations: prometheus.io/port: 10254 prometheus.io/scrape: true Status: Running IP: 10.244.2.73 ......
---同样去官网下载配置清单文件,也可以自定义创建。 [root@k8s-master ingress-nginx]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/service-nodeport.yaml [root@k8s-master ingress-nginx]# kubectl apply -f service-nodeport.yaml #创建service资源 service/ingress-nginx created [root@k8s-master ingress-nginx]# kubectl get svc -n ingress-nginx #查看service资源 NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx NodePort 10.107.40.18280:32699/TCP,443:30842/TCP 9s [root@k8s-master ingress-nginx]# kubectl describe svc/ingress-nginx -n ingress-nginx #查看该service的详细信息 Name: ingress-nginx Namespace: ingress-nginx Labels: app.kubernetes.io/name=ingress-nginx app.kubernetes.io/part-of=ingress-nginx Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app.kubernetes.io/name":"ingress-nginx","app.kubernetes.io/par... Selector: app.kubernetes.io/name=ingress-nginx,app.kubernetes.io/part-of=ingress-nginx Type: NodePort IP: 10.107.40.182 Port: http 80/TCP TargetPort: 80/TCP NodePort: http 32699/TCP Endpoints: 10.244.2.73:80 Port: https 443/TCP TargetPort: 443/TCP NodePort: https 30842/TCP Endpoints: 10.244.2.73:443 Session Affinity: None External Traffic Policy: Cluster Events:
首先创建一个单独的目录为了方便管理
[root@k8s-master ~]# mkdir ingress-nginx/ingress [root@k8s-master ~]# cd ingress-nginx/ingress/
[root@k8s-master ingress]# vim namespace-testing.yaml #编写namespace清单文件 apiVersion: v1 kind: Namespace metadata: name: testing labels: env: testing [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl apply -f namespace-testing.yaml #创建namespace namespace/testing created [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl get namespace testing #验证 NAME STATUS AGE testing Active 12s
[root@k8s-master ingress]# vim deployment-nginx.yaml apiVersion: apps/v1 kind: Deployment metadata: name: deploy-nginx namespace: testing spec: replicas: 3 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: containers: - name: nginx image: nginx:1.12 ports: - name: http containerPort: 80 [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl apply -f deployment-nginx.yaml deployment.apps/deploy-nginx created [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl get deploy -n testing NAME READY UP-TO-DATE AVAILABLE AGE deploy-nginx 3/3 3 3 5s [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl get pods -n testing NAME READY STATUS RESTARTS AGE deploy-nginx-686bddcb56-9g7pq 1/1 Running 0 6s deploy-nginx-686bddcb56-gqpm2 1/1 Running 0 6s deploy-nginx-686bddcb56-vtwkq 1/1 Running 0 6s
[root@k8s-master ingress]# vim service-nginx.yaml apiVersion: v1 kind: Service metadata: name: svc-nginx namespace: testing labels: app: svc-nginx spec: selector: app: nginx ports: - name: http port: 80 targetPort: 80 protocol: TCP [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl apply -f service-nginx.yaml service/svc-nginx created [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl get svc -n testing NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE svc-nginx ClusterIP 10.99.233.9080/TCP 6s [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl describe svc/svc-nginx -n testing Name: svc-nginx Namespace: testing Labels: app=svc-nginx Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"v1","kind":"Service","metadata":{"annotations":{},"labels":{"app":"svc-nginx"},"name":"svc-nginx","namespace":"testing"},"s... Selector: app=nginx Type: ClusterIP IP: 10.99.233.90 Port: http 80/TCP TargetPort: 80/TCP Endpoints: 10.244.1.76:80,10.244.1.77:80,10.244.2.74:80 Session Affinity: None Events:
[root@k8s-master ingress]# vim ingress-nginx.yaml apiVersion: extensions/v1beta1 kind: Ingress metadata: name: nginx namespace: testing annotations: kubernetes.io/ingress.class: "nginx" spec: rules: - host: nginx.ilinux.io http: paths: - path: backend: serviceName: svc-nginx servicePort: 80 [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl apply -f ingress-nginx.yaml ingress.extensions/nginx created [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl get ingress -n testing NAME HOSTS ADDRESS PORTS AGE nginx nginx.ilinux.io 80 16s [root@k8s-master ingress]# [root@k8s-master ingress]# kubectl describe ingress -n testing Name: nginx Namespace: testing Address: Default backend: default-http-backend:80 () Rules: Host Path Backends ---- ---- -------- tomcat.ilinux.io svc-nginx:80 (10.244.1.76:80,10.244.1.77:80,10.244.2.74:80) Annotations: kubectl.kubernetes.io/last-applied-configuration: {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{"kubernetes.io/ingress.class":"nginx"},"name":"nginx","namespace":"testing"},"spec":{"rules":[{"host":"nginx.ilinux.io","http":{"paths":[{"backend":{"serviceName":"svc-nginx","servicePort":80},"path":null}]}}]}} kubernetes.io/ingress.class: nginx Events:
#首先查看前面部署Ingress控制器的前端的Service资源的映射端口 [root@k8s-master ingress-nginx]# kubectl get svc -n ingress-nginx NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE ingress-nginx NodePort 10.107.40.18280:32699/TCP,443:30842/TCP 3m59s #终端测试,添加hosts [root@k8s-master ~]# cat /etc/hosts 192.168.1.31 k8s-master nginx.ilinux.io 192.168.1.32 k8s-node1 nginx.ilinux.io 192.168.1.33 k8s-node2 nginx.ilinux.io #访问测试 [root@k8s-master ~]# curl nginx.ilinux.io:32699 Welcome to nginx!