如何从Minikube连接到在本地主机上运行的MongoDB [英] How to Connect to MongoDB running on localhost from Minikube
问题描述
我正在了解Kubernetes,我正尝试从docker-compose迁移为使用Kubernetes,但在连接到在本地主机上运行的MongoDB时遇到了问题.
I am learning about Kubernetes am trying move from docker-compose to use Kubernetes but having problem with connecting to MongoDB running on localhost.
通过'network_mode: "host"'
//docker-compose.yaml
version: '3'
services:
eureka:
image: sage-eureka
ports:
- "8080:8080"
network_mode: "host"
但是我在Kubernetes中遇到问题.我使用了 Kompose 转换docker-compose.
But I'm having issues in Kubernetes. I used Kompose to convert the docker-compose.
//application.properties
spring.data.mongodb.host=localhost (tried adding mongo instead of localhost when used ExternalName and ClusterIP)
spring.data.mongodb.port=27017
spring.data.mongodb.database=MyMongo
// eureka-service.yaml
apiVersion: v1
kind: Service
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.21.0 (992df58d8)
creationTimestamp: null
labels:
io.kompose.service: eureka
name: eureka
spec:
ports:
- name: "8080"
port: 8080
targetPort: 8080
- name: "27017"
port: 27017
targetPort: 27017
selector:
io.kompose.service: eureka
status:
loadBalancer: {}
//eureka-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.21.0 (992df58d8)
creationTimestamp: null
labels:
io.kompose.service: eureka
name: eureka
spec:
replicas: 1
selector:
matchLabels:
io.kompose.service: eureka
strategy: {}
template:
metadata:
annotations:
kompose.cmd: kompose convert
kompose.version: 1.21.0 (992df58d8)
creationTimestamp: null
labels:
io.kompose.service: eureka
spec:
hostNetwork: true
dnsPolicy: Default
containers:
- args:
- --spring.profiles.active=local
image: sage-eureka
imagePullPolicy: Never
name: sageeureka
ports:
- containerPort: 8080
- containerPort: 27017
resources: {}
restartPolicy: Always
serviceAccountName: ""
volumes: null
status: {}
当我向LoadBalencer添加类型时,我也获得了外部IP地址,并且我还尝试为Mongo创建服务类型ExternalName和NodePort,如
I'm also getting on external ip address when I've added type to LoadBalencer and I've also tried creating Service type ExternalName and NodePort for Mongo as shown in Kubernetes best practices: mapping external services:
eureka ClusterIP 10.102.22.91 <none> 8080/TCP,27017/TCP 45h
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 4d2h
mongo ExternalName <none> host.docker.internal <none> 45h
我在这里已经提到了类似的帖子,但是我不知道到底需要做什么,而这些帖子也对我不起作用,因为我不知道自己在做什么错了:
I've referred to similar posts here but I don't know exactly what needs to be done and following these post are also not working for me as I don't know what I'm doing wrong:
- com.mongodb.MongoSocketOpenException:异常打开套接字(MongoDB,Docker)
- 在使用Kubernetes进行本地开发期间/minikube,我应该如何连接到在本地主机上运行的postgres数据库?
- com.mongodb.MongoSocketOpenException: Exception opening socket(MongoDB, Docker)
- During local development with Kubernetes/minikube, how should I connect to postgres database running on localhost?
我也尝试像第二篇文章一样创建mongo-serivce:
I also tried creating the mongo-serivce as in 2nd post like this:
kind: Service
apiVersion: v1
metadata:
name: mongo
spec:
type: ExternalName
externalName: host.docker.internal
请帮助!.谢谢.
推荐答案
如何在Minikube中从主机连接到Pod
如果问题是您无法通过Minikube访问Pod(通过service
).
If the issue is that you can't access your Pods (via the service
) on Minikube.
让我们先检查一下Service
.
$ kubectl get service eureka
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
eureka LoadBalancer 10.104.196.32 <pending> 8080:30167/TCP 25m
请注意,运行Minikube时,LoadBalancer
服务不会获取外部IP地址.
Note that, when running Minikube, a LoadBalancer
service won’t acquire an external IP address.
在本地,Minikube不与其他服务集成以运行外部负载平衡器.
Locally, Minikube doesn’t integrate with another service to run external load balancers.
但是在大多数其他环境中,当您使用Kubernetes LoadBalancer
时,它将在群集外部设置负载均衡器,例如AWS中的Elastic Load Balancer(ELB)或GKE中的Cloud Load Balancer.
But in most other environments, when you use a Kubernetes LoadBalancer
, it will provision a load balancer external to your cluster, for example an Elastic Load Balancer (ELB) in AWS, or a Cloud Load Balancer in GKE.
由于Minikube没有与单独的本地负载均衡器集成,因此您必须要求Minikube使用minikube服务来模拟连接:
Because Minikube doesn’t integrate with a separate local load balancer, you have to ask Minikube to simulate the connection using minikube service:
$ minikube service eureka
这将为您提供Minikube的IP地址和Minikube路由到"eureka"的端口号,因此您可以将其复制并粘贴到浏览器/其他应用中以测试该应用.
That will provide you with the Minikube’s IP address and the port number Minikube routes to 'eureka', so and you can copy and paste that into a browser/other app to test the app.
其他信息可以在 Minikube的文档中找到:
Additional info can be found in Minikube's documentation:
要访问hello-minikube部署,请将其作为服务公开:
To access the hello-minikube Deployment, expose it as a Service:
kubectl expose deployment hello-minikube --type=NodePort --port=8080
获取公开服务的URL,以查看服务详细信息:
Get the URL of the exposed Service to view the Service details:
minikube服务hello-minikube --url
minikube service hello-minikube --url
编辑
如何从Pod连接到Minikube.Host上的localhost
How to connect from Pod to localhost
on Minikube.Host
在使用以下docker-compose.yaml并通过'network_mode:"host"'连接时没有任何问题
don't have any problems connecting using following docker-compose.yaml by using 'network_mode: "host"'
是的.
如果您对容器使用host
网络模式,则该容器的网络堆栈不会与Docker主机隔离(该容器共享主机的网络名称空间),并且该容器不会分配自己的IP地址.因此,如果您运行绑定到端口80的容器并使用host
网络,则该容器的应用程序可在主机IP地址上的端口80上使用.
If you use the host
network mode for a container, that container’s network stack is not isolated from the Docker host (the container shares the host’s networking namespace), and the container does not get its own IP-address allocated. So if you run a container which binds to port 80 and you use host
networking, the container’s application is available on port 80 on the host’s IP address.
Since minikube runs everything from within a VM, networking can get fairly complicated
一旦Minikube运行并建立了网络,您就应该能够在127.0.0.1
和192.168.99.1
上都访问您的本地主机服务器(如果没有,则后续步骤将不起作用).如果成功,则意味着Minikube VM可以访问192.168.99.1
上的主机的本地主机(来自Minikube的127.0.0.1
仍然是Minicube的localhost
)
Once Minikube is running and it’s network has been established, you should be able to visit your localhost server on both 127.0.0.1
and 192.168.99.1
(If not, subsequent steps will not work). If successful, this means that the Minikube VM can access your host machine’s localhost on 192.168.99.1
(127.0.0.1
from Minikube would still be a Minicube's localhost
)
这就是为什么在这种情况下,您的应用程序应连接到192.168.99.1
而不是localhost
的原因.
That is why in this very case instead of localhost
your app shall connect to 192.168.99.1
.
kind: Service
apiVersion: v1
metadata:
name: mongo
spec:
type: ExternalName
externalName: host.docker.internal
请确保将host.docker.internal
解析为主机的IP地址.
Please make sure that host.docker.internal
is resolved to the IP address of Host machine.
此外,您可以编辑Minikube的/etc/hosts
并添加该IP的任何域名,以便能够连接到该IP.
Additionally you can edit /etc/hosts
of Minikube and add any domain name for that IP, so you'll be able connecting to it.
希望说明使用Minikube时如何从外部访问您的应用程序以及如何专门访问"localhost
".
Hope that explains how to access your app from outside and access specifically "localhost
" when using Minikube.
这篇关于如何从Minikube连接到在本地主机上运行的MongoDB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!