enter image description hereI’m deploying a Node.js application and a MongoDB instance in a Kubernetes cluster using kind. The Node.js application is exposed via a NodePort service. However, I’m encountering a connection timeout error when the application tries to connect to MongoDB:
Error during signup: Operation users.insertOne()
buffering timed out after 10000ms
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const path = require('path');
const multer = require('multer');
const sharp = require('sharp');
const { S3Client, DeleteObjectCommand, PutObjectCommand } = require('@aws-sdk/client-s3');
require('dotenv').config();
const app = express();
// AWS S3 configuration
const s3 = new S3Client({
region: process.env.AWS_REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY_ID,
secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
},
});
// Multer configuration for S3
const upload = multer({
storage: multer.memoryStorage(),
});
// Connect to MongoDB with retry logic
const connectWithRetry = () => {
console.log('Attempting MongoDB connection...');
mongoose.connect(process.env.MONGO_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
auth: 'none',
connectTimeoutMS: 30000,
socketTimeoutMS: 45000
})
.then(() => console.log('MongoDB connected'))
.catch(err => {
console.log('MongoDB connection error:', err);
console.log('Retrying MongoDB connection in 5 seconds...');
setTimeout(connectWithRetry, 5000);
});
};
connectWithRetry();
// Define a schema
const userSchema = new mongoose.Schema({
name: String,
email: String,
phno: String,
password: String,
address: String,
imageUrl: String,
smallImageUrl: String,
additionalAddresses: [{
address: String,
phno: String,
imageUrl: String,
smallImageUrl: String,
}]
});
// Define a model
const User = mongoose.model('User', userSchema);
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
// Debugging middleware to log incoming requests
app.use((req, res, next) => {
console.log(`Incoming ${req.method} request to ${req.url}`);
next();
});
// Serve static files
app.use(express.static('views'));
// Routes
app.get('/', (req, res) => {
res.redirect('/login');
});
app.get('/signup', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'signup.html'));
});
app.post('/signup', async (req, res) => {
try {
const newUser = new User({
name: req.body.name,
email: req.body.email,
phno: req.body.phno,
password: req.body.password,
address: req.body.address,
});
await newUser.save();
res.redirect('/login');
} catch (err) {
res.status(500).send(`Error during signup: ${err.message}`);
}
});
app.get('/login', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'login.html'));
});
app.post('/login', async (req, res) => {
try {
const user = await User.findOne({ email: req.body.email, password: req.body.password });
if (user) {
res.redirect('/add-more');
} else {
res.status(401).send('Invalid login credentials');
}
} catch (err) {
res.status(500).send(`Error during login: ${err.message}`);
}
});
app.get('/user-data', async (req, res) => {
try {
const users = await User.find({});
res.json(users);
} catch (err) {
res.status(500).send(`Error fetching user data: ${err.message}`);
}
});
app.get('/fetch-user-data', async (req, res) => {
try {
const users = await User.find({});
res.json(users);
} catch (err) {
res.status(500).send(`Error fetching user data: ${err.message}`);
}
});
app.delete('/delete-user/:email', async (req, res) => {
try {
const email = req.params.email;
const user = await User.findOne({ email });
if (user) {
user.address = "";
user.phno = "";
if (user.imageUrl) {
const mainImageKey = user.imageUrl.split('/').pop();
await s3.send(new DeleteObjectCommand({
Bucket: process.env.AWS_BUCKET_NAME,
Key: mainImageKey
}));
user.imageUrl = "";
}
await user.save();
res.json({ success: true });
} else {
res.status(404).json({ success: false, message: 'User not found' });
}
} catch (err) {
res.status(500).json({ success: false, message: `Error deleting user: ${err.message}` });
}
});
app.get('/add-more', (req, res) => {
res.sendFile(path.join(__dirname, 'views', 'add-more.html'));
});
app.post('/add-more', upload.single('image'), async (req, res) => {
try {
const user = await User.findOne({ email: req.body.email });
if (user) {
const smallImageBuffer = await sharp(req.file.buffer)
.resize({ width: 50 })
.toBuffer();
const uploadOriginal = {
Bucket: process.env.AWS_BUCKET_NAME,
Key: Date.now().toString() + '-' + req.file.originalname,
Body: req.file.buffer,
//ACL: 'public-read'
};
const [dataOriginal] = await Promise.all([
s3.send(new PutObjectCommand(uploadOriginal))
]);
const getUrl = (key) => `https://${process.env.AWS_BUCKET_NAME}.s3.${process.env.AWS_REGION}.amazonaws.com/${key}`;
user.additionalAddresses.push({
address: req.body.address,
phno: req.body.phno,
imageUrl: getUrl(uploadOriginal.Key)
});
await user.save();
res.redirect('/add-more');
} else {
res.status(404).send('User not found');
}
} catch (err) {
res.status(500).send(`Error during adding more info: ${err.message}`);
}
});
// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
this is my deplyment code to run in kubernetes cluster
apiVersion: apps/v1
kind: Deployment
metadata:
name: nodejs-deployment
spec:
replicas: 1
selector:
matchLabels:
app: nodejs
template:
metadata:
labels:
app: nodejs
spec:
containers:
- name: nodejs
image: kiranrepository2023/nodejsapp:v22
ports:
- containerPort: 3000
env:
- name: PORT
value: "3000"
- name: MONGO_URL
value: "mongodb://root:example@mongodb-service:27017/signupDB?authSource=admin"
- name: AWS_REGION
valueFrom:
configMapKeyRef:
name: app-config
key: AWS_REGION
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: aws-secrets
key: aws_access_key_id
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: aws-secrets
key: aws_secret_access_key
- name: AWS_BUCKET_NAME
valueFrom:
configMapKeyRef:
name: app-config
key: AWS_BUCKET_NAME
---
apiVersion: v1
kind: Service
metadata:
name: nodejs-service
spec:
selector:
app: nodejs
ports:
- protocol: TCP
port: 3000
targetPort: 3000
nodePort: 30010
type: NodePort
deployment code to use mongodb image to run nodejs application
apiVersion: apps/v1
kind: Deployment
metadata:
name: mongodb-deployment
spec:
selector:
matchLabels:
app: mongodb
replicas: 1
template:
metadata:
labels:
app: mongodb
spec:
containers:
- name: mongodb
image: mongo:4.4
ports:
- containerPort: 27017
resources:
requests:
memory: "2Gi"
cpu: "1"
limits:
memory: "4Gi"
cpu: "2"
env:
- name: MONGO_INITDB_ROOT_USERNAME
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-username
- name: MONGO_INITDB_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mongodb-secret
key: mongo-root-password
- name: MONGO_INITDB_DATABASE
valueFrom:
configMapKeyRef:
name: app-config
key: MONGO_INITDB_DATABASE
---
apiVersion: v1
kind: Service
metadata:
name: mongodb-service
spec:
selector:
app: mongodb
ports:
- protocol: TCP
port: 27017
targetPort: 27017
type: ClusterIP
Questions:
What could be causing the buffering timed out error in this setup?
Are there any additional configurations needed for MongoDB or Node.js to work seamlessly in a Kubernetes environment?
How can I further debug or resolve this issue?
Any help or pointers would be greatly appreciated!
KIRAN H K is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.