OpenShift container to container localhost communication not working in a pod

1.3k views Asked by At

I am trying to implement a SideCar pattern in OpenShift. My example has two Project API's. I am running the two Projects in different containers and in a single pod. Below is my deployment config specifying 2 images.

 containers:
        - env:
            - name: ASPNETCORE_URLS
              value: 'http://*:8080'
          image: 'docker-registry.default.svc:5000/it-mfg/hello-api-n:4.0'
          imagePullPolicy: Always
          name: hello-api-n
          ports:
            - containerPort: 8080
              name: http
              protocol: TCP
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
        - env:
            - name: ASPNETCORE_URLS
              value: 'http://*:8090'
          image: 'docker-registry.default.svc:5000/it-mfg/hello-sidecar-api-n:4.0'
          imagePullPolicy: Always
          name: hello-sidecar-api-n
          ports:
            - containerPort: 8090
              name: http
              protocol: TCP
          resources: {}

I want to hit the api from one container to api in another container through localhost. When I go to the Pod Terminal and select hello-api-n container and do http://localhost:8080/FromSidecar, the communication is not happenning.

Actually when I hit /FromSidecar the internal code implementaion makes a API call to localhost:8090/hello which is a different container.

Below is the error I am facing

I have no name!@hello-api-n-deployment-config-3-xvlsd:/app$ curl -v http://localhost:8080/FromSidecar
* Uses proxy env variable no_proxy == 'localhost'
*   Trying ::1:8080...
* TCP_NODELAY set
* Connected to localhost (::1) port 8080 (#0)
> GET /FromSidecar HTTP/1.1
> Host: localhost:8080
> User-Agent: curl/7.68.0
> Accept: */*
>
* Mark bundle as not supporting multiuse
< HTTP/1.1 404 Not Found
< Date: Wed, 05 May 2021 09:00:22 GMT
< Server: Kestrel
< Content-Length: 0
<
* Connection #0 to host localhost left intact
I have no name!@hello-api-n-deployment-config-3-xvlsd:/app$

The example I am trying to do is from below https://github.com/cesaroll/dotnet-sidecar

UPDATED CODE:

Project A Container code:

 [HttpGet]
        [Route("FromSidecar")]
        public async Task<IActionResult> GetFromSidecar()
        {
            return Ok(await _helloService.RetrieveHelloMessage());
        }

HelloService:

 public async Task<HelloMessage> RetrieveHelloMessage()
        {
            //string sidecarUrl = configuration.GetSection("SideCar").GetSection("SideCarUrl").Value;
            
           **string URL = "http://localhost:8090/Hello" /// hitting Container B**
            var client = new HttpClient();

            var response = client.GetAsync(URL).Result;

            if (!response.IsSuccessStatusCode)
            {
                return new HelloMessage()
                {
                    Message = $"Error Status Code: {response.StatusCode}, Reason: {response.ReasonPhrase}"
                };
            }
                
            _logger.LogInformation(response.Content.ReadAsStringAsync().Result);
                
            var helloMessage = await response.Content.ReadFromJsonAsync<HelloMessage>();
                
            _logger.LogInformation(helloMessage?.Message);

            return helloMessage;

        }

PROJECT B Container code:

  public IActionResult Get()
        {
            return Ok(new {Message = "Hello from Sidecar Api"});
        }
1

There are 1 answers

3
Suresh Vishnoi On

The communication is working fine, as you can see the response HTTP/1.1 404 Not Found from the container. The error 404 means the resource you are looking is not there. The connection is established Connected to localhost