Integrating Kubeless with Azure Event Hubs via Azure Functions

We need to expose our Kubeless function to the world so that it can be called by Azure Functions. Run the following command to expose the hello service:

kc expose --name hello-http svc/hello --type=LoadBalancer -n serverless

Use kubectl get svc -n serverless to get the LoadBalancer IP.

You can check whether the function works by running the following curl command:

curl -L --insecure --data '{"Hello world!"}'   --header "<Load Balancer IP"   --header "Content-Type:application/json" <Load Balancer IP>:8080

Now we can modify the Azure Function code whenever an event occurs in the user Event Hub.

Edit the function shown at the end of the previous section, as follows. Remember to replace the LoadBalancer IP with your value:

module.exports = async function (context, eventHubMessages) {
context.log(`JavaScript eventhub trigger function called for message array ${eventHubMessages}`);

eventHubMessages.forEach((message, index) => {
context.log(`Processed message ${message}`);
const http = require('http');

var postData = JSON.stringify({
'msg' : 'Hello World from Event Hub!'
});

var options = {
hostname: '<insert-your-kubeless-function-load-balancer-ip>', // <<<----- IMPORTANT CHANGE THE IP HERE
port: 8080,
path: '',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': postData.length
}
};

var req = http.request(options, (res) => {
console.log('statusCode:', res.statusCode);
console.log('headers:', res.headers);

res.on('data', (d) => {
process.stdout.write(d);
});
});

req.on('error', (e) => {
console.error(e);
});

req.write(postData);
req.end();

});
};

Click on Save and Run once you have changed the IP address.

Your Kubeless function would have been called, and you can verify this by running the following command:

kc logs -f <hello-pod-name> -n serverless

You should see an output similar to the following:

10.244.3.20 - - [18/Mar/2019:08:10:17 +0000] "GET /metrics HTTP/1.1" 200 5421 "" "Prometheus/2.6.0" 0/1312
10.244.3.1 - - [18/Mar/2019:08:10:31 +0000] "GET /healthz HTTP/1.1" 200 2 "" "kube-probe/1.9" 0/102
{'event-time': None, 'extensions': {'request': <LocalRequest: POST http://23.101.132.46:8080/>}, 'event-type': None, 'event-namespace': None, 'data': {u'msg': u'Hello World fromEvent Hub!'}, 'event-id': None}
10.244.3.1 - - [18/Mar/2019:08:10:59 +0000] "POST / HTTP/1.1" 200 38 "" "" 0/10385

You can verify the Event Hub integration by changing the names in event-sourcing-microservices-example/deployment/sbin/names-15.txt and running the following command:

cd event-sourcing-microservices-example && ./deployment/sbin/generate-serial.sh

You will see that the function is triggered in the Kubeless function logs and also in the Azure Portal, as shown in the following screenshot, by choosing the Monitor option:

The actual log entries are shown in the following screenshot. Note that it might take couple of minutes before you see the Event Hub entries:

Congratulations, you have successfully triggered a Kubeless serverless function using an Azure Function that, in turn, was triggered by an event that occurred in Event Hub. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset