我已经在 Kubernetes 集群上部署了一个 C# api
据我所知,我们应该有:GET http 请求 -> Node(30000) -> Pod(80) -> C# API(8080)
我的 docker 镜像暴露了 8080 端口
FROM our-registry/dotnet/sdk:8.0 AS build
WORKDIR /app
# Copy the project file and restore any dependencies (use .csproj for the project name)
COPY MyApi/MyApi/*.csproj ./
RUN dotnet restore
# Copy the rest of the application code
COPY MyApi/MyApi/. ./
# Publish the application
ARG BUILD_CONFIG=Release
RUN echo "Building with configuration: ${BUILD_CONFIG}"
RUN dotnet publish -c ${BUILD_CONFIG} -o out
# Build the runtime image
FROM our-registry/dotnet/aspnet:8.0 AS runtime
WORKDIR /app
COPY --from=build /app/out ./
# Expose the port your application will run on
EXPOSE 8080
# Start the application
ENTRYPOINT ["dotnet", "MyApi.dll"]
我的 K8 api-service.yaml 设置如下
apiVersion: v1
kind: Service
metadata:
name: my-api-service
namespace: somenamespace
spec:
selector:
app: my-api
ports:
- protocol: TCP
port: 80
targetPort: 8080
nodePort: 30000
type: NodePort
我的 C# API 启动设置设置了端口8080 ,并且它在该端口上的本地调试/发布中运行良好
{
"profiles": {
"http": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
...
},
"dotnetRunMessages": true,
"applicationUrl": "http://localhost:8080"
},
"https": {
"commandName": "Project",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
...
},
"dotnetRunMessages": true,
"applicationUrl": "https://localhost:7084;http://localhost:8080"
},
...
在集群上,服务运行
kubectl get svc -n somenamespace
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
my-api-service NodePort 10.*.*.* <none> 80:30000/TCP 3h56m
在那个吊舱里
kubectl get pods -n somenamespace -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
my-apipod-*********-***** 1/1 Running 0 138m 10.*.*.* somenode <none> <none>
我检查了吊舱内部
kubectl exec -it my-apipod-*********-***** -n somenamespace -- bash
...
root@my-apipod-*********-*****:/app# netstat -tulnp | grep LISTEN
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 1/dotnet
获取节点
kubectl get nodes -o wide
NAME STATUS ROLES AGE VERSION INTERNAL-IP EXTERNAL-IP OS-IMAGE KERNEL-VERSION CONTAINER-RUNTIME
somenode Ready worker 108d v1.28.11+rke2r1 192.168.1.23 192.168.1.23 Ubuntu 20.04.6 LTS 5.4.0-200-generic containerd://1.7.17-k3s1
我尝试使用 nodePort 30000 连接到该节点的 IP
curl -X GET http://192.168.1.23:30000/swagger/index.html -v
Note: Unnecessary use of -X or --request, GET is already inferred.
* Trying 192.168.1.23:30000...
* Connected to 192.168.1.23 (192.168.1.23) port 30000 (#0)
> GET /swagger/index.html HTTP/1.1
> Host: 192.168.1.23:30000
> User-Agent: curl/7.81.0
> Accept: */*
>
* Recv failure: Connection reset by peer
* Closing connection 0
curl: (56) Recv failure: Connection reset by peer
我不确定还要检查什么,我在本地运行了我的 api 代码(.NET),并且 swagger 可以正常工作并且可以访问
感谢您的帮助
[编辑]
当我在浏览器中执行如下请求时:
http://192.168.1.23:30000
http://192.168.1.23:30000/swagger/index.html
日志中没有任何内容
kubectl logs -f -n mynamespace my-apipod-*********-*****
warn: Microsoft.AspNetCore.Hosting.Diagnostics[15]
Overriding HTTP_PORTS '8080' and HTTPS_PORTS ''. Binding to values defined by URLS instead 'http://0.0.0.0:8080'.
info: Microsoft.Hosting.Lifetime[14]
Now listening on: http://0.0.0.0:8080
info: Microsoft.Hosting.Lifetime[0]
Application started. Press Ctrl+C to shut down.
info: Microsoft.Hosting.Lifetime[0]
Hosting environment: Production
info: Microsoft.Hosting.Lifetime[0]
Content root path: /app
如果我尝试 https 就会出现这种情况
warn: Microsoft.AspNetCore.HttpsPolicy.HttpsRedirectionMiddleware[3]
Failed to determine the https port for redirect.
所以 API 似乎运行良好