在本地开发环境(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"
但是,吊舱无法通过以下错误连接到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控制台中的“终端”标签连接到Pod,然后尝试
$ 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吊舱到Docker容器的连接?
总体目标是使用任意外部服务。
答案 0 :(得分:0)
从与Minishift一起使用的vm-driver的角度来看,端点的IP必须是主机系统的IP。
在我的情况下,虚拟机驱动程序是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可以访问在主机上与docker一起运行的MariaDB。