kubernetes之ingress部署


創建nginx服務
[root@k8s-master k8s-yaml]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
selector:
matchLabels:
app: nginx
replicas: 2 # tells deployment to run 2 pods matching the template
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: registry.cn-shanghai.aliyuncs.com/jovi/nginx:alpine
ports:
- containerPort: 80

 

 

 

執行命令
kubectl apply -f nginx-deployment.yaml

創建service本地復制均衡
[root@k8s-master k8s-yaml]# cat nginx-service.yaml
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
type: ClusterIP
clusterIP: 10.0.171.239 #手動分配的集群IP
type: NodePort #將復制均衡IP自動暴露到本地均衡跟下面的nodePort 端口對應
selector:
app: nginx #對應之前的 nginx-deployment.yaml 應用
ports:
- protocol: TCP
port: 8084 # port 80 是集群分配的Ip
targetPort: 80 # targetPort 是后端節點IP
nodePort: 30030 #映射的是物理機的Ip
執行命令
kubectl apply -f nginx-service.yaml

 


在本地做host解析
windows 或linux hosts
192.168.66.5 my-service
訪問: http://my-service:30030/

 

 


創建ingress
[root@k8s-master k8s-yaml]# cat nginx-ingress.yaml
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
name: my-ingress-for-nginx # Ingress 的名字,僅用於標識
spec:
rules: # Ingress 中定義 L7 路由規則
- host: a.demo.kuboard.cn # 根據 virtual hostname 進行路由(請使用您自己的域名)
http:
paths: # 按路徑進行路由
- path: /
backend:
serviceName: my-service # 指定后端的 Service 為之前創建的 nginx-service
servicePort: 80

執行命令
kubectl apply -f nginx-ingress.yaml
查看生成的應用

 

 


在本地做host解析
windows 或linux hosts
192.168.66.5 k8s-master foo.bar.com my-service a.demo.kuboard.cn

訪問
curl http://a.demo.kuboard.cn:30030

 

 

總結:
1、創建一個deployment資源用於運行nginx,pod副本為3個;
2、為后端的nginx pod創建service服務,名稱為myapp-ingress-svc
3、創建ingress規則,通過ingress把后端的pod暴露出去
注意:這里對於為nginx創建的svc服務,並沒有起到調度作用,只是收集pod資源,用於分類。因為實際調度過程中,流量是直接通過ingress規則調度到后端的pod,而沒有經過svc服務,svc只是提供一個收集pod的作用。

報錯1:

 

 


為什么會找不到:官網給出的解釋: Note: Depending on the Ingress controller you are using, you may need to create a default-http-backend Service.
Default Backend
An Ingress with no rules sends all traffic to a single default backend. The default backend is typically a configuration option of the Ingress controller and is not specified in your Ingress resources.
If none of the hosts or paths match the HTTP request in the Ingress objects, the traffic is routed to your default backend.

翻譯:
控制器。注意:根據您正在使用的Ingress控制器,您可能需要創建一個default-http-backend服務。
違約后端
沒有規則的入口將所有流量發送到一個默認后端。默認后端通常是Ingress控制器的一個配置選項,在您的Ingress資源中沒有指定。
如果Ingress對象中的主機或路徑都不匹配HTTP請求,則流量將被路由到默認后端。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM