How do I access MySQL as a Service on Kubernetes?
How do I access MySQL as a Service on Kubernetes?
https://stackoverflow.com/questions/57030925/how-do-i-access-mysql-as-a-service-on-kubernetes
Asked 6 years, 6 months ago Modified 6 years, 6 months ago Viewed 11k times 1
I have the following deployment…
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mysql-data-disk spec: accessModes: - ReadWriteOnce resources: requests: storage: 1Gi — apiVersion: apps/v1 kind: Deployment metadata: name: mysql-deployment labels: app: mysql spec: replicas: 1 selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql:5.7 ports: - containerPort: 3306 volumeMounts: - mountPath: “/var/lib/mysql” subPath: “mysql” name: mysql-data env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-secrets key: ROOT_PASSWORD volumes: - name: mysql-data persistentVolumeClaim: claimName: mysql-data-disk This works great I can access the db like this…
kubectl exec -it mysql-deployment-
mysql -u root -h localhost -p And I can log into it. However, when I try to access it as a service by using the following yaml…
apiVersion: v1 kind: Service metadata: name: mysql-service spec: selector: app: mysql ports: - protocol: TCP port: 3306 targetPort: 3306 I can see it by running this kubectl describe service mysql-service I get…
Name: mysql-service
Namespace: default
Labels:
#kubectl cluster-info Kubernetes master is running at https://192.168.99.100:8443 KubeDNS is running at https://192.168.99.100:8443/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy but when I try to connect using Oracle SQL Developer like this…
enter image description here
It says it cannot connect.
enter image description here
How do I connect to the MySQL running on K8s?
mysqlkubernetes Share Improve this question Follow edited Jul 14, 2019 at 20:38 asked Jul 14, 2019 at 20:28 Jackie’s user avatar Jackie 24k4343 gold badges178178 silver badges343343 bronze badges I also tried using the mysql command line locally like “/usr/local/mysql-8.0.16-macos10.14-x86_64/bin/mysql -u root -h 192.168.99.100 -p” but it still didn’t work. “ERROR 2003 (HY000): Can’t connect to MySQL server on ‘192.168.99.100’ (61)” – Jackie CommentedJul 14, 2019 at 20:39 When you add something, update your post ;) … Did you try to port forward? – night-gold CommentedJul 14, 2019 at 20:51 Add a comment 2 Answers Sorted by:
Highest score (default) 3
Service type ClusterIP will not be accessible outside of Pod network. If you don’t have LoadBalancer option, then you have to use either Service type NodePort or kubectl port-forward
Share Improve this answer Follow answered Jul 14, 2019 at 21:29 Ansil’s user avatar Ansil 52333 silver badges1111 bronze badges Sign up to request clarification or add additional context in comments.
4 Comments
tadman Over a year ago Exposing it externally with no firewall rules is probably a bad plan. The port-forward option is safest if this is for introspection and not some kind of permanent connection.
Jackie Over a year ago I tried this… “kubectl port-forward service/mysql-service 3310:3306” but it still fails. “/usr/local/mysql-8.0.16-macos10.14-x86_64/bin/mysql -u root -h 192.168.99.100 –port=3310 -p”
Jackie Over a year ago NM needed to use 127.0.0.1 instead of the cluster IP
Subrata Fouzdar Over a year ago Forwarding a port with kubectl is fairly easy, however, it only works with single Pods and not with Services. docs.giantswarm.io/guides/accessing-services-from-the-outside Add a comment 1
You need your mysql service to be of Type NodePort instead of ClusterIP to access it outside Kubernetes.
Use the Node Port in your client config
Example Service:
apiVersion: v1 kind: Service metadata: name: mysql-service spec: type: NodePort selector: app: mysql ports: - protocol: TCP port: 3306 nodePort: 30036 targetPort: 3306 So then you can use the port: 30036 in your client.
Share Improve this answer Follow edited Jul 14, 2019 at 22:05 answered Jul 14, 2019 at 21:20 Ijaz Ahmad’s user avatar Ijaz Ahmad 12.3k1010 gold badges6161 silver badges81