Home > Software design >  Can't connect to mysql service on kuberenetes through workbench
Can't connect to mysql service on kuberenetes through workbench

Time:11-14

I am using Minikube and here is my configuration:

kubectl describe deployment mysql

the output:

Name:                   mysql
Namespace:              default
CreationTimestamp:      Sat, 12 Nov 2022 02:20:54  0200
Labels:                 <none>
Annotations:            deployment.kubernetes.io/revision: 1
Selector:               app=mysql
Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=mysql
  Containers:
   mysql:
    Image:      mysql
    Port:       3306/TCP
    Host Port:  0/TCP
    Environment:
      MYSQL_ROOT_PASSWORD:  <set to the key 'password' in secret 'mysql-pass'>  Optional: false
    Mounts:
      /docker-entrypoint-initdb.d from mysql-init (rw)
  Volumes:
   mysql-init:
    Type:      ConfigMap (a volume populated by a ConfigMap)
    Name:      mysql-init
    Optional:  false
Conditions:
  Type           Status  Reason
  ----           ------  ------
  Available      True    MinimumReplicasAvailable
  Progressing    True    NewReplicaSetAvailable
OldReplicaSets:  <none>
NewReplicaSet:   mysql-77fd55bbd9 (1/1 replicas created)

when I try to connect to it using mysql workbench:

workbench config

it shows me:

failed to connect to mysql

However, when I execute this line to create a mysql-client to try to connect to mysql server:

kubectl run -it --rm --image=mysql:8.0 --restart=Never mysql-client -- mysql -h mysql -u skaffold -p and then enter the password, it works well! but still I need to use workbench better.

any help please?

edit 1:

Here is the yaml file for the deployment and the service:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-pass
                  key: password
          ports:
            - containerPort: 3306
          volumeMounts:
            - name: mysql-init
              mountPath: /docker-entrypoint-initdb.d
      volumes:
        - name: mysql-init
          configMap:
            name: mysql-init
---
apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  ports:
    - port: 3306
      targetPort: 3306
      protocol: TCP
  selector:
    app: mysql

CodePudding user response:

If you want to connect directly to your mysql Deployment's Pod via localhost, first, you have to forward a Pod's container port to the localhost.

kubectl port-forward <pod-name> <local-port>:<container-port>

Then your mysql will be accessible on localhost:<local-port>.

The other way to communicate with your Pod is created a Service object that will pass your requests directly to the Pod. There are couple type of Services for different types of usage. Check the documentation to learn more.

The reason the following command

kubectl run -it --rm --image=mysql:8.0 --restart=Never mysql-client -- mysql -h mysql -u skaffold -p

connects to the database correctly is because the connect command is done inside the mysql container itself.

Edit 1

If you not specified the type of Service, the default is going to be ClusterIP which not allow you to expose port outside the cluster.

Because Minikube doesn't handle LoadBalancer use NodePort Service type instead.

Your Service YAML manifest should look like this:

apiVersion: v1
kind: Service
metadata:
  name: mysql
  labels:
    name: mysql
spec:
  type: NodePort
  ports:
    - port: 3306
      targetPort: 3306
      protocol: TCP
  selector:
    app: mysql

Finally, therefore your cluster is provisioned via Minikube, you still need to call the command below for fetch the Minikube IP and a Service’s NodePort:

minikube service <service-name> --url

CodePudding user response:

First make sure your service is running, so

kubectl get service

should return something like :

NAME         TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)    AGE
mysql        ClusterIP   10.99.140.115   <none>        3306/TCP     2d6h

From that point onwards, I'd try running a port-forward first :

kubectl port-forward service/mysql 3306:3306

This should allow you to connect even when using a ClusterIP service.

  • Related