将blazor服务器应用程序部署到Kubernetes时出现问题



在彻底清除Kubernetes后使用新数据进行编辑

最近,我试图在docker桌面上运行的本地托管Kubernetes实例上测试部署Blazor服务器应用程序。

我设法在容器中正确地启动了应用程序,应用了迁移等,日志告诉我应用程序正在运行并等待。

在Docker Desktop中使用Reset Kubernetes Kluster重置Kubernetes后所采取的步骤:

  • 修改hosts文件以包含127.0.0.1 scp.com

  • 为mssql 添加了包含密钥的秘密

  • 使用kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.0/deploy/static/provider/cloud/deploy.yaml安装Ngnix控制器

  • 应用的本地卷声明-local-pvc.yaml

    apiVersion: v1
    kind: PersistentVolumeClaim
    metadata:
    name: mssql-claim
    spec:
    accessModes:
    - ReadWriteMany
    resources:
    requests:
    storage: 250Mi
    
  • 应用mssql实例和集群ip-mssql-scanapp-depl.yaml

    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: mssql-depl
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: mssql
    template:
    metadata:
    labels:
    app: mssql
    spec:
    containers:
    - name: mssql
    image: mcr.microsoft.com/mssql/server:2019-latest
    ports:
    - containerPort: 1433
    env:
    - name: MSSQL_PID
    value: "Express"
    - name: ACCEPT_EULA
    value: "Y"
    - name: SA_PASSWORD
    valueFrom:
    secretKeyRef:
    name: mssql
    key: SA_PASSWORD
    volumeMounts:
    - mountPath: /var/opt/mssql/data
    name: mssqldb
    volumes:
    - name: mssqldb
    persistentVolumeClaim:
    claimName: mssql-claim
    ---
    apiVersion: v1
    kind: Service
    metadata:
    name: mssql-clusterip-srv
    spec:
    type: ClusterIP
    selector:
    app: mssql
    ports:
    - name: mssql
    protocol: TCP
    port: 1433
    targetPort: 1433
    ---
    apiVersion: v1
    kind: Service
    metadata:
    name: mssql-loadbalancer
    spec:
    type: LoadBalancer
    selector:
    app: mssql
    ports:        
    - protocol: TCP
    port: 1433
    targetPort: 1433
    
  • 应用Blazor应用程序和集群ip-scanapp-depl.yaml:

    apiVersion: apps/v1
    kind: Deployment
    metadata:
    name: scanapp-depl
    spec:
    replicas: 1
    selector:
    matchLabels:
    app: scanapp
    template:
    metadata:
    labels:
    app: scanapp
    spec:
    containers:
    - name: scanapp
    image: scanapp:1.0
    ---
    apiVersion: v1
    kind: Service
    metadata:
    name: scanapp-clusterip-srv
    spec:
    type: ClusterIP
    selector:
    app: scanapp
    ports:
    - name: ui
    protocol: TCP
    port: 8080
    targetPort: 80
    - name: ui2
    protocol: TCP
    port: 8081
    targetPort: 443
    - name: scanapp0
    protocol: TCP
    port: 5000
    targetPort: 5000
    - name: scanapp1
    protocol: TCP
    port: 5001
    targetPort: 5001
    - name: scanapp5
    protocol: TCP
    port: 5005
    targetPort: 5005
    
  • 应用入口-ingress-srv.yaml:

    apiVersion: networking.k8s.io/v1
    kind: Ingress
    metadata:
    name: ingress-srv
    annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /    
    nginx.ingress.kubernetes.io/affinity: "cookie"
    nginx.ingress.kubernetes.io/session-cookie-name: "affinity"
    nginx.ingress.kubernetes.io/session-cookie-expires: "14400"
    nginx.ingress.kubernetes.io/session-cookie-max-age: "14400"
    spec:
    ingressClassName: nginx
    rules:
    - host: scp.com
    http:
    paths:
    - path: /
    pathType: Prefix
    backend:
    service:
    name: scanapp-clusterip-srv
    port:
    number: 8080
    

所有这些之后,Blazor应用程序启动良好,连接到mssql实例,种子数据库并等待客户端。日志如下:

[15:18:53 INF]正在启动
[15:18:53 WRN]在目录'/root/.aspnet中存储密钥/"DataProtection Keys",可能不会在容器外部持久化。当容器被销毁时,受保护的数据将不可用
[15:18:55 INF]AuthorizationPolicy配置已启动
[15:18:55 INF]已成功配置策略"LocationMustBeSady"。[15:18:55 INF]AuthorizationPolicy配置完成。[15:18:55 INF]现在收听:http://[::]:80[15:18:55 INF]应用程序已启动。按Ctrl+C关闭。[15:18:55 INF]托管环境:docker[15:18:55 INF]内容根路径:/app

如开头所述-出于所有人的喜爱,我无法从浏览器进入我的blazor应用程序-我尝试过:

  • scp.com
  • scp.com:8080
  • scp.com:5000
  • scp.com:5001
  • scp.com:5005

此外,kubectl get ingress现在不再像以前那样显示ADDRESS值,kubectl get services现在表示mssql-loadbalanceringress-nginx-controllerEXTERNAL-IP的挂起-此文章末尾的详细日志

似乎什么都不起作用,所以我的配置文件一定有问题,我不知道可能是什么。另外,请注意,这次没有配置NodePort

此外,Blazor应用程序的Dockerfile:

# https://hub.docker.com/_/microsoft-dotnet
FROM mcr.microsoft.com/dotnet/sdk:5.0 AS build
WORKDIR /source
EXPOSE 5000
EXPOSE 5001
EXPOSE 5005
EXPOSE 80
EXPOSE 443

LABEL name="ScanApp"

# copy csproj and restore as distinct layers
COPY ScanApp/*.csproj ScanApp/
COPY ScanApp.Application/*.csproj ScanApp.Application/
COPY ScanApp.Common/*.csproj ScanApp.Common/
COPY ScanApp.Domain/*.csproj ScanApp.Domain/
COPY ScanApp.Infrastructure/*.csproj ScanApp.Infrastructure/
COPY ScanApp.Tests/*.csproj ScanApp.Tests/

Run ln -sf /usr/share/zoneinfo/posix/Europe/Warsaw /etc/localtime
RUN dotnet restore ScanApp/ScanApp.csproj

# copy and build app and libraries
COPY ScanApp/ ScanApp/
COPY ScanApp.Application/ ScanApp.Application/
COPY ScanApp.Common/ ScanApp.Common/
COPY ScanApp.Domain/ ScanApp.Domain/
COPY ScanApp.Infrastructure/ ScanApp.Infrastructure/
COPY ScanApp.Tests/ ScanApp.Tests/
WORKDIR /source/ScanApp
RUN dotnet build -c release --no-restore

# test stage -- exposes optional entrypoint
# target entrypoint with: docker build --target test
FROM build AS test
WORKDIR /source/ScanApp.Tests
COPY tests/ .
ENTRYPOINT ["dotnet", "test", "--logger:trx"]

FROM build AS publish
RUN dotnet publish -c release --no-build -o /app

# final stage/image
FROM mcr.microsoft.com/dotnet/aspnet:5.0
WORKDIR /app
COPY --from=publish /app .
ENV ASPNETCORE_ENVIRONMENT="docker"
ENTRYPOINT ["dotnet", "ScanApp.dll"]

kubectl输出

kubectl get ingress输出:

主机地址端口年龄scp.com//tr>
NAMEclass
入口srvnginx8035m

完全重置KUBERNETES后,此解决方案不起作用

将重新编辑主要问题
离职供将来使用

我解决了这个问题,或者至少我认为是这样。
除了@moonkote建议将ingressClassName: nginx添加到ingress-srv.yaml之外,我还更改了入口端口配置,使其现在指向端口80

由于这些更改,使用scp.com现在可以正确打开我的应用程序。此外,使用NodePort访问,我可以使用localhost:30080访问我的应用程序,其中30080端口是自动设置的(我从scanapp-np-srv.yaml中删除了nodePort配置行(

如果clusterIp配置状态为将端口8080设置为目标端口80,为什么ingress-srv.yaml中的端口必须设置为80?我不知道,我不完全理解Kubernetes配置的内部工作原理。欢迎所有解释

主要配置文件的当前状态:

ingress-srv.yaml:

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: ingress-srv
annotations:  
nginx.ingress.kubernetes.io/affinity: "cookie"
nginx.ingress.kubernetes.io/session-cookie-name: "affinity"
nginx.ingress.kubernetes.io/session-cookie-expires: "14400"
nginx.ingress.kubernetes.io/session-cookie-max-age: "14400"
spec:
ingressClassName: nginx
rules:
- host: scp.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: scanapp-clusterip-srv
port:
number: 80



scanapp-np-srv.yaml:

apiVersion: v1
kind: Service
metadata:
name: scanappnpservice-srv
spec:
type: NodePort
selector:
app: scanapp
ports:    
- name: ui
port: 8080
targetPort: 80
- name: scanapp0
protocol: TCP
port: 5000
targetPort: 5000
- name: scanapp1
protocol: TCP
port: 5001
targetPort: 5001
- name: scanapp5
protocol: TCP
port: 5005
targetPort: 5005



scanapp-depl.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
name: scanapp-depl
spec:
replicas: 1
selector:
matchLabels:
app: scanapp
template:
metadata:
labels:
app: scanapp
spec:
containers:
- name: scanapp
image: scanapp:1.0
---
apiVersion: v1
kind: Service
metadata:
name: scanapp-clusterip-srv
spec:
type: ClusterIP
selector:
app: scanapp
ports:
- name: ui
protocol: TCP
port: 8080
targetPort: 80
- name: ui2
protocol: TCP
port: 8081
targetPort: 443
- name: scanapp0
protocol: TCP
port: 5000
targetPort: 5000
- name: scanapp1
protocol: TCP
port: 5001
targetPort: 5001
- name: scanapp5
protocol: TCP
port: 5005
targetPort: 5005

其余文件保持原样。

首先,您不需要NodePort服务,只需要ClusterIP。如果您想在不经过入口控制器的情况下直接访问服务,则会使用节点端口。

如果你想用NodePort测试这一点,你需要定义一个,例如

- name: ui
port: 8080
targetPort: 80
nodePort: 38080  # needs to be higher than 30000

但看看你的进入规则,它似乎有点偏离。我会尝试以下方法:

spec:
rules:
- host: scp.com
http:
paths:
- path: /
backend:
serviceName: scanapp-clusterip-srv
servicePort: ui

相关内容

  • 没有找到相关文章

最新更新