2

可以通过将网络请求发送到它们所属的服务的 dns 来访问 kubernetes 集群中的 Pod。网络请求必须发送到[service].[namespace].svc.cluster.local该服务的所有成员并在该服务的所有成员之间进行负载平衡。

这可以很好地让某个 pod 到达另一个 pod,但是如果一个 pod 尝试通过他所属的服务到达自己,它会失败。它总是导致超时。

这是 Kubernetes 中的一个错误(在我的例子中是 minikube v0.35.0)还是需要一些额外的配置?


这是一些调试信息:

让我们从其他 pod 联系服务。这工作正常:

daemon@auth-796d88df99-twj2t:/opt/docker$ curl -v -X POST -H "Accept: application/json" --data '{}' http://message-service.message.svc.cluster.local:9000/message/get-messages
Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 10.107.209.9...
* TCP_NODELAY set
* Connected to message-service.message.svc.cluster.local (10.107.209.9) port 9000 (#0)
> POST /message/get-messages HTTP/1.1
> Host: message-service.message.svc.cluster.local:9000
> User-Agent: curl/7.52.1
> Accept: application/json
> Content-Length: 2
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 2 out of 2 bytes
< HTTP/1.1 401 Unauthorized
< Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
< X-Frame-Options: DENY
< X-XSS-Protection: 1; mode=block
< X-Content-Type-Options: nosniff
< Content-Security-Policy: default-src 'self'
< X-Permitted-Cross-Domain-Policies: master-only
< Date: Wed, 20 Mar 2019 04:36:51 GMT
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 12
< 
* Curl_http_done: called premature == 0
* Connection #0 to host message-service.message.svc.cluster.local left intact
Unauthorized

现在我们尝试让 pod 联系他所属的服务:

Note: Unnecessary use of -X or --request, POST is already inferred.
*   Trying 10.107.209.9...
* TCP_NODELAY set
* connect to 10.107.209.9 port 9000 failed: Connection timed out
* Failed to connect to message-service.message.svc.cluster.local port 9000: Connection timed out
* Closing connection 0
curl: (7) Failed to connect to message-service.message.svc.cluster.local port 9000: Connection timed out

如果我正确阅读了 curl 调试日志,则 dns 解析为 ip 地址 10.107.209.9。可以通过该 ip 从任何其他 pod 访问该 pod,但该 pod 不能使用它来访问自己。

以下是尝试访问自身的 pod 的网络接口:

daemon@message-58466bbc45-lch9j:/opt/docker$ ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
2: sit0@NONE: <NOARP> mtu 1480 qdisc noop state DOWN group default qlen 1000
    link/sit 0.0.0.0 brd 0.0.0.0
296: eth0@if297: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default 
    link/ether 02:42:ac:11:00:09 brd ff:ff:ff:ff:ff:ff link-netnsid 0
    inet 172.17.0.9/16 brd 172.17.255.255 scope global eth0
       valid_lft forever preferred_lft forever

这是部署到 minikube 的 kubernetes 文件:

apiVersion: v1
kind: Namespace
metadata:
  name: message

---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  labels:
    app: message
  name: message
  namespace: message
spec:
  replicas: 1
  selector:
    matchLabels:
      app: message
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 0
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: message
    spec:
      containers:
        - name: message
          image: message-impl:0.1.0-SNAPSHOT
          imagePullPolicy: Never
          ports:
            - name: http
              containerPort: 9000
              protocol: TCP
          env:
            - name: NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace    
            - name: KAFKA_KUBERNETES_NAMESPACE
              value: kafka
            - name: KAFKA_KUBERNETES_SERVICE
              value: kafka-svc
            - name: CASSANDRA_KUBERNETES_NAMESPACE
              value: cassandra
            - name: CASSANDRA_KUBERNETES_SERVICE
              value: cassandra
            - name: CASSANDRA_KEYSPACE
              value: service_message
---

# Service for discovery
apiVersion: v1
kind: Service
metadata:
  name: message-service
  namespace: message
spec:
  ports:
    - port: 9000
      protocol: TCP
  selector:
    app: message
---

# Expose this service to the api gateway
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: message
  namespace: message
  annotations:
    kubernetes.io/ingress.class: traefik
spec:
  rules:
    - host: api.fload.cf
      http:
        paths:
          - path: /message
            backend:
              serviceName: message-service
              servicePort: 9000
4

1 回答 1

2

这是一个已知的minikube 问题。讨论包含以下解决方法:

1)尝试:

minikube ssh
sudo ip link set docker0 promisc on 

2)使用无头服务clusterIP: None

于 2019-05-13T08:51:59.683 回答