如何从局域网中的其他机器访问服务? [英] How to access a service from other machine in LAN?

查看:272
本文介绍了如何从局域网中的其他机器访问服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我正在用minikube学习kubernetes.我可以在运行minikube的计算机上通过minikubeip:NodePort访问服务,现在我想从其他计算机通过LAN访问该服务.我尝试了Ingress,但对我没有用.

Hello Im learning kubernetes with the minikube. I can access a service via minikubeip:NodePort on the machine where the minikube is running and now I want to access the Service via LAN from other machine. I tried ingress but it didn't work for me.

部署文件:

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: aspnetapp-deployment
  labels:
    app: aspnetapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: aspnetapp
  template:
    metadata:
      labels:
        app: aspnetapp
    spec:
      containers:
        - name: aspnetapp-cn
          image: localhost:5000/aspnetapp
          ports: 
            - containerPort: 80

服务文件:

---
apiVersion: v1
kind: Service
metadata:
    name: aspnetapp-service
spec:
    type: NodePort
    ports:
    - name: http
      targetport: 80      
      port: 80
      protocol: TCP
    selector:
      app: aspnetapp

入口文件:

---
apiVersion: networking.k8s.io/v1beta1
kind: Ingress
metadata:
  name: aspnetapp-ingress
  annotations:
    nginx.ingress.kubernetes.io/rewrite-target: /$1
spec:
  rules:
    - host:
      http:
        paths:
        - path: /aspnetapp
          backend:
            serviceName: aspnetapp-service
            servicePort: 80

推荐答案

要使用Ubuntu的--docker驱动程序将应用程序公开到LAN,可以使用:

To expose your application to LAN with Ubuntu with a --docker driver you can use:

  • $ kubectl port-forward ...

免责声明!

  1. 您的$ kubectl port-forward应该在运行minikube的主机上运行.
  2. 上面的命令将连续运行(&可用于在后台运行它)
  1. Your $ kubectl port-forward should be run on a host running minikube.
  2. Command above will operate continuously (& can be used to run it in a background)

示例:

假设您有一台IP地址为192.168.0.115的Ubuntu计算机.

Let's assume that you have an Ubuntu machine with IP: 192.168.0.115.

我使用nginx图片创建了一个示例:

I've created an example using nginx image:

Deployment.yaml

apiVersion: apps/v1 # for versions before 1.9.0 use apps/v1beta2
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  replicas: 1
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80

关于公开您的Deployment的服务,您可以任一:

As for the service exposing your Deployment you can either:

  • 使用以下命令:
    • $ kubectl expose deployment nginx --port=80 --type=NodePort
    • Use following command:
      • $ kubectl expose deployment nginx --port=80 --type=NodePort
      apiVersion: v1
      kind: Service
      metadata:
        name: nginx
      spec:
        type: NodePort
        selector:
          app: nginx
        ports:
          - protocol: TCP
            port: 80
            targetPort: 80   
      



      您可以通过两种方式公开您的nginx:



      You can expose your nginx in two ways:

      • 直接与$ kubectl port-forward联系.
      • 将流量定向到Ingress控制器.
      • Directly with $ kubectl port-forward.
      • Directing the traffic to the Ingress controller.

      您可以通过以下方式直接使用Ingress公开您的Service:

      You can expose your Service directly without using Ingress by:

      • $ kubectl port-forward --address=0.0.0.0 deployment/nginx 10000:80

      剖析以上命令:

      • --address=0.0.0.0-在本地主机之外公开
      • deployment/nginx-资源/资源名称
      • 10000:80-主机上的端口/pod上的端口,用于将流量发送到
      • --address=0.0.0.0 - expose outside of localhost
      • deployment/nginx - resource/resource_name
      • 10000:80 - port on host machine/port on pod to send the traffic to

      分配1024以下的本地端口将需要root用户访问权限!

      Assigning local ports under 1024 will need root access!

      您将需要登录到root用户,或者将.kube/config复制到/root/目录,或者指定kubectl应该在哪里寻找配置!

      You will need to login to root and either copy .kube/config to /root/ directory or specify where kubectl should look for config!

      运行上述命令后,您应该可以运行:

      After running above command you should be able to run:

      • curl 192.168.1.115:10000

      命令$ kubectl port-forward将生成:

      Forwarding from 0.0.0.0:10000 -> 80 # AT THE START
      Handling connection for 10000 # CURL FROM 192.168.0.2
      


      将流量定向到Ingress控制器


      Directing the traffic to the Ingress controller

      您需要运行$ minikube addons enable ingress才能具有Ingress资源的功能

      在您的示例中,您使用了Ingress资源.在这种情况下,您应该:

      In your example you used Ingress resource. In this situation you should:

      • 创建Ingress资源(与您一样).
      • Create Ingress resource (as you did).
      apiVersion: extensions/v1beta1
      kind: Ingress
      metadata:
        name: ingress
      spec:
        rules:
        - host:
          http:
            paths:
            - path: /
              backend:
                serviceName: nginx
                servicePort: 80
      

      • 将流量转发到Ingress控制器!
        • Forward the traffic to the Ingress controller!
        • Ingress控制器在收到流量后会进一步将其转发(到您的Service然后到Pod)

          Ingress controller after receiving the traffic will forward it further (to your Service and then to Pod)

          要将流量转发到您的Ingress控制器,请运行以下命令:

          To forward the traffic to your Ingress controller run this command:

          • kubectl port-forward --address=0.0.0.0 --namespace=kube-system deployment/ingress-nginx-controller 80:80

          再次剖析以上命令:

          • --address=0.0.0.0-在本地主机之外公开
          • --namespace=kube-system-Ingress控制器的Deployment所在的命名空间
          • deployment/ingress-nginx-controller-资源/资源名称
          • 80:80-主机上的端口/pod上的端口,用于将流量发送到
          • --address=0.0.0.0 - expose outside of localhost
          • --namespace=kube-system - namespace that the Deployment of Ingress controller resides in
          • deployment/ingress-nginx-controller - resource/resource-name
          • 80:80 - port on host machine/port on pod to send the traffic to

          命令$ kubectl port-forward将生成:

          Forwarding from 0.0.0.0:80 -> 80 # AT THE START 
          Handling connection for 80 # CURL FROM 192.168.0.2
          


          我也建议您使用其他--driver,例如Virtualbox.您将可以在不使用$ kubectl port-forward(NAT)的情况下公开您的应用程序.


          I also encourage you to use different --driver like for example Virtualbox. You will be able to expose your application without $ kubectl port-forward (NAT).

          其他资源:

          这篇关于如何从局域网中的其他机器访问服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆