无法在本地访问 kubernetes 服务

Not able to access kubernetes service locally

我无法从本地计算机访问我的服务。我已经使用 minikube 完成了 k8 部署,并创建了一个 NodePort 类型的服务来访问它。

C:\WINDOWS\system32>kubectl get deployments
NAME                 READY   UP-TO-DATE   AVAILABLE   AGE
fromk8dashboard      2/2     2            2           25m

和服务

C:\WINDOWS\system32>kubectl get svc
NAME              TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)           AGE
fromk8dashboard   NodePort    10.100.68.204    <none>        31000:32058/TCP   26m
kubernetes        ClusterIP   10.96.0.1        <none>        443/TCP           24h

当我运行命令时:

C:\WINDOWS\system32>minikube service fromk8dashboard
|-----------|-----------------|--------------------------|----------------------------|
| NAMESPACE |      NAME       |       TARGET PORT        |            URL             |
|-----------|-----------------|--------------------------|----------------------------|
| default   | fromk8dashboard | tcp-31000-80-6bw28/31000 | http://172.26.75.182:32058 |
|-----------|-----------------|--------------------------|----------------------------|
* Opening service default/fromk8dashboard in default browser...

这会在浏览器上打开 url“http://172.26.75.182:32058”,但显示无法访问该站点。我是否缺少某些配置或代理设置?

另一个查询是,我的 kubernetes 仪表板 运行ning on "http://127.0.0.1:61750/", so shouldn't the service nodeport url be http://127.0.0.1:32058/ or localhost:32058 ?

描述服务 returns 以下结果:

C:\WINDOWS\system32>kubectl describe svc fromk8dashboard
Name:                     fromk8dashboard
Namespace:                default
Labels:                   k8s-app=fromk8dashboard
Annotations:              <none>
Selector:                 k8s-app=fromk8dashboard
Type:                     NodePort
IP:                       10.100.68.204
Port:                     tcp-31000-80-6bw28  31000/TCP
TargetPort:               80/TCP
NodePort:                 tcp-31000-80-6bw28  32058/TCP
Endpoints:                172.17.0.7:80,172.17.0.8:80
Session Affinity:         None
External Traffic Policy:  Cluster
Events:                   <none>



C:\WINDOWS\system32>kubectl describe pod fromk8dashboard-6f98f9468c-wjrd5
Name:           fromk8dashboard-6f98f9468c-wjrd5
Namespace:      default
Priority:       0
Node:           minikube/172.26.75.182
Start Time:     Tue, 12 May 2020 17:43:05 +0530
Labels:         k8s-app=fromk8dashboard
                pod-template-hash=6f98f9468c
Annotations:    <none>
Status:         Running
IP:             172.17.0.8
Controlled By:  ReplicaSet/fromk8dashboard-6f98f9468c
Containers:
  fromk8dashboard:
    Container ID:   docker://eef3ecc8e564d9439b66592aa2f1eebd0a886e6380986066ae61bb00193c3687
    Image:          ranjit1827/niguel
    Image ID:       docker-pullable://ranjit1827/niguel@sha256:b9229b2fdc7e5e3e9872189b3840f3c6f23d312af1bff9bbf5a5c68428be5894
    Port:           <none>
    Host Port:      <none>
    State:          Running
      Started:      Tue, 12 May 2020 17:43:30 +0530
    Ready:          True
    Restart Count:  0
    Environment:    <none>
    Mounts:
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-rn7lm (ro)
Conditions:
  Type              Status
  Initialized       True
  Ready             True
  ContainersReady   True
  PodScheduled      True
Volumes:
  default-token-rn7lm:
    Type:        Secret (a volume populated by a Secret)
    SecretName:  default-token-rn7lm
    Optional:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     node.kubernetes.io/not-ready:NoExecute for 300s
                 node.kubernetes.io/unreachable:NoExecute for 300s
Events:          <none>

我的服务 yaml 如下:

kind: Service
apiVersion: v1
metadata:
  name: fromk8dashboard
  namespace: default
  selfLink: /api/v1/namespaces/default/services/fromk8dashboard
  uid: 406e99ce-dec1-4e80-91aa-84f75c2d9892
  resourceVersion: '70115'
  creationTimestamp: '2020-05-12T12:13:05Z'
  labels:
    k8s-app: fromk8dashboard
  managedFields:
    - manager: dashboard
      operation: Update
      apiVersion: v1
      time: '2020-05-12T12:13:41Z'
      fieldsType: FieldsV1
      fieldsV1:
        'f:metadata':
          'f:labels':
            .: {}
            'f:k8s-app': {}
        'f:spec':
          'f:externalTrafficPolicy': {}
          'f:ports':
            .: {}
            'k:{"port":31000,"protocol":"TCP"}':
              .: {}
              'f:name': {}
              'f:port': {}
              'f:protocol': {}
              'f:targetPort': {}
          'f:selector':
            .: {}
            'f:k8s-app': {}
          'f:sessionAffinity': {}
          'f:type': {}
spec:
  ports:
    - name: tcp-31000-80-6bw28
      protocol: TCP
      port: 31000
      targetPort: 80
      nodePort: 32058
  selector:
    k8s-app: fromk8dashboard
  clusterIP: 10.100.68.204
  type: NodePort
  sessionAffinity: None
  externalTrafficPolicy: Cluster
status:
  loadBalancer: {}

使用下面的 yaml,你应该通过 http://172.26.75.182:30007/

访问它
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  type: NodePort
  selector:
    k8s-app: fromk8dashboard
  ports:
      # By default and for convenience, the `targetPort` is set to the same value as the `port` field.
    - port: 80
      targetPort: 80
      # Optional field
      # By default and for convenience, the Kubernetes control plane will allocate a port from a range (default: 30000-32767)
      nodePort: 30007

检查 guide 以通过 nodeport 服务公开 pod。