从minikube访问在本地主机上运行的mysql [英] Access mysql running on localhost from minikube

查看:221
本文介绍了从minikube访问在本地主机上运行的mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在minikube中运行一些服务,并尝试连接到在3306端口上的localhost(127.0.0.1)上运行的mysql.

I am running some services in minikube and trying to connect to mysql running on localhost(127.0.0.1) on 3306 port.

我阅读了,并尝试创建serviceEndpoints.但是,当我将127.0.0.1指定为IP时,它将引发如下错误:

I read this and trying to create service and Endpoints. However, when I specify 127.0.0.1 as IP, it throws error as below:

The Endpoints "mysql-service" is invalid: subsets[0].addresses[0].ip: Invalid value: "127.0.0.1": may not be in the loopback range (127.0.0.0/8)

我的部署如下:

---
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  ports:
  - protocol: TCP
    port: 1443
    targetPort: mysql

---
apiVersion: v1
kind: Endpoints
metadata:
  name: mysql-service
subsets:
  - addresses:
    - ip: 127.0.0.1
    ports:
    - name: mysql
      port: 3306

请协助我了解如何从minikube连接到mysql db.

Please assist me to understand how can I connect to mysql db from minikube.

我还尝试用计算机的公共IP替换127.0.0.1(虽然不知道为什么),并且连接超时.

I have also tried replacing 127.0.0.1 with public IP of my computer(Don't know why though) and connection was timed out.

感谢任何对正确方向的帮助或指导.

Any help or guide towards right direction is appreciated.

推荐答案

由于未提及操作系统和迷你库vm-driver,因此我认为它是--vm-driver=virtualbox,因为这可能是最常见的情况.如果您使用其他方法,则需要根据您的配置调整此解决方案.

As the OS and minikube vm-driver wasn't mentioned, I assume it is --vm-driver=virtualbox because it's probably most common case. If you use something different you need to adjust this solution according to your configuration.

127.0.0.1localhost(lo0)接口IP地址.节点,主机和Pod具有自己的本地主机接口,并且彼此之间未连接.

127.0.0.1 is a localhost(lo0) interface IP address. Nodes, Hosts and Pods have their own localhost interfaces and they are not connected to each other.

您的mysql-server在主机上运行,​​无法通过localhost(或其IP范围)从minikube集群容器内部或minikube vm内部进行访问.

Your mysql-server is running on the Host machine and cannot be accessible using the localhost (or it's IP range) from inside a minikube cluster pod or from inside minikube vm.

  1. 您应该在minikube VM和主机之间建立网络. Virtualbox中的默认NAT网络不适合这样做,因此最好创建另一个仅主机的网络.让我们创建 Virtualbox UI中名称为vmnet2和IP范围192.168.77.1/24的其他仅主机网络.您无需为该网络启用DHCP.

  1. You should have a network between minikube VM and the host. Default NAT network in Virtualbox is not good for that, so it's better to create another host-only network. Let's create additional host-only network in Virtualbox UI with the name vmnet2 and IP range 192.168.77.1/24 . You don't need to enable DHCP for that network.

您必须配置mysql以侦听vmnet2或ip 192.168.77.1接口,默认情况下该接口用于主机.检查是否可以从主机访问它:

You have to configure mysql to listen on the interface vmnet2 or ip 192.168.77.1 which is by default used for the host machine. Check if it's accessible from the host:

mysql -h 192.168.77.1 -u root -p 

  1. 要将此网络连接到minikube VM,应使用host-only-cidr密钥.为此,不同类型的vm-driver使用不同的cli选项.检查minikube start --help输出.因此,对于virtualbox,它将如下所示:

  1. To attach this network to minikube VM --host-only-cidr key should be used. Different type of vm-driver use different cli options for this purpose. Check the minikube start --help output. So, for virtualbox it will look like the following:

minikube start --cpus 2 \
               --memory 2048 \
               --disk-size 20g \
               --vm-driver virtualbox \
               --network-plugin flannel \
               --kubernetes-version v1.12.2 \
               --host-only-cidr 192.168.77.1/24

为了方便起见,我写了其他一些最常见的cli选项.

I wrote other most common cli options just for convenience.

MinikubeVM将获得以下IP地址:192.168.77.100(

MinikubeVM will get the following IP address: 192.168.77.100 (at least the first time.) You can check it using minikube ssh and then ifconfig commands.

最后一部分-我们需要在minikube集群内为其创建服务和端点:

Last part - we need to create a Service and Endpoint for it inside the minikube cluster:

kubectl apply -f mysql-service.yaml

这是mysql-service.yaml文件的内容:

---
apiVersion: v1
kind: Service
metadata:
   name: mysql-service
spec:
   type: ClusterIP
   ports:
   - protocol: TCP
     port: 3306
     targetPort: 3306
---
apiVersion: v1
kind: Endpoints
metadata:
  name: mysql-service
subsets:
  - addresses:
      - ip: 192.168.77.1
    ports:
      - port: 3306

  1. 现在,我们可以在该群集的任何吊舱内使用mysql-service名称和端口3306作为目的地.
  1. Now we can use the mysql-service name and port 3306 inside any pod of this cluster as a destination point.

这篇关于从minikube访问在本地主机上运行的mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆