1

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"});
        }
user804401
  • 1,990
  • 9
  • 38
  • 71

1 Answers1

0

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

Suresh Vishnoi
  • 17,341
  • 8
  • 47
  • 55
  • Yes, Thanks for your reply, when I do curl http://localhost:8080/FromSidecar from Container A, it internall hits http://localhost:8090/hello to the response from Container B, this container to container communication is not happenning, when I do Curl from Individual containers they are working fine. – user804401 May 05 '21 at 14:45
  • sorry, I could not understand your situation. Communication is happening, but you want to achieve something different here – Suresh Vishnoi May 05 '21 at 14:58
  • Sorry that I Could explain clearly, I have one API in a project and is running in a container in openshift, This API hits another API in another container. Both the containers are running in a single pod. One Container in Port 8080 and another in 8090. When I do http://localhost:8080/FromSidecar it calls the another API in another container with port 8090. Here is where I am getting 404. I have updated the code for your understanding – user804401 May 05 '21 at 15:21