咱们知道Openshift容器化平台中, POD有本身的IP地址, 可是它只能在集群的内部可用. 那若是我要从其余物理计算机经过网络访问容器内的MySQL怎么办呢?node
我想到了Router, 可是, Router 只支持HTTP协议的转发, 咱们要使用TCP. 所以, NodePort出场了!mysql
这种方式适合长期使用, 对外提供sql
先看看有DC的名称centos
➜ oc get dc NAME REVISION DESIRED CURRENT TRIGGERED BY hello-microservice 1 1 1 config,image(hello-microservice:latest) mysql-57-centos7 11 1 1 config,image(mysql-57-centos7:latest) nodejs-ex 1 1 1 config,image(nodejs-ex:latest)
mysql-57-centos7
是咱们须要的api
暴露指定DC, 暴露类型为 LoadBalancer, 暴露的名称为网络
oc expose dc mysql-57-centos7 --type=LoadBalancer --name=mysql-ingress
导出session
➜ oc export svc mysql-ingress apiVersion: v1 kind: Service metadata: creationTimestamp: null labels: app: mysql-57-centos7 name: mysql-ingress spec: deprecatedPublicIPs: - 172.29.208.121 externalIPs: - 172.29.208.121 ports: - nodePort: 32621 port: 3306 protocol: TCP targetPort: 3306 selector: app: mysql-57-centos7 deploymentconfig: mysql-57-centos7 sessionAffinity: None type: LoadBalancer status: loadBalancer: {}
在导出的配置中, 咱们看到 spec.ports.nodePort
为 32621
, 这个端口就是咱们能够从外部访问MySQL的目标端口.app
登陆MySQL测试连通性测试
➜ mysql --user=data --password=data --host=$(minishift ip) --port=32621 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 13 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
OK, 看起来没有问题.centos7
注意: 注意分配合适的登陆权限. 好比:
ERROR 1045 (28000): Access denied for user 'data'@'172.17.0.1' (using password: NO)
所以须要给登陆客户端所在的IP地址分配权限:
CREATE USER 'data'@'172.17.0.1' IDENTIFIED BY 'data'; GRANT ALL PRIVILEGES ON *.* TO 'data'@'172.17.0.1'; FLUSH PRIVILEGES;
端口转发能够经过你的物理机器所在的网络链接到POD,多用于开发测试环境
开启端口转发
➜ oc port-forward mysql-57-centos7-11-2wfs4 10001:3306 Forwarding from 127.0.0.1:10001 -> 3306 Forwarding from [::1]:10001 -> 3306 Handling connection for 10001
MySQL链接测试
➜ mysql -udata --password=data --host=127.0.0.1 --port=10001 mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 19 Server version: 5.7.16 MySQL Community Server (GPL) Copyright (c) 2000, 2017, Oracle and/or its affiliates. All rights reserved. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
上述方法不限于MySQL这样的应用, 各类基于TCP的应用均可以使用这两种方式, 在合适的环境中使用.