无法使用Service和Ingress向外部公开pod

时间:2019-11-23 11:43:04

标签: mongodb kubernetes kubernetes-ingress kubernetes-pod kubernetes-service

我为mongodb创建了一个pod,部署文件如下所示:

永久数量声明定义为:

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: mongodb-data-claim
  annotations:
    volume.beta.kubernetes.io/storage-class: vmfs1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi

状态部署yaml:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mongodb
spec:
  serviceName: "mongodb"
  selector:
    matchLabels:
      app: mongodb
  replicas: 1
  template:
    metadata:
      labels:
        app: mongodb
    spec:
      volumes:
       - name: mongodb-data
         persistentVolumeClaim:
           claimName: mongodb-data-claim
      containers:
        - name: mongodb       
          image: imagefromInternalRepo/mongo:4.0.12
          command:
            - mongod
            - --bind_ip
            - 0.0.0.0
          volumeMounts:
             - name: mongodb-data
               mountPath: /data/db
          ports:
            - containerPort: 27017

服务Yaml是:

apiVersion: v1
kind: Service
metadata:
  name: mongodb
  labels:
    app: mongodb
spec:
  type: NodePort
  ports:
    - port: 27017
      targetPort: 27017
      nodePort: 32017
      protocol: TCP
      name: http
  selector:
    app: mongodb

入口yaml是:

apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress-mongodb
spec:
  rules:
    - host: "bb-mongo-db.namespace.com"
      http:
        paths:
          - path: /
            backend:
              serviceName: mongodb
              servicePort: 32017
  tls:
    - hosts:
      - "bb-mongo-db.namespace.com"

吊舱已启动并正在运行。 但是当我尝试点击bb-mongo-db.namespace.com时,我得到了503:service 不可用。

我对此并不陌生。请帮助我。

1 个答案:

答案 0 :(得分:0)

如果您正在使用Nginx Ingress,则它不支持开箱即用的TCP / UDP服务,您正尝试通过无法访问Http的方式访问MongoDB,以便为可以使用的Nginx Ingress配置TCP / UDP解决方法:

https://kubernetes.github.io/ingress-nginx/user-uide/exposing-tcp-udp-services/

https://github.com/nginxinc/kubernetes-ingress/tree/master/examples/tcp-udp