使用 Kubernetes 设置的 Rails 应用中的错误网关

Bad Gateway in Rails app with Kubernetes setup

我尝试在 Kubernetes 集群中设置一个 Rails 应用程序(这是在我的本地计算机上使用 k3d 创建的。

k3d cluster create --api-port 6550 -p "8081:80@loadbalancer" --agents 2

kubectl create deployment nginx --image=nginx

kubectl create service clusterip nginx --tcp=80:80

# apiVersion: networking.k8s.io/v1beta1 # for k3s < v1.19
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: nginx
  annotations:
    ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: nginx
            port:
              number: 80

我可以获得一个 Ingress 运行,它正确地公开了 运行 Nginx 部署(“欢迎使用 nginx!”) (我从这里拿了这个例子:https://k3d.io/usage/guides/exposing_services/

所以我知道我的设置工作正常(使用 nginx)。

现在,我只是想将该入口指向我的 Rails 应用程序,但我总是得到一个“错误的网关”。 (我也尝试指向我的其他服务(elasticsearch、kibana、pgadminer),但我总是得到一个“错误的网关”。

我可以在 (http://localhost:62333/) 查看我的 Rails 应用 运行 我的 Dockerfile 的最后几行:

EXPOSE 3001:3001
CMD rm -f tmp/pids/server.pid && bundle exec rails s -b 0.0.0.0 -p 3001

为什么我的 API 有“坏网关”而 Nginx 没有?

这与我通过 kompose convert 创建的选择器和标签有关吗?

这是我完整的 Rails-API 部署:

kubectl apply -f api-deployment.yml -f api.service.yml -f ingress.yml

api-deployment.yml

apiVersion: apps/v1
kind: Deployment
metadata:
  annotations:
    kompose.cmd: kompose convert -c --file metashop-backend/docker-compose.yml --file metashop-backend/docker-compose.override.yml
    kompose.version: 1.22.0 (HEAD)
  creationTimestamp: null
  labels:
    io.kompose.service: api
  name: api
spec:
  replicas: 1
  selector:
    matchLabels:
      io.kompose.service: api
  strategy:
    type: Recreate
  template:
    metadata:
      annotations:
        kompose.cmd: kompose convert -c --file metashop-backend/docker-compose.yml --file metashop-backend/docker-compose.override.yml
        kompose.version: 1.22.0 (HEAD)
      creationTimestamp: null
      labels:
        io.kompose.network/metashop-net: 'true'
        io.kompose.service: api
    spec:
      containers:
        - env:
            - name: APPLICATION_URL
              valueFrom:
                configMapKeyRef:
                  key: APPLICATION_URL
                  name: env
            - name: DEBUG
              value: 'true'
            - name: ELASTICSEARCH_URL
              valueFrom:
                configMapKeyRef:
                  key: ELASTICSEARCH_URL
                  name: env
          image: metashop-backend-api:DockerfileJeanKlaas
          name: api
          ports:
            - containerPort: 3001
          resources: {}
      #     volumeMounts:
      #       - mountPath: /usr/src/app
      #         name: api-claim0
      # restartPolicy: Always
      # volumes:
      #   - name: api-claim0
      #     persistentVolumeClaim:
      #       claimName: api-claim0
status: {}

api-service.yml

apiVersion: v1
kind: Service
metadata:
  annotations:
    kompose.cmd: kompose convert -c --file metashop-backend/docker-compose.yml --file metashop-backend/docker-compose.override.yml
    kompose.version: 1.22.0 (HEAD)
  creationTimestamp: null
  labels:
    app: api
    io.kompose.service: api
  name: api
spec:
  type: ClusterIP
  ports:
    - name: '3001'
      protocol: TCP
      port: 3001
      targetPort: 3001
  selector:
    io.kompose.service: api
status:
  loadBalancer: {}

ingress.yml

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: api
  annotations:
    ingress.kubernetes.io/ssl-redirect: "false"
spec:
  rules:
  - http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service:
            name: api
            port:
              number: 3001

configmap.yml

apiVersion: v1
data:
  APPLICATION_URL: localhost:3001
  ELASTICSEARCH_URL: elasticsearch
  RAILS_ENV: development
  RAILS_MAX_THREADS: '5'
kind: ConfigMap
metadata:
  creationTimestamp: null
  labels:
    io.kompose.service: api-env
  name: env

希望我没有遗漏任何东西。

提前谢谢你

编辑:添加端点,在评论中请求:

种类:端点

kind: Endpoints
apiVersion: v1
metadata:
  name: api
  namespace: default
  labels:
    app: api
    io.kompose.service: api
  selfLink: /api/v1/namespaces/default/endpoints/api
subsets:
  - addresses:
      - ip: 10.42.1.105
        nodeName: k3d-metashop-cluster-server-0
        targetRef:
          kind: Pod
          namespace: default
          apiVersion: v1
    ports:
      - name: '3001'
        port: 3001
        protocol: TCP

问题出在 Dockerfile 中:

我没有定义 ENV RAILS_LOG_TO_STDOUT true,所以我在 pod 日志中看不到任何错误。

添加 ENV RAILS_LOG_TO_STDOUT true 后,我看到了类似 database xxxx does not exist

的错误