比如阿里云的kubernetes的namespace是test的里部署了一个mysql-a你想在本地命令行操作。

1、查看mysql service和port
kubectl get svc -n test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql-a ClusterIP xxx.xx.xx.xx 3306/TCP 1h
2、port-forward
kubectl port-forward service/mysql-a -n test 3307:3306
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
mysql-a ClusterIP xxx.xx.xx.xx 3306/TCP 1h
3、登陆MySQ
mysql -h127.0.0.1 --port 3307 -uxxx -p

Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
…………
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>