How can I connect to my mysql pod or node of mysql in k8s?

5/10/2019

I deploy an mysql pod to my k8s. this is the config, I am confused about the ports, like that:

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

@question1. who proxy this port @question2. what is this port ? I mean container has exposed the port 3306, and the service exposed also. In my single node, These don't clash? @question3. this port is mean to the port the container exposed?

question4: when I deploy this with way 1. how can I visit my mysql server From pods & my localMachine .

question5: when I deploy this with way 2.how can I visit my mysql server From pods & my localMachine .

--
go
kubernetes
mysql

1 Answer

5/10/2019
  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.
-- Vasily Angapov
Source: StackOverflow