k8s部署Ingress并創(chuàng)建規(guī)則的詳細(xì)介紹
一、Ingress介紹
Ingress由兩部分組成:Ingress controller和Ingress服務(wù)- 通過(guò) Service 發(fā)現(xiàn) Pod 進(jìn)行關(guān)聯(lián)?;谟蛎L問(wèn)
- 通過(guò) Ingress Controller 實(shí)現(xiàn) Pod 負(fù)載均衡
- 支持 TCP/UDP 4 層負(fù)載均衡和 HTTP 7 層負(fù)載均衡
底層實(shí)現(xiàn)是nignx


二、Ingress文件獲取
1.1 、說(shuō)明
官方地址:https://github.com/kubernetes/ingress-nginx
部署文件說(shuō)明
## 這個(gè)地址被墻,需要科學(xué)上網(wǎng)!??! # mandatory.yaml為ingress所有資源yml文件的集合 # 若是單獨(dú)部署,需要分別下載configmap.yaml、namespace.yaml、rbac.yaml、service-nodeport.yaml、with-rbac.yaml wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/mandatory.yaml # service-nodeport.yaml為ingress通過(guò)nodeport對(duì)外提供服務(wù),注意默認(rèn)nodeport暴露端口為隨機(jī),可以編輯該文件自定義端口 wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/master/deploy/static/provider/baremetal/service-nodeport.yaml
1.2 、文件內(nèi)容
ingress-controller.yaml
apiVersion: v1
kind: Namespace
metadata:
name: ingress-nginx
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
---
kind: ConfigMap
name: nginx-configuration
namespace: ingress-nginx
name: tcp-services
name: udp-services
kind: ServiceAccount
name: nginx-ingress-serviceaccount
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
name: nginx-ingress-clusterrole
rules:
- apiGroups:
- ""
resources:
- configmaps
- endpoints
- nodes
- pods
- secrets
verbs:
- list
- watch
- get
- services
- "extensions"
- ingresses
- events
- create
- patch
- ingresses/status
- update
kind: Role
name: nginx-ingress-role
- namespaces
resourceNames:
# Defaults to "<election-id>-<ingress-class>"
# Here: "<ingress-controller-leader>-<nginx>"
# This has to be adapted if you change either parameter
# when launching the nginx-ingress-controller.
- "ingress-controller-leader-nginx"
kind: RoleBinding
name: nginx-ingress-role-nisa-binding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: Role
subjects:
- kind: ServiceAccount
name: nginx-ingress-serviceaccount
namespace: ingress-nginx
kind: ClusterRoleBinding
name: nginx-ingress-clusterrole-nisa-binding
kind: ClusterRole
apiVersion: apps/v1
kind: DaemonSet
name: nginx-ingress-controller
spec:
selector:
matchLabels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
template:
metadata:
labels:
app.kubernetes.io/name: ingress-nginx
app.kubernetes.io/part-of: ingress-nginx
annotations:
prometheus.io/port: "10254"
prometheus.io/scrape: "true"
spec:
hostNetwork: true
serviceAccountName: nginx-ingress-serviceaccount
containers:
- name: nginx-ingress-controller
image: siriuszg/nginx-ingress-controller:0.20.0
args:
- /nginx-ingress-controller
- --configmap=$(POD_NAMESPACE)/nginx-configuration
- --tcp-services-configmap=$(POD_NAMESPACE)/tcp-services
- --udp-services-configmap=$(POD_NAMESPACE)/udp-services
- --publish-service=$(POD_NAMESPACE)/ingress-nginx
- --annotations-prefix=nginx.ingress.kubernetes.io
securityContext:
allowPrivilegeEscalation: true
capabilities:
drop:
- ALL
add:
- NET_BIND_SERVICE
# www-data -> 33
runAsUser: 33
env:
- name: POD_NAME
valueFrom:
fieldRef:
fieldPath: metadata.name
- name: POD_NAMESPACE
fieldPath: metadata.namespace
ports:
- name: http
containerPort: 80
- name: https
containerPort: 443
livenessProbe:
failureThreshold: 3
httpGet:
path: /healthz
port: 10254
scheme: HTTP
initialDelaySeconds: 10
periodSeconds: 10
successThreshold: 1
timeoutSeconds: 10
readinessProbe:
kind: Service
#type: NodePort
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
- name: https
port: 443
targetPort: 443三、k8s應(yīng)用 ingress-controller.yaml
應(yīng)用
kubectl apply -f ingress-controller.yaml
如圖:

查看是否完成
kubectl get pods --all-namespaces

四、創(chuàng)建Ingress規(guī)則
如果沒(méi)有部署和服務(wù)==>查看k8s部署并映射tomcat8
查看自己創(chuàng)建的服務(wù),以tomcat8為例
kubectl get all
自己創(chuàng)建文件 ingress-tomcat8.yaml
vi ingress-tomcat8.yaml
ingress-tomcat8.yaml規(guī)則內(nèi)容
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: web
spec:
rules:
- host: k8s.tomcat8.com
http:
paths:
- backend:
serviceName: tomcat8
servicePort: 80如圖:

五、應(yīng)用文件
kubectl apply -f ingress-tomcat8.yaml

六、訪問(wèn)
1.本地測(cè)試用域名會(huì)訪問(wèn)不到,所以要配置下本地hosts文件
hosts文件位置:C:\Windows\System32\drivers\etc\HOSTS
如圖:

不帶端口訪問(wèn),直接使用域名

使用ip端口訪問(wèn)的效果

到此這篇關(guān)于k8s部署Ingress并創(chuàng)建規(guī)則的文章就介紹到這了,更多相關(guān)k8s部署Ingress內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
k8s?pod和service網(wǎng)絡(luò)暴露詳解
這篇文章主要介紹了借助iptables的路由轉(zhuǎn)發(fā)功能,打通k8s集群內(nèi)的pod和service網(wǎng)絡(luò),與外部網(wǎng)絡(luò)聯(lián)通,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-11-11
Podman開(kāi)機(jī)自啟容器實(shí)現(xiàn)過(guò)程及與Docker對(duì)比
這篇文章主要為大家介紹了Podman開(kāi)機(jī)自啟容器實(shí)現(xiàn)過(guò)程,通過(guò)示例代碼的形式進(jìn)行演繹過(guò)程,有需要的朋友可以參考下,希望可以有所幫助2021-09-09
ES業(yè)務(wù)數(shù)據(jù)遷移遇到的精度問(wèn)題BUG
這篇文章主要為大家介紹了ES業(yè)務(wù)數(shù)據(jù)遷移遇到的BUG精度問(wèn)題,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06
K8s Pod調(diào)度機(jī)制詳解(從理論到生成實(shí)戰(zhàn)指南)
Kubernetes調(diào)度機(jī)制是集群的智能調(diào)度中樞,主要完成過(guò)濾和打分兩個(gè)決策,在生產(chǎn)環(huán)境中,核心調(diào)度策略包括資源調(diào)度、親和性調(diào)度、污點(diǎn)與容忍、拓?fù)浞植技s束等,本文介紹K8s Pod調(diào)度機(jī)制詳解(從理論到生成實(shí)戰(zhàn)指南),感興趣的朋友一起看看吧2025-03-03
Kubernetes實(shí)現(xiàn)CI與CD配置教程
這篇文章主要為大家介紹了基于Kubernetes實(shí)現(xiàn)CI與CD配置教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
IoT邊緣集群Kubernetes?Events告警通知實(shí)現(xiàn)示例
這篇文章主要為大家介紹了IoT邊緣集群Kubernetes?Events告警通知實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-02-02

