Kubernetes中的角度和dotnetcore连接抛出404 [英] Angular and dotnetcore connection in Kubernetes throw 404

查看:28
本文介绍了Kubernetes中的角度和dotnetcore连接抛出404的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为此苦苦挣扎了几天,但找不到任何解决办法.

这些是我的文件:部署:

  • 数据库:

  apiVersion:apps/v1种类:部署元数据:名称:dbdeployment标签:应用:dbdemployment规格:选择器:matchLabels:应用:dbdeployment复制品:1模板:元数据:名称:dbdeployment标签:应用:dbdeployment规格:容器:-名称:dbcontainer图像:docker.io/library/sc_db:clusterimagePullPolicy:从不端口:-containerPort:5432 

  • 后端:

  apiVersion:apps/v1种类:部署元数据:名称:就业标签:应用程序:部署规格:选择器:matchLabels:应用程序:部署复制品:1模板:元数据:名称:就业标签:应用程序:部署规格:容器:-名称:apicontainer图像:docker.io/library/sc_api:clusterimagePullPolicy:从不端口:-containerPort:5000 

  • 前端:

  apiVersion:apps/v1种类:部署元数据:名称:webdeployment标签:应用程式:webdeployment规格:选择器:matchLabels:应用程式:webdeployment复制品:1模板:元数据:名称:webdeployment标签:应用程式:webdeployment规格:容器:-名称:webcontainer图像:docker.io/library/sc_web:clusterimagePullPolicy:从不端口:-containerPort:80生命周期:preStop:执行:命令:["/usr/sbin/nginx",-s",退出"] 

服务:

  • 数据库:

  apiVersion:v1种类:服务元数据:名称:dbservice标签:应用程序:dbservice规格:选择器:应用:dbdeployment端口:-协议:TCP端口:5432targetPort:5432 

  • 后端:

  apiVersion:v1种类:服务元数据:名称:apiservice标签:应用:apiservice规格:选择器:应用程序:部署端口:-协议:TCP港口:5000targetPort:5000 

  • 前端(服务公开为NodePort-30002):

 版本:v1种类:服务元数据:名称:webservice标签:应用:网络服务规格:选择器:应用程式:webdeployment端口:-名称:http协议:TCP端口:80targetPort:80nodePort:30002类型:NodePort 

部署工作正常:

  NAME READY STATUS重新启动年龄apideployment-9 ... 1/1跑步0 7m50sdbdeployment-5 ... 1/1运行中0 7m49swebdeployment-54 ... 1/1运行0 7m48s 

服务还:

 名称类型集群IP外部IP端口年龄apiservice ClusterIP 10.105.32.96< none>5000/TCP 11分钟dbservice ClusterIP 10.105.121.156< none>5432/TCP 11分钟kubernetes ClusterIP 10.96.0.1< none>443/TCP 13d网络服务NodePort 10.99.221.170< none>80:30002/TCP 11分钟 

服务已链接:

  • appsettings.json中的ConnectionString包含:

  ...;服务器= dbservice;端口= 5432; ... 

  • nginx默认在代理通过中包含apiservice的名称:

 服务器{听80;server_name scserver;地点/{根目录/usr/share/nginx/html;try_files $ uri/index.html;}位置/api {proxy_pass http://apiservice:5000;proxy_set_header升级$ http_upgrade;proxy_set_header连接保持活动;proxy_set_header主机$ host;proxy_cache_bypass $ http_upgrade;}} 

一切看起来都不错,但是当我尝试登录时我得到了404:

我还检查了日志:

  kubectl记录webdeployment-5 ... 

我得到了:(错误标记为黄色)

  [错误] 29#29:* 1 open()"/usr/share/nginx/html/api/Account/Login" 

这是否意味着代理无法正常工作?

根据: https://appdevmusings.com/azure-kubernetes-service-aks-deploying-angular-asp-net-core-and-sql-server-on-linux 我添加到Angular环境中:

  ApplicationConfig:{'API_URL':'http://apiservice:5000/api'} 

但这可能不是问题.

也许有人知道什么是不正确的.

解决方案

有时answear比我们想象的要近.在花了几个小时解决这个问题之后(我尝试了nginx Ingress-它也可以工作,但是现在我更喜欢其他解决方案),我回到了最简单的例子:将前端连接到后端但这一次,我实际上执行了与示例中相同的步骤,并且有效.

这是我的dockerfile:

  FROM nginxCOPY WEB/dist/usr/share/nginx/html运行rm/etc/nginx/conf.d/default.conf复制WEB/proxy.conf/etc/nginx/conf.dCOPY WEB/cert/etc/nginx/cert展览80 

如示例所示,我删除了etc/nginx/conf.d/default.conf并将其替换为我自己的文件(而不是将配置保留在启用站点的文件夹中):

 上游后端{服务器apiservice;}服务器 {听80;地点/{根目录/usr/share/nginx/html;index index.html index.htm;try_files $ uri $ uri//index.html = 404;}位置/api {proxy_pass http://后端;}} 

I struggle several days with that, and I can't find any solution.

These are my files: Deployments:

  • database:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: dbdeployment
  labels:
    app: dbdemployment
spec:
  selector:
    matchLabels:
      app: dbdeployment
  replicas: 1
  template:
    metadata:
      name: dbdeployment
      labels:
        app: dbdeployment
    spec:
      containers:
      - name: dbcontainer
        image: docker.io/library/sc_db:cluster
        imagePullPolicy: Never
        ports:
        - containerPort: 5432

  • backend:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: apideployment
  labels:
    app: apideployment
spec:
  selector:
    matchLabels:
      app: apideployment
  replicas: 1
  template:
    metadata:
      name: apideployment
      labels:
        app: apideployment
    spec:
      containers:
      - name: apicontainer
        image: docker.io/library/sc_api:cluster
        imagePullPolicy: Never
        ports:
        - containerPort: 5000

  • frontend:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: webdeployment
  labels:
    app: webdeployment
spec:
  selector:
    matchLabels:
      app: webdeployment
  replicas: 1
  template:
    metadata:
      name: webdeployment
      labels:
        app: webdeployment
    spec:
      containers:
      - name: webcontainer
        image: docker.io/library/sc_web:cluster
        imagePullPolicy: Never
        ports:
          - containerPort: 80
        lifecycle:
          preStop:
            exec:
              command: ["/usr/sbin/nginx","-s","quit"]

Services:

  • database:

apiVersion: v1
kind: Service
metadata:
  name: dbservice
  labels:
    app: dbservice
spec:
  selector:
    app: dbdeployment
  ports:
  - protocol: TCP
    port: 5432
    targetPort: 5432

  • backend:

apiVersion: v1
kind: Service
metadata:
  name: apiservice
  labels:
    app: apiservice
spec:
  selector:
    app: apideployment
  ports:
  - protocol: TCP
    port: 5000
    targetPort: 5000

  • frontend (service is exposed as NodePort - 30002):

Version: v1
kind: Service
metadata:
  name: webservice
  labels:
    app: webservice
spec:
  selector:
    app: webdeployment
  ports:
  - name: http
    protocol: TCP
    port: 80
    targetPort: 80
    nodePort: 30002
  type: NodePort

Deployments are working:

NAME             READY   STATUS    RESTARTS   AGE
apideployment-9...   1/1     Running   0       7m50s   
dbdeployment-5...    1/1     Running   0       7m49s   
webdeployment-54...   1/1     Running   0      7m48s  

Services also:

NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
apiservice   ClusterIP   10.105.32.96     <none>        5000/TCP       11m
dbservice    ClusterIP   10.105.121.156   <none>        5432/TCP       11m
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        13d
webservice   NodePort    10.99.221.170    <none>        80:30002/TCP   11m

Services are linked:

  • ConnectionString in appsettings.json contains:

...;Server=dbservice;Port=5432;...

  • nginx default contains name of apiservice in proxy pass:

server {
        listen 80;
        server_name scserver;
        location / {
                root /usr/share/nginx/html;
                try_files $uri /index.html;
        }
        location /api {
                proxy_pass http://apiservice:5000;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection keep-alive;
                proxy_set_header Host $host;
                proxy_cache_bypass $http_upgrade;
        }
}

As Connecting to API with Angular Webapp using Minikube and Kubernetes suggests I run:

kubectl -exec -ti webdeployment-5... -- bash

and then curl to service:

curl -v apiservice

command returned ip of apiservice:

 Expire in 3 ms for 1 (transfer 0x55922301af50)
*   Trying 10.105.32.96...
* TCP_NODELAY set

I checked my node IP:

kubectl get node -o wide

And when I hit NodeIP:NodePort I got login page (frontend files are loaded)

Everything looks good, but when i try login i got 404:

I also checked logs:

kubectl logs webdeployment-5...

And I got this: (error is marked yellow color)

[error] 29#29: *1 open() "/usr/share/nginx/html/api/Account/Login"

Does it mean that proxy doesn't work properly?

According to: https://appdevmusings.com/azure-kubernetes-service-aks-deploying-angular-asp-net-core-and-sql-server-on-linux I added to Angular environment:

ApplicationConfig: {
    'API_URL': 'http://apiservice:5000/api'
    }

but it's propably not the issue.

Maybe someone know what isn't correct.

解决方案

Sometimes answear is closer than we think. After hours spent on solving this issue (I tried nginx Ingress - it also works, but for now I prefer different solution) I went back to simpliest exampe: connect frontend to backend But this time I did literally the same steps as in example, and it works.

This is my dockerfile:

FROM nginx
COPY WEB/dist /usr/share/nginx/html
RUN rm /etc/nginx/conf.d/default.conf
COPY WEB/proxy.conf /etc/nginx/conf.d
COPY WEB/cert /etc/nginx/cert
EXPOSE 80

As the example suggests I removed etc/nginx/conf.d/default.conf and replaced it by my own file (instead keep my configuration in sites-enabled folder):

upstream Backend {
    server apiservice;
}

server {
    listen 80;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
        try_files $uri $uri/ /index.html =404;
    }

    location /api {
        proxy_pass http://Backend;
    }
}

这篇关于Kubernetes中的角度和dotnetcore连接抛出404的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆