4 Service-LoadBalance
通常需要第三方云提供商支持震叙,有約束性
Ingress
官網(wǎng):https://kubernetes.io/docs/concepts/services-networking/ingress/
GitHub Ingress Nginx:https://github.com/kubernetes/ingress-nginx
Nginx Ingress Controller:<https://kubernetes.github.io/ingress-nginx/
An API object that manages external access to the services in a cluster, typically HTTP.
Ingress can provide load balancing, SSL termination and name-based virtual hosting.
Ingress exposes HTTP and HTTPS routes from outside the cluster to services within the cluster. Traffic routing is controlled by rules defined on the Ingress resource.
? ? internet
? ? ? ? |
? [ Ingress ]
? --|-----|--
? [ Services ]
可以發(fā)現(xiàn)捐友,Ingress就是幫助我們?cè)L問(wèn)集群內(nèi)的服務(wù)的匣砖。為了彰顯其優(yōu)勢(shì)猴鲫,我們?cè)谑褂肐ngress之前,先以一個(gè)簡(jiǎn)單案例出發(fā)牺弄。
4.1使用NodePort類型的service在K8S集群中部署tomcat
(也為了演示將service寫在yaml文件中)
瀏覽器想要訪問(wèn)這個(gè)tomcat势告,也就是外部要訪問(wèn)該tomcat,用之前的Service-NodePort的方式是可以的络拌,比如暴露一個(gè)端口春贸,只需要訪問(wèn) :即可遗遵。
01 創(chuàng)建yaml文件
vim my-tomcat.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
? name: tomcat-deployment
? labels:
? ? app: tomcat
spec:
? replicas: 1
? selector:
? ? matchLabels:
? ? ? app: tomcat
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: tomcat
? ? spec:
? ? ? containers:
? ? ? - name: tomcat
? ? ? ? image: tomcat
? ? ? ? ports:
? ? ? ? - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
? name: tomcat-service
spec:
? ports:
? - port: 80?
? ? protocol: TCP
? ? targetPort: 8080
? selector:
? ? app: tomcat
? type: NodePort?
02 創(chuàng)建service
`創(chuàng)建pod雄坪、service`
[root@henry001 network]# kubectl apply -f my-tomcat.yaml
deployment.apps/tomcat-deployment created
service/tomcat-service created
`查看service`
[root@henry001 network]# kubectl get svc
NAME? ? ? ? ? ? ? ? TYPE? ? ? ? CLUSTER-IP? ? ? EXTERNAL-IP? PORT(S)? ? ? ? ? AGE
kubernetes? ? ? ? ? ClusterIP? 10.96.0.1? ? ? ? <none>? ? ? ? 443/TCP? ? ? ? ? 42h
tomcat-service? ? ? NodePort? ? 10.106.112.183? <none>? ? ? ? 80:30747/TCP? ? 31s
whoami-deployment? NodePort? ? 10.103.129.91? ? <none>? ? ? ? 8000:31999/TCP? 42m
`查看pod`
[root@henry001 network]# kubectl get pods
NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? ? ? ? ? ? RESTARTS? AGE
tomcat-deployment-6b9d6f8547-6mmh2? 1/1? ? Running? 0? ? ? ? ? 69s
tomcat-deployment-6b9d6f8547-79nck? 1/1? ? Running? 0? ? ? ? ? 69s
tomcat-deployment-6b9d6f8547-c8bps? 1/1? ? Running? 0? ? ? ? ? 69s
`查看deployment`
[root@henry001 network]# kubectl get deploy
NAME? ? ? ? ? ? ? ? READY? UP-TO-DATE? AVAILABLE? AGE
nginx? ? ? ? ? ? ? 1/1? ? 1? ? ? ? ? ? 1? ? ? ? ? 24h
tomcat-deployment? 3/3? ? 3? ? ? ? ? ? 3? ? ? ? ? 97s
顯然,Service-NodePort的方式生產(chǎn)環(huán)境不推薦使用登澜,那接下來(lái)就基于上述需求脑蠕,使用Ingress實(shí)現(xiàn)訪問(wèn)tomcat的需求谴仙。下面就開始講解使用ingress插件來(lái)實(shí)現(xiàn)外網(wǎng)訪問(wèn)集群pod碾盐。
4.2 使用ingress實(shí)現(xiàn)
4.2.1架構(gòu)圖
說(shuō)明:
本文中采用的ingress-controller是nginx-ingress-controller毫玖,具體詳情可以參考官網(wǎng):https://www.nginx.com/products/nginx/kubernetes-ingress-controller付枫;
大家也可以根據(jù)自己需要采用不同的ingress-controller阐滩,可參考https://kubernetes.io/docs/concepts/services-networking/ingress-controllers/
4.2.2 實(shí)例
(1)以Deployment方式創(chuàng)建Pod,該P(yáng)od為Ingress Nginx Controller继效,要想讓外界訪問(wèn)莲趣,可以通過(guò)Service的NodePort或者HostPort方式,這里選擇HostPort走芋,比如指定henry002機(jī)器上運(yùn)行:
# 確保nginx-controller運(yùn)行到henry002節(jié)點(diǎn)上
kubectl label node henry002 name=ingress?
`先下載mandatory.yaml文件翁逞,下載地址:https://github.com/kubernetes/ingress-nginx/blob/nginx-0.20.0/deploy/mandatory.yaml溉仑,并對(duì)mandatory.yaml并進(jìn)行修改浊竟,如下:
# 使用HostPort方式運(yùn)行振定,需要增加配置
hostNetwork: true? #使用hostport
? ? ? nodeSelector:?
? ? ? ? name: ingress? #指定節(jié)點(diǎn)
# 搜索nodeSelector,并且要確保henry002節(jié)點(diǎn)上的80和443端口沒(méi)有被占用梳庆,鏡像拉取需要較長(zhǎng)的時(shí)間卑惜,這塊要特別注意一下
#運(yùn)行mandatory.yaml
kubectl apply -f mandatory.yaml?
#查看ingress-nginx命名空間下的資源
kubectl get all -n ingress-nginx
(2)查看henry002的80和443端口
lsof -i tcp:80
lsof -i tcp:443
(3)創(chuàng)建tomcat的pod和service
記得將之前的tomcat刪除:kubectl delete -f my-tomcat.yaml
vim tomcat.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
? name: tomcat-deployment
? labels:
? ? app: tomcat
spec:
? replicas: 3
? selector:
? ? matchLabels:
? ? ? app: tomcat
? template:
? ? metadata:
? ? ? labels:
? ? ? ? app: tomcat
? ? spec:
? ? ? containers:
? ? ? - name: tomcat
? ? ? ? image: tomcat
? ? ? ? ports:
? ? ? ? - containerPort: 8080
---
apiVersion: v1
kind: Service
metadata:
? name: tomcat-service
spec:
? ports:
? - port: 80?
? ? protocol: TCP
? ? targetPort: 8080
? selector:
? ? app: tomcat
`執(zhí)行yaml文件`
[root@henry001 network]# kubectl apply -f tomcat-ingress.yaml
deployment.apps/tomcat-deployment created
service/tomcat-service created
`查看service`
[root@henry001 network]# kubectl get svc
NAME? ? ? ? ? ? ? ? TYPE? ? ? ? CLUSTER-IP? ? ? EXTERNAL-IP? PORT(S)? ? ? ? ? AGE
kubernetes? ? ? ? ? ClusterIP? 10.96.0.1? ? ? ? <none>? ? ? ? 443/TCP? ? ? ? ? 43h
tomcat-service? ? ? ClusterIP? 10.101.231.253? <none>? ? ? ? 80/TCP? ? ? ? ? 20s
whoami-deployment? NodePort? ? 10.103.129.91? ? <none>? ? ? ? 8000:31999/TCP? 150m
`查看pod`
[root@henry001 network]# kubectl get pods
NAME? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? READY? STATUS? ? RESTARTS? AGE
tomcat-deployment-6b9d6f8547-8wxgx? 1/1? ? Running? 0? ? ? ? ? 42s
tomcat-deployment-6b9d6f8547-hrhrr? 1/1? ? Running? 0? ? ? ? ? 42s
tomcat-deployment-6b9d6f8547-p7zhz? 1/1? ? Running? 0? ? ? ? ? 42s
kubectl get svc
kubectl get pods
(4)創(chuàng)建Ingress以及定義轉(zhuǎn)發(fā)規(guī)則
1>創(chuàng)建 nginx-ingress.yaml文件
vim? nginx-ingress.yaml
#ingress
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
? name: nginx-ingress
spec:
? rules:
? - host: tomcat.henry.com
? ? http:
? ? ? paths:
? ? ? - path: /
? ? ? ? backend:
? ? ? ? ? serviceName: tomcat-service
? ? ? ? ? servicePort: 80
2>創(chuàng)建ingress并查看
`創(chuàng)建ingress`
[root@henry001 network]# kubectl apply -f nginx-ingress.yaml
ingress.extensions/nginx-ingress created
`查看ingress`
[root@henry001 network]# kubectl get ingress
NAME? ? ? ? ? ? HOSTS? ? ? ? ? ? ? ADDRESS? PORTS? AGE
nginx-ingress? tomcat.henry.com? ? ? ? ? ? 80? ? ? 47s
`查看ingress詳細(xì)信息`
[root@henry001 network]# kubectl describe ingress nginx-ingress
Name:? ? ? ? ? ? nginx-ingress
Namespace:? ? ? ? default
Address:? ? ? ? ?
Default backend:? default-http-backend:80 (<none>)
Rules:
? Host? ? ? ? ? ? ? Path? Backends
? ----? ? ? ? ? ? ? ----? --------
? tomcat.henry.com?
? ? ? ? ? ? ? ? ? ? /? tomcat-service:80 (192.168.217.22:8080,192.168.254.215:8080,192.168.254.216:8080)
Annotations:
? kubectl.kubernetes.io/last-applied-configuration:? {"apiVersion":"extensions/v1beta1","kind":"Ingress","metadata":{"annotations":{},"name":"nginx-ingress","namespace":"default"},"spec":{"rules":[{"host":"tomcat.henry.com","http":{"paths":[{"backend":{"serviceName":"tomcat-service","servicePort":80},"path":"/"}]}}]}}
Events:
? Type? ? Reason? Age? From? ? ? ? ? ? ? ? ? ? ? Message
? ----? ? ------? ----? ----? ? ? ? ? ? ? ? ? ? ? -------
? Normal? CREATE? 53s? nginx-ingress-controller? Ingress default/nginx-ingress
(5)修改win的hosts文件膏执,添加dns解析
182.92.105.161 tomcat.henry.com
(6)打開瀏覽器,訪問(wèn)tomcat.henry.com
總結(jié):如果以后想要使用Ingress網(wǎng)絡(luò)露久,其實(shí)只要定義ingress更米,service和pod即可,前提是要保證nginx ingress controller已經(jīng)配置好了毫痕。