kubectl port-forward:第一次运行时“pod不存在”?

时间:2017-12-28 06:51:55

标签: docker kubernetes docker-registry minikube

关注this指南,我正在尝试启动minikube并在启动时转发端口。

我的剧本:

#!/bin/bash

set -eux

export PATH=/usr/local/bin:$PATH

minikube status || minikube start
minikube ssh 'grep docker.for.mac.localhost /etc/hosts || echo -e "127.0.0.1\tdocker.for.mac.localhost" | sudo tee -a /etc/hosts'
minikube ssh 'test -f wait-for-it.sh || curl -O https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh'
minikube ssh 'chmod +x wait-for-it.sh && ./wait-for-it.sh 127.0.1.1:10250'
POD=$(kubectl get po --namespace kube-system | awk '/kube-registry-v0/ { print $1 }')
kubectl port-forward --namespace kube-system $POD 5000:5000

一切正常,只有kubectl port-forward表示pod在第一次运行时不存在:

++ kubectl get po --namespace kube-system
++ awk '/kube-registry-v0/ { print $1 }'
+ POD=kube-registry-v0-qr2ml
+ kubectl port-forward --namespace kube-system kube-registry-v0-qr2ml 5000:5000
error: error upgrading connection: unable to upgrade connection: pod does not exist

如果我重新跑步:

+ minikube status
minikube: Running
cluster: Running
kubectl: Correctly Configured: pointing to minikube-vm at 192.168.99.100
+ minikube ssh 'grep docker.for.mac.localhost /etc/hosts || echo -e "127.0.0.1\tdocker.for.mac.localhost" | sudo tee -a /etc/hosts'
127.0.0.1   docker.for.mac.localhost
+ minikube ssh 'test -f wait-for-it.sh || curl -O https://raw.githubusercontent.com/vishnubob/wait-for-it/master/wait-for-it.sh'
+ minikube ssh 'chmod +x wait-for-it.sh && ./wait-for-it.sh 127.0.1.1:10250'
wait-for-it.sh: waiting 15 seconds for 127.0.1.1:10250
wait-for-it.sh: 127.0.1.1:10250 is available after 0 seconds
++ kubectl get po --namespace kube-system
++ awk '/kube-registry-v0/ { print $1 }'
+ POD=kube-registry-v0-qr2ml
+ kubectl port-forward --namespace kube-system kube-registry-v0-qr2ml 5000:5000
Forwarding from 127.0.0.1:5000 -> 5000
Forwarding from [::1]:5000 -> 5000

我在转发之前添加了一个调试行:

kubectl describe pod --namespace kube-system $POD

并看到了这个:

+ POD=kube-registry-v0-qr2ml
+ kubectl describe pod --namespace kube-system kube-registry-v0-qr2ml
Name:       kube-registry-v0-qr2ml
Namespace:  kube-system
Node:       minikube/192.168.99.100
Start Time: Thu, 28 Dec 2017 10:00:00 +0700
Labels:     k8s-app=kube-registry
        version=v0
Annotations:    kubernetes.io/created-by={"kind":"SerializedReference","apiVersion":"v1","reference":{"kind":"ReplicationController","namespace":"kube-system","name":"kube-registry-v0","uid":"317ecc42-eb7b-11e7-a8ce-...
Status:     Running
IP:     172.17.0.6
Controllers:    ReplicationController/kube-registry-v0
Containers:
  registry:
    Container ID:   docker://6e8f3f33399605758354f3f546996067d834459781235d51eef3ffa9c6589947
    Image:      registry:2.5.1
    Image ID:       docker-pullable://registry@sha256:946480a23b33480b8e7cdb89b82c1bd6accae91a8e66d017e21e8b56551f6209
    Port:       5000/TCP
    State:      Running
      Started:      Thu, 28 Dec 2017 13:22:44 +0700

为什么kubectl说它不存在?

Fri Dec 29 04:58:06 +07 2017

仔细观察这些事件,我找到了一些东西:

Events:
  FirstSeen     LastSeen        Count   From                    SubObjectPath                   Type            Reason                  Message
  ---------     --------        -----   ----                    -------------                   --------        ------                  -------
  20m           20m             1       kubelet, minikube                                       Normal          SuccessfulMountVolume   MountVolume.SetUp succ
eeded for volume "image-store"
  20m           20m             1       kubelet, minikube                                       Normal          SuccessfulMountVolume   MountVolume.SetUp succ
eeded for volume "default-token-fs7kr"
  20m           20m             1       kubelet, minikube                                       Normal          SandboxChanged          Pod sandbox changed, it will be killed and re-created.
  20m           20m             1       kubelet, minikube       spec.containers{registry}       Normal          Pulled                  Container image "registry:2.5.1" already present on machine
  20m           20m             1       kubelet, minikube       spec.containers{registry}       Normal          Created                 Created container
  20m           20m             1       kubelet, minikube       spec.containers{registry}       Normal          Started                 Started container
  

Pod沙箱已更改,它将被终止并重新创建。

在:

Containers:
  registry:
    Container ID:       docker://47c510dce00c6c2c29c9fe69665e1241c457d0666174a7723062c534e7229c58
    Image:              registry:2.5.1
    Image ID:           docker-pullable://registry@sha256:946480a23b33480b8e7cdb89b82c1bd6accae91a8e66d017e21e8b56551f6209
    Port:               5000/TCP
    State:              Running
      Started:          Thu, 28 Dec 2017 13:47:02 +0700
    Last State:         Terminated
      Reason:           Error
      Exit Code:        2
      Started:          Thu, 28 Dec 2017 13:22:44 +0700
      Finished:         Thu, 28 Dec 2017 13:45:18 +0700
    Ready:              True
    Restart Count:      14

后:

Containers:
  registry:
    Container ID:       docker://3a7da784d3d596796111348757725f5af22b47c5edd0fc29a4ffbb84f3f08956
    Image:              registry:2.5.1
    Image ID:           docker-pullable://registry@sha256:946480a23b33480b8e7cdb89b82c1bd6accae91a8e66d017e21e8b56551f6209
    Port:               5000/TCP
    State:              Running
      Started:          Thu, 28 Dec 2017 19:03:04 +0700
    Last State:         Terminated
      Reason:           Error
      Exit Code:        2
      Started:          Thu, 28 Dec 2017 13:47:02 +0700
      Finished:         Thu, 28 Dec 2017 19:00:48 +0700
    Ready:              True
    Restart Count:      15

minikube logs:

  

Dec 28 22:15:41 minikube localkube [3250]:W1228 22:15:41.102038
  3250 docker_sandbox.go:343]无法从插件/ docker中读取pod IP:   找不到kube-system / kube-registry-v0-qr2ml的网络状态   通过插件:

的网络状态无效

1 个答案:

答案 0 :(得分:3)

  

POD = $(kubectl get po --namespace kube-system | awk' / kube-registry-v0 / {print $ 1}')

请注意,使用selector几乎肯定比使用文本实用程序更好,特别是使用"非结构化"来自kubectl的输出。我不知道他们对默认输出格式做出的任何承诺,这就是--output=json和朋友存在的原因。但是,在您只想要名称的情况下,有一个特殊的--output=name可以执行它所说的内容,但有一点需要注意,资源前缀将位于名称前面(pods/kube-registry-v0-qr2ml情况下)

另外,我看到你已经等待了它,"但仅仅因为端口接受连接并不意味着Pod 就绪。您实际上想要使用--output=json(或更多awk脚本,以确保Pod已运行且就绪,当kubernetes和Pod同意所有内容时达到后者状态很酷。

怀疑,但必须试验才能确定错误消息只是误导;并不是说kubernetes对你的Pod一无所知,只是说它在它所处的状态下无法向前移动。

您可以通过创建Service type: NodePort然后在分配的端口上与节点IP进行通信来获得更好的成功;完全支持这个kubectl-shell混乱,但支持Ready部分 - 只有处于Ready状态的Pod将从服务接收流量

作为一个辅修,迂腐的说明,--namespacekubectl的参数,而不是port-forward,所以最正确的调用是kubectl --namespace=kube-system port-forward kube-registry-v0-qr2ml 5000:5000,以确保参数不是{ #39; t误解