我使用kubeadm
创建了一个kubernetes集群。服务声明为ClusterIP
。目前,我正试图部署我的应用程序作为类型loadbalancer
与Metallb的入口,但我面临一些问题。如果我将应用部署为入口,则找不到一些jv和css组件。将应用程序作为服务运行没有问题,但在使用Ingress时出现了问题。它是一个ASP。. NET Core应用程序
My Ingress来源:
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
annotations:
nginx.ingress.kubernetes.io/rewrite-target: /
name: taco-ingress
spec:
rules:
- host: tasty.taco.com
http:
paths:
- path: /web1
pathType: Prefix
backend:
service:
name: web1
port:
number: 80
- path: /web2
pathType: Prefix
backend:
service:
name: web2
port:
number: 80
My Deployment来源:
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web1
labels:
app: taco
taco: web1
spec:
replicas: 2
selector:
matchLabels:
app: taco
task: web1
template:
metadata:
labels:
app: taco
task: web1
version: v0.0.1
spec:
containers:
- name: taco
image: yatesu0x00/webapplication1:ingress-v1
ports:
- containerPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: web2
labels:
app: taco
taco: web2
spec:
replicas: 2
selector:
matchLabels:
app: taco
task: web2
template:
metadata:
labels:
app: taco
task: web2
version: v0.0.1
spec:
containers:
- name: taco
image: yatesu0x00/webapplication2:ingress-v1
ports:
- containerPort: 80
My Service来源:
---
apiVersion: v1
kind: Service
metadata:
name: web1
spec:
ports:
- targetPort: 80
port: 80
selector:
app: taco
task: web1
---
apiVersion: v1
kind: Service
metadata:
name: web2
spec:
ports:
- targetPort: 80
port: 80
selector:
app: taco
task: web2
应用的html文件:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Epic Website - (⌐■_■)</title>
<link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="/css/site.css" />
</head>
<body>
<header>
<nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
<div class="container">
<a class="navbar-brand" href="/">Home</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="navbar-brand" href="/Home2">Home2</a>
</li>
<li class="nav-item">
<a class="navbar-brand" href="/Home/ItWorks">Click me!</a>
</li>
</ul>
</div>
</div>
</nav>
</header>
<div class="container">
<main role="main" class="pb-3">
<h2>Want this taco?</h2>
<pre>
{__/}
( ●.●)
( >🌮
</pre>
</main>
</div>
<footer class="border-top footer text-muted">
<div class="container">
© 2020 - <a href="/Home/Privacy">Privacy</a>
</div>
</footer>
<script src="/lib/jquery/dist/jquery.min.js"></script>
<script src="/lib/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/darkmode-js@1.5.7/lib/darkmode-js.min.js"></script>
<script src="/js/site.js?v=8ZRc1sGeVrPBx4lD717BgRaQekyh78QKV9SKsdt638U"></script>
</body>
</html>
如果我在浏览器中打开控制台,我可以看到<script>
类型的所有元素上都有404 not found
。
如果你仔细查看日志,你可以发现问题的原因是你的应用程序在路径tasty.taco.com/css/site.css
中请求静态内容(例如css/site.css
文件),并且由于入口控制器在其定义中没有前缀/css
的定义,它返回404错误代码。
静态内容在以下路径tasty.taco.com/web1/css/site.css
中可用-注意,我使用了web1
前缀,因此Ingress知道将此请求重定向到哪个服务。
一般来说,在请求静态内容的应用程序中使用注释nginx.ingress.kubernetes.io/rewrite-target
经常会导致这样的问题。
修复不是在你的例子中,使用UsePathBaseMiddleware
类来设置基础URL。
表示从请求路径中提取指定路径基并将其延迟到请求路径基的中间件。
关于详细步骤,我建议按照这个答案中给出的步骤操作。