kubernetes - 从主机访问 Minikube Loadbalancer 服务

标签 kubernetes load-balancing minikube

我正在尝试学习如何将 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

关于kubernetes - 从主机访问 Minikube Loadbalancer 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55462654/

相关文章:

linux - 如何更新minikube最新版本?

kubernetes - Kubernetes入口-访问Web服务容器子路径

kubernetes - K8s : Log response body of the HTTP probe 'httpGet'

kubernetes - Helm 图表微服务

php - 如何平衡网络服务器带宽使用?

language-agnostic - 如何让您的代码为负载平衡做好准备

erlang - 使用 Erlang,我应该如何在集群之间分配负载?

kubernetes - 如何在kubernetes集群上修补statefulset并设置imagePullPolicy

kubernetes - 启用Minihube入口插件的外部IP分配

Docker 构建立即挂起(使用 minikube 和 ubuntu,目录中的文件不多)