In this hands-on lab, you will learn how to deploy a Node.js app with a MySQL database, secured over SSL, on a Kubernetes cluster. We will cover the following steps:
- Set up a Kubernetes cluster (You can use a cloud provider or a local cluster).
- Deploy a MySQL database with SSL support.
- Create a Node.js application.
- Configure and deploy the Node.js app with SSL connectivity to the MySQL database.
Prerequisites
Before you begin, ensure you have the following tools and resources:
- Kubernetes Cluster: You should have a working Kubernetes cluster set up. You can use a cloud provider like AWS, GCP, or a local solution like Minikube or Docker Desktop.
kubectl
: Make surekubectl
is installed and configured to connect to your Kubernetes cluster.- Docker: You’ll need Docker installed on your local machine for building and pushing Docker images.
- Node.js: Ensure Node.js is installed on your local machine.
- Helm: Helm is a package manager for Kubernetes. Make sure Helm is installed on your local machine.
- MySQL Client: You’ll need a MySQL client like
mysql
or a GUI tool to connect to the MySQL database.
Step 1: Deploy MySQL Database
In this step, you’ll deploy a MySQL database with SSL support to your Kubernetes cluster. We will use Helm to simplify the MySQL deployment.
- Create a file
mysql-values.yaml
with the following content. Make sure to replace placeholders (<your-password>
) with your actual values:
mysqlRootPassword: <your-root-password>
mysqlUser: <your-db-user>
mysqlPassword: <your-db-password>
mysqlDatabase: <your-db-name>
- Deploy the MySQL Helm chart using your
mysql-values.yaml
file:
helm repo add stable https://charts.helm.sh/stable
helm install my-mysql stable/mysql -f mysql-values.yaml
- Check the status of the MySQL pod:
kubectl get pods
Wait until the pod is in the Running
state.
- Connect to the MySQL database using a MySQL client and confirm that SSL connectivity is enabled.
Step 2: Create a Node.js Application
In this step, you’ll create a simple Node.js application that connects to the MySQL database over SSL.
- Create a directory for your Node.js application and navigate to it:
mkdir nodejs-app
cd nodejs-app
- Create a
package.json
file for your Node.js app:
npm init -y
- Install the required Node.js modules:
npm install express mysql2
- Create an
index.js
file and add the following code to set up a basic Node.js app:
const express = require('express');
const app = express();
const mysql = require('mysql2');
const connection = mysql.createConnection({
host: '<mysql-service-name>',
user: '<your-db-user>',
password: '<your-db-password>',
database: '<your-db-name>',
ssl: {
ca: fs.readFileSync('<path-to-ca-cert.pem>'),
},
});
app.get('/', (req, res) => {
connection.query('SELECT 1 + 1 AS solution', (error, results, fields) => {
if (error) throw error;
res.send(`The solution is: ${results[0].solution}`);
});
});
app.listen(3000, () => {
console.log('Node.js app is running on port 3000');
});
Ensure you replace <mysql-service-name>
, <your-db-user>
, <your-db-password>
, <your-db-name>
, and <path-to-ca-cert.pem>
with your actual values.
Step 3: Build and Dockerize the Node.js App
- Create a Dockerfile for your Node.js app in the same directory as your
index.js
file:
FROM node:14
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD [ "node", "index.js" ]
- Build a Docker image for your Node.js app:
docker build -t nodejs-app:1.0 .
- Push the Docker image to a container registry (e.g., Docker Hub, Amazon ECR) if you plan to deploy it to a Kubernetes cluster.
Step 4: Deploy the Node.js App on Kubernetes
- Create a Kubernetes Deployment YAML file, e.g.,
nodejs-app-deployment.yaml
, with the following content:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-app
spec:
replicas: 1
selector:
matchLabels:
app: nodejs-app
template:
metadata:
labels:
app: nodejs-app
spec:
containers:
- name: nodejs-app
image: nodejs-app:1.0
ports:
- containerPort: 3000
- Create a Kubernetes Service YAML file, e.g.,
nodejs-app-service.yaml
, with the following content:
apiVersion: v1
kind: Service
metadata:
name: nodejs-app-service
spec:
selector:
app: nodejs-app
ports:
- protocol: "TCP"
port: 80
targetPort: 3000
type: LoadBalancer
- Deploy the Node.js app to your Kubernetes cluster:
kubectl apply -f nodejs-app-deployment.yaml
kubectl apply -f nodejs-app-service.yaml
- Check the status of the Node.js app pod:
kubectl get pods
- Check the service status and obtain the external IP:
kubectl get svc
Step 5: Access Your Node.js App
- Use the external IP of the service from the previous step to access your Node.js app in a web browser or via
curl
:
curl http://<external-ip>
You should see the response from your Node.js app, indicating a successful connection to the MySQL database over SSL.
Congratulations! You’ve successfully deployed a Node.js app with SSL connectivity to a MySQL database on Kubernetes.
Conclusion
In this hands-on lab, you learned how to set up a Kubernetes cluster, deploy a MySQL database with SSL support, create a Node.js application, and configure SSL connectivity between the app and the database. This knowledge can be valuable for deploying secure and scalable applications in Kubernetes environments.