目錄
目錄
目錄
ingress與service
ingress的組成
ingress-controller:
ingress暴露服務(wù)的方式
2.方式二:DaemonSet+hostnetwork+nodeSelector
DaemonSet+hostnetwork+nodeSelector如何實(shí)現(xiàn)
3.deployment+NodePort:
虛擬主機(jī)的方式實(shí)現(xiàn)http代理
總結(jié)
deploymentit+loadbalancer:
daemonset+hostnetwork+nodeselector:
deployment+nodeport:
ingress與service
service作用體現(xiàn)在兩個(gè)方面:
1.集群內(nèi)部:不斷跟蹤pod的變化,更新endpoint中的pod對(duì)象,基于pod的IP地址不斷變化的一種服務(wù)發(fā)現(xiàn)機(jī)制。
2.集群外部:類似負(fù)載均衡,把流量IP+端口,不涉及轉(zhuǎn)發(fā)url(http https),把請(qǐng)求轉(zhuǎn)發(fā)到pod當(dāng)中。
service:
nodeport:容器端口---service端口---nodeport,每個(gè)節(jié)點(diǎn)都會(huì)有一個(gè)端口被打開(kāi)(30000-32767)
IP+端口:節(jié)點(diǎn)IP+30000-32767實(shí)現(xiàn)負(fù)載均衡
loadbalancer:云平臺(tái)上的一種service服務(wù)。云平臺(tái)提高負(fù)載均衡的IP地址。
externalname:域名映射。
通過(guò)ingress基于域名映射,把url(http,https)請(qǐng)求轉(zhuǎn)發(fā)到service,再由service把請(qǐng)求轉(zhuǎn)發(fā)到每一個(gè)pod。
ingress只要一個(gè)或者是少量的公網(wǎng)IP或者LB,可以把多個(gè)http請(qǐng)求暴露到外網(wǎng),七層反向代理。
ingress是service的service,是一組基于域名和URL路徑,把一個(gè)或者多個(gè)請(qǐng)求(基于域名和URL的請(qǐng)求)轉(zhuǎn)發(fā)到service的一種規(guī)則。
先是七層代理(ingress)----四層代理(service)---pod(nginx)
ingress的組成
ingress是一個(gè)api對(duì)象,通過(guò)yaml文件來(lái)進(jìn)行配置。
ingress的作用-->定義請(qǐng)求如何轉(zhuǎn)發(fā)到service的規(guī)則。
ingress通過(guò)http和https暴露集群內(nèi)部的service,給service提供一個(gè)外部的url,負(fù)載均衡,ssl/tls(https)的能力,實(shí)現(xiàn)一個(gè)基于域名的負(fù)載均衡。
ingress-controller:
具體的實(shí)現(xiàn)反向代理和負(fù)載均衡的程序。對(duì)ingress定義的規(guī)則進(jìn)行解析,根據(jù)ingress的配置規(guī)則進(jìn)行請(qǐng)求的轉(zhuǎn)發(fā)。
ingress-controller不是K8s自帶的組件功能,ingress-controller一個(gè)統(tǒng)稱。
nginx ingress controller traefik都是ingress-controller,開(kāi)源。
ingress暴露服務(wù)的方式
1.deployment+LoadBalancer模式,ingress部署在公有云。會(huì)ingress配置文件里面會(huì)有一個(gè)type,type:loadbalancer。
公有云平臺(tái)會(huì)為這個(gè)loadbalancer的service創(chuàng)建一個(gè)負(fù)載均衡器。綁定一個(gè)公網(wǎng)地址。
通過(guò)域名指向這個(gè)公網(wǎng)地址就可以實(shí)現(xiàn)集群對(duì)外暴露。
2.方式二:DaemonSet+hostnetwork+nodeSelector
DaemonSet:在每個(gè)節(jié)點(diǎn)都會(huì)創(chuàng)建一個(gè)pod。
hostnetwork:pod共享節(jié)點(diǎn)主機(jī)的網(wǎng)絡(luò)命名空間。容器內(nèi)直接使用節(jié)點(diǎn)主機(jī)的IP+端口。pod中的容器可以直接訪問(wèn)主機(jī)上的網(wǎng)絡(luò)資源。
nodeSelector:根據(jù)標(biāo)簽來(lái)選擇部署的節(jié)點(diǎn)。nginx-ingress-controller部署的節(jié)點(diǎn)。
缺點(diǎn):直接利用節(jié)點(diǎn)主機(jī)的網(wǎng)絡(luò)和端口,一個(gè)node只能部署一個(gè)controller的pod。比較適合大并發(fā)的生產(chǎn)環(huán)境。性能是最好的。
解析出來(lái)的域名到ingress-controller,再到Ingress的配置,根據(jù)標(biāo)簽service匹配來(lái)對(duì)容器進(jìn)行發(fā)現(xiàn)和監(jiān)控。controller實(shí)現(xiàn)請(qǐng)求轉(zhuǎn)發(fā)和負(fù)載均衡。
ingress-controller的安裝
wget https://gitee.com/mirrors/ingress-nginx/raw/nginx-0.30.0/deploy/static/mandatory.yaml
DaemonSet+hostnetwork+nodeSelector如何實(shí)現(xiàn)
查看端口,只有node02有
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: nfs-pvc
spec:
accessModes:
- ReadWriteMany:
storageClassName: nfs-client-storageclass
resources:
request:
storage: 2Gi
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-app
labels:
app: nginx1
spec:
replicas: 3
selector:
matchLabels:
app: nginx1
template:
metadata:
labels:
app: nginx1
spec:
containers:
- name: nginx
image: nginx:1.22
volumeMounts:
- name: nfs-pvc
mountPath: /usr/share/nginx/html
volumes:
- name: nfs-pvc
persistentVolumeClaim:
claimName: nfs-pvc
---
apiVersion: v1
kind: Service
metadata:
name: nginx-app-svc
spec:
ports:
- protocol: Tcp
port: 80
targetPort: 80
selector:
app: nginx1
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx-app-ingress
spec:
rules:
- host: www.test1.com
http:
path: /
pathType: Prefix
#根據(jù)前綴進(jìn)行匹配。以/為開(kāi)頭都能匹配
3.deployment+NodePort:
host--->ingress的配置找到Pod--->controller--->請(qǐng)求發(fā)到pod
nodeport---controller---ingress---service---pod
nodeport暴露端口的方式是最簡(jiǎn)單的方法,nodeport 多了一層nat(地址轉(zhuǎn)換),并發(fā)量大的對(duì)性能會(huì)有一定影響。內(nèi)部都會(huì)用nodeport
apiVersion: v1
kind: Service
metadata:
name: ingress-nginx
namespace: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
- name: https
port: 443
targetPort: 443
protocol: TCP
selector:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
現(xiàn)在執(zhí)行這個(gè)yaml文件,會(huì)生成一個(gè)service,在ingress-nginx這個(gè)命名空間生成一個(gè)service,所有的controller的請(qǐng)求都會(huì)從這個(gè)定義的service的nodeport的端口,把請(qǐng)求轉(zhuǎn)發(fā)到自定義的service的Pod
ingress--nodeport的端口---service---pod
入口:nodeport不再是創(chuàng)建pod的deployment傳附件的,是ingress的service創(chuàng)建的
數(shù)據(jù)流向圖
虛擬主機(jī)的方式實(shí)現(xiàn)http代理
通過(guò)ingress的方式實(shí)現(xiàn):一個(gè)ingress可以訪問(wèn)不同的主機(jī)
vim pod1.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment1
labels:
test: nginx1
spec:
replicas: 1
selector:
matchLabels:
test: nginx1
template:
metadata:
labels:
test: nginx1
spec:
containers:
- name: nginx1
image: nginx:1.22
---
apiVersion: v1
kind: Service
metadata:
name: svc-1
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
test: nginx1
kubectl apply -f pod1.yaml
vim pod2.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment2
labels:
test: nginx2
spec:
replicas: 1
selector:
matchLabels:
test: nginx2
template:
metadata:
labels:
test: nginx2
spec:
containers:
- name: nginx1
image: nginx:1.22
---
apiVersion: v1
kind: Service
metadata:
name: svc-2
spec:
ports:
- port: 80
targetPort: 80
protocol: TCP
selector:
test: nginx2
wq
kubectl apply -f pod2.yaml
vim pod-ingress.yaml
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress1
spec:
rules:
- host: www.test.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:
rules:
- host: www.test2.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: svc-2
port:
number:80
wq
kubectl apply -f pod-ingress.yaml
vim /etc/hosts
curl www.
wq
總結(jié)
ingress的核心組件
nginx-ingress-controller
traefik
都是開(kāi)源的ingress-controller
deploymentit+loadbalancer:
需要云平臺(tái)提供一個(gè)負(fù)載均衡的公網(wǎng)地址,公有云上做($$$$$$)
daemonset+hostnetwork+nodeselector:
指定節(jié)點(diǎn)部署controller,缺點(diǎn)是和宿主機(jī)共享網(wǎng)絡(luò),只能是一個(gè)controller的pod。
hostnetwork會(huì)和宿主機(jī)共享網(wǎng)絡(luò)
deployment+nodeport:
最常用,最常見(jiàn),最簡(jiǎn)單的方式。
集中一個(gè)nodeport端口,所有的ingress的請(qǐng)求都會(huì)轉(zhuǎn)發(fā)到nodeport,然后把service把流量轉(zhuǎn)到pod。
一個(gè)ingress的nodeport,可以實(shí)現(xiàn)訪問(wèn)多個(gè)虛擬主機(jī)。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-813635.html
和nginx一樣。文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-813635.html
到了這里,關(guān)于k8s---對(duì)外服務(wù) ingress的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!