k8s 對(duì)外服務(wù)之 Ingress
//Ingress 簡介
service的作用體現(xiàn)在兩個(gè)方面,對(duì)集群內(nèi)部,它不斷跟蹤pod的變化,更新endpoint中對(duì)應(yīng)pod的對(duì)象,提供了ip不斷變化的pod的服務(wù)發(fā)現(xiàn)機(jī)制;對(duì)集群外部,他類似負(fù)載均衡器,可以在集群內(nèi)外部對(duì)pod進(jìn)行訪問。
在Kubernetes中,Pod的IP地址和service的ClusterIP僅可以在集群網(wǎng)絡(luò)內(nèi)部使用,對(duì)于集群外的應(yīng)用是不可見的。為了使外部的應(yīng)用能夠訪問集群內(nèi)的服務(wù),Kubernetes目前提供了以下幾種方案:
●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ò)的橋梁。
測試環(huán)境使用還行,當(dāng)有幾十上百的服務(wù)在集群中運(yùn)行時(shí),NodePort的端口管理就是個(gè)災(zāi)難。因?yàn)槊總€(gè)端口只能是一種服務(wù),默認(rèn)端口范圍只能是 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ù)做后端。
●externalIPs:service允許為其分配外部IP,如果外部IP路由到集群中一個(gè)或多個(gè)Node上,Service會(huì)被暴露給這些externalIPs。通過外部IP進(jìn)入到集群的流量,將會(huì)被路由到Service的Endpoint上。?
●Ingress:只需一個(gè)或者少量的公網(wǎng)IP和LB,即可同時(shí)將多個(gè)HTTP服務(wù)暴露到外網(wǎng),七層反向代理。
可以簡單理解為service的service,它其實(shí)就是一組基于域名和URL路徑,把用戶的請(qǐng)求轉(zhuǎn)發(fā)到一個(gè)或多個(gè)service的規(guī)則。
//Ingress 組成
●ingress:
ingress是一個(gè)API對(duì)象,通過yaml文件來配置,ingress對(duì)象的作用是定義請(qǐng)求如何轉(zhuǎn)發(fā)到service的規(guī)則,可以理解為配置模板。
ingress通過http或https暴露集群內(nèi)部service,給service提供外部URL、負(fù)載均衡、SSL/TLS能力以及基于域名的反向代理。ingress要依靠 ingress-controller 來具體實(shí)現(xiàn)以上功能。
●ingress-controller:
ingress-controller是具體實(shí)現(xiàn)反向代理及負(fù)載均衡的程序,對(duì)ingress定義的規(guī)則進(jìn)行解析,根據(jù)配置的規(guī)則來實(shí)現(xiàn)請(qǐng)求轉(zhuǎn)發(fā)。
ingress-controller并不是k8s自帶的組件,實(shí)際上ingress-controller只是一個(gè)統(tǒng)稱,用戶可以選擇不同的ingress-controller實(shí)現(xiàn),目前,由k8s維護(hù)的ingress-controller只有g(shù)oogle云的GCE與ingress-nginx兩個(gè),其他還有很多第三方維護(hù)的ingress-controller,具體可以參考官方文檔。但是不管哪一種ingress-controller,實(shí)現(xiàn)的機(jī)制都大同小異,只是在具體配置上有差異。
一般來說,ingress-controller的形式都是一個(gè)pod,里面跑著daemon程序和反向代理程序。daemon負(fù)責(zé)不斷監(jiān)控集群的變化,根據(jù) ingress對(duì)象生成配置并應(yīng)用新配置到反向代理,比如ingress-nginx就是動(dòng)態(tài)生成nginx配置,動(dòng)態(tài)更新upstream,并在需要的時(shí)候reload程序應(yīng)用新配置。為了方便,后面的例子都以k8s官方維護(hù)的ingress-nginx為例。
Ingress-Nginx github 地址:https://github.com/kubernetes/ingress-nginx
Ingress-Nginx 官方網(wǎng)站:https://kubernetes.github.io/ingress-nginx/
總結(jié):ingress-controller才是負(fù)責(zé)具體轉(zhuǎn)發(fā)的組件,通過各種方式將它暴露在集群入口,外部對(duì)集群的請(qǐng)求流量會(huì)先到 ingress-controller, 而ingress對(duì)象是用來告訴ingress-controller該如何轉(zhuǎn)發(fā)請(qǐng)求,比如哪些域名、哪些URL要轉(zhuǎn)發(fā)到哪些service等等。
//Ingress-Nginx 工作原理
(1)ingress-controller通過和 kubernetes APIServer 交互,動(dòng)態(tài)的去感知集群中ingress規(guī)則變化,
(2)然后讀取它,按照自定義的規(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.conf文件中,
(4)然后reload一下使配置生效。以此達(dá)到域名區(qū)分配置和動(dòng)態(tài)更新的作用。
//部署 nginx-ingress-controller
1、部署ingress-controller Pod及相關(guān)資源
mkdir /opt/ingress
cd /opt/ingress
官方下載地址:
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.3.0/deploy/static/provider/cloud/deploy.yaml
上面可能無法下載,可用國內(nèi)的 gitee
wget https://gitee.com/mirrors/ingress-nginx/raw/controller-v1.3.0/deploy/static/provider/cloud/deploy.yaml
修改鏡像地址為:
image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.3.0@sha256:d1707ca76d3b044ab8a28277a2466a02100ee9f58a86af1535a3edf9323ea1b5
image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a91b20cc67868660
//ingress 暴露服務(wù)的方式
●方式一:DaemonSet+HostNetwork+nodeSelector
用DaemonSet結(jié)合nodeselector來部署ingress-controller到特定的node上,然后使用HostNetwork直接把該pod與宿主機(jī)node的網(wǎng)絡(luò)打通,直接使用宿主機(jī)的80/433端口就能訪問服務(wù)。這時(shí),ingress-controller所在的node機(jī)器就很類似傳統(tǒng)架構(gòu)的邊緣節(jié)點(diǎn),比如機(jī)房入口的nginx服務(wù)器。該方式整個(gè)請(qǐng)求鏈路最簡單,性能相對(duì)NodePort模式更好。缺點(diǎn)是由于直接利用宿主機(jī)節(jié)點(diǎn)的網(wǎng)絡(luò)和端口,一個(gè)node只能部署一個(gè)ingress-controller pod。 比較適合大并發(fā)的生產(chǎn)環(huán)境使用。
●方式二:Deployment+NodePort模式的Service
同樣用deployment模式部署ingress-controller,并創(chuàng)建對(duì)應(yīng)的service,但是type為NodePort。這樣,ingress就會(huì)暴露在集群節(jié)點(diǎn)ip的特定端口上。由于nodeport暴露的端口是隨機(jī)端口,一般會(huì)在前面再搭建一套負(fù)載均衡器來轉(zhuǎn)發(fā)請(qǐng)求。該方式一般用于宿主機(jī)是相對(duì)固定的環(huán)境ip地址不變的場景。
NodePort方式暴露ingress雖然簡單方便,但是NodePort多了一層NAT,在請(qǐng)求量級(jí)很大時(shí)可能對(duì)性能會(huì)有一定影響。
●方式一:Deployment+LoadBalancer 模式的 Service
如果要把ingress部署在公有云,那用這種方式比較合適。用Deployment部署ingress-controller,創(chuàng)建一個(gè) type為 LoadBalancer 的 service 關(guān)聯(lián)這組 pod。 大部分公有云,都會(huì)為 LoadBalancer 的 service 自動(dòng)創(chuàng)建一個(gè)負(fù)載均衡器,通常還綁定了公網(wǎng)地址。只要把域名解析指向該地址,就實(shí)現(xiàn)了集群服務(wù)的對(duì)外暴露
//采用方式二:DaemonSet+HostNetwork+nodeSelector
3、指定 nginx-ingress-controller 運(yùn)行在 node02 節(jié)點(diǎn)
kubectl label node node02 ingress=true
kubectl get nodes --show-labels
4、修改 Deployment 為 DaemonSet ,指定節(jié)點(diǎn)運(yùn)行,并開啟 hostNetwork 網(wǎng)絡(luò)
vim deploy.yaml
...
apiVersion: apps/v1
# 修改 kind
#kind: Deployment
kind: DaemonSet
......
spec:
? ......
? template:
? ? ......
? ? spec:
? ? ? # 使用宿主機(jī)網(wǎng)絡(luò)
? ? ? hostNetwork: true
? ? ? # 修改選擇節(jié)點(diǎn)選擇器
? ? ? nodeSelector:
? ? ? ? ingress: "true"
......
5、啟動(dòng) nginx-ingress-controller
kubectl apply -f deploy.yaml
//nginx-ingress-controller 已經(jīng)運(yùn)行 node02 節(jié)點(diǎn)
kubectl get pod -n ingress-nginx -owide
NAME ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?READY ? STATUS ? ? ?RESTARTS ? AGE ? IP ? ? ? ? ? ? ?NODE ? ? NOMINATED NODE ? READINESS GATES
ingress-nginx-admission-create-vvxjr ? ? ? ?0/1 ? ? Completed ? 0 ? ? ? ? ?19m ? 10.244.1.24 ? ? node01 ? <none> ? ? ? ? ? <none>
ingress-nginx-admission-patch-9gmdd ? ? ? ? 0/1 ? ? Completed ? 0 ? ? ? ? ?19m ? 10.244.1.25 ? ? node01 ? <none> ? ? ? ? ? <none>
ingress-nginx-controller-575585684c-9qbwp ? 1/1 ? ? Running ? ? 0 ? ? ? ? ?10m ? 192.168.80.30 ? node02 ? <none> ? ? ? ? ? <none>
kubectl get svc -n ingress-nginx
NAME ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TYPE ? ? ? ?CLUSTER-IP ? ? ?EXTERNAL-IP ? PORT(S) ? ? ? ? ? ? ? ? ? ? ?AGE
ingress-nginx-controller ? ? ? ? ? ? NodePort ? ?10.96.3.13 ? ? ?<none> ? ? ? ?80:32338/TCP,443:30008/TCP ? 19m
ingress-nginx-controller-admission ? ClusterIP ? 10.96.175.162 ? <none> ? ? ? ?443/TCP ? ? ? ? ? ? ? ? ? ? ?19m
//到 node02 節(jié)點(diǎn)查看
netstat -lntp | grep nginx
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:80 ? ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?68911/nginx: master?
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:8181 ? ? ? ? ? ?0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?68911/nginx: master?
tcp ? ? ? ?0 ? ? ?0 0.0.0.0:443 ? ? ? ? ? ? 0.0.0.0:* ? ? ? ? ? ? ? LISTEN ? ? ?68911/nginx: master?
......
由于配置了 hostnetwork,nginx 已經(jīng)在 node 主機(jī)本地監(jiān)聽 80/443/8181 端口。其中 8181 是 nginx-controller 默認(rèn)配置的一個(gè) default backend(Ingress 資源沒有匹配的 rule 對(duì)象時(shí),流量就會(huì)被導(dǎo)向這個(gè) default backend)。
這樣,只要訪問 node 主機(jī)有公網(wǎng) IP,就可以直接映射域名來對(duì)外網(wǎng)暴露服務(wù)了。如果要 nginx 高可用的話,可以在多個(gè) node 上部署,并在前面再搭建一套 LVS+keepalived 做負(fù)載均衡。
6、創(chuàng)建 ingress 規(guī)則
//創(chuàng)建一個(gè) deploy 和 svc
vim service-nginx.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
? name: nginx-app
spec:
? replicas: 2
? selector:
? ? matchLabels:
? ? ? app: nginx
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: nginx
? ? spec:
? ? ? containers:
? ? ? ? - name: nginx
? ? ? ? ? image: nginx
? ? ? ? ? imagePullPolicy: IfNotPresent
? ? ? ? ? ports:
? ? ? ? ? ? - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
? name: nginx-app-svc
spec:
? type: ClusterIP
? ports:
? - protocol: TCP
? ? port: 80
? ? targetPort: 80
? selector:
? ? app: nginx
//創(chuàng)建 ingress
kubectl get ingressclasses
NAME ? ?CONTROLLER ? ? ? ? ? ? PARAMETERS ? AGE
nginx ? k8s.io/ingress-nginx ? <none> ? ? ? 22m
vim ingress-app.yaml?? ? ?
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
? name: nginx-app-ingress
spec:
? ingressClassName: "nginx"
? rules:
? - host: www.kgc.com
? ? http:
? ? ? paths:
? ? ? - path: /
? ? ? ? pathType: Prefix
? ? ? ? backend:
? ? ? ? ? service:
? ? ? ? ? ? name: nginx-app-svc
? ? ? ? ? ? port:
? ? ? ? ? ? ? number: 80
-------------------------------------------------------------------------------------------
ingressClassName 指定 IngressClass,用來指定選擇的 Ingress Controller
host 主機(jī)名可以是精確匹配,或者使用通配符來匹配,但通配符僅覆蓋一個(gè) DNS 標(biāo)簽(例如 *.foo.com 不匹配 baz.bar.foo.com)。
pathType 支持的路徑類型有三種:
●Exact:精確匹配 URL 路徑,且區(qū)分大小寫。
●Prefix:基于以 / 分隔的 URL 路徑前綴匹配。匹配區(qū)分大小寫。如果路徑的最后一個(gè)元素是請(qǐng)求路徑中最后一個(gè)元素的子字符串,則不會(huì)匹配 (例如:/foo/bar 匹配 /foo/bar/baz, 但不匹配 /foo/barbaz)。
●ImplementationSpecific:對(duì)于這種路徑類型,匹配方法取決于 IngressClass。具體實(shí)現(xiàn)可以將其作為單獨(dú)的 pathType 處理或者與 Prefix 或 Exact 類型作相同處理。
具體可詳見:https://kubernetes.io/zh-cn/docs/concepts/services-networking/ingress/#the-ingress-resource
-------------------------------------------------------------------------------------------
kubectl apply -f service-nginx.yaml
kubectl apply -f ingress-app.yaml
kubectl get pods
NAME ? ? ? ? ? ? ? ? ? ? ? ? READY ? STATUS ? ?RESTARTS ? AGE
nginx-app-7bffc778db-sw2hl ? 1/1 ? ? Running ? 0 ? ? ? ? ?42s
nginx-app-7bffc778db-xsd5q ? 1/1 ? ? Running ? 0 ? ? ? ? ?42s
kubectl get ingress
NAME ? ? ? ? CLASS ? HOSTS ? ? ? ? ADDRESS ? ? ?PORTS ? AGE
ingress-v1 ? nginx ? www.kgc.com ? 10.96.3.13 ? 80 ? ? ?39s
7、測試訪問
//本地 host 添加域名解析
vim /etc/hosts
192.168.80.10 master
192.168.80.11 node01
192.168.80.12 node02
192.168.80.12 www.kgc.com
curl http://www.kgc.com
8、查看 nginx-ingress-controller
kubectl get pod -n ingress-nginx -o wide
NAME ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY ? STATUS ? ?RESTARTS ? AGE ? IP ? ? ? ? ? ? ?NODE ? ? NOMINATED NODE ? READINESS GATES
nginx-ingress-controller-99h72 ? 1/1 ? ? Running ? 0 ? ? ? ? ?93s ? 192.168.80.15 ? node02 ? <none> ? ? ? ? ? <none>
kubectl exec -it nginx-ingress-controller-99h72 -n ingress-nginx /bin/bash
?# more /etc/nginx/nginx.conf
//可以看到從 start server www.kgc.com 到 end server www.kgc.com 之間包含了此域名用于反向代理的配置
//采用方式二:Deployment+NodePort模式的Service
1、下載 nginx-ingress-controller 配置文件
mkdir /opt/ingress-nodeport
cd /opt/ingress-nodeport
官方下載地址:
wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.3.0/deploy/static/provider/cloud/deploy.yaml
上面可能無法下載,可用國內(nèi)的 gitee
wget https://gitee.com/mirrors/ingress-nginx/raw/controller-v1.3.0/deploy/static/provider/cloud/deploy.yaml
修改鏡像地址為:
image: registry.cn-hangzhou.aliyuncs.com/google_containers/nginx-ingress-controller:v1.3.0@sha256:d1707ca76d3b044ab8a28277a2466a02100ee9f58a86af1535a3edf9323ea1b5
image: registry.cn-hangzhou.aliyuncs.com/google_containers/kube-webhook-certgen:v1.1.1@sha256:64d8c73dca984af206adf9d6d7e46aa550362b1d7a01f3a0a91b20cc67868660
2、修改配置文件
apiVersion: v1
kind: Service
metadata:
? labels:
? ? app.kubernetes.io/component: controller
? ? app.kubernetes.io/instance: ingress-nginx
? ? app.kubernetes.io/name: ingress-nginx
? ? app.kubernetes.io/part-of: ingress-nginx
? ? app.kubernetes.io/version: 1.3.0
? name: ingress-nginx-controller
? namespace: ingress-nginx
spec:
? #externalTrafficPolicy: Local ? ? ?#注釋此行,值為Cluster表示流量可以轉(zhuǎn)發(fā)到其他節(jié)點(diǎn)上的Pod;Local表示:流量只發(fā)給本機(jī)的Pod
? ports:
? ......
? type: NodePort ? ? #Service類型設(shè)置為 NodePort 或 LoadBalancer
3、啟動(dòng) nginx-ingress-controller
kubectl apply -f deploy.yaml
kubectl get pod,svc -n ingress-nginx
NAME ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?READY ? STATUS ? ? ?RESTARTS ? AGE
pod/ingress-nginx-admission-create-vvxjr ? ? ? ?0/1 ? ? Completed ? 0 ? ? ? ? ?37m
pod/ingress-nginx-admission-patch-9gmdd ? ? ? ? 0/1 ? ? Completed ? 0 ? ? ? ? ?37m
pod/ingress-nginx-controller-575585684c-9qbwp ? 1/1 ? ? Running ? ? 0 ? ? ? ? ?29m
NAME ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? TYPE ? ? ? ?CLUSTER-IP ? ? ?EXTERNAL-IP ? PORT(S) ? ? ? ? ? ? ? ? ? ? ?AGE
service/ingress-nginx-controller ? ? ? ? ? ? NodePort ? ?10.96.3.13 ? ? ?<none> ? ? ? ?80:32338/TCP,443:30008/TCP ? 37m
service/ingress-nginx-controller-admission ? ClusterIP ? 10.96.175.162 ? <none> ? ? ? ?443/TCP ? ? ? ? ? ? ? ? ? ? ?37m
//Ingress HTTP 代理訪問
cd /opt/ingress-nodeport
#創(chuàng)建 deployment、Service、Ingress Yaml 資源
vim ingress-nginx.yaml?
apiVersion: apps/v1
kind: Deployment
metadata:
? name: nginx-app
spec:
? replicas: 2
? selector:
? ? matchLabels:
? ? ? name: nginx
? template:
? ? metadata:
? ? ? labels:
? ? ? ? name: nginx
? ? spec:
? ? ? containers:
? ? ? ? - name: nginx
? ? ? ? ? image: nginx
? ? ? ? ? imagePullPolicy: IfNotPresent
? ? ? ? ? ports:
? ? ? ? ? ? - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
? name: nginx-svc
spec:
? ports:
? ? - port: 80
? ? ? targetPort: 80
? ? ? protocol: TCP
? selector:
? ? name: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
? name: nginx-test
spec:
? ingressClassName: "nginx"
? rules:
? - host: www.benet.com
? ? http:
? ? ? paths:
? ? ? - path: /
? ? ? ? pathType: Prefix
? ? ? ? backend:
? ? ? ? ? service:?
? ? ? ? ? ? name: nginx-svc
? ? ? ? ? ? port:
? ? ? ? ? ? ? number: 80
?? ?
kubectl apply -f ingress-nginx.yaml
kubectl get svc,pods -o wide
NAME ? ? ? ? ? ? ? ? TYPE ? ? ? ?CLUSTER-IP ? ? ?EXTERNAL-IP ? PORT(S) ? AGE ? SELECTOR
service/kubernetes ? ClusterIP ? 10.96.0.1 ? ? ? <none> ? ? ? ?443/TCP ? 8h ? ?<none>
service/nginx-svc ? ?ClusterIP ? 10.96.212.214 ? <none> ? ? ? ?80/TCP ? ?65s ? name=nginx
NAME ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY ? STATUS ? ?RESTARTS ? AGE ? IP ? ? ? ? ? NODE ? ? NOMINATED NODE ? READINESS GATES
pod/nginx-app-65d7b99f6b-l4g65 ? 1/1 ? ? Running ? 0 ? ? ? ? ?65s ? 10.244.1.8 ? node01 ? <none> ? ? ? ? ? <none>
pod/nginx-app-65d7b99f6b-zcqgp ? 1/1 ? ? Running ? 0 ? ? ? ? ?65s ? 10.244.2.8 ? node02 ? <none> ? ? ? ? ? <none>
kubectl exec -it pod/nginx-app-65d7b99f6b-l4g65 bash
?# cd /usr/share/nginx/html/
?# echo 'this is web1' >> index.html?
kubectl exec -it pod/nginx-app-65d7b99f6b-zcqgp bash
?# cd /usr/share/nginx/html/
?# echo 'this is web2' >> index.html
#測試訪問
curl 10.96.212.214
kubectl get svc -n ingress-nginx
NAME ? ? ? ? ? ?TYPE ? ? ? CLUSTER-IP ? ? EXTERNAL-IP ? PORT(S) ? ? ? ? ? ? ? ? ? ? ?AGE
ingress-nginx ? NodePort ? 10.96.67.119 ? <none> ? ? ? ?80:32383/TCP,443:32133/TCP ? 59m
#本地 host 添加域名解析
vim /etc/hosts
192.168.80.10 master
192.168.80.11 node01
192.168.80.12 node02
192.168.80.12 www.kgc.com www.benet.com
#外部訪問
curl http://www.benet.com:32383
//Ingress HTTP 代理訪問虛擬主機(jī)
mkdir /opt/ingress-nodeport/vhost
cd /opt/ingress-nodeport/vhost
#創(chuàng)建虛擬主機(jī)1資源
vim deployment1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
? name: deployment1
spec:
? replicas: 2
? selector:
? ? matchLabels:
? ? ? name: nginx1
? template:
? ? metadata:
? ? ? labels:
? ? ? ? name: nginx1
? ? spec:
? ? ? containers:
? ? ? ? - name: nginx1
? ? ? ? ? image: soscscs/myapp:v1
? ? ? ? ? imagePullPolicy: IfNotPresent
? ? ? ? ? ports:
? ? ? ? ? ? - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
? name: svc-1
spec:
? ports:
? ? - port: 80
? ? ? targetPort: 80
? ? ? protocol: TCP
? selector:
? ? name: nginx1
?? ?
?? ?
kubectl apply -f deployment1.yaml
#創(chuàng)建虛擬主機(jī)2資源
vim deployment2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
? name: deployment2
spec:
? replicas: 2
? selector:
? ? matchLabels:
? ? ? name: nginx2
? template:
? ? metadata:
? ? ? labels:
? ? ? ? name: nginx2
? ? spec:
? ? ? containers:
? ? ? ? - name: nginx2
? ? ? ? ? image: soscscs/myapp:v2
? ? ? ? ? imagePullPolicy: IfNotPresent
? ? ? ? ? ports:
? ? ? ? ? ? - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
? name: svc-2
spec:
? ports:
? ? - port: 80
? ? ? targetPort: 80
? ? ? protocol: TCP
? selector:
? ? name: nginx2
?? ?
?? ?
kubectl apply -f deployment2.yaml
#創(chuàng)建ingress資源
vim ingress-nginx.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
? name: ingress1
spec:
? ingressClassName: "nginx"
? rules:
? ? - host: www1.kgc.com
? ? ? http:
? ? ? ? paths:
? ? ? ? - path: /
? ? ? ? ? pathType: Prefix
? ? ? ? ? backend:
? ? ? ? ? ? service:?
? ? ? ? ? ? ? name: svc-1
? ? ? ? ? ? ? port:
? ? ? ? ? ? ? ? number: 80
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
? name: ingress2
spec:
? ingressClassName: "nginx"
? rules:
? ? - host: www2.kgc.com
? ? ? http:
? ? ? ? paths:
? ? ? ? - path: /
? ? ? ? ? pathType: Prefix
? ? ? ? ? backend:
? ? ? ? ? ? service:?
? ? ? ? ? ? ? name: svc-2
? ? ? ? ? ? ? port:
? ? ? ? ? ? ? ? number: 80
kubectl apply -f ingress-nginx.yaml
#測試訪問
kubectl get svc -n ingress-nginx
NAME ? ? ? ? ? ?TYPE ? ? ? CLUSTER-IP ? ? EXTERNAL-IP ? PORT(S) ? ? ? ? ? ? ? ? ? ? ?AGE
ingress-nginx ? NodePort ? 10.96.67.119 ? <none> ? ? ? ?80:32383/TCP,443:32133/TCP ? 176m
curl www1.kgc.com:32383
Hello MyApp | Version: v1 | <a href="hostname.html">Pod Name</a>
curl www2.kgc.com:32383
Hello MyApp | Version: v2 | <a href="hostname.html">Pod Name</a>
//Ingress ?HTTPS 代理訪問?
mkdir /opt/ingress-nodeport/https
cd /opt/ingress-nodeport/https
#創(chuàng)建ssl證書
openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout tls.key -out tls.crt -subj "/CN=nginxsvc/O=nginxsvc"
#創(chuàng)建 secret 資源進(jìn)行存儲(chǔ)
kubectl create secret tls tls-secret --key tls.key --cert tls.crt
kubectl get secret
NAME ? ? ? ? ? ? ? ? ?TYPE ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?DATA ? AGE
tls-secret ? ? ? ? ? ?kubernetes.io/tls ? ? ? ? ? ? ? ? ? ? 2 ? ? ?2m22s
kubectl describe secret tls-secret
Name: ? ? ? ? tls-secret
Namespace: ? ?default
Labels: ? ? ? <none>
Annotations: ?<none>
Type: ?kubernetes.io/tls
Data
====
tls.crt: ?1143 bytes
tls.key: ?1704 bytes
#創(chuàng)建 deployment、Service、Ingress Yaml 資源
vim ingress-https.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
? name: nginx-app
spec:
? replicas: 2
? selector:
? ? matchLabels:
? ? ? name: nginx
? template:
? ? metadata:
? ? ? labels:
? ? ? ? name: nginx
? ? spec:
? ? ? containers:
? ? ? ? - name: nginx
? ? ? ? ? image: nginx
? ? ? ? ? imagePullPolicy: IfNotPresent
? ? ? ? ? ports:
? ? ? ? ? ? - containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
? name: nginx-svc
spec:
? ports:
? ? - port: 80
? ? ? targetPort: 80
? ? ? protocol: TCP
? selector:
? ? name: nginx
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
? name: nginx-https
spec:
? ingressClassName: "nginx"
? tls:
? ? - hosts:
? ? ? - www3.kgc.com
? ? ? secretName: tls-secret
? rules:
? ? - host: www3.kgc.com
? ? ? http:
? ? ? ? paths:
? ? ? ? - path: /
? ? ? ? ? pathType: Prefix
? ? ? ? ? backend:
? ? ? ? ? ? service:?
? ? ? ? ? ? ? name: nginx-svc
? ? ? ? ? ? ? port:
? ? ? ? ? ? ? ? number: 80
kubectl apply -f ingress-https.yaml
kubectl get svc -n ingress-nginx
NAME ? ? ? ? ? ?TYPE ? ? ? CLUSTER-IP ? ? EXTERNAL-IP ? PORT(S) ? ? ? ? ? ? ? ? ? ? ?AGE
ingress-nginx ? NodePort ? 10.96.67.119 ? <none> ? ? ? ?80:32383/TCP,443:32133/TCP ? 3h41m
#訪問測試
在宿主機(jī)的 C:\Windows\System32\drivers\etc\hosts 文件中添加 192.168.80.10 www3.kgc.com 記錄。
使用谷歌瀏覽器訪問 https://www3.kgc.com:32133
//Nginx 進(jìn)行 BasicAuth
mkdir /opt/ingress-nodeport/basic-auth
cd /opt/ingress-nodeport/basic-auth
#生成用戶密碼認(rèn)證文件,創(chuàng)建 secret 資源進(jìn)行存儲(chǔ)
yum -y install httpd
htpasswd -c auth zhangsan?? ??? ??? ?#認(rèn)證文件名必須為 auth
kubectl create secret generic basic-auth --from-file=auth
#創(chuàng)建 ingress 資源
vim ingress-auth.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
? name: ingress-auth
? annotations:
? ? #設(shè)置認(rèn)證類型basic
? ? nginx.ingress.kubernetes.io/auth-type: basic
?? ?#設(shè)置secret資源名稱basic-auth
? ? nginx.ingress.kubernetes.io/auth-secret: basic-auth
?? ?#設(shè)置認(rèn)證窗口提示信息
? ? nginx.ingress.kubernetes.io/auth-realm: 'Authentication Required - zhangsan'
spec:
? ingressClassName: "nginx"
? rules:
? - host: auth.kgc.com
? ? http:
? ? ? paths:
? ? ? - path: /
? ? ? ? pathType: Prefix
? ? ? ? backend:
? ? ? ? ? service:?
? ? ? ? ? ? name: nginx-svc
? ? ? ? ? ? port:
? ? ? ? ? ? ? number: 80
//具體詳細(xì)設(shè)置方法可參考官網(wǎng)https://kubernetes.github.io/ingress-nginx/examples/auth/basic/
kubectl apply -f ingress-auth.yaml
#訪問測試
kubectl get svc -n ingress-nginx
NAME ? ? ? ? ? ?TYPE ? ? ? CLUSTER-IP ? ? EXTERNAL-IP ? PORT(S) ? ? ? ? ? ? ? ? ? ? ?AGE
ingress-nginx ? NodePort ? 10.96.67.119 ? <none> ? ? ? ?80:32383/TCP,443:32133/TCP ? 8h
echo '192.168.80.10 auth.kgc.com' >> /etc/hosts
瀏覽器訪問:http://auth.kgc.com:32383
//Nginx 進(jìn)行重寫
#metadata.annotations 配置說明
●nginx.ingress.kubernetes.io/rewrite-target: <字符串> #必須重定向流量的目標(biāo)URI
●nginx.ingress.kubernetes.io/ssl-redirect: <布爾值> #指示位置部分是否僅可訪問SSL(當(dāng)Ingress包含證書時(shí),默認(rèn)為true)
●nginx.ingress.kubernetes.io/force-ssl-redirect: <布爾值> #即使Ingress未啟用TLS,也強(qiáng)制重定向到HTTPS
●nginx.ingress.kubernetes.io/app-root: <字符串> #定義Controller必須重定向的應(yīng)用程序根,如果它在'/'上下文中
●nginx.ingress.kubernetes.io/use-regex: <布爾值> #指示Ingress上定義的路徑是否使用正則表達(dá)式
vim ingress-rewrite.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
? name: nginx-rewrite
? annotations:
? ? nginx.ingress.kubernetes.io/rewrite-target: http://www1.kgc.com:32383
spec:
? ingressClassName: "nginx"
? rules:
? - host: re.kgc.com
? ? http:
? ? ? paths:
? ? ? - path: /
? ? ? ? pathType: Prefix
? ? ? ? backend:
?? ??? ? ?#由于re.kgc.com只是用于跳轉(zhuǎn)不需要真實(shí)站點(diǎn)存在,因此svc資源名稱可隨意定義
? ? ? ? ? service:?
? ? ? ? ? ? name: nginx-svc
? ? ? ? ? ? port:
? ? ? ? ? ? ? number: 80
kubectl apply -f ingress-rewrite.yaml
echo '192.168.80.10 re.kgc.com' >> /etc/hosts
瀏覽器訪問:http://re.kgc.com:32383文章來源:http://www.zghlxwxcb.cn/news/detail-593103.html
//總結(jié)
ingress是k8s集群的請(qǐng)求入口,可以理解為對(duì)多個(gè)service的再次抽象
通常說的ingress一般包括ingress資源對(duì)象及ingress-controller兩部分組成
ingress-controller有多種實(shí)現(xiàn),社區(qū)原生的是ingress-nginx,根據(jù)具體需求選擇
ingress自身的暴露有多種方式,需要根據(jù)基礎(chǔ)環(huán)境及業(yè)務(wù)類型選擇合適的方式文章來源地址http://www.zghlxwxcb.cn/news/detail-593103.html
到了這里,關(guān)于kubernetes Ingress資源管理的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!