ingress实现同一域名加不同上下文实现两个服务的代理
创始人
2025-06-01 03:11:21
0

fes.test.com访问fes-pc服务

fes.test.com/mobile访问fes-web服务

  1. 准备工作:先准备两个前端,用nginx镜像发布

  • fes-pc.yaml

apiVersion: v1
kind: ConfigMap
metadata:name: nginx-cmnamespace: dev
data:app.conf: |server {listen       80;server_name  fes.test.com;location / {root   /opt/application/fes-pc/;index  index.html index.htm;}}
---
apiVersion: apps/v1
kind: Deployment
metadata:name: fes-pcnamespace: dev
spec:replicas: 1selector:matchLabels:app: fes-pctemplate:metadata:labels:app: fes-pcspec:containers:- name: nginximage: mynginx:1.17.1imagePullPolicy: IfNotPresentports:- containerPort: 80volumeMounts:- mountPath: /opt/application/fes-pc/name: fes-pc-app- mountPath: /etc/nginx/conf.d/name: appconfvolumes:- name: fes-pc-apphostPath:path: /opt/application/fes-pc/- name: appconfconfigMap:name: nginx-cm
---
apiVersion: v1
kind: Service
metadata:name: fes-pc-svcnamespace: dev
spec:selector:app: fes-pctype: ClusterIPports:- port: 80targetPort: 80

fes-web.yaml

apiVersion: v1
kind: ConfigMap
metadata:name: nginx-cmnamespace: dev
data:app.conf: |server {listen       80;server_name  localhost;location /mobile {alias   /opt/application/fes-web/;index  index.html index.htm;}}
---
apiVersion: apps/v1
kind: Deployment
metadata:name: fes-webnamespace: dev
spec:replicas: 1selector:matchLabels:app: fes-webtemplate:metadata:labels:app: fes-webspec:containers:- name: nginximage: mynginx:1.17.1imagePullPolicy: IfNotPresentports:- containerPort: 80volumeMounts:- mountPath: /opt/application/fes-web/name: fes-web-app- mountPath: /etc/nginx/conf.d/name: appconfvolumes:- name: fes-web-apphostPath:path: /opt/application/fes-web/- name: appconfconfigMap:name: nginx-cm
---
apiVersion: v1
kind: Service
metadata:name: fes-web-svcnamespace: dev
spec:selector:app: fes-webtype: ClusterIPports:- port: 80targetPort: 80

本机curl测试

[root@master fesapp]# kubectl get pod -n dev -o wide
NAME                      READY   STATUS    RESTARTS   AGE   IP            NODE     NOMINATED NODE   READINESS GATES
fes-pc-679bf5c875-bl7gt   1/1     Running   0          98m   10.244.0.61   master              
fes-web-987bfb95b-k559k   1/1     Running   0          8s    10.244.0.65   master              [root@master fesapp]# kubectl get svc -n dev -o wide
NAME          TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE   SELECTOR
fes-pc-svc    ClusterIP   10.102.47.17            80/TCP    99m   app=fes-pc
fes-web-svc   ClusterIP   10.99.243.248           80/TCP    53s   app=fes-web[root@master fesapp]# curl 10.102.47.17
[root@master fesapp]# curl 10.99.243.248/mobile
  1. ingress部署(上网随便找的yaml文件)

[root@master fesapp]# kubectl apply -f mandatory.yaml
apiVersion: v1
kind: Namespace
metadata:name: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx---kind: ConfigMap
apiVersion: v1
metadata:name: nginx-configurationnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx---
kind: ConfigMap
apiVersion: v1
metadata:name: tcp-servicesnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx---
kind: ConfigMap
apiVersion: v1
metadata:name: udp-servicesnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx---
apiVersion: v1
kind: ServiceAccount
metadata:name: nginx-ingress-serviceaccountnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRole
metadata:name: nginx-ingress-clusterrolelabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
rules:- apiGroups:- ""resources:- configmaps- endpoints- nodes- pods- secretsverbs:- list- watch- apiGroups:- ""resources:- nodesverbs:- get- apiGroups:- ""resources:- servicesverbs:- get- list- watch- apiGroups:- ""resources:- eventsverbs:- create- patch- apiGroups:- "extensions"- "networking.k8s.io"resources:- ingressesverbs:- get- list- watch- apiGroups:- "extensions"- "networking.k8s.io"resources:- ingresses/statusverbs:- update---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: Role
metadata:name: nginx-ingress-rolenamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
rules:- apiGroups:- ""resources:- configmaps- pods- secrets- namespacesverbs:- get- apiGroups:- ""resources:- configmapsresourceNames:# Defaults to "-"# Here: "-"# This has to be adapted if you change either parameter# when launching the nginx-ingress-controller.- "ingress-controller-leader-nginx"verbs:- get- update- apiGroups:- ""resources:- configmapsverbs:- create- apiGroups:- ""resources:- endpointsverbs:- get---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: RoleBinding
metadata:name: nginx-ingress-role-nisa-bindingnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: nginx-ingress-role
subjects:- kind: ServiceAccountname: nginx-ingress-serviceaccountnamespace: ingress-nginx---
apiVersion: rbac.authorization.k8s.io/v1beta1
kind: ClusterRoleBinding
metadata:name: nginx-ingress-clusterrole-nisa-bindinglabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
roleRef:apiGroup: rbac.authorization.k8s.iokind: ClusterRolename: nginx-ingress-clusterrole
subjects:- kind: ServiceAccountname: nginx-ingress-serviceaccountnamespace: ingress-nginx---apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-ingress-controllernamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
spec:replicas: 1selector:matchLabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginxtemplate:metadata:labels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginxannotations:prometheus.io/port: "10254"prometheus.io/scrape: "true"spec:# wait up to five minutes for the drain of connectionsterminationGracePeriodSeconds: 300serviceAccountName: nginx-ingress-serviceaccountnodeSelector:kubernetes.io/os: linuxcontainers:- name: nginx-ingress-controllerimage: quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.30.0args:- /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.iosecurityContext:allowPrivilegeEscalation: truecapabilities:drop:- ALLadd:- NET_BIND_SERVICE# www-data -> 101runAsUser: 101env:- name: POD_NAMEvalueFrom:fieldRef:fieldPath: metadata.name- name: POD_NAMESPACEvalueFrom:fieldRef:fieldPath: metadata.namespaceports:- name: httpcontainerPort: 80protocol: TCP- name: httpscontainerPort: 443protocol: TCPlivenessProbe:failureThreshold: 3httpGet:path: /healthzport: 10254scheme: HTTPinitialDelaySeconds: 10periodSeconds: 10successThreshold: 1timeoutSeconds: 10readinessProbe:failureThreshold: 3httpGet:path: /healthzport: 10254scheme: HTTPperiodSeconds: 10successThreshold: 1timeoutSeconds: 10lifecycle:preStop:exec:command:- /wait-shutdown---apiVersion: v1
kind: LimitRange
metadata:name: ingress-nginxnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
spec:limits:- min:memory: 90Micpu: 100mtype: Container[root@master fesapp]# kubectl apply -f service-nodeport.yaml
apiVersion: v1
kind: Service
metadata:name: ingress-nginxnamespace: ingress-nginxlabels:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
spec:type: NodePortports:- name: httpport: 80targetPort: 80nodePort: 30080 protocol: TCP- name: httpsport: 443targetPort: 443nodePort: 30443protocol: TCPselector:app.kubernetes.io/name: ingress-nginxapp.kubernetes.io/part-of: ingress-nginx
  1. 定义ingress策略

[root@master fesapp]# kubectl apply -f ingress-http.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:name: ingress-httpnamespace: dev
spec:rules:- host: fes.test.comhttp:paths:- path: /backend:serviceName: fes-pc-svcservicePort: 80- path: /mobilebackend:serviceName: fes-web-svcservicePort: 80

本地修改hosts,浏览器访问测试

注意事项:

  • 在ingress生效之前,需要先将应用部署完成

  • ingress中path的定义,需要与后端真实service提供的path一致,否则将被转发到一个不存在的path上引发错误

相关内容

热门资讯

AWSECS:访问外部网络时出... 如果您在AWS ECS中部署了应用程序,并且该应用程序需要访问外部网络,但是无法正常访问,可能是因为...
AWSElasticBeans... 在Dockerfile中手动配置nginx反向代理。例如,在Dockerfile中添加以下代码:FR...
银河麒麟V10SP1高级服务器... 银河麒麟高级服务器操作系统简介: 银河麒麟高级服务器操作系统V10是针对企业级关键业务...
北信源内网安全管理卸载 北信源内网安全管理是一款网络安全管理软件,主要用于保护内网安全。在日常使用过程中,卸载该软件是一种常...
AWR报告解读 WORKLOAD REPOSITORY PDB report (PDB snapshots) AW...
AWS管理控制台菜单和权限 要在AWS管理控制台中创建菜单和权限,您可以使用AWS Identity and Access Ma...
​ToDesk 远程工具安装及... 目录 前言 ToDesk 优势 ToDesk 下载安装 ToDesk 功能展示 文件传输 设备链接 ...
群晖外网访问终极解决方法:IP... 写在前面的话 受够了群晖的quickconnet的小水管了,急需一个新的解决方法&#x...
不能访问光猫的的管理页面 光猫是现代家庭宽带网络的重要组成部分,它可以提供高速稳定的网络连接。但是,有时候我们会遇到不能访问光...
Azure构建流程(Power... 这可能是由于配置错误导致的问题。请检查构建流程任务中的“发布构建制品”步骤,确保正确配置了“Arti...