【问题标题】:Cannot access Kubernetes pod无法访问 Kubernetes pod
【发布时间】:2020-07-06 05:58:31
【问题描述】:

我在 Windows 上使用 Docker 和 Minikube 来学习 k8s,但我未能部署我的第一个应用程序。

我运行了以下命令:

  1. kubectl run testapp --image=saphyra/testapp:latest --port=8080
  2. kubectl expose deployment testapp --type=NodePort
  3. minikube service testapp

使用命令minikube dashboard 可以看到 pod 已创建并正在运行,我可以看到日志,它说 Tomcat 按预期在端口 8080 上启动。

看来一切正常。

但是如何调用我启动的服务的端点(从浏览器)? 我关注的tutorial(以及许多其他 YouTube 教程)说现在我必须能够调用应用程序的端点。

我错过了什么?如何访问我的应用?

编辑:

描述结果

kubectl describe svc testapp >
Name:                     testapp
Namespace:                default
Labels:                   run=testapp
Annotations:              <none>
Selector:                 run=testapp
Type:                     NodePort
IP:                       10.110.10.61
Port:                     <unset>  8080/TCP
TargetPort:               8080/TCP
NodePort:                 <unset>  31612/TCP
Endpoints:                172.18.0.4:8080
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>

kube 代理日志:

server_others.go:323] Unknown proxy mode "", assuming iptables proxy
node.go:135] Successfully retrieved node IP: 172.17.0.2
server_others.go:145] Using iptables Proxier.
server.go:571] Version: v1.17.3
conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_max' to 524288
conntrack.go:52] Setting nf_conntrack_max to 524288
conntrack.go:127] sysfs is not writable: {Device:sysfs Path:/sys Type:sysfs Opts:[ro nosuid nodev noexec relatime] Freq:0 Pass:0} (mount options are [ro nosuid nodev noexec relatime])
conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_tcp_timeout_established' to 86400
conntrack.go:100] Set sysctl 'net/netfilter/nf_conntrack_tcp_timeout_close_wait' to 3600
config.go:313] Starting service config controller
shared_informer.go:197] Waiting for caches to sync for service config
config.go:131] Starting endpoints config controller
shared_informer.go:197] Waiting for caches to sync for endpoints config
shared_informer.go:204] Caches are synced for service config 
shared_informer.go:204] Caches are synced for endpoints config 

编辑 2

kubectl get svc testapp -o yaml >

apiVersion: v1
kind: Service
metadata:
  creationTimestamp: "2020-03-25T17:03:57Z"
  labels:
    run: testapp
  name: testapp
  namespace: default
  resourceVersion: "3454"
  selfLink: /api/v1/namespaces/default/services/testapp
  uid: 048d05df-eaad-4d4b-845f-d98b222fe101
spec:
  clusterIP: 10.110.10.61
  externalTrafficPolicy: Cluster
  ports:
  - nodePort: 31612
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    run: testapp
  sessionAffinity: None
  type: NodePort
status:
  loadBalancer: {}

kubectl get pod testapp-c565bfccc-xht6j -o yaml >

apiVersion: v1
kind: Pod
metadata:
  creationTimestamp: "2020-03-25T17:03:52Z"
  generateName: testapp-c565bfccc-
  labels:
    pod-template-hash: c565bfccc
    run: testapp
  name: testapp-c565bfccc-xht6j
  namespace: default
  ownerReferences:
  - apiVersion: apps/v1
    blockOwnerDeletion: true
    controller: true
    kind: ReplicaSet
    name: testapp-c565bfccc
    uid: 21e2c9ab-3771-482f-8849-71754aaf5ff6
  resourceVersion: "3491"
  selfLink: /api/v1/namespaces/default/pods/testapp-c565bfccc-xht6j
  uid: c4dfdcb6-d050-4c44-99d1-6881bc39f805
spec:
  containers:
  - image: saphyra/testapp:latest
    imagePullPolicy: Always
    name: testapp
    ports:
    - containerPort: 8080
      protocol: TCP
    resources: {}
    terminationMessagePath: /dev/termination-log
    terminationMessagePolicy: File
    volumeMounts:
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-689j9
      readOnly: true
  dnsPolicy: ClusterFirst
  enableServiceLinks: true
  nodeName: m01
  priority: 0
  restartPolicy: Always
  schedulerName: default-scheduler
  securityContext: {}
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  tolerations:
  - effect: NoExecute
    key: node.kubernetes.io/not-ready
    operator: Exists
    tolerationSeconds: 300
  - effect: NoExecute
    key: node.kubernetes.io/unreachable
    operator: Exists
    tolerationSeconds: 300
  volumes:
  - name: default-token-689j9
    secret:
      defaultMode: 420
      secretName: default-token-689j9
status:
  conditions:
  - lastProbeTime: null
    lastTransitionTime: "2020-03-25T17:03:52Z"
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: "2020-03-25T17:04:12Z"
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: "2020-03-25T17:04:12Z"
    status: "True"
    type: ContainersReady
  - lastProbeTime: null
    lastTransitionTime: "2020-03-25T17:03:52Z"
    status: "True"
    type: PodScheduled
  containerStatuses:
  - containerID: docker://9a3ddc83df779bb4dc4b88718d82f7065d2dc647360fa200b4815ecc260a7ed4
    image: saphyra/testapp:latest
    imageID: docker-pullable://saphyra/testapp@sha256:b328a874297521f35c84a37cde160e23a39d6a12c7184dbe3c88ff0250b05df6
    lastState: {}
    name: testapp
    ready: true
    restartCount: 0
    started: true
    state:
      running:
        startedAt: "2020-03-25T17:04:11Z"
  hostIP: 172.17.0.2
  phase: Running
  podIP: 172.18.0.4
  podIPs:
  - ip: 172.18.0.4
  qosClass: BestEffort
  startTime: "2020-03-25T17:03:52Z"

【问题讨论】:

  • minikube service testapp 没有启动浏览器?
  • 它在浏览器中启动了一个选项卡,但是 ERR_CONNECTION_REFUSED
  • 添加 kubectl describe svc testapp 的输出和 kube-system 命名空间中 kube 代理 pod 的日志
  • 请求的数据已添加@ArghyaSadhu

标签: spring-boot kubernetes minikube docker-desktop


【解决方案1】:

Minikube VM 通过主机专用 IP 地址暴露给主机系统,可以使用 $ minikube ip 命令获取。任何NodePort 类型的服务都可以通过该IP 地址访问NodePort

根据您提供的信息,您的服务正在收听NodePort:31612。因此,您可以通过以下 URL 从浏览器访问您的应用:minikube-ip:NodePort

假设你的 minikube ip 是 192.168.99.100,NodePort 是 31612,那么 URL 就是 192.168.99.100:31612

【讨论】:

  • minikube ip 返回 172.17.0.2。仍然没有连接到 172.17.0.2:31612
  • 确保您的服务和应用程序正常运行,检查testapp.default.svc:8080 在集群内是否可访问。为此,请使用$ minikube sshcurl
  • curl 172.18.0.4:8080 返回了预期的响应
  • 将 yamls 添加到问题中,kubectl get pod &lt;pod-name&gt; -o yamlkubectl get svc &lt;service-name&gt; -o yaml
  • 已添加到问题中
【解决方案2】:

好吧,我终于找到了: minikube start 选择 'docker' 作为驱动,所以整个 kubernetes 是在一个 docker 容器中启动的,我猜 docker 容器没有暴露给主机。

当我显式设置驱动程序 hyperv 并且 minikube 启动它自己的虚拟机时,一切正常。

但是,如何公开在 docker 容器中运行的 minikube 仍然是一个问题。

【讨论】:

    猜你喜欢
    • 2023-04-07
    • 2021-06-24
    • 2022-01-15
    • 2020-09-10
    • 1970-01-01
    • 2019-05-03
    • 2019-05-08
    • 1970-01-01
    • 2020-04-12
    相关资源
    最近更新 更多