gpt4 book ai didi

kubernetes - rfc7231#section-6.5.1 Kubernetes上的dotnet核心入口 Controller api访问问题

转载 作者:行者123 更新时间:2023-12-02 11:51:51 26 4
gpt4 key购买 nike

我已经在Kubernetes中部署了一个简单的dotnet核心应用程序。公开的服务如下

apiVersion: v1
kind: Service
metadata:
creationTimestamp: "2020-01-17T18:07:23Z"
labels:
app.kubernetes.io/instance: expo-api
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: expo-api
app.kubernetes.io/version: 0.0.4
helm.sh/chart: expo-api-0.0.4
name: expo-api-service
namespace: default
resourceVersion: "997971"
selfLink: /api/v1/namespaces/default/services/expo-api-service
uid: 144b9d1d-87d2-4096-9851-9563266b2099
spec:
clusterIP: 10.12.0.122
ports:
- name: http
port: 80
protocol: TCP
targetPort: http
selector:
app.kubernetes.io/instance: expo-api
app.kubernetes.io/name: expo-api
sessionAffinity: None
type: ClusterIP
status:
loadBalancer: {}

我正在使用的入口 Controller 是nginx入口 Controller ,简单的入口规则设置如下-
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: "true"
creationTimestamp: "2020-01-17T18:07:24Z"
generation: 3
labels:
app.kubernetes.io/instance: expo-api
app.kubernetes.io/managed-by: Helm
app.kubernetes.io/name: expo-api
app.kubernetes.io/version: 0.0.4
helm.sh/chart: expo-api-0.0.4
name: expo-api
namespace: default
resourceVersion: "1004650"
selfLink: /apis/extensions/v1beta1/namespaces/default/ingresses/expo-api
uid: efef4e15-ed0a-417f-8b34-4e0f46cb1e70
spec:
rules:
- http:
paths:
- backend:
serviceName: expo-api-service
servicePort: 80
path: /expense
status:
loadBalancer:
ingress:
- ip: 34.70.45.62

启动简单的dotnet核心应用-
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}

public IConfiguration Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
}

// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
}

这是入口输出-
Name:             expo-api
Namespace: default
Address: 34.70.45.62
Default backend: default-http-backend:80 (10.8.0.9:8080)
Rules:
Host Path Backends
---- ---- --------
*
/expense expo-api-service:80 (10.8.0.26:80,10.8.0.27:80,10.8.1.14:80)
Annotations:
kubernetes.io/ingress.class: nginx
nginx.ingress.kubernetes.io/use-regex: true
Events: <none>

以下是Nginx入口 Controller 的设置-
Name:                     nginx-nginx-ingress-controller
Namespace: default
Labels: app=nginx-ingress
chart=nginx-ingress-1.29.2
component=controller
heritage=Helm
release=nginx
Annotations: <none>
Selector: app=nginx-ingress,component=controller,release=nginx
Type: LoadBalancer
IP: 10.12.0.107
LoadBalancer Ingress: 34.66.164.70
Port: http 80/TCP
TargetPort: http/TCP
NodePort: http 30144/TCP
Endpoints: 10.8.1.6:80
Port: https 443/TCP
TargetPort: https/TCP
NodePort: https 30469/TCP
Endpoints: 10.8.1.6:443
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>

问题是,当我将入口规则路径更改为仅 /并使用- curl 34.66.164.70/weatherforecast进行访问时,它可以很好地工作。

但是,当我将入口路径更改为 /expense并尝试使用-curl 34.66.164.70/expense/weatherforecast进行访问时。输出错误为-
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|4dec8cf0-4fddb4d168cb9569.",
"errors": {
"id": [
"The value 'weatherforecast' is not valid."
]
}
}

我无法理解这背后的问题是什么。它是从dotnet核心还是Kubernetes出现的?如果是dotnet,可能是什么分辨率,如果在Kubernetes上,则是预期的分辨率。

最佳答案

更新了1

我在您的Ingress对象中看不到nginx.ingress.kubernetes.io/rewrite-target批注。无法说出是否有意跳过了它。

如果不存在此注释,则您的应用程序会收到“GET:/ expense / weatherforecast”。如果这是您想要的,一切都会很好。但是,如果您希望您的应用收到“GET:/ weatherforecast”,则应在您的Ingress注释中添加nginx.ingress.kubernetes.io/rewrite-target: /

更新了2

Ingress-nginx文档包含有关“重写”注释的文章:
https://kubernetes.github.io/ingress-nginx/examples/rewrite/#rewrite-target

有一个非常简洁的示例,可以帮助您了解如何公开/expense/weatherforecast端点。但不幸的是,我也无法实现/expense公开。我尝试了更复杂的正则表达式,并尝试了该文章中的“app-root”注释,但没有任何效果-Ingress始终返回404的/expense endpoinnt。

我找不到有关如何处理根端点和相对端点的任何有用信息,因此我开始即兴创作。我发现您可以使用两个支持的路径来使其工作。不知道这是否是错误或功能:)。

遵循Ingress规范对我的测试应用程序非常有效。它可以正确处理/expense/expense/weatherforecast

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
annotations:
kubernetes.io/ingress.class: "nginx"
# nginx.ingress.kubernetes.io/use-regex: "true" # can be true or false, no matter
nginx.ingress.kubernetes.io/rewrite-target: "/$1"
name: app
spec:
rules:
- http:
paths:
- backend:
serviceName: app
servicePort: 80
path: "/expense/(.+)" # handle relative path
- backend:
serviceName: app
servicePort: 80
path: "/expense" # handle root

关于kubernetes - rfc7231#section-6.5.1 Kubernetes上的dotnet核心入口 Controller api访问问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59798898/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com