Thursday, August 9, 2018

Azure Container Instance (8): Deploy IIS nanoserver Image


When customers try to deploy their customized image with ACI, they may meet problems. This article provides a workaround for this problem.

Problem

If you customize an image with the following commands:

docker pull microsoft/iis:nanoserver
docker run --name 2048 -d -v C:\2048:C:\data microsoft/iis:nanoserver
docker exec -i 2048 powershell
#inside the container
mkdir C:\sitecontent
copy-item C:\data\* C:\sitecontent -Recurse
Import-Module IIS*
stop-iissite -name "Default First Site" -confirm:$false
remove-iissite -name "Default First Site" -confirm:$false
new-iissite -name demosite -physicalpath C:\sitecontent -bindinginformation "*:80:"
#exit out of the container
docker stop 2048
docker commit 2048 2048_v1
docker login
docker tag 2048_v1 <dockertag>:2048_v1
docker push <dockertag>:2048_v1

and they deploy the image with ACI, you may see the following error from log:

"Service 'w3svc' has been stopped
APPCMD failed with error code 183
Failed to update IIS configuration"

Workaround

It seems that the IIS docker image has a bug similar to https://github.com/Microsoft/iis-docker/issues/42.

We can work around this problem before the bug is fixed. If we build the docker image from Docker file, then we can successfully deploy it with ACI.

1.       Define a Dockerfile, as shown below, supposing your site content files are in the folder ./source of the path which contains the Dockerfile:

FROM microsoft/iis:nanoserver
RUN powershell -NoProfile -Command Remove-Item -Recurse C:\inetpub\wwwroot\*
WORKDIR /inetpub/wwwroot
COPY source/ .

2.       Build Docker image with command “docker build -t <your-image-name> .” and push it into Docker hub with command “docker push <your-image-name> .

3.       Deploy the image into ACI with CLI command: az container create --resource-group <your-resource-group> --name <your-container-group> --image <your-image-name> --os-type Windows --port 80 --ip-address "Public"

Thursday, August 2, 2018

Azure Container Instance (7): Socket Connections between ACI


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.

AKS (1) - Five seconds latency when resolving DNS

We intermittently meet 5s latencies in an AKS clusters with CNI when it’s resolving DNS. This article is to summarize what we have learned...