【问题标题】:POD + Service Kubernetes HoverflyPOD + 服务 Kubernetes Hoverfly
【发布时间】:2020-10-02 10:41:06
【问题描述】:

我正在尝试在 k8s 集群上部署此配置:

apiVersion: v1
kind: ConfigMap
metadata:
  name: simulations-test
  labels: 
    mock-services: "true"
data:
  simulations-test.json: |
    {
      "data":{
          "pairs":[
            {
                "request":{
                  "path":[
                      {
                        "matcher":"glob",
                        "value":"*/b2io60000082"
                      }
                  ]
                },
                "response":{
                  "status":200,
                  "body":"...",
                  "encodedBody":false,
                  "headers":{
                      "Content-Type":[
                        "application/json"
                      ]
                  }
                },
                "templated":false
            },
            {
                "request":{
                  "path":[
                      {
                        "matcher":"glob",
                        "value":"*/b2io60000080"
                      }
                  ]
                },
                "response":{
                  "status":404,
                  "body":"",
                  "encodedBody":false,
                  "headers":{
                      "Content-Type":[
                        "text/plain"
                      ]
                  }
                },
                "templated":false
            }
          ]
      },
      "meta":{
          "schemaVersion":"v5",
          "hoverflyVersion":"v1.0.0"
      }
    }

---

apiVersion: v1
kind: Pod
metadata:
  name: test-mock
  labels:
    mock-services: "test"
spec:
  containers:
  - name: test-mock
    image: spectolabs/hoverfly:latest
    volumeMounts:
    - mountPath: /simulations/
      name: simulations-test
    command: ["hoverfly", "-webserver", "-import", "/simulations/simulations-test.json"]
  volumes:
  - name: simulations-test
    configMap:
      name: simulations-test

---

apiVersion: v1
kind: Service
metadata:
  labels:
  name: test-mock-service
spec:
  ports:
    - name: "admin"
      port: 8888
      protocol: TCP
      targetPort: 8888
    - name: "proxy"
      protocol: TCP
      port: 8500
      targetPort: 8500
  selector:
    mock-service: "test"

当我运行这个时:

kubectl apply -f mock-service.yaml -n mock

结果:

configmap/simulations-test created
pod/test-mock created
service/test-mock-service created

但是当我尝试使用http://test-mock-service.mock.svc.cluster.local:8500/b2io60000082 访问服务时,即使在 POD 内部,我也收到连接被拒绝!

在我运行的 pod 内:

/ # netstat -tulpn
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:8500          0.0.0.0:*               LISTEN      1/hoverfly
tcp        0      0 127.0.0.1:8888          0.0.0.0:*               LISTEN      1/hoverfly

我也尝试进行部署,结果相同。

TKS!

更新 #1

当我尝试进行端口转发时,它可以工作:

k port-forward test-mock 8888:8888 8500:8500 -n mock

结果:

Forwarding from 127.0.0.1:8888 -> 8888
Forwarding from [::1]:8888 -> 8888
Forwarding from 127.0.0.1:8500 -> 8500
Forwarding from [::1]:8500 -> 8500
Handling connection for 8500
Handling connection for 8500

【问题讨论】:

  • netstat 输出表明容器中的程序仅在侦听容器私有 localhost 接口。通常在容器中运行的程序需要监听 0.0.0.0 “所有接口”。容器是否在普通 Docker 中在 Kubernetes 之外运行?你能展示设置监听器的应用程序代码吗?

标签: docker kubernetes hoverfly


【解决方案1】:

由于服务中有 targetPort: 8500,因此 pod 内的容器需要侦听端口 8500

服务中的标签是 mock-service: test,但在 pod mock-services: true 中。他们需要匹配。

apiVersion: v1
kind: Pod
metadata:
  name: test-mock
  labels:
    mock-services: "test"
spec:
  containers:
  - name: test-mock
    image: spectolabs/hoverfly:latest
    ports:
    - containerPort: 8500
    volumeMounts:
    - mountPath: /simulations/
      name: simulations-test
    command: ["hoverfly", "-webserver", "-import", "/simulations/simulations-test.json"]
  volumes:
  - name: simulations-test
    configMap:
      name: simulations-test

还要确保应用正在监听 0.0.0.0。而不是 127.0.0.1

【讨论】:

  • 配置和你描述的一样。我应该把错误的配置。所以,我得到了同样的结果。还有其他建议吗?
  • 确保应用正在监听 0.0.0.0。而不是 127.0.0.1
  • 成功了!!谢谢@ArghyaSadhu!我覆盖了 hoverfly 上的默认命令:默认是 /bin/hoverfly -listen-on-host=0.0.0.0 ,我把 /bin/hoverfly -webserver -import /simulations /simulations-test.json,我忘记了 -listen-on-host=0.0.0.0。所以最后的命令:/bin/hoverfly -webserver -import /simulations/simulations-test.json -listen-on-host=0.0.0.0。再次感谢!!!
猜你喜欢
  • 1970-01-01
  • 2020-08-04
  • 2020-12-25
  • 1970-01-01
  • 2019-11-22
  • 2019-01-29
  • 2018-09-03
  • 2022-06-16
  • 2019-07-27
相关资源
最近更新 更多