建設(shè)網(wǎng)站有哪些術(shù)語百度信息流投放
一、ingress 簡介
在k8s集群中,service和pod的ip為內(nèi)網(wǎng)ip,僅集群內(nèi)部才可以訪問。如果外部應(yīng)用想要直接訪問集群內(nèi)的服務(wù),就需要把外部請(qǐng)求通過負(fù)載均衡轉(zhuǎn)發(fā)到service上,然后再由kube-proxy組件將其轉(zhuǎn)發(fā)給后端pod。一般service可以通過NodePort和LoadBalancer兩種方式對(duì)外提供訪問,但是這兩種方式都有以下缺點(diǎn)
-
NodePort:將service暴露在節(jié)點(diǎn)網(wǎng)絡(luò)上,NodePort背后就是Kube-Proxy,Kube-Proxy是溝通service網(wǎng)絡(luò)、Pod網(wǎng)絡(luò)和節(jié)點(diǎn)網(wǎng)絡(luò)的橋梁。
小集群使用還行,當(dāng)集群規(guī)模較大時(shí),NodePort的端口管理就是個(gè)災(zāi)難。因?yàn)槊總€(gè)端口只能是一種服務(wù),端口范圍只能是 30000-32767。 -
LoadBalancer:通過設(shè)置LoadBalancer映射到云服務(wù)商提供的LoadBalancer地址。這種用法僅用于在公有云服務(wù)提供商的云平臺(tái)上設(shè)置 Service 的場景。受限于云平臺(tái),且通常在云平臺(tái)部署LoadBalancer還需要額外的費(fèi)用。在service提交后,Kubernetes就會(huì)調(diào)用CloudProvider在公有云上為你創(chuàng)建一個(gè)負(fù)載均衡服務(wù),并且把被代理的Pod的IP地址配置給負(fù)載均衡服務(wù)做后端。
基于這種現(xiàn)狀,kubernetes提供了Ingress資源對(duì)象,Ingress只需要一個(gè)NodePort或者一個(gè)LB就可以滿足暴露多個(gè)Service的需求。
什么是ingress
Ingress 是對(duì)集群中服務(wù)的外部訪問進(jìn)行管理的 API 對(duì)象,通過yaml文件來配置,定義請(qǐng)求如何轉(zhuǎn)發(fā)到service的規(guī)則。ingress通過http或https暴露集群內(nèi)部service,給service提供外部URL、負(fù)載均衡、SSL/TLS能力以及基于域名的反向代理。Ingress相當(dāng)于一個(gè)7層的負(fù)載均衡器,它的工作原理類似于Nginx,當(dāng)在Ingress中建立映射規(guī)則,Ingress Controller通過監(jiān)聽這些配置規(guī)則并轉(zhuǎn)化成Nginx的反向代理配置 , 然后對(duì)外部提供服務(wù)。ingress要依靠 ingress-controller 來具體實(shí)現(xiàn)以上功能。
ingress組成
- ingress
通過yaml文件定義的規(guī)則集合,一個(gè)api對(duì)象。
- 反向負(fù)載均衡器
反向代理負(fù)載均衡器就是 nginx、apache等中間件,新版k8s已經(jīng)將Nginx與Ingress Controller合并為一個(gè)組件,所以Nginx無需單獨(dú)部署,只需要部署Ingress Controller即可。在集群中反向代理負(fù)載均衡器可以自由部署,可以使用Replication Controller、Deployment、DaemonSet等等方式。常用的負(fù)載均衡器有以下幾種1:Kubernetes Ingress作為"官方"控制器,它是由社區(qū)基于NGINX Web服務(wù)器開發(fā)的,并補(bǔ)充了一組用于實(shí)現(xiàn)額外功能的Lua插件。
2:NGINX Ingress 這是NGINX開發(fā)人員的官方產(chǎn)品,NGINX控制器具有很高的穩(wěn)定性,持續(xù)的向后兼容性,沒有任何第三方模塊,并且由于消除了Lua代碼而保證了較高的速度(與官方控制器相比)
3:Kong Ingress 由Kong Inc開發(fā),并且有兩個(gè)版本:商業(yè)版本和免費(fèi)版本。Kong Ingress建立在NGINX之上,并增加了擴(kuò)展其功能的Lua模塊。
4:HAProxy Ingress 由HAProxy開發(fā),它提供了“軟”配置更新(無流量丟失),基于DNS的服務(wù)發(fā)現(xiàn),通過API的動(dòng)態(tài)配置。
5:Traefik 是一個(gè)全功能的 ingress 控制器 (Let’s Encrypt,secrets,http2,websocket),并且它也有來自 Containous 的商業(yè)支持。
6:Istio 是IBM,Google和Lyft(Envoy的原始作者)的聯(lián)合項(xiàng)目,它是一個(gè)全面的服務(wù)網(wǎng)格解決方案。它不僅可以管理所有傳入的外部流量(作為Ingress控制器),還可以控制集群內(nèi)部的所有流量。
- Ingress Controller
監(jiān)聽apiserver,獲取服務(wù)新增,刪除等變化,并結(jié)合ingress規(guī)則動(dòng)態(tài)更新到反向代理負(fù)載均衡器上,并重載配置使其生效。
使用watch機(jī)制: Service始終watch著后端pod變化。只要pod發(fā)生變化,api-server立刻檢測到
實(shí)現(xiàn)原理
1:ingress controller通過和kubernetes api交互,動(dòng)態(tài)的去感知集群中ingress規(guī)則變化。
2:讀取ingress自定義的規(guī)則,規(guī)則就是寫明了那個(gè)域名對(duì)應(yīng)哪個(gè)service,生成一段nginx配置。
3;寫入nginx-ingress-controller的pod里,這個(gè)Ingress controller的pod里運(yùn)行著一個(gè)Nginx服務(wù),控制器會(huì)把生成的nginx配置寫入/etc/nginx/nginx.conf文件中的upstream里面
4:重新加載配置,nginx -s reload一下使配置生效,以此達(dá)到分配和動(dòng)態(tài)更新問題。
以nginx為例,流程圖如下:
架構(gòu)圖如下:
以上圖片來源https://cloud.tencent.com/developer/article/1718482
二、ingress部署
- 下載ingress yaml文件
通過wget下載需要用到的ingress文件,此次部署用的是1.9.0版本。
[root@node1 ~]# wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.9.0/deploy/static/provider/baremetal/deploy.yaml
- 修改yaml配置
目前國外的鏡像源已經(jīng)不可用,需要修改鏡像的源地址,如下:
1:修改ingress-controller的鏡像源地址- name: POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespace- name: LD_PRELOADvalue: /usr/local/lib/libmimalloc.soimage: registry.aliyuncs.com/google_containers/nginx-ingress-controller:v1.9.0 ###此處修改為阿里的源 imagePullPolicy: IfNotPresent#############################
2:修改webhook-certgen的鏡像源地址(有兩處需要修改)env:- name: POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespaceimage: registry.aliyuncs.com/google_containers/kube-webhook-certgen:v20230407 #####修改鏡像源地址 imagePullPolicy: IfNotPresent3:修改ingress-controller的網(wǎng)絡(luò)模式spec: hostNetwork: true #####添加此處配置,修改為主機(jī)模式containers: - args: - /nginx-ingress-controller- --election-id=ingress-nginx-leader- --controller-class=k8s.io/ingress-nginx- --ingress-class=nginx- --configmap=$(POD_NAMESPACE)/ingress-nginx-controller- --validating-webhook=:8443- --validating-webhook-certificate=/usr/local/certificates/cert- --validating-webhook-key=/usr/local/certificates/keyenv: - name: POD_NAME
- 啟動(dòng)ingress
[root@node1 ingress]# kubectl apply -f deploy.yaml
namespace/ingress-nginx created
serviceaccount/ingress-nginx created
serviceaccount/ingress-nginx-admission created
role.rbac.authorization.k8s.io/ingress-nginx created
role.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrole.rbac.authorization.k8s.io/ingress-nginx created
clusterrole.rbac.authorization.k8s.io/ingress-nginx-admission created
rolebinding.rbac.authorization.k8s.io/ingress-nginx created
rolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx created
clusterrolebinding.rbac.authorization.k8s.io/ingress-nginx-admission created
configmap/ingress-nginx-controller created
service/ingress-nginx-controller created
service/ingress-nginx-controller-admission created
deployment.apps/ingress-nginx-controller created
job.batch/ingress-nginx-admission-create created
job.batch/ingress-nginx-admission-patch created
ingressclass.networking.k8s.io/nginx created
validatingwebhookconfiguration.admissionregistration.k8s.io/ingress-nginx-admission created
[root@node1 ingress]# kubectl get po -n ingress-nginx
NAME READY STATUS RESTARTS AGE
ingress-nginx-admission-create-hk2sx 0/1 ContainerCreating 0 3s
ingress-nginx-admission-patch-h6f5d 0/1 ContainerCreating 0 3s
ingress-nginx-controller-548689bbd7-8lzrj 0/1 ContainerCreating 0 3s[root@node1 ~]# kubectl get po -n ingress-nginx -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
ingress-nginx-admission-create-hk2sx 0/1 Completed 0 43h 172.16.44.4 node2 <none> <none>
ingress-nginx-admission-patch-h6f5d 0/1 Completed 0 43h 172.16.28.8 node3 <none> <none>
ingress-nginx-controller-6c88c85c98-v8mgs 1/1 Running 0 30h 192.168.5.79 node1 <none> <none>
[root@node1 ~]#
###########以上可以看到ingress-controller的ip使用的是主機(jī)的ip。
三、測試http方式
- 啟動(dòng)nginx和tomcat
1:編寫yaml如下:
[root@node1 yaml]# cat nginx-tomcat.yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:name: nginxlabels:app: nginx
spec:replicas: 1selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: docker.io/library/nginx:latestimagePullPolicy: IfNotPresentports:- containerPort: 80---
apiVersion: v1
kind: Service
metadata:name: nginx-svc
spec:selector:app: nginxports:- port: 80protocol: TCPtargetPort: 80type: ClusterIP---
apiVersion: apps/v1
kind: Deployment
metadata:name: tomcatlabels:app: tomcat
spec:replicas: 1selector:matchLabels:app: tomcattemplate:metadata:labels:app: tomcatspec:containers:- name: tomcatimage: docker.io/library/tomcat:8.0-alpine ###測試了好幾個(gè)鏡像,就這個(gè)能顯示tom 貓imagePullPolicy: IfNotPresentports:- containerPort: 8080---
apiVersion: v1
kind: Service
metadata: name: tomcat-svc
spec: selector: app: tomcat ports: - port: 8080protocol: TCPtargetPort: 8080type: ClusterIP[root@node1 yaml]# #################################################
2;啟動(dòng)nginx 和tomcat
[root@node1 yaml]# kubectl get po -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-5977dc5756-glczk 1/1 Running 0 30h 172.16.44.6 node2 <none> <none>
tomcat-656df44d6c-6znd9 1/1 Running 0 29h 172.16.28.13 node3 <none> <none>
[root@node1 yaml]# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-svc ClusterIP 10.233.43.219 <none> 80/TCP 30h
tomcat-svc ClusterIP 10.233.52.85 <none> 8080/TCP 30h####################################################
3:測試nginx服務(wù)沒問題
[root@node1 yaml]# curl 10.233.43.219
<!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>
[root@node1 yaml]# ###################################
4:測試tomcat 服務(wù)
[root@node1 yaml]# curl 10.233.52.85:8080<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8" /><title>Apache Tomcat/8.0.53</title><link href="favicon.ico" rel="icon" type="image/x-icon" /><link href="favicon.ico" rel="shortcut icon" type="image/x-icon" /><link href="tomcat.css" rel="stylesheet" type="text/css" /></head><body><div id="wrapper"><div id="navigation" class="curved container"><span id="nav-home"><a href="https://tomcat.apache.org/">Home</a></span><span id="nav-hosts"><a href="/docs/">Documentation</a></span>
- 創(chuàng)建ingress 規(guī)則
1:編寫ingress yaml
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: ingress-test
spec:ingressClassName: nginx ########使用的實(shí)際負(fù)載均衡器類型rules:- host: "test.nginx.com" #########nginx的域名http:paths:- path: "/"pathType: Prefixbackend:service:name: nginx-svc ###nginx的svc nameport:number: 80- host: "test.tomcat.com" ######tomcat的域名http:paths:- path: "/"pathType: Prefixbackend:service:name: tomcat-svc ###tomcat的svc nameport:number: 8080###############################################
2:apply創(chuàng)建規(guī)則
[root@node1 yaml]# kubectl apply -f ingress.yaml
[root@node1 ~]#
[root@node1 ~]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-test nginx test.nginx.com,test.tomcat.com 192.168.5.79 80 29h可以看到address為ingress-controller pod所在節(jié)點(diǎn)的物理地址,port為80##################################################
3:進(jìn)入ingress-controller pod里面,查看更新的規(guī)則
[root@node1 ~]# kubectl exec -it ingress-nginx-controller-6c88c85c98-v8mgs -n ingress-nginx bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl exec [POD] -- [COMMAND] instead.
bash-5.1$
bash-5.1$ cat /etc/nginx/nginx.conf
## start server test.nginx.comserver {server_name test.nginx.com ; ###ingress中定義的hostlisten 80 ; ###container portlisten [::]:80 ; listen 443 ssl http2 ;listen [::]:443 ssl http2 ;set $proxy_upstream_name "-";ssl_certificate_by_lua_block {certificate.call()}location / {set $namespace "default"; ###命名空間set $ingress_name "ingress-test"; ###ingress 名稱set $service_name "nginx-svc"; ###nginx svc名稱set $service_port "80";set $location_path "/";set $global_rate_limit_exceeding n;#############下面是tomcat的配置
## start server test.tomcat.comserver {server_name test.tomcat.com ;listen 80 ;listen [::]:80 ;listen 443 ssl http2 ;listen [::]:443 ssl http2 ;set $proxy_upstream_name "-";ssl_certificate_by_lua_block {certificate.call()}location / {set $namespace "default";set $ingress_name "ingress-test";set $service_name "tomcat-svc";set $service_port "8080";set $location_path "/";set $global_rate_limit_exceeding n;
- 瀏覽器測試
windows 機(jī)器添加host解析
文件位置:C:\WINDOWS\System32\drivers\etc\hosts
192.168.5.79 test.nginx.com test.tomcat.com
訪問nginx界面
訪問tomcat界面
四、測試https模式
- 創(chuàng)建secret
1:生成證書
[root@node1 cert]# openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/C=CN/ST=BJ/L=BJ/O=nginx/CN=test.com" ###注意此處的域名
Generating a RSA private key
.........................................................+++++
...+++++
writing new private key to 'tls.key'
-----
[root@node1 cert]# ll
total 8
-rw-r--r-- 1 root root 1261 Nov 10 16:55 tls.crt
-rw------- 1 root root 1704 Nov 10 16:55 tls.key
[root@node1 cert]# 2:創(chuàng)建secret
[root@node1 cert]# kubectl create secret tls tls-secret --key tls.key --cert tls.crt
secret/tls-secret created
[root@node1 cert]# kubectl get secret | grep tls-secret
tls-secret kubernetes.io/tls 2 19s
[root@node1 cert]#
- 創(chuàng)建帶tls的ingress
1:編輯yaml文件
[root@node1 yaml]# cat ingress-https.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:name: ingress-test
spec:tls:- hosts:- nginx.test.com ###制定使用tls的主機(jī)- tomcat.test.comsecretName: tls-secret ###指定secretingressClassName: nginxrules:- host: "nginx.test.com"http:paths:- path: "/"pathType: Prefixbackend:service:name: nginx-svcport:number: 80- host: "tomcat.test.com"http:paths:- path: "/"pathType: Prefixbackend:service:name: tomcat-svcport:number: 8080[root@node1 yaml]# ########################################
2:創(chuàng)建ingress,如下可以看到ports 多了個(gè)443端口
[root@node1 yaml]# kubectl apply -f ingress-https.yaml
ingress.networking.k8s.io/ingress-test created
[root@node1 yaml]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-test nginx nginx.test.com,tomcat.test.com 80, 443 7s
[root@node1 yaml]# kubectl get ingress
NAME CLASS HOSTS ADDRESS PORTS AGE
ingress-test nginx nginx.test.com,tomcat.test.com 192.168.5.79 80, 443 43s
- 瀏覽器測試
windows 機(jī)器添加host解析
文件位置:C:\WINDOWS\System32\drivers\etc\hosts
192.168.5.79 nginx.test.com tomcat.test.com
訪問nginx,如下:
訪問tomcat,如下:
注意:如果顯示以下問題,可以在當(dāng)前瀏覽器頁面,直接敲鍵盤輸入thisisunsafe就可以了