Kubernetes multi-container pod

10/6/2018

Hello I try to have a Pod with 2 container, one a c++ app, one a mysql database. I used to have the mysql deployed in its own service, but i got latency issue. So i want to try multi-container pod.

But i've been struggling to connect my app with the mysql through localhost. It says..

Can\'t connect to local MySQL server through socket \'/var/run/mysqld/mysqld.sock

Here is my kubernetes.yaml. Please I need help :(

# Database setup
kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
  name: storage-camera
  labels:     
    group: camera
provisioner: kubernetes.io/gce-pd
parameters:
  type: pd-ssd
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: camera-pv
  labels:     
    group: camera
spec:
  storageClassName: db-camera
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 20Gi
  storageClassName: storage-camera
---
# Service setup
apiVersion: v1
kind: Service
metadata:
  name: camera-service
  labels:
    group: camera
spec:
  ports:
    - port: 50052
      targetPort: 50052
  selector:
    group: camera
    tier: service
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: camera-service
  labels:
    group: camera
    tier: service
spec:
  replicas: 1
  strategy:
    type: RollingUpdate
    rollingUpdate:
      maxUnavailable: 0
      maxSurge: 1
  minReadySeconds: 60
  template:
    metadata:
      labels:
        group: camera
        tier: service
    spec:
      containers:
      - image: asia.gcr.io/test/db-camera:latest
        name: db-camera
        env:
        - name : MYSQL_ROOT_PASSWORD
          value : root
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: camera-persistent-storage
          mountPath: /var/lib/mysql
      - name: camera-service
        image: asia.gcr.io/test/camera-service:latest
        env:
        - name : DB_HOST
          value : "localhost"
        - name : DB_PORT
          value : "3306"
        - name : DB_NAME
          value : "camera"
        - name : DB_ROOT_PASS
          value : "password"
        ports:
        - name: http-cam
          containerPort: 50052
      volumes:
      - name: camera-persistent-storage
        persistentVolumeClaim:
          claimName: camera-pv
      restartPolicy: Always
-- Hana Alaydrus
google-kubernetes-engine
kubernetes

1 Answer

10/6/2018

Your MySQL client is configured to use a socket and not talk over the network stack, cf. the MySQL documentation:

On Unix, MySQL programs treat the host name localhost specially, in a way that is likely different from what you expect compared to other network-based programs. For connections to localhost, MySQL programs attempt to connect to the local server by using a Unix socket file. This occurs even if a --port or -P option is given to specify a port number. To ensure that the client makes a TCP/IP connection to the local server, use --host or -h to specify a host name value of 127.0.0.1, or the IP address or name of the local server. You can also specify the connection protocol explicitly, even for localhost, by using the --protocol=TCP option.

If you still want camera-service to talk over the file system socket you need to mount the file system for the camera-service as well. Currently you only mount it for db-camera

-- Christoffer Soop
Source: StackOverflow