【问题标题】:OpenShift container to container localhost communication not working in a podOpenShift 容器到容器 localhost 的通信在 pod 中不起作用
【发布时间】:2021-07-27 14:12:42
【问题描述】:

我正在尝试在 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: {}

我想通过本地主机将 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;

        }

PROJECT B 容器代码:

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

【问题讨论】:

    标签: kubernetes openshift sidecar


    【解决方案1】:

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

    【讨论】:

    • 是的,感谢您的回复,当我从容器 A 执行 curl localhost:8080/FromSidecar 时,它在内部命中 localhost:8090/hello 以响应容器 B 的响应,这个容器到容器的通信没有发生,当我从他们工作正常的个别容器做卷曲。
    • 抱歉,我无法理解您的情况。交流正在进行中,但您想在这里实现不同的目标
    • 对不起,我可以解释清楚,我在一个项目中有一个 API,并且在 openshift 的一个容器中运行,这个 API 命中另一个容器中的另一个 API。两个容器都在一个 pod 中运行。一个容器位于 8080 端口,另一个容器位于 8090。当我执行 localhost:8080/FromSidecar 时,它会调用另一个具有 8090 端口的容器中的另一个 API。这是我得到 404 的地方。我已经更新了代码以供您理解
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-13
    • 1970-01-01
    • 2016-11-17
    • 2022-10-13
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    相关资源
    最近更新 更多