从主机访问 Minikube Loadbalancer 服务

Access Minikube Loadbalancer Service From Host Machine

我正在尝试学习如何将 Kibernetes 与 Minikube 结合使用,并拥有以下部署和服务:

---
kind: Service
apiVersion: v1
metadata:
  name: exampleservice
spec:
  selector:
    app: myapp
  ports:
  - protocol: "TCP"
    # Port accessible inside cluster
    port: 8081
    # Port to forward to inside the pod
    targetPort: 8080
    # Port accessible outside cluster
    nodePort: 30002
  type: LoadBalancer
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: myappdeployment
spec:
  replicas: 5
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - name: myapp
        image: tutum/hello-world
        ports:
        - containerPort: 8080

我希望能够从我位于

的本地计算机访问此服务
http://192.168.64.2:30002

根据命令:minikube service exampleservice --url 但是当我尝试从浏览器访问它时,出现无法访问站点错误。

一些可能有助于调试的信息:

kubectl get services --all-namespaces:

NAMESPACE     NAME                   TYPE           CLUSTER-IP       EXTERNAL-IP   PORT(S)                  AGE
default       exampleservice         LoadBalancer   10.104.248.158   <pending>     8081:30002/TCP           26m
default       kubernetes             ClusterIP      10.96.0.1        <none>        443/TCP                  2h
default       user-service-service   LoadBalancer   10.110.181.202   <pending>     8080:30001/TCP           42m
kube-system   kube-dns               ClusterIP      10.96.0.10       <none>        53/UDP,53/TCP,9153/TCP   2h
kube-system   kubernetes-dashboard   ClusterIP      10.110.65.24     <none>        80/TCP                   2h

我在 OSX 上 运行 minikube。

这是意料之中的。 请注意,LoadBalancer 用于云创建外部负载均衡器,例如 AWS 中的 ALP/NLP 和 GCP/Azure 中的类似内容

按此处所示更新服务。这里我假设 192.168.64.2 是你的 minikube ip。如果没有,请使用 minikube ip 更新它以使其工作。

kind: Service
apiVersion: v1
metadata:
  name: exampleservice
spec:
  selector:
    app: myapp
  ports:
  - protocol: "TCP"
    # Port accessible inside cluster
    port: 8081
    # Port to forward to inside the pod
    targetPort: 80
    # Port accessible outside cluster
    nodePort: 30002
  type: LoadBalancer
  externalIPs:
  - 192.168.64.2

现在您可以在 http://192.168.64.2:8081/

访问您的应用程序

如果需要访问30002的应用,可以这样使用

    kind: Service
    apiVersion: v1
    metadata:
      name: exampleservice
    spec:
      selector:
        app: myapp
      ports:
      - protocol: "TCP"
        # Port accessible inside cluster
        port: 8081
        # Port to forward to inside the pod
        targetPort: 80
        # Port accessible outside cluster
        nodePort: 30002
      type: NodePort

我觉得你的部署文件不正确。

删除它 kubectl delete deploy/myappdeployment

用它来重新创建。

apiVersion: apps/v1beta1
kind: Deployment
metadata:
  labels:
    app: myapp
  name: myappdeployment
spec:
  replicas: 5
  selector:
    matchLabels:
      app: myapp
  strategy: {}
  template:
    metadata:
      labels:
        app: myapp
    spec:
      containers:
      - image: tutum/hello-world
        name: myapp
        ports:
        - containerPort: 80

注意: Minikube 支持 LoadBalancer 服务(通过 minikube tunnel

你可以得到你通过的IP和端口 可以通过 运行

访问服务

minikube service kubia-http #=> To open a browser with an IP and port

minikube service kubia --url #=> To get the IP and port in the terminal