This article is going to summarize how to have socket
connections between Azure container groups. The container for socket server and
client locate in different container, so they are in different virtual machines.
The sample code, for both socket server and client, is in Node.js.
Define Socket Server
The following Node.js code
creates a socket server, which listens on port 1337:
var net = require('net');
function getTimePrefix() {
return (new Date()).toString() + ' ';
}
var server = net.createServer(function(socket) {
socket.on('data', function(data) {
textChunk = data.toString('utf8');
console.log(getTimePrefix() + textChunk);
socket.write(textChunk);
});
socket.on('error', function(err) {
console.log(getTimePrefix() + err)
})
});
server.listen(1337);
Build Docker Image for Socket Server
The following is the Dockerfile for the socket server:
FROM node:8.2.0-alpine
RUN mkdir -p /usr/src/app
COPY ./app/* /usr/src/app/
WORKDIR /usr/src/app
RUN npm install
EXPOSE 1337/tcp
CMD node /usr/src/app/server.js
Notice it exposes port 1337.
I’ve published the image as containerinstance/socket-server:20180802
in Docker hub.
Create Container Group for Socket Server
We may create a container group for the socket server, with
the following definition:
{
"properties": {
"containers": [
{
"name": "socket-server",
"properties": {
"command": [],
"image": "containerinstance/socket-server:20180802",
"ports": [
{
"port": 1337
}
],
"resources": {
"requests": {
"cpu": 1,
"memoryInGb": 1.5
}
}
}
}
],
"osType": "Linux",
"ipAddress": {
"ports": [
{
"protocol": "TCP",
"port": 1337
}
],
"type": "Public"
}
},
"location": "westus"
}
We can get the public IP address when the container group is created successfully.
Define Socket Client
The following Node.js code
creates 200 socket clients, which try to connect to the socket server
concurrently:
var connections = 200;
const cp = require('child_process');
var net = require('net');
var host = process.env.SOCKET_SERVER_HOST;
var port = process.env.SOCKET_SERVER_PORT;
console.log('host: ' + host + ', port: ' + port);
function getTimePrefix() {
return (new Date()).toString() + ' ';
}
var onConnected = function(client, connection) {
return function() {
client.write(connection + ': Hello, server! Love,
Client.');
};
};
var onData = function(client, connection) {
return function(data) {
console.log(getTimePrefix() + 'Connection ' + connection + ' received: ' + data);
client.destroy();
};
}
var onClosed = function(connection) {
return function() {
console.log(getTimePrefix() + 'Connection ' + connection + ' closed');
};
};
var onError = function(connection) {
return function(err) {
console.log(getTimePrefix() + 'Connection ' + connection + ' has error: ' + err);
};
};
var startClient = function () {
for (var i = 0; i < connections; ++i) {
var client = new net.Socket();
client.connect(port, host, onConnected(client, i));
client.on('data', onData(client, i));
client.on('close', onClosed(i));
client.on('error', onError(i));
}
};
setInterval(startClient, 10000);
The host and port of the socket server are passed as environment variables.
Build Docker Image for Socket Client
The following is the Dockerfile for the socket server:
FROM node:8.2.0-alpine
RUN mkdir -p /usr/src/app
COPY ./app/* /usr/src/app/
WORKDIR /usr/src/app
RUN npm install
CMD node /usr/src/app/client.js
I’ve published the image as containerinstance/socket-client:20180802 in Docker hub.
Create Container Group for Socket Client
We may create a container group for the socket client, with
the following definition:
{
"properties": {
"containers": [
{
"name": "socket-client",
"properties": {
"command": [],
"environmentVariables": [
{
"name": "SOCKET_SERVER_HOST",
"value": "<YourSocketServerIP>"
},
{
"name": "SOCKET_SERVER_PORT",
"value": "<YourSocketServerPort>"
}
],
"image": "containerinstance/socket-client:20180802",
"ports": [
{
"port": 80
}
],
"resources": {
"requests": {
"cpu": 1,
"memoryInGb": 1.5
}
}
}
}
],
"osType": "Linux",
"ipAddress": {
"ports": [
{
"protocol": "TCP",
"port": 80
}
],
"type": "Public"
}
},
"location": "westus"
}
Remember to set the value of environment variable SOCKET_SERVER_HOST as the IP of the container group of the socket server and set the value of environment variable SOCKET_SERVER_PORT as 1337.
We can verify that the server and client talk smoothly with container logs. You may read the logs via ACI API or on Azure portal.
Notes
The sample code files for this article have been shared at https://github.com/zhedahht/ACISocketSample/tree/master/SocketBetweenContainerGroups.
No comments:
Post a Comment