IoT edge module fails to start on kubernetes - kubernetes

I can't seem to be able to deploy a module that works on a stand alone deployment on a Kubernetes edge.
Is there anything special that needs to be done in the module?
Error thrown is as follows (thrown when trying to create hub client):
Error: connect ECONNREFUSED 127.0.0.1:35001
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
errno: 'ECONNREFUSED',
code: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 35001
}
Can someone help me? Thanks!

Related

Unable to connect the mongodb container to node container in docker

I made 3 docker containers from 2 images in this repo and 1 using MongoDB public image. I turned ON all three containers using sudo docker-compose -f docker-compose.yaml up
docker-compose.yaml is:
version: '3'
services:
frontend:
image: samar080301/mern-frontend:1.0
ports:
- 3000:3000
backend:
image: samar080301/mern-backend:1.0
ports:
- 5000:5000
mongodb:
image: mongo:latest
ports:
- 27017:27017
But the MongoDB couldn't connect with the node server and gave this error:
backend_1 | > crud-app#1.0.0 start /home/app
backend_1 | > node server.js
backend_1 |
backend_1 | (node:18) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
backend_1 | (Use `node --trace-deprecation ...` to show where the warning was created)
backend_1 | App running on port 5000
backend_1 | Error with the database! MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
backend_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
backend_1 | name: 'MongoNetworkError'
backend_1 | }]
backend_1 | at Pool.<anonymous> (/home/app/node_modules/mongodb/lib/core/topologies/server.js:438:11)
backend_1 | at Pool.emit (events.js:315:20)
backend_1 | at /home/app/node_modules/mongodb/lib/core/connection/pool.js:562:14
backend_1 | at /home/app/node_modules/mongodb/lib/core/connection/pool.js:995:11
backend_1 | at /home/app/node_modules/mongodb/lib/core/connection/connect.js:32:7
backend_1 | at callback (/home/app/node_modules/mongodb/lib/core/connection/connect.js:280:5)
backend_1 | at Socket.<anonymous> (/home/app/node_modules/mongodb/lib/core/connection/connect.js:310:7)
backend_1 | at Object.onceWrapper (events.js:422:26)
backend_1 | at Socket.emit (events.js:315:20)
backend_1 | at emitErrorNT (internal/streams/destroy.js:84:8)
backend_1 | at processTicksAndRejections (internal/process/task_queues.js:84:21)
backend_1 | (node:18) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [Error: connect ECONNREFUSED 127.0.0.1:27017
backend_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
backend_1 | name: 'MongoNetworkError'
backend_1 | }]
Code of backend/db.js:
const mongoose = require('mongoose');
// Allow Promises
mongoose.Promise = global.Promise;
// Connection
mongoose.connect('mongodb://localhost:27017/db_test', { useNewUrlParser: true });
// Validation
mongoose.connection
.once('open', () => console.log('Connected to the database!'))
.on('error', err => console.log('Error with the database!', err));
Terminal Output of docker inspect mongodb:
Terminal output after adding the mongo uri as environment variable:
backend_1 | App running on port 5000
backend_1 | (node:19) UnhandledPromiseRejectionWarning: MongooseError: The `uri` parameter to `openUri()` must be a string, got "undefined". Make sure the first parameter to `mongoose.connect()` or `mongoose.createConnection()` is a string.
backend_1 | (node:19) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
New error:
backend_1 | Error with the database! MongoNetworkError: failed to connect to server [merncrudapp_mongodb_1:27017] on first connect [Error: connect ECONNREFUSED 172.23.0.3:27017
backend_1 | at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1141:16) {
backend_1 | name: 'MongoNetworkError'
backend_1 | }]
Try to connect not using localhost but a container name. So for example, if you want to connect to MongoDB from another container (in the same docker network) you can use mongodb:27017.
It should work.
When you run docker-compose up, the following happens:
A network called MernCrudApp(takes the default name of the directory) is created.
A container is created using the frontend's configuration. It joins the network MernCrudApp under the name frontend.
A container is created using the backend's configuration. It joins the network MernCrudApp under the name backend.
A container is created using mongodb’s configuration. It joins the network MernCrudApp under the name mongodb.
now if you use mongodb://localhost:27017/db_test to connect to the db, the node app will look for MongoDB in the backend container which you will get a connection error since it does not exist.
To remedy this, change the MongoDB connection string to mongodb://mongodb:27017/db_test so that it
Aditional comments
I would recommend the following to help solve some problems you might face in the future using the current configuration.
Add the connection string as an environment variable. This will make it easier for you to change the DB instances without rebuilding the container.
Since the backend application depends on the database add depend_on on the docker-compose files so that the MongoDB container starts before the backend container
Modify your backend/db.js code.
Your are getting error because when you mention 'localhost' in code your container is trying to connect to backend container on port 2017 which not in use.
// Connection
//mongodb is a container name of mongo image
mongoose.connect('mongodb://mongodb:27017/db_test', { useNewUrlParser: true });
Pro Tip -
1)If you are in same docker network then use hostname or Docker Container Name to communicate/Link with each other.
2)Never use container IP address in code if IP's are not assign manually. Whenever you restart container it may change.

Pm2 Reboot with Next.js application

Trying to setup pm2 to restart the next.js node app on reboot of the server according to Windows: Auto start PM2 and node apps
on reboot and running pm2 ls I get :
anyone have any ideas?
C:\Users\snoruzi>pm2 ls
connect EPERM //./pipe/rpc.sock
[PM2] Spawning PM2 daemon with pm2_home=C:\Users\snoruzi\.pm2
node:events:368
throw er; // Unhandled 'error' event
^
Error: connect EPERM //./pipe/rpc.sock
at PipeConnectWrap.afterConnect [as oncomplete] (node:net:1161:16)
Emitted 'error' event on ReqSocket instance at:
at Socket.<anonymous> (C:\Users\snoruzi\AppData\Roaming\npm\node_modules\pm2\node_modules\pm2-axon\lib\sockets\sock.js:201:49)
at Socket.emit (node:events:390:28)
at emitErrorNT (node:internal/streams/destroy:157:8)
at emitErrorCloseNT (node:internal/streams/destroy:122:3)
at processTicksAndRejections (node:internal/process/task_queues:83:21) {
errno: -4048,
code: 'EPERM',
syscall: 'connect',
address: '//./pipe/rpc.sock'
}
The app is started by running yarn pm2
which runs the file pm2.json - which tells pm2 where the entry point of the node application is
{
"apps": [
{
"name": "centra-website",
"script": "node_modules/next/dist/bin/next",
"args": "start",
"cwd": "./",
"instances": "max",
"exec_mode": "cluster"
}
]
}
Service seems to be added fine and the pathing to executable looks correct
Try making sure that your command prompt is run as administrator. This fixed the issue for me.

Can not connect MongoDB docker-compose

Can not connect mongodb run in docker-compose
MONGO_URL=mongodb://db:27017/500ae_cms
error:
MongoDB connection error: mongodb://db:27017/500ae_cms.
MongoNetworkError: failed to connect to server [db:27017] on first connect [Error: getaddrinfo ENOTFOUND db
at GetAddrInfoReqWrap.onlookup [as oncomplete] (dns.js:60:26) {
name: 'MongoNetworkError',
errorLabels: [Array],
}]
docker-compose file
For your db service you expose port 27775 but in your app you try to connect to 27017.
Docker resolves the service name in an IP address but this fails here (too?)
failed to connect to server [db:27017];;; getaddrinfo ENOTFOUND db
should at least be something like:
failed to connect to server [db:27017] on first connect [Error: connect ECONNREFUSED 17.19.0.2:27017
Can you try running nslookup db from the api container? If that resolves, then it might be an issue with your mongo driver.

How to connect to external Mongo (MLab) from a Node app in Docker

I have a database setup on Mlab and I have aconnection string that does work remotely. If I run my node app on docker using my local mongo, everything works as expected, however if I change the configuration and connect to my Mlab mongo, I get the following error:
UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [localhost:27017] on first connect [MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017]
at Pool.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/topologies/server.js:564:11)
at emitOne (events.js:116:13)
at Pool.emit (events.js:211:7)
at Connection.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/pool.js:317:12)
at Object.onceWrapper (events.js:317:30)
at emitTwo (events.js:126:13)
at Connection.emit (events.js:214:7)
at Socket.<anonymous> (/usr/src/app/node_modules/mongodb-core/lib/connection/connection.js:246:50)
at Object.onceWrapper (events.js:315:30)
at emitOne (events.js:116:13)
at Socket.emit (events.js:211:7)
at emitErrorNT (internal/streams/destroy.js:66:8)
at _combinedTickCallback (internal/process/next_tick.js:139:11)
at process._tickDomainCallback (internal/process/next_tick.js:219:9)
(node:24) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 6)
Apart from handling the error on Node side (which I'm not catching, but that's another problem), how can I connect to an external mongo server form my Docker container?
I've seen this and follow the steps described there but it didn't help me.
Any suggestions?
EDIT
This is a Node app. In my Dockerfile I have the following setting:
ENV DB_HOST=mongodb+srv://<user>:<password>#<mlab>/<db_name>?retryWrites=true
And in my Node app where I have setup the database I have this:
mongoose.connect(process.env.DB_HOST, {useNewUrlParser: true, useFindAndModify: false, useCreateIndex: true});
When I print the above env variable at run time, I get the correct string for the db (running locally everything works fine).
I don't understand why the error shows localhost. This is my first time using Docker. Do I need any additional configuration so my container can talk to an external service?

MongoError: failed to connect to server [192.168.100.2:9001] on first connect - Meteor

I have a Meteor Application. I am trying to deploy my application by the custom deployment procedure (on my local machine) by following this article:
https://guide.meteor.com/deployment.html#custom-deployment
But I am facing this error when I run the application via node:
MongoError: failed to connect to server [192.168.100.2:9001] on first connect [MongoError: connect ECONNREFUSED 192.168.100.2:9001]
at Object.wait (/home/martini-henry/myapp-build/bundle/programs/server/node_modules/fibers/future.js:449:15)
at new MongoConnection (packages/mongo/mongo_driver.js:210:27)
at new MongoInternals.RemoteCollectionDriver (packages/mongo/remote_collection_driver.js:4:16)
at Object.<anonymous> (packages/mongo/remote_collection_driver.js:38:10)
at Object.defaultRemoteCollectionDriver (packages/underscore.js:784:19)
at new Mongo.Collection (packages/mongo/collection.js:99:40)
at AccountsServer.AccountsCommon (packages/accounts-base/accounts_common.js:23:18)
at AccountsServer (packages/accounts-base/accounts_server.js:18:5)
at server_main.js (packages/accounts-base/server_main.js:9:12)
at fileEvaluate (packages/modules-runtime.js:343:9)
- - - - -
at Pool.<anonymous> (/home/martini-henry/myapp-build/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/topologies/server.js:336:35)
at emitOne (events.js:96:13)
at Pool.emit (events.js:188:7)
at Connection.<anonymous> (/home/martini-henry/myapp-build/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/pool.js:280:12)
at Connection.g (events.js:292:16)
at emitTwo (events.js:106:13)
at Connection.emit (events.js:191:7)
at Socket.<anonymous> (/home/martini-henry/myapp-build/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/connection.js:187:49)
at Socket.g (events.js:292:16)
at emitOne (events.js:96:13)
Steps I performed:
First I came into the Meteor App directory and ran:
meteor build ../myapp-build --architecture os.linux.x86_64
Then I provided me the myapp.tar.gz and I extracted that.
Than I did:
- cd programs/server && npm install
- export MONGO_URL='mongodb://martini-henry:123456#192.168.100.2:9001/myappdb'
- export ROOT_URL='http://192.168.100.2:9000'
And at last when I ran:
- cd ../.. node main.js
I got that error above. What could be wrong here? Can anyone help me what I am doing wrong here?