In this chapter, we will deploy a database, configure our application to use it, and set up automatic DB migrations and initializations.

The application described in this chapter is not intended for use in production environments as-is. Note that successful completion of this entire guide is required to create a production-ready application.

Preparing the environment

If you haven’t prepared your environment during previous steps, please, do it using the instructions provided in the “Preparing the environment” chapter.

If your environment has stopped working or instructions in this chapter don’t work, please, refer to these hints:

Is Docker running?

Let’s launch Docker Desktop. It takes some time for this application to start Docker. If there are no errors during the startup process, check that Docker is running and is properly configured:

docker run hello-world

You will see the following output if the command completes successfully:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

Should you have any problems, please refer to the Docker documentation.

Let’s launch the Docker Desktop application. It takes some time for the application to start Docker. If there are no errors during the startup process, then check that Docker is running and is properly configured:

docker run hello-world

You will see the following output if the command completes successfully:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

Should you have any problems, please refer to the Docker documentation.

Start Docker:

sudo systemctl restart docker

Make sure that Docker is running:

sudo systemctl status docker

If the Docker start is successful, you will see the following output:

● docker.service - Docker Application Container Engine
     Loaded: loaded (/lib/systemd/system/docker.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2021-06-24 13:05:17 MSK; 13s ago
TriggeredBy: ● docker.socket
       Docs: https://docs.docker.com
   Main PID: 2013888 (dockerd)
      Tasks: 36
     Memory: 100.3M
     CGroup: /system.slice/docker.service
             └─2013888 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock

dockerd[2013888]: time="2021-06-24T13:05:16.936197880+03:00" level=warning msg="Your kernel does not support CPU realtime scheduler"
dockerd[2013888]: time="2021-06-24T13:05:16.936219851+03:00" level=warning msg="Your kernel does not support cgroup blkio weight"
dockerd[2013888]: time="2021-06-24T13:05:16.936224976+03:00" level=warning msg="Your kernel does not support cgroup blkio weight_device"
dockerd[2013888]: time="2021-06-24T13:05:16.936311001+03:00" level=info msg="Loading containers: start."
dockerd[2013888]: time="2021-06-24T13:05:17.119938367+03:00" level=info msg="Loading containers: done."
dockerd[2013888]: time="2021-06-24T13:05:17.134054120+03:00" level=info msg="Daemon has completed initialization"
systemd[1]: Started Docker Application Container Engine.
dockerd[2013888]: time="2021-06-24T13:05:17.148493957+03:00" level=info msg="API listen on /run/docker.sock"

Now let’s check if Docker is available and its configuration is correct:

docker run hello-world

You will see the following output if the command completes successfully:

Unable to find image 'hello-world:latest' locally
latest: Pulling from library/hello-world
b8dfde127a29: Pull complete
Digest: sha256:9f6ad537c5132bcce57f7a0a20e317228d382c3cd61edae14650eec68b2b345c
Status: Downloaded newer image for hello-world:latest

Hello from Docker!
This message shows that your installation appears to be working correctly.

Should you have any problems, please refer to the Docker documentation.

Have you restarted the computer after setting up the environment?

Let’s start the minikube cluster we have already configured in the “Preparing the environment” chapter:

minikube start

Set the default Namespace so that you don’t have to specify it every time you invoke kubectl:

kubectl config set-context minikube --namespace=werf-guide-app

You will see the following output if the command completes successfully:

😄  minikube v1.20.0 on Ubuntu 20.04
✨  Using the docker driver based on existing profile
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🎉  minikube 1.21.0 is available! Download it: https://github.com/kubernetes/minikube/releases/tag/v1.21.0
💡  To disable this notice, run: 'minikube config set WantUpdateNotification false'

🔄  Restarting existing docker container for "minikube" ...
🐳  Preparing Kubernetes v1.20.2 on Docker 20.10.6 ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/google_containers/kube-registry-proxy:0.4
    ▪ Using image k8s.gcr.io/ingress-nginx/controller:v0.44.0
    ▪ Using image registry:2.7.1
    ▪ Using image docker.io/jettech/kube-webhook-certgen:v1.5.1
    ▪ Using image docker.io/jettech/kube-webhook-certgen:v1.5.1
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🔎  Verifying registry addon...
🔎  Verifying ingress addon...
🌟  Enabled addons: storage-provisioner, registry, default-storageclass, ingress
🏄  Done! kubectl is now configured to use "minikube" cluster and "werf-guide-app" namespace by default

Make sure that the command output contains the following line:

Restarting existing docker container for "minikube"

Its absence means that a new minikube cluster was created instead of using the old one. In this case, repeat all the steps required to install the environment using minikube.

Now run the command in the background PowerShell terminal (do not close its window):

minikube tunnel --cleanup=true

Let’s start the minikube cluster we have already configured in the “Preparing the environment” chapter:

minikube start --namespace werf-guide-app

Set the default Namespace so that you don’t have to specify it every time you invoke kubectl:

kubectl config set-context minikube --namespace=werf-guide-app

You will see the following output if the command completes successfully:

😄  minikube v1.20.0 on Ubuntu 20.04
✨  Using the docker driver based on existing profile
👍  Starting control plane node minikube in cluster minikube
🚜  Pulling base image ...
🎉  minikube 1.21.0 is available! Download it: https://github.com/kubernetes/minikube/releases/tag/v1.21.0
💡  To disable this notice, run: 'minikube config set WantUpdateNotification false'

🔄  Restarting existing docker container for "minikube" ...
🐳  Preparing Kubernetes v1.20.2 on Docker 20.10.6 ...
🔎  Verifying Kubernetes components...
    ▪ Using image gcr.io/google_containers/kube-registry-proxy:0.4
    ▪ Using image k8s.gcr.io/ingress-nginx/controller:v0.44.0
    ▪ Using image registry:2.7.1
    ▪ Using image docker.io/jettech/kube-webhook-certgen:v1.5.1
    ▪ Using image docker.io/jettech/kube-webhook-certgen:v1.5.1
    ▪ Using image gcr.io/k8s-minikube/storage-provisioner:v5
🔎  Verifying registry addon...
🔎  Verifying ingress addon...
🌟  Enabled addons: storage-provisioner, registry, default-storageclass, ingress
🏄  Done! kubectl is now configured to use "minikube" cluster and "werf-guide-app" namespace by default

Make sure that the command output contains the following line:

Restarting existing docker container for "minikube"

Its absence means that a new minikube cluster was created instead of using the old one. In this case, repeat all the steps required to install the environment from scratch using minikube.

Did you accidentally delete the application's Namespace?

If you have inadvertently deleted Namespace of the application, you must run the following commands to proceed with the guide:

kubectl create namespace werf-guide-app
kubectl create secret docker-registry registrysecret \
  --docker-server='https://index.docker.io/v1/' \
  --docker-username='<Docker Hub username>' \
  --docker-password='<Docker Hub password>'

You will see the following output if the command completes successfully:

namespace/werf-guide-app created
secret/registrysecret created
Nothing helps; the environment or instructions keep failing.

If nothing worked, repeat all the steps described in the “Preparing the environment” chapter and create a new environment from scratch. If creating an environment from scratch did not help either, please, tell us about your problem in our Telegram chat or create an issue on GitHub. We will be happy to help you!

Preparing the repository

Update the existing repository containing the application:

Run the following commands in PowerShell:

cd ~/werf-guide/app

# To see what changes we will make later in this chapter, let's replace all the application files
# in the repository with new, modified files containing the changes described below.
git rm -r .
cp -Recurse -Force ~/werf-guide/guides/examples/nodejs/040_db/* .
git add .
git commit -m WIP
What changes we will make
# Enter the command below to show the files we are going to change.
git show --stat
# Enter the command below to show the changes that will be made.
git show

Run the following commands in Bash:

cd ~/werf-guide/app

# To see what changes we will make later in this chapter, let's replace all the application files
# in the repository with new, modified files containing the changes described below.
git rm -r .
cp -rf ~/werf-guide/guides/examples/nodejs/040_db/. .
git add .
git commit -m WIP
What changes we will make
# Enter the command below to show files we are going to change.
git show --stat
# Enter the command below to show the changes that will be made.
git show

Doesn’t work? Try the instructions on the “I am just starting from this chapter” tab above.

Prepare a new repository with the application:

Run the following commands in PowerShell:

# Clone the example repository to ~/werf-guide/guides (if you have not cloned it yet).
if (-not (Test-Path ~/werf-guide/guides)) {
  git clone https://github.com/werf/website $env:HOMEPATH/werf-guide/guides
}

# Copy the (unchanged) application files to ~/werf-guide/app.
rm -Recurse -Force ~/werf-guide/app
cp -Recurse -Force ~/werf-guide/guides/examples/nodejs/030_assets ~/werf-guide/app

# Make the ~/werf-guide/app directory a git repository.
cd ~/werf-guide/app
git init
git add .
git commit -m initial

# To see what changes we will make later in this chapter, let's replace all the application files
# in the repository with new, modified files containing the changes described below.
git rm -r .
cp -Recurse -Force ~/werf-guide/guides/examples/nodejs/040_db/* .
git add .
git commit -m WIP
What changes we will make
# Enter the command below to show the files we are going to change.
git show --stat
# Enter the command below to show the changes that will be made.
git show

Run the following commands in Bash:

# Clone the example repository to ~/werf-guide/guides (if you have not cloned it yet).
test -e ~/werf-guide/guides || git clone https://github.com/werf/website ~/werf-guide/guides

# Copy the (unchanged) application files to ~/werf-guide/app.
rm -rf ~/werf-guide/app
cp -rf ~/werf-guide/guides/examples/nodejs/030_assets ~/werf-guide/app

# Make the ~/werf-guide/app directory a git repository.
cd ~/werf-guide/app
git init
git add .
git commit -m initial

# To see what changes we will make later in this chapter, let's replace all the application files
# in the repository with new, modified files containing the changes described below.
git rm -r .
cp -rf ~/werf-guide/guides/examples/nodejs/040_db/. .
git add .
git commit -m WIP
What changes we will make
# Enter the command below to show files we are going to change.
git show --stat
# Enter the command below to show the changes that will be made.
git show

Making our application stateful

At this point, our application does not use a database and does not store any data (i.e., it is stateless). To make it stateful, we need to add libraries for working with a database and configure the application to use them.

The following changes have been made to our application:

  1. Dependencies for working with the MySQL database are installed.
  2. Access to the database is configured.
  3. Database model and schema migration are added.
  4. Routes that use the new model are added.

We used the following command to install the new dependencies:

npm i mysql2 sequelize sequelize-cli express-async-handler
  • mysql2 — a MySQL driver;
  • sequelize — an ORM for the database;
  • sequelize-cli — a command-line tool for generating models and migrations;
  • express-async-handler — a wrapper over the query handlers to write async/await code.

Also, a sequelize configuration file was created. It defines the structure of sequelize files and the database connection configuration. All the DB-related directories are located in the db directory while the configuration is included in config/database.json:

const path = require('path');

module.exports = {
  config: path.resolve('config', 'database.json'),
  'models-path': path.resolve('db', 'models'),
  'seeders-path': path.resolve('db', 'seeders'),
  'migrations-path': path.resolve('db', 'migrations'),
};
const path = require('path'); module.exports = { config: path.resolve('config', 'database.json'), 'models-path': path.resolve('db', 'models'), 'seeders-path': path.resolve('db', 'seeders'), 'migrations-path': path.resolve('db', 'migrations'), };

Adding /remember and /say endpoints to the application

Let’s add two new endpoints to our application. The /remember endpoint will store the data to the database while the /say endpoint will retrieve the data from it.

Below are our listings for a new controller and model:

//@ts-check
const express = require('express');
const router = express.Router();
const asyncHandler = require('express-async-handler');

module.exports = (db) => {
  router.get(
    '/say',
    asyncHandler(async (req, res) => {
      try {
        const talker = await db.sequelize.transaction(async (t) => {
          return await db.Talker.findOne({ where: { id: 1 }, transaction: t });
        });

        if (!talker) {
          res.send(`I have nothing to say.\n`);
          return;
        }

        res.send(`${talker.answer}, ${talker.name}!\n`);
      } catch (e) {
        res.status(500).send(`Something went wrong: ${e.message}\n`);
      }
    })
  );

  router.get(
    '/remember',
    asyncHandler(async (req, res) => {
      const { answer, name } = req.query;
      if (!answer) {
        res.status(422).send('You forgot the answer :(\n');
        return;
      }

      if (!name) {
        res.status(422).send('You forgot the name :(\n');
        return;
      }

      try {
        await db.sequelize.transaction(async (t) => {
          const [talker] = await db.Talker.findOrCreate({
            where: { id: 1 },
            transaction: t,
          });
          talker.set({ answer, name });
          await talker.save({ transaction: t });
        });

        res.send(`Got it.\n`);
      } catch (e) {
        res.status(500).send(`Something went wrong: ${e.message}\n`);
      }
    })
  );

  return router;
};
//@ts-check const express = require('express'); const router = express.Router(); const asyncHandler = require('express-async-handler'); module.exports = (db) => { router.get( '/say', asyncHandler(async (req, res) => { try { const talker = await db.sequelize.transaction(async (t) => { return await db.Talker.findOne({ where: { id: 1 }, transaction: t }); }); if (!talker) { res.send(`I have nothing to say.\n`); return; } res.send(`${talker.answer}, ${talker.name}!\n`); } catch (e) { res.status(500).send(`Something went wrong: ${e.message}\n`); } }) ); router.get( '/remember', asyncHandler(async (req, res) => { const { answer, name } = req.query; if (!answer) { res.status(422).send('You forgot the answer :(\n'); return; } if (!name) { res.status(422).send('You forgot the name :(\n'); return; } try { await db.sequelize.transaction(async (t) => { const [talker] = await db.Talker.findOrCreate({ where: { id: 1 }, transaction: t, }); talker.set({ answer, name }); await talker.save({ transaction: t }); }); res.send(`Got it.\n`); } catch (e) { res.status(500).send(`Something went wrong: ${e.message}\n`); } }) ); return router; };

The following command was used for generating a model and a migration:

npx sequelize-cli model:generate --name Talker --attributes answer:string,name:string
'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
  class Talker extends Model {
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) {
      // Define association here.
    }
  }
  Talker.init(
    {
      answer: DataTypes.STRING,
      name: DataTypes.STRING,
    },
    {
      sequelize,
      modelName: 'Talker',
    }
  );
  return Talker;
};
'use strict'; const { Model } = require('sequelize'); module.exports = (sequelize, DataTypes) => { class Talker extends Model { /** * Helper method for defining associations. * This method is not a part of Sequelize lifecycle. * The `models/index` file will call this method automatically. */ static associate(models) { // Define association here. } } Talker.init( { answer: DataTypes.STRING, name: DataTypes.STRING, }, { sequelize, modelName: 'Talker', } ); return Talker; };

Let’s add new paths to the route list:

...
const talkersRouter = require('./routes/talkers');
...
app.use('/', talkersRouter(db));
... const talkersRouter = require('./routes/talkers'); ... app.use('/', talkersRouter(db));

The migration was generated automatically together with the model:

'use strict';
module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('Talkers', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      answer: {
        type: Sequelize.STRING,
      },
      name: {
        type: Sequelize.STRING,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
    });
  },
  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('Talkers');
  },
};
'use strict'; module.exports = { up: async (queryInterface, Sequelize) => { await queryInterface.createTable('Talkers', { id: { allowNull: false, autoIncrement: true, primaryKey: true, type: Sequelize.INTEGER, }, answer: { type: Sequelize.STRING, }, name: { type: Sequelize.STRING, }, createdAt: { allowNull: false, type: Sequelize.DATE, }, updatedAt: { allowNull: false, type: Sequelize.DATE, }, }); }, down: async (queryInterface, Sequelize) => { await queryInterface.dropTable('Talkers'); }, };

New endpoints, /remember and /say, are ready.

Deploying a MySQL database and connecting to it

In real life, a database can be a part of the Kubernetes infrastructure or run outside of it. Outside of Kubernetes, you can deploy and maintain a database yourself or use a managed solution like Amazon RDS. For illustrative purposes, let’s deploy a MySQL database inside the Kubernetes cluster using the following basic StatefulSet:

apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: mysql
spec:
  serviceName: mysql
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        ports:
        - containerPort: 3306
        env:
        - name: MYSQL_ROOT_PASSWORD
          value: password
        volumeMounts:
        - name: mysql-data
          mountPath: /var/lib/mysql
  volumeClaimTemplates:
  - metadata:
      name: mysql-data
    spec:
      accessModes: ["ReadWriteOnce"]
      resources:
        requests:
          storage: 100Mi

---
apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  selector:
    app: mysql
  ports:
  - port: 3306
apiVersion: apps/v1 kind: StatefulSet metadata: name: mysql spec: serviceName: mysql selector: matchLabels: app: mysql template: metadata: labels: app: mysql spec: containers: - name: mysql image: mysql:5.7 ports: - containerPort: 3306 env: - name: MYSQL_ROOT_PASSWORD value: password volumeMounts: - name: mysql-data mountPath: /var/lib/mysql volumeClaimTemplates: - metadata: name: mysql-data spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 100Mi --- apiVersion: v1 kind: Service metadata: name: mysql spec: selector: app: mysql ports: - port: 3306

Note that you can also use a database deployed differently. In this case, you will not need the above StatefulSet, while all further steps remain unchanged.

Now let’s configure our application to use the new database:

{
  "development": {
    "username": "root",
    "password": "null",
    "database": "werf-guide-app",
    "host": "mysql",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "werf-guide-app",
    "host": "mysql",
    "dialect": "mysql"
  },
  "production": {
    "username": "root",
    "password": "password",
    "database": "werf-guide-app",
    "host": "mysql",
    "dialect": "mysql"
  }
}
{ "development": { "username": "root", "password": "null", "database": "werf-guide-app", "host": "mysql", "dialect": "mysql" }, "test": { "username": "root", "password": null, "database": "werf-guide-app", "host": "mysql", "dialect": "mysql" }, "production": { "username": "root", "password": "password", "database": "werf-guide-app", "host": "mysql", "dialect": "mysql" } }

Sequelize assumes that connections for all environments are defined in a single file. For now, the production environment’s data are hardcoded. However, we will fix this in future chapters of the guide.

Great, the database and the application are ready to be deployed.

Initializing and migrating the database

There are several ways to initialize and migrate a database when deploying applications to Kubernetes. We will use one straightforward yet efficient method. It involves database migration (and initialization, if needed) by a dedicated Job simultaneously with deploying the application and the database.

We require the following conditions to be met to deploy resources in the desired order:

  1. Before starting, the job to initialize/migrate the database must make sure the database is available and ready.
  2. Before starting, applications must wait until the database is available and ready and all migrations are complete.

This way, all K8s resources are created at the same time but being run in the following order:

  1. The database starts.
  2. The initialization/migration Job runs.
  3. The applications start.

Let’s now add a Job to perform database migrations/initializations:

apiVersion: batch/v1
kind: Job
metadata:
  # The release revision in the Job name will cause the Job to be recreated every time.
  # This way, we can get around the fact that the Job is immutable.
  name: "setup-and-migrate-db-rev{{ .Release.Revision }}"
spec:
  backoffLimit: 0
  template:
    spec:
      restartPolicy: Never
      imagePullSecrets:
      - name: registrysecret
      containers:
      - name: setup-and-migrate-db
        image: {{ .Values.werf.image.backend }}
        command:
        - sh
        - -euc
        - |
          is_mysql_available() {
            tries=$1
            i=0
            while [ $i -lt $tries ]; do
              mysqladmin -h mysql -P 3306 -u root -p=password ping || return 1
              i=$((i+1))
              sleep 1
            done
          }

          # Waiting for `mysqladmin ping` to run 10 times in a row.
          until is_mysql_available 10; do
            sleep 1
          done

          # Performing the initial configuration (if necessary) or migrations.
          ./node_modules/.bin/sequelize-cli db:create
          ./node_modules/.bin/sequelize-cli db:migrate
        env:
        - name: NODE_ENV
          value: production
apiVersion: batch/v1 kind: Job metadata: # The release revision in the Job name will cause the Job to be recreated every time. # This way, we can get around the fact that the Job is immutable. name: "setup-and-migrate-db-rev{{ .Release.Revision }}" spec: backoffLimit: 0 template: spec: restartPolicy: Never imagePullSecrets: - name: registrysecret containers: - name: setup-and-migrate-db image: {{ .Values.werf.image.backend }} command: - sh - -euc - | is_mysql_available() { tries=$1 i=0 while [ $i -lt $tries ]; do mysqladmin -h mysql -P 3306 -u root -p=password ping || return 1 i=$((i+1)) sleep 1 done } # Waiting for `mysqladmin ping` to run 10 times in a row. until is_mysql_available 10; do sleep 1 done # Performing the initial configuration (if necessary) or migrations. ./node_modules/.bin/sequelize-cli db:create ./node_modules/.bin/sequelize-cli db:migrate env: - name: NODE_ENV value: production
Why wait for ten consecutive successful DB availability checks?

Running mysqladmin ping, say, once may result in an incorrect conclusion that the database is ready. This is because the check has been completed before MySQL’s StatefulSet restart during deployment. In such cases, the database may be unavailable during initialization/migrations.

In addition, in database images, the main database process may restart several times during the first run (while the container does not restart). In this case, the single database check may show that the database is ready; next, the initialization/migration job starts only to find that the main database process is restarting. To avoid this, we run mysqladmin ping several times in a row.

You can change the number of successful checks in a row; 10 is used as an example.

apiVersion: apps/v1
kind: Deployment
metadata:
  name: werf-guide-app
spec:
  replicas: 1
  selector:
    matchLabels:
      app: werf-guide-app
  template:
    metadata:
      labels:
        app: werf-guide-app
    spec:
      imagePullSecrets:
      - name: registrysecret
      initContainers:
      - name: wait-db-readiness
        image: {{ .Values.werf.image.backend }}
        command:
        - sh
        - -euc
        - |
          # Waiting until the database is available and migrations are completed.
          until ./node_modules/.bin/sequelize-cli db:migrate:status; do
            sleep 1
          done
        env:
        - name: NODE_ENV
          value: production
      containers:
      - name: backend
        image: {{ .Values.werf.image.backend }}
        command: ["node", "./bin/www"]
        ports:
        - containerPort: 3000
        env:
        - name: NODE_ENV
          value: production
      - name: frontend
        image: {{ .Values.werf.image.frontend }}
        ports:
        - containerPort: 80
apiVersion: apps/v1 kind: Deployment metadata: name: werf-guide-app spec: replicas: 1 selector: matchLabels: app: werf-guide-app template: metadata: labels: app: werf-guide-app spec: imagePullSecrets: - name: registrysecret initContainers: - name: wait-db-readiness image: {{ .Values.werf.image.backend }} command: - sh - -euc - | # Waiting until the database is available and migrations are completed. until ./node_modules/.bin/sequelize-cli db:migrate:status; do sleep 1 done env: - name: NODE_ENV value: production containers: - name: backend image: {{ .Values.werf.image.backend }} command: ["node", "./bin/www"] ports: - containerPort: 3000 env: - name: NODE_ENV value: production - name: frontend image: {{ .Values.werf.image.frontend }} ports: - containerPort: 80

Testing the application/database

Let’s deploy the application:

werf converge --repo <DOCKER HUB USERNAME>/werf-guide-app

You should see the following output:


┌ ⛵ image backend
│ ┌ Building stage backend/dockerfile
│ │ backend/dockerfile  Sending build context to Docker daemon  315.9kB
│ │ backend/dockerfile  Step 1/26 : FROM node:12-alpine as builder
    ...
│ │ backend/dockerfile  Successfully built 968f3f3edddd
│ │ backend/dockerfile  Successfully tagged e2aff96b-4d59-4758-a10a-d186f7b6d566:latest
│ │ ┌ Store stage into <DOCKER HUB USERNAME>/werf-guide-app
│ │ └ Store stage into <DOCKER HUB USERNAME>/werf-guide-app (15.06 seconds)
│ ├ Info
│ │      name: <DOCKER HUB USERNAME>/werf-guide-app:602fe44580c949e018eadd58d8f43d155fff39068a513d27482ddb85-1637079271243
│ │        id: 968f3f3edddd
│ │   created: 2022-11-16 19:14:31 +0000 UTC
│ │      size: 46.7 MiB
│ └ Building stage backend/dockerfile (21.05 seconds)
└ ⛵ image backend (29.25 seconds)

┌ ⛵ image frontend
│ ┌ Building stage frontend/dockerfile
│ │ frontend/dockerfile  Sending build context to Docker daemon  315.9kB
│ │ frontend/dockerfile  Step 1/30 : FROM node:12-alpine as builder
    ...
│ │ frontend/dockerfile  Successfully built 8283025c5383
│ │ frontend/dockerfile  Successfully tagged 9dd1b277-83e3-4f2e-a84d-72bb41166670:latest
│ │ ┌ Store stage into <DOCKER HUB USERNAME>/werf-guide-app
│ │ └ Store stage into <DOCKER HUB USERNAME>/werf-guide-app (11.91 seconds)
│ ├ Info
│ │      name: <DOCKER HUB USERNAME>/werf-guide-app:ef127812b5a9205c59428381e5d09bad041058e6366ae9d254a59df8-1637079271091
│ │        id: 8283025c5383
│ │   created: 2022-11-16 19:14:30 +0000 UTC
│ │      size: 9.4 MiB
│ └ Building stage frontend/dockerfile (17.94 seconds)
└ ⛵ image frontend (25.53 seconds)

┌ Waiting for release resources to become ready
│ ┌ Status progress
│ │ DEPLOYMENT                                                                    REPLICAS      AVAILABLE       UP-TO-DATE
│ │ werf-guide-app                                                                2/1           1               1
│ │ │   POD                          READY      RESTARTS      STATUS              ---
│ │ ├── guide-app-54784dd77-kkhhb    0/2        0             Init:0/1            Waiting for: replicas 2->1                          ↵
│ │
│ │ └── guide-app-84fbcf787f-69zmm   2/2        0             Running
│ │ STATEFULSET                                                                   REPLICAS      READY           UP-TO-DATE
│ │ mysql                                                                         1/1           1               1
│ │ JOB                                                                           ACTIVE        DURATION        SUCCEEDED/FAILED
│ │ setup-and-migrate-db-rev17                                                    1             0s              0/0                   ↵
│ │
│ │ │   POD                          READY      RESTARTS      STATUS              ---
│ │ └── and-migrate-db-rev17--1-bbsp 0/1        0             ContainerCreating   Waiting for: pods should be terminated, succeeded 0->1
│ │     2
│ └ Status progress
│
│ ┌ job/setup-and-migrate-db-rev17 po/setup-and-migrate-db-rev17--1-bbsp2 container/setup-and-migrate-db logs
│ │ mysqladmin: connect to server at 'mysql' failed
│ │ error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
│ │ mysqladmin: connect to server at 'mysql' failed
│ │ error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
│ │ mysqladmin: connect to server at 'mysql' failed
│ │ error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
│ │ mysqladmin: connect to server at 'mysql' failed
│ │ error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
│ └ job/setup-and-migrate-db-rev17 po/setup-and-migrate-db-rev17--1-bbsp2 container/setup-and-migrate-db logs
│
│ ┌ deploy/werf-guide-app po/werf-guide-app-54784dd77-kkhhb container/wait-db-readiness logs
│ │
│ │ Sequelize CLI [Node: 12.22.7, CLI: 6.3.0, ORM: 6.9.0]
│ │
│ │ Loaded configuration file "config/database.json".
│ │ Using environment "production".
│ │ up 20211101064002-create-talker.js
│ └ deploy/werf-guide-app po/werf-guide-app-54784dd77-kkhhb container/wait-db-readiness logs
│
│ ┌ job/setup-and-migrate-db-rev17 po/setup-and-migrate-db-rev17--1-bbsp2 container/setup-and-migrate-db logs
│ │ mysqladmin: connect to server at 'mysql' failed
│ │ error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
│ └ job/setup-and-migrate-db-rev17 po/setup-and-migrate-db-rev17--1-bbsp2 container/setup-and-migrate-db logs
│
│ ┌ Status progress
│ │ DEPLOYMENT                                                                    REPLICAS      AVAILABLE       UP-TO-DATE
│ │ werf-guide-app                                                                2/1           1               1
│ │ │   POD                          READY      RESTARTS      STATUS              ---
│ │ ├── guide-app-54784dd77-kkhhb    0/2        0             Init:0/1 ->         Waiting for: replicas 2->1                          ↵
│ │
│ │ │                                                         PodInitializing
│ │ └── guide-app-84fbcf787f-69zmm   2/2        0             Running
│ │ STATEFULSET                                                                   REPLICAS      READY           UP-TO-DATE
│ │ mysql                                                                         1/1           1               1
│ │ JOB                                                                           ACTIVE        DURATION        SUCCEEDED/FAILED
│ │ setup-and-migrate-db-rev17                                                    1             6s              0/0                   ↵
│ │
│ │ │   POD                          READY      RESTARTS      STATUS              ---
│ │ └── and-migrate-db-rev17--1-bbsp 1/1        0             ContainerCreating   Waiting for: pods should be terminated, succeeded 0->1
│ │     2                                                     -> Running
│ └ Status progress
│
│ ┌ job/setup-and-migrate-db-rev17 po/setup-and-migrate-db-rev17--1-bbsp2 container/setup-and-migrate-db logs
│ │ mysqladmin: connect to server at 'mysql' failed
│ │ error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
│ │ mysqladmin: connect to server at 'mysql' failed
│ │ error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
│ │ mysqladmin: connect to server at 'mysql' failed
│ │ error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
│ │ mysqladmin: connect to server at 'mysql' failed
│ │ error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
│ └ job/setup-and-migrate-db-rev17 po/setup-and-migrate-db-rev17--1-bbsp2 container/setup-and-migrate-db logs
│
│ ┌ Status progress
│ │ DEPLOYMENT                                                                    REPLICAS      AVAILABLE       UP-TO-DATE
│ │ werf-guide-app                                                                2->1/1        1               1
│ │ │   POD                          READY      RESTARTS      STATUS
│ │ ├── guide-app-54784dd77-kkhhb    2/2        0             PodInitializing ->
│ │ │                                                         Running
│ │ └── guide-app-84fbcf787f-69zmm   2/2        0             Running ->
│ │                                                           Terminating
│ │ STATEFULSET                                                                   REPLICAS      READY           UP-TO-DATE
│ │ mysql                                                                         1/1           1               1
│ │ JOB                                                                           ACTIVE        DURATION        SUCCEEDED/FAILED
│ │ setup-and-migrate-db-rev17                                                    1             6s              0/0                   ↵
│ │
│ │ │   POD                          READY      RESTARTS      STATUS              ---
│ │ └── and-migrate-db-rev17--1-bbsp 1/1        0             Running             Waiting for: pods should be terminated, succeeded 0->1
│ │     2
│ └ Status progress
│
│ ┌ job/setup-and-migrate-db-rev17 po/setup-and-migrate-db-rev17--1-bbsp2 container/setup-and-migrate-db logs
│ │ mysqladmin: connect to server at 'mysql' failed
│ │ error: 'Access denied for user 'root'@'172.17.0.1' (using password: YES)'
│ │
│ │ Sequelize CLI [Node: 12.22.7, CLI: 6.3.0, ORM: 6.9.0]
│ │
│ │ Loaded configuration file "config/database.json".
│ │ Using environment "production".
│ │ No migrations were executed, database schema was already up to date.
│ └ job/setup-and-migrate-db-rev17 po/setup-and-migrate-db-rev17--1-bbsp2 container/setup-and-migrate-db logs
│
│ ┌ Status progress
│ │ DEPLOYMENT                                                                    REPLICAS      AVAILABLE       UP-TO-DATE
│ │ werf-guide-app                                                                1/1           1               1
│ │ │   POD                          READY      RESTARTS      STATUS
│ │ ├── guide-app-54784dd77-kkhhb    2/2        0             Running
│ │ └── guide-app-84fbcf787f-69zmm   2/2        0             Terminating
│ │ STATEFULSET                                                                   REPLICAS      READY           UP-TO-DATE
│ │ mysql                                                                         1/1           1               1
│ │ JOB                                                                           ACTIVE        DURATION        SUCCEEDED/FAILED
│ │ setup-and-migrate-db-rev17                                                    0             17s             0->1/0                ↵
│ │
│ │ │   POD                          READY      RESTARTS      STATUS
│ │ └── and-migrate-db-rev17--1-bbsp 0/1        0             Running ->
│ │     2                                                     Completed
│ └ Status progress
└ Waiting for release resources to become ready (17.54 seconds)

Release "werf-guide-app" has been upgraded. Happy Helming!
NAME: werf-guide-app
LAST DEPLOYED: Tue Nov 16 19:14:53 2022
NAMESPACE: werf-guide-app
STATUS: deployed
REVISION: 17
TEST SUITE: None
Running time 54.20 seconds

Don’t worry if the process seems to be stuck at this point and many errors appear in the messages. This happens due to checking the MySQL status; you just need to wait a bit when it’s done (usually, it takes no more than 1-2 minutes).

Now let’s try to access the /say endpoint that retrieves the data from the database:

curl http://werf-guide-app.test/say

Since the database is still empty, it should return the following message:

I have nothing to say.

Let’s save some data to the database using /remember:

curl "http://werf-guide-app.test/remember?answer=Love+you&name=sweetie"

The database must respond with the following:

Got it.

Let’s try to retrieve the data from the database using the /say endpoint once again:

curl http://werf-guide-app.test/say

If successful, you will see the following output:

Love you, sweetie!

You can also make sure that the data is in the database by directly querying the table contents:

kubectl exec -it statefulset/mysql -- mysql -ppassword -e "SELECT * from Talkers" werf-guide-app

You should see the following output:

+----+----------+---------+---------------------+---------------------+
| id | answer   | name    | createdAt           | updatedAt           |
+----+----------+---------+---------------------+---------------------+
|  1 | Love you | sweetie | 2021-11-11 16:28:44 | 2021-11-11 16:28:44 |
+----+----------+---------+---------------------+---------------------+

Done!

In this chapter, we turned our application into a stateful one by connecting it to the corresponding database. We deployed the database to the Kubernetes cluster, initialized it, and performed necessary DB migrations. Note that the above approach should work well with any relational database.

As usual, you can see all the changes made in this chapter by running the commands provided at the beginning.

prev
next