How to implement a custom TCP health probe for Azure Cloud Services (classic) worker roles?

hnnsj 46 Reputation points
2024-01-18T11:42:11.2166667+00:00

I have a Cloud Services (classic extended support) app that exposes a couple of UDP and a couple of TCP endpoints in a worker role. I want to add a custom health probe that probes the application state (not just the accessibility of the service itself which is what the default health probe checks if I'm not mistaken) and disable the instance in the load balancer under certain conditions.

The documentation states:

A TCP probe fails when: 1. The TCP listener on the instance doesn't respond at all during the timeout period. A probe is marked down based on the number of timed-out probe requests, which were configured to go unanswered before marking down the probe. 2. The probe receives a TCP reset from the instance.

However, I'm not aware of how to produce this effect. How can I send a TCP RST message to the probe using a C# API? Or is there another way I should implement the custom probe?

This is the code I have so far:

        var healthCheckEndpoint = RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["health"];
        var healthCheckIpEndpoint = healthCheckEndpoint.IPEndpoint;
        var server = new TcpListener(healthCheckIpEndpoint.Address, healthCheckIpEndpoint.Port);
        server.Start();

        while (true)
        {
            using (var client = server.AcceptTcpClient())
            {
                client.LingerState = new LingerOption(false, 0);
                client.Close();
            }
        }

I have also tried to call client.Client.Close() to force the underlying Socket to immediately close. However, the health check still passes. Also, when I use nc -v -w 65 localhost 10101 the connection seems to successfully close, or at least I can't see any logs that indicate that a reset was sent.

Azure Cloud Services
Azure Cloud Services
An Azure platform as a service offer that is used to deploy web and cloud applications.
665 questions
{count} votes

2 answers

Sort by: Most helpful
  1. vipullag-MSFT 25,606 Reputation points
    2024-01-18T14:19:07.62+00:00

    Hello hnnsj

    Welcome to Microsoft Q&A Platform, thanks for posting your query here.

    You can just close\stop the server. Looks like there might be a slight delay before the health probe is marked down.

    Hope this helps.


  2. Waleed Siddique 5 Reputation points
    2024-01-21T18:06:55.5033333+00:00

    Implementing a custom TCP health probe for Azure Cloud Services (classic) worker roles involves configuring the necessary settings in the ServiceDefinition.csdef file, implementing the probe logic in the worker role code, and ensuring the proper deployment process. Below is a step-by-step guide:

    Step 1: Modify ServiceDefinition.csdef

    Open the ServiceDefinition.csdef file for your Azure Cloud Services project. This file defines the configuration for your cloud service.

    <?xml version="1.0" encoding="utf-8"?>
    <ServiceDefinition name="YourCloudService" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
      <WorkerRole name="YourWorkerRole" vmsize="Small">
        <!-- ... other configuration settings ... -->
    
        <!-- Add the following for custom TCP health probe -->
        <Endpoints>
          <InputEndpoint name="TcpHealthProbe" protocol="tcp" port="8080" />
        </Endpoints>
      </WorkerRole>
    </ServiceDefinition>
    

    In this example, a new input endpoint named TcpHealthProbe is added, listening on port 8080. Adjust the port number based on your requirements.

    Step 2: Implement TCP Health Probe in Worker Role Code

    Open your worker role code (WorkerRole.cs), and modify the Run method to include the TCP health probe logic.

    public class WorkerRole : RoleEntryPoint
    {
        public override void Run()
        {
            // Implement your worker role logic here
    
            while (true)
            {
                // Custom TCP health probe logic
                var tcpListener = new TcpListener(IPAddress.Any, 8080);
                tcpListener.Start();
                var client = tcpListener.AcceptTcpClient();
                // If accepted, the probe is successful
    
                // ... rest of the worker role logic ...
            }
        }
        
        // ... other role methods ...
    }
    

    Ensure that the health probe logic appropriately checks the health of your worker role. In this example, it listens on port 8080, and a successful probe is accepted by the TcpListener.

    Step 3: Deploy and Test

    Deploy your Azure Cloud Services project to Azure using Visual Studio or the Azure Management Portal. Ensure your worker role implementation handles the custom TCP health probe requests correctly. Test the health probe endpoint using tools like Telnet or PowerShell. Test-NetConnection -ComputerName <YourCloudService>.cloudapp.net -Port 8080

    Notes:

    • Replace <YourCloudService> with the actual name of your cloud service.
    • This guide assumes the use of Azure Cloud Services (classic) and may not be applicable for newer service models like Azure Functions or Azure Service Fabric.
    • Always follow best practices for security, and consider adding proper error handling in your probe logic.

    By following these steps, you can successfully implement a custom TCP health probe for Azure Cloud Services (classic) worker roles.