MySQL pod access via kubectl¶
To establish a direct connection to a [[mysql]] database running within a Kubernetes cluster, local port forwarding can be utilized to tunnel the traffic from the local machine to the specific Pod.^[400-devops__06-Kubernetes__k8s-mysql__README.md]
Port Forwarding¶
The standard method to access the service involves using the kubectl port-forward command^[400-devops__06-Kubernetes__k8s-mysql__README.md]. This command maps a local port to the port exposed by the Pod, enabling interaction with the database via localhost.
The following command syntax forwards port 3306 on the local interface to port 3306 on the specified Pod:
shell
kubectl port-forward mysql-dp-8dfb795cf-2hkgm 3306:3306 --address 0.0.0.0^[400-devops__06-Kubernetes__k8s-mysql__README.md]
Identification¶
To execute the command, the specific name of the target Pod is required^[400-devops__06-Kubernetes__k8s-mysql__README.md]. A list of all Pods across all namespaces can be retrieved to identify the correct target name.
shell
kubectl get pods -A^[400-devops__06-Kubernetes__k8s-mysql__README.md]