kubernetes 从主机访问Minikube负载平衡器服务

z3yyvxxp  于 2023-03-07  发布在  Kubernetes
关注(0)|答案(2)|浏览(168)

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

58wvjzkj

58wvjzkj1#

您可以使用External IPs,如k8s docs中所述。更新服务如下所示(我假设192.168.64.2是您的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
2lpgd968

2lpgd9682#

**注意:**Minikube支持负载均衡器服务(通过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

相关问题