首页 > 解决方案 > 无法在 kubernetes minikube 上访问 IP 和 PORT

问题描述

我在 kubernetes 上创建了 Web 部署和服务当我使用$ kubectl get service命令列出服务以列出 IP 和端口以访问网页但当我尝试访问它时无法访问该站点。似乎 minikube 上的 kubernetes 没有从 docker hub 拉取图像。

这是我的示例脚本。我使用 Kubernetes v1.19.2 和默认驱动程序 Docker 19.03.8

  1. $ kubectl create -f web-pets.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: web
spec:
  replicas: 1
  selector:
    matchLabels:
      app: pets
      service: web
  template:
    metadata:
      labels:
        app: pets
        service: web
    spec:
      containers:
      - image: nginx:alpine-1.19.2
        name: web
        ports:
        - containerPort: 80
          protocol: TCP
---
apiVersion: v1
kind: Service
metadata:
  name: web
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
  selector:
    app: pets
    service: web
  1. 输出创建web-pets.yaml
$ kubectl create -f web-pets.yaml
deployment.apps/web created
service/web created
  1. 输出$ kubectl get service
$ kubectl get service
NAME         TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
kubernetes   ClusterIP   10.96.0.1        <none>        443/TCP        11h
web          NodePort    10.110.107.234   <none>        80:30100/TCP   41s
  1. 输出$ kubectl get service web
Name:                     web
Namespace:                default
Labels:                   <none>
Annotations:              <none>
Selector:                 app=pets,service=web
Type:                     NodePort
IP:                       10.109.10.68
Port:                     <unset>  3000/TCP
TargetPort:               3000/TCP
NodePort:                 <unset>  31830/TCP
Endpoints:                <none>
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>
  1. 当列出所有 docker 镜像时没有 nginx 镜像
$ docker images
REPOSITORY                     TAG                 IMAGE ID      CREATED       SIZE
kicbase/stable                 v0.0.12-snapshot3   25ac91b9c8d7  4 weeks ago   952MB
gcr.io/k8s-minikube/kicbase    v0.0.12-snapshot3   25ac91b9c8d7  4 weeks ago   952MB
  1. 部署网页描述
$ kubectl describe deploy web
Name:                   web
Namespace:              default
CreationTimestamp:      Thu, 24 Sep 2020 17:25:42 +0700
Labels:                 <none>
Annotations:            <none>
Selector:               app=pets,service=web
Replicas:               1 desired | 0 updated | 0 total | 0 available | 0 unavailable
StrategyType:           RollingUpdate
MinReadySeconds:        0
RollingUpdateStrategy:  25% max unavailable, 25% max surge
Pod Template:
  Labels:  app=pets
           service=web
  Containers:
   web:
    Image:        nginx:alpine-1.19.2
    Port:         3000/TCP
    Host Port:    0/TCP
    Environment:  <none>
    Mounts:       <none>
  Volumes:        <none>
OldReplicaSets:   <none>
NewReplicaSet:    <none>
Events:           <none>
  1. $ kubectl get pods
$ kubectl get pods
No resources found in default namespace.
  1. $ kubectl get events
LAST SEEN   TYPE     REASON                    OBJECT          MESSAGE
7m13s       Normal   Starting                  node/minikube   Starting kubelet.
7m11s       Normal   NodeHasSufficientMemory   node/minikube   Node minikube status is now: NodeHasSufficientMemory
7m11s       Normal   NodeHasNoDiskPressure     node/minikube   Node minikube status is now: NodeHasNoDiskPressure
7m11s       Normal   NodeHasSufficientPID      node/minikube   Node minikube status is now: NodeHasSufficientPID
7m12s       Normal   NodeAllocatableEnforced   node/minikube   Updated Node Allocatable limit across pods
6m28s       Normal   Starting                  node/minikube   Starting kube-proxy.

标签: dockerkubernetesminikube

解决方案


Nginx 监听端口80。因此,您需要在部署和服务中使用端口80而不是端口。3000文档中,您应该可以使用 minikube service web


推荐阅读