为什么我的服务无法将流量传递到minikube上具有指定端口的Pod?

时间:2020-02-03 16:22:47

标签: kubernetes minikube kubernetes-service

我对Marko Luksa的 Kubernetes In Action 中的5.1.1 使用命名端口中的示例感到困惑。示例如下:

首先-创建

我正在创建一个具有命名端口的容器,该容器运行一个Node.js容器,该容器在命中时会以You've hit <hostname>响应:

apiVersion: v1
kind: Pod
metadata:
  name: named-port-pod
  labels:
    app: named-port
spec: 
  containers:
  - name: kubia
    image: michaellundquist/kubia
    ports:
    - name: http
      containerPort: 8080

还有这样的服务(请注意,这是original example的简化版本,也无法正常工作。:

apiVersion: v1
kind: Service
metadata:
  name: named-port-service
spec:
  ports:
  - name: http
    port: 80
    targetPort: http
  selector:
    app: named-port

第二个-验证

$ kubectl get po -o wide --show-labels
NAME             READY   STATUS    RESTARTS   AGE   IP           NODE       NOMINATED NODE   READINESS GATES   LABELS
named-port-pod   1/1     Running   0          45m   172.17.0.7   minikube   <none>           <none>            app=named-port


$ kubectl get services
NAME                 TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)   AGE
kubernetes           ClusterIP   10.96.0.1       <none>        443/TCP   53m
named-port-service   ClusterIP   10.96.115.108   <none>        80/TCP    19m

$ kubectl describe service named-port-service 
Name:              named-port-service
Namespace:         default
Labels:            <none>
Annotations:       <none>
Selector:          app=named-port
Type:              ClusterIP
IP:                10.96.115.108
Port:              http  80/TCP
TargetPort:        http/TCP
Endpoints:         172.17.0.7:8080
Session Affinity:  None
Events:            <none>

第三项-测试(失败)

$ kubectl exec named-port-pod -- curl named-port-pod:8080
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    26    0    26    0     0   5494      0 --:--:-- --:--:-- --:--:--  6500
You've hit named-port-pod

$ kubectl exec named-port-pod -- curl --max-time 20 named-port-service
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:19 --:--:--     0curl: (28) Connection timed out after 20001 milliseconds
command terminated with exit code 28

如您所见,当我按下named-port-pod:8080时,一切正常,但是当我按下named-port-service时,一切都失败了。我敢肯定我的映射正确,因为kubectl describe service named-port-service的端点正确,我认为minikube can use named ports但我的服务无法将连接传递到我的Pod。为什么?

p.s这是我的minikube版本:

$ minikube version
minikube version: v1.6.2
commit: 54f28ac5d3a815d1196cd5d57d707439ee4bb392

1 个答案:

答案 0 :(得分:2)

这是minikube的已知问题。 Pod无法通过服务IP到达自身。您可以尝试从其他Pod访问服务,或使用以下变通办法解决此问题。

minikube ssh
sudo ip link set docker0 promisc on

公开问题:https://www.db-fiddle.com/f/7MnPYEJW2uiGYaPhSSjtKa/1