簡體   English   中英

POD + 服務 Kubernetes Hoverfly

[英]POD + Service Kubernetes Hoverfly

我正在嘗試在 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 內部,連接也被拒絕!

在我運行的吊艙內:

/ # 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

由於您在服務中有targetPort: 8500 ,因此 pod 內的容器需要偵聽端口8500

服務中的 label 是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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM