集成外部服务:在 Minishift 上从 Pod 运行 连接到 Docker MariaDB
Integrating External Services: Connect to Docker MariaDB from Pod running on Minishift
在本地开发环境(Ubuntu)上安装了Docker和Minishift。
Docker 启动了 mariadb:10.3.11 的容器:
docker run -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 --name mariadb 4f2e75647d2a
运行 来自主机系统的 curl 具有以下输出:
curl 172.17.0.1:3306
curl: (56) Recv failure: Connection reset by peer
5.5.5-10.3.11-MariaDB-1:10.3.11+maria~bionic
QpatFAPM���rv{(RC:7G@H+mysql_native_password!��#08S01Got packets out of order%
目标是从 Minishift 中的 pod 运行 连接到此 MariaDB。我遵循了本教程:https://docs.okd.io/latest/dev_guide/integrating_external_services.html
apiVersion: v1
kind: List
items:
- kind: "Service"
apiVersion: "v1"
metadata:
name: "external-mysql-service"
spec:
ports:
- name: "mysql"
protocol: "TCP"
port: 3306
targetPort: 3306
nodePort: 0
selector: {}
- kind: "Endpoints"
apiVersion: "v1"
metadata:
name: "external-mysql-service"
subsets:
- addresses:
- ip: "172.17.0.1"
ports:
- port: 3306
name: "mysql"
但是,pod 无法连接到 MariaDB,出现以下错误:
2019-02-19 18:00:42 [ERROR] HikariPool:567 - HikariPool-1 - Exception during pool initialization.
java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=external-mysql-service)(port=3306)(type=master) : Connection refused (Connection refused)
如果我通过 OpenShift Web 控制台中的终端选项卡连接到容器并尝试
$ curl -v external-mysql-service:3306
* Rebuilt URL to: external-mysql-service:3306/
* Trying 172.30.23.62...
* TCP_NODELAY set
* connect to 172.30.23.62 port 3306 failed: Connection refused
* Failed to connect to external-mysql-service port 3306: Connection refused
* Closing connection 0
curl: (7) Failed to connect to external-mysql-service port 3306: Connection refused
如何建立从 Minishift pod 到 Docker 容器的连接?
总体目标是使用任意外部服务。
从与 Minishift 一起使用的 vm-driver 的角度来看,Endpoint 的 IP 必须是主机系统的 IP。
在我的例子中,vm-driver 是 VirtualBox,访问主机系统的 IP 是 10.0.2.2:
https://github.com/kubernetes/minikube/issues/352#issuecomment-237615642
更改端点的 IP 对我有用:
- kind: "Endpoints"
apiVersion: "v1"
metadata:
name: "external-mysql-service"
subsets:
- addresses:
- ip: "10.0.2.2"
ports:
- port: 3306
name: "mysql"
现在 MiniShift 中的 pod 可以访问主机上的 MariaDB 运行 docker。
在本地开发环境(Ubuntu)上安装了Docker和Minishift。
Docker 启动了 mariadb:10.3.11 的容器:
docker run -e MYSQL_ROOT_PASSWORD=root -p 3306:3306 --name mariadb 4f2e75647d2a
运行 来自主机系统的 curl 具有以下输出:
curl 172.17.0.1:3306
curl: (56) Recv failure: Connection reset by peer
5.5.5-10.3.11-MariaDB-1:10.3.11+maria~bionic
QpatFAPM���rv{(RC:7G@H+mysql_native_password!��#08S01Got packets out of order%
目标是从 Minishift 中的 pod 运行 连接到此 MariaDB。我遵循了本教程:https://docs.okd.io/latest/dev_guide/integrating_external_services.html
apiVersion: v1
kind: List
items:
- kind: "Service"
apiVersion: "v1"
metadata:
name: "external-mysql-service"
spec:
ports:
- name: "mysql"
protocol: "TCP"
port: 3306
targetPort: 3306
nodePort: 0
selector: {}
- kind: "Endpoints"
apiVersion: "v1"
metadata:
name: "external-mysql-service"
subsets:
- addresses:
- ip: "172.17.0.1"
ports:
- port: 3306
name: "mysql"
但是,pod 无法连接到 MariaDB,出现以下错误:
2019-02-19 18:00:42 [ERROR] HikariPool:567 - HikariPool-1 - Exception during pool initialization.
java.sql.SQLNonTransientConnectionException: Could not connect to address=(host=external-mysql-service)(port=3306)(type=master) : Connection refused (Connection refused)
如果我通过 OpenShift Web 控制台中的终端选项卡连接到容器并尝试
$ curl -v external-mysql-service:3306
* Rebuilt URL to: external-mysql-service:3306/
* Trying 172.30.23.62...
* TCP_NODELAY set
* connect to 172.30.23.62 port 3306 failed: Connection refused
* Failed to connect to external-mysql-service port 3306: Connection refused
* Closing connection 0
curl: (7) Failed to connect to external-mysql-service port 3306: Connection refused
如何建立从 Minishift pod 到 Docker 容器的连接?
总体目标是使用任意外部服务。
从与 Minishift 一起使用的 vm-driver 的角度来看,Endpoint 的 IP 必须是主机系统的 IP。
在我的例子中,vm-driver 是 VirtualBox,访问主机系统的 IP 是 10.0.2.2: https://github.com/kubernetes/minikube/issues/352#issuecomment-237615642
更改端点的 IP 对我有用:
- kind: "Endpoints"
apiVersion: "v1"
metadata:
name: "external-mysql-service"
subsets:
- addresses:
- ip: "10.0.2.2"
ports:
- port: 3306
name: "mysql"
现在 MiniShift 中的 pod 可以访问主机上的 MariaDB 运行 docker。