我有一个简单的应用程序,该应用程序使用MongoDB docker映像调用URL:mongodb:// mongo:27017 /。使用docker-compose可以正常工作:
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: {}
我的POD /服务运行正常(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连接到群集。
答案 0 :(得分:0)
要使其正常运行,您必须致电minikube service <servicename>
,在我的情况下,服务名称为mongo。