0

我尝试在 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),但我总是得到一个“坏网关”。

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

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: {}

入口.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

4

1 回答 1

1

问题出在 Dockerfile 中:

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

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

于 2021-06-28T21:23:44.057 回答