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:
it shows me:
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
2
Answers
If you want to connect directly to your
mysql
Deployment’s Pod vialocalhost
, first, you have to forward a Pod’s container port to thelocalhost
.Then your
mysql
will be accessible onlocalhost:<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
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 handleLoadBalancer
useNodePort
Service type instead.Your Service YAML manifest should look like this:
Finally, therefore your cluster is provisioned via
Minikube
, you still need to call the command below for fetch theMinikube
IP and a Service’sNodePort
:First make sure your service is running, so
should return something like :
From that point onwards, I’d try running a port-forward first :
This should allow you to connect even when using a ClusterIP service.