【问题标题】:cannot connect to mongo-express behind ingress k8s无法连接到入口 k8s 后面的 mongo-express
【发布时间】:2021-05-06 01:17:08
【问题描述】:

我正在尝试创建以下内容:

  1. Deployment 为 mongo
  2. Deployment 用于 mongo-express
  3. ClusterIp for mongo
  4. ClusterIp 用于 mongo-express
  5. 将请求路由到 mongo-express 的 Ingress 服务

我希望能够转到xyz.com/admin/auth-db-gui 并查看 mongo-express gui。

我在 Linux minikube 上运行它。

当转到 xyz.com/admin/auth-db-gui 时,我得到 503 Service Temporarily Unavailable,但是在执行 kubectl get pods 时,我可以看到 2 个 Pod 正在运行。

我将在/etc/hosts 中手动设置xyz.com 的映射,因为这仅用于开发目的

db.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-db-deployment
spec:
  selector:
    matchLabels:
      app: auth-db
  template:
    metadata:
      labels:
        app: auth-db
    spec:
      containers:
        - name: auth-db
          image: mongo
---
apiVersion: v1
kind: Service
metadata:
  name: auth-db-service
spec:
  selector:
    app: auth-db
  ports:
    - name: auth-db
      protocol: TCP
      port: 27017
      targetPort: 27017

db-gui

apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-db-gui-deployment
spec:
  selector:
    matchLabels:
      app: auth-db-gui
  template:
    metadata:
      labels:
        app: auth-db-gui
    spec:
      containers:
        - name: auth-db-gui
          image: mongo-express
          env:
            - name: ME_CONFIG_MONGODB_SERVER
              value: auth-db-service
---
apiVersion: v1
kind: Service
metadata:
  name: auth-db-gui-service
spec:
  selector:
    app: auth-db-gui
  ports:
    - name: auth-db-gui
      protocol: TCP
      port: 27017
      targetPort: 27017

ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: xyz-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - host: xyz.com
      http:
        paths:
          - path: /admin/auth-db-gui
            backend:
              serviceName: auth-db-gui-service
              servicePort: 8081

如果上面有明显的错误,请见谅。

【问题讨论】:

  • 你得到了什么确切的错误?你在哪个平台上工作?你在使用具体的教程吗?
  • 感谢我更新了问题! 1. 503 服务在访问 url 时暂时不可用 2. linux minikube 3. 不,我没有使用特定的教程
  • 您是否尝试在 Ingress yaml 文件中添加 27017 服务端口而不是 8081?
  • 我试过了,但它对我没有用......但如果这有所作为......实际上 27017 就像两者的“pod”端口,而 8081 就像“入口端口”对吗?

标签: mongodb kubernetes kubernetes-ingress mongo-express


【解决方案1】:

我注意到some cases 503 表示端口配置正确。

我已经测试了你的入口,以及服务和部署,在纠正入口对象中的端口错误后,它工作得很好:

curl -H "Host:  xyz.com"  "192.168.49.2/admin/auth-db-gui"   
{
  "path": "/admin/auth-db-gui",
  "headers": {
    "host": "xyz.com",
    "x-request-id": "ff272df6d729af6c1fb4d5f510de88f4",
    "x-real-ip": "192.168.49.1",
    "x-forwarded-for": "192.168.49.1",
    "x-forwarded-host": "xyz.com",
    "x-forwarded-port": "80",
    "x-forwarded-proto": "http",
    "x-scheme": "http",
    "user-agent": "curl/7.52.1",
    "accept": "*/*"
  },
  "method": "GET",
  "body": "",
  "fresh": false,
  "hostname": "xyz.com",
  "ip": "192.168.49.1",
  "os": {
    "hostname": "auth-db-gui-deployment-555c77cf75-fjbf2"

出于测试目的,我强烈推荐mendhak/http-https-echo。我交换了您部署的图像并修复了端口。您可以使用以下 yaml 文件自行测试入口:

#ingress.yaml

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: xyz-ingress
  annotations:
    kubernetes.io/ingress.class: nginx
    nginx.ingress.kubernetes.io/use-regex: "true"
spec:
  rules:
    - host: xyz.com
      http:
        paths:
          - path: /admin/auth-db-gui
            backend:
              serviceName: auth-db-gui-service
              servicePort: 27017

注意端口的设置方式。入口中的servicePort 对应于服务port,即27017。我更改了服务的目标端口,因为echo-server 在80 上工作

#sevice.yaml

apiVersion: v1
kind: Service
metadata:
  name: auth-db-gui-service
spec:
  selector:
    app: auth-db-gui
  ports:
    - name: auth-db-gui
      protocol: TCP
      port: 27017
      targetPort: 80
#deployment.yaml
 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: auth-db-gui-deployment
spec:
  selector:
    matchLabels:
      app: auth-db-gui
  template:
    metadata:
      labels:
        app: auth-db-gui
    spec:
      containers:
        - name: auth-db-gui
          image: mendhak/http-https-echo

请查看有关 services 的文档以获取更多示例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多