Helm Kubernetes setup mysql with NodeJs application

2/6/2019

I'm new to Kubernetes and helm world and trying to deploy a Nodejs application with a MYSQL database using Kubernetes and Helm on Azure kKubernetes Service.

Here's what I have done so far:

My Dockerfile:

FROM node:10-alpine
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 32000
CMD [ "node", "app.js" ]

I have built the image using this Dockerfile and then push it to a repository on my ACR registry. This application is working with mysql database, so before installing the helm chart of my application, I have installed the mysql helm chart on my cluster using the following command:

helm install --name mysql --set mysqlRootPassword=rootpassword,mysqlUser=mysql,mysqlPassword=my-password,mysqlDatabase=mydatabase,persistence.existingClaim=mysql-pvc stable/mysql

After that, I have export two variable in the cluster as:

MYSQL_HOST=127.0.0.1
MYSQL_PORT=3306

Then forward the port as a background process like:

kubectl port-forward svc/mysql 3306

After that I have installed my own application chart, I have created a chart just by running helm create mychart and then create a custom values.yaml file and pass it on installation, which is as:

image:
    registry: helmcr.azurecr.io
    repository: helmcr.azurecr.io/helloworldtest01
    tag: 0.3
service:
    name: http
    type: LoadBalancer
    port: 32000
    internalPort: 32000
hosts:
    - name: mychart.local
      path: /

After the installation of both charts, the application should be connected to the mysql but it says: CrashLoopBackOff

Update: Here's how I have configured the database connection in my

application:
module.exports = {
    adapters: {
        mysqlAdapt: mysqlAdapter
    },

  connections: {
    mysqlDB: {
        adapter: 'mysqlAdapt',
        host: process.env.MYSQL_SERVICE_HOST,
        database: 'mydatabase',
        user:'root',
        password:'my-password',
        port: process.env.MMYSQL_SERVICE_PORT,
        supportBigNumbers:true, //true/false
        debug:['ComQueryPacket'], //false or array of node-mysql debug options
        trace:true //true/false
    } 
  }
};

When I run the command below to get the logs:

kubectl logs myrel09-mychart09-5b8bb86788-9k627 -p

here the output of the above command:

Wed, 06 Feb 2019 13:29:21 GMT body-parser deprecated bodyParser: use individual json/urlencoded middlewares at app.js:10:9
Wed, 06 Feb 2019 13:29:21 GMT body-parser deprecated undefined extended: provide extended option at node_modules/body-parser/index.js:105:29
Wed, 06 Feb 2019 13:29:21 GMT express deprecated app.del: Use app.delete instead at node_modules/express-resource/index.js:153:19
Load model crud
(node:1) [DEP0095] DeprecationWarning: timers.enroll() is deprecated. Please use setTimeout instead.

/usr/src/app/app.js:32
    if (err) throw err;
             ^
Error (E_UNKNOWN) :: Encountered an unexpected error
: Could not connect to MySQL:
Error: connect ECONNREFUSED 127.0.0.1:3306
    at afterwards (/usr/src/app/node_modules/sails-mysql/lib/connections/spawn.js:72:13)
    at /usr/src/app/node_modules/sails-mysql/lib/connections/spawn.js:40:7
    at Handshake.onConnect [as _callback] (/usr/src/app/node_modules/sails-mysql/node_modules/mysql/lib/Pool.js:54:9)
    at Handshake.Sequence.end (/usr/src/app/node_modules/sails-mysql/node_modules/mysql/lib/protocol/sequences/Sequence.js:96:24)
    at Protocol.handleNetworkError (/usr/src/app/node_modules/sails-mysql/node_modules/mysql/lib/protocol/Protocol.js:364:14)
    at PoolConnection.Connection._handleNetworkError (/usr/src/app/node_modules/sails-mysql/node_modules/mysql/lib/Connection.js:421:18)
    at Socket.emit (events.js:189:13)
    at emitErrorNT (internal/streams/destroy.js:82:8)
    at emitErrorAndCloseNT (internal/streams/destroy.js:50:3)
    at process._tickCallback (internal/process/next_tick.js:63:19)

When I try to connect to this mysql database from my local system, it's connected successfully, it means the database is working correctly but there's something wrong with my configuration.

What can be wrong here?

Thanks in advance!

-- Abdul Rehman
docker
kubernetes
kubernetes-helm
mysql
node.js

1 Answer

2/6/2019

As you are using same cluster to deploy both applications, you can use cluster ip as well.

If you want to use port forward, I think you should use ip of your node not localhost ip.

-- Rajesh Deshpande
Source: StackOverflow