OpenShift 容器到容器本地主机的通信在 pod 中不起作用

问题描述

我正在尝试在 OpenShift 中实现 SideCar 模式。我的示例有两个项目 API。 我在不同的容器和单个 pod 中运行这两个项目。下面是我的部署配置,指定了 2 个图像。

 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: {}

我想通过 localhost 从一个容器访问 api 到另一个容器中的 api。 当我转到 Pod 终端并选择 hello-api-n 容器并执行 http://localhost:8080/FromSidecar 时,通信没有发生。

实际上,当我点击 /FromSidecar 时,内部代码实现会对 localhost:8090/hello 进行 API 调用,这是一个不同的容器。

以下是我面临的错误

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$

我试图做的例子是从下面 https://github.com/cesaroll/dotnet-sidecar

更新代码

项目 A 容器代码

 [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;

        }

项目 B 容器代码

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

解决方法

通信工作正常,因为您可以看到来自容器的响应 HTTP/1.1 404 Not Found。 错误 404 表示您正在查找的资源不存在。建立连接Connected to localhost

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...