MongoDB 在 Minikube 上开始使用 Kompose 时停止访问

MongoDB stopped being accessible when started using Kompose on Minikube

我有一个简单的应用程序,它使用 MongoDB docker 图像调用 url:mongodb://mongo:27017/。使用 docker-compose:

工作正常

docker-compose.yml

version: "3.3"
services:
  mongo:
    image: mongo
    restart: always
    container_name: mongo
    volumes:
      - /data/mongodb:/data/db
    ports:
     - "30001:27017"
  myapp:
    image: myapp:0.1.0
    restart: always
    links:
     - mongo # link this service to the database service
    depends_on:
     - mongo
networks:
  default:

然后,我开始使用 Minikube(在 VMWare 之上)迁移到 Kubernetes,生成以下清单 YAML:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  creationTimestamp: null
  labels:
    name: mongo-claim0
  name: mongo-claim0
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 100Mi
status: {}
---
apiVersion: v1
kind: Service
metadata:
  creationTimestamp: null
  labels:
    name: mongo
  name: mongo
spec:
  ports:
  - name: "30001"
    port: 30001
    targetPort: 27017
  selector:
    name: mongo
status:
  loadBalancer: {}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    name: mongo
  name: mongo
spec:
  replicas: 1
  strategy:
    type: Recreate
  template:
    metadata:
      creationTimestamp: null
      labels:
        name: mongo
    spec:
      containers:
      - image: mongo
        name: mongo
        ports:
        - containerPort: 27017
        resources: {}
        volumeMounts:
        - mountPath: /data/db
          name: mongo-claim0
      restartPolicy: Always
      volumes:
      - name: mongo-claim0
        persistentVolumeClaim:
          claimName: mongo-claim0
status: {}
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    name: myapp
  name: myapp
spec:
  replicas: 1
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        name: myapp
    spec:
      containers:
        image: myapp:0.1.0
        name: myapp
        resources: {}
      restartPolicy: Always
status: {}

我的 PODs/services 运行 很好 (kubectl get pods,svc):

NAME                                 READY   STATUS             RESTARTS   AGE
pod/mongo-5d89cc6f7f-c2p4b           1/1     Running            0          43m
pod/myapp-6c9c4f89fb-gg68t           1/1     Running            0          43m

NAME                    TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)           AGE
service/kubernetes      ClusterIP   10.96.0.1        <none>        443/TCP           110m
service/mongo           NodePort    10.101.179.59    <none>        27017:30001/TCP   43m

我也无法通过 Robo3T(MongoDB 客户端)连接端口 30001,因为我可以使用 docker-compose(即使使用 vboxnet0 IP 192.168.99.1)。

我不知道可能是什么问题。

更新 1:我发现 Kompose 创建了一个 ClusterIP 并将集群内部的端口转移到 30001,但这需要是从集群外部连接的端口。我将服务更改为 NodePort,如下所示:

apiVersion: v1
kind: Service
metadata:
  labels:
    name: mongo
  name: mongo
spec:
  type: NodePort 
  ports:
    - port: 27017
      nodePort: 30001
      targetPort: 27017
  selector:
    name: mongo
status:
  loadBalancer: {}

现在 myapp pod 可以连接到 mongo,但我仍然无法使用 Robo3t 通过 VMWare IP (192.168.99.1) 和端口 30001 连接到集群。

要使其正常工作,您必须调用 minikube service <servicename>,在我的例子中,服务名称是 mongo。