Search code examples
kubernetesserviceclientcluster-computingcall

Kubernetes-Client Rest Call from Client to Service or Pod


I have a Kubernetes cluster which i manage provisioning of configs, storages, services and statefulsets using the kubernetes-client library for .NET. The provisioning is working fine and the necessary resources are up and running.

Normally, I also have provisioned some ingresses, because there is the need to make some web calls in order to create some connectors, for example debezium connectors:

curl -i -X POST -H "Accept:application/json" -H "Content-Type:application/json" <domain or ip>:8083/connectors/ --data "@connectors/source.json"

Call something like this though the kubernetes client programmatically

http://<servicename>.<namespace>.svc.cluster.local:<port>/connectors/

Is possible to make a call to the service from inside the cluster as I am using already the kubernetes client without using any ingress and public calls?


Solution

  • I was able to do call from my application to the pods of the cluster using the Kubernetes .NET SDK by using the following method:

    private static async Task ExecInPod(IKubernetes client, string podname, string space, string[] command, string container)
    {
        var webSocket = await client.WebSocketNamespacedPodExecAsync(podname, space, command, container, true, true, true, true).ConfigureAwait(true);
    
        var buffer = new byte[1024 * 4];
        var receiveResult = await webSocket.ReceiveAsync(
        new ArraySegment<byte>(buffer), CancellationToken.None);
    
        while (!receiveResult.CloseStatus.HasValue)
        {
            await webSocket.SendAsync(
            new ArraySegment<byte>(buffer, 0, receiveResult.Count),
            receiveResult.MessageType,
            receiveResult.EndOfMessage,
            CancellationToken.None);
    
            receiveResult = await webSocket.ReceiveAsync(
                new ArraySegment<byte>(buffer), CancellationToken.None);
    
        }
    
        await webSocket.CloseAsync(
            receiveResult.CloseStatus.Value,
            receiveResult.CloseStatusDescription,
            CancellationToken.None);
    }