doucheng1063
2019-05-10 02:27
采纳率: 100%
浏览 2.3k

如何在k8s中连接到我的mysql pod或mysql的节点?

我在我的 k8s 上部署了一个 mysql pod,下面是配置代码,我对端口感到很困惑:

kind: Deployment
...
   containers:
     - ports:
        - containerPort: 3306  # @1
---

apiVersion: v1
kind: Service 
metadata:
  name: mysql
  labels:
    app: mysql
spec:
  selector:
    app: mysql

  # way 1
  ports:
    - port: 3306  # @2

      targetPort: 3306  # @3

  # way 2
  type: NodePort
  ports: 
    - port: 3306
      targetPort: 3306
      nodePort: 30600


问题1:谁代理了这个port?问题2:这是什么port? 我的意思是container已经暴露了port3306,而且服务也暴露了。那这在单一节点中不冲突吗? 问题3:这个port是不是意味着其container暴露了?

问题4:当我使用方式1部署它时,如何从pods&我的本地机器访问MySQL服务器?

问题5:当我使用方法2部署它时,如何从pods&我的本地机器访问MySQL服务器?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhangshao6369 2019-05-10 06:17
    已采纳
    1. Pod listens to port, Service proxies port.
    2. Pod listens to port 3306 and Service only proxies port 3306 to Pod. Service does not listen the port, it's just a simple iptables rule.
    3. When you create default ClusterIP service you can visit your DB by hostname "mysql" from pods. From outside of Kubernetes you cannot reach DB unless you do "kubectl port-forward".
    4. When you create NodePort service, you still can visit your DB by hostname "mysql" from pods, but also you can visit DB using any node IP + nodePort. So if your node have address 192.168.1.10 and your nodePort is 30600 then you must connect to DB to 192.168.1.10:30600.
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题