Search code examples
kuberneteskubernetes-pod

How to access mysql pod in another pod(busybox)?


I am given a task of connecting mysql pod with any other working pod(preferably busybox) but was not able to that. Is there a way to do this task. I referred many places but the explanations was bit complicated as I am new to Kubernetes.


Solution

  • MySQL YAML config for Kubernets

    apiVersion: v1
    kind: Service
    metadata:
      name: mysql
    spec:
      ports:
      - port: 3306
      selector:
        app: mysql
      clusterIP: None
    ---
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: mysql
    spec:
      selector:
        matchLabels:
          app: mysql
      strategy:
        type: Recreate
      template:
        metadata:
          labels:
            app: mysql
        spec:
          containers:
          - image: mysql:5.6
            name: mysql
            env:
              # Use secret in real usage
            - name: MYSQL_ROOT_PASSWORD
              value: password
            ports:
            - containerPort: 3306
              name: mysql
            volumeMounts:
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
          volumes:
          - name: mysql-persistent-storage
            persistentVolumeClaim:
              claimName: mysql-pv-claim
    

    you can use the service name to connect with the MySQL from busy box container

    kubectl run -i --tty mysql-client --image=mysql --restart=Never -- sh   
    

    above command will start one container of Mysql as client.

    run kubectl get pods to check both pod status.

    In Mysql-client container you will be able to run the command to connect with the MySQL

    mysql -h <MySQL service name> -u <Username> -p<password>
    

    Ref doc : MySQL : https://kubernetes.io/blog/2015/10/some-things-you-didnt-know-about-kubectl_28/