如何从局域网内的其他机器访问服务?

Yat*_*esu 2 portforwarding kubernetes minikube kubernetes-ingress kubernetes-service

你好,我正在使用 minikube 学习 Kubernetes。我可以在运行 minikube 的机器上通过 minikubeip:NodePort 访问服务,现在我想从其他机器通过 LAN 访问该服务。我尝试过 Ingress 但它对我不起作用。

部署文件:

---
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
Run Code Online (Sandbox Code Playgroud)

服务文件:

---
apiVersion: v1
kind: Service
metadata:
    name: aspnetapp-service
spec:
    type: NodePort
    ports:
    - name: http
      targetport: 80      
      port: 80
      protocol: TCP
    selector:
      app: aspnetapp
Run Code Online (Sandbox Code Playgroud)

入口文件:

---
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
Run Code Online (Sandbox Code Playgroud)

Daw*_*ruk 7

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

  • $ kubectl port-forward ...

免责声明!

  1. $ kubectl port-forward应该在运行 minikube 的主机上运行。
  2. 上面的命令将连续运行(&可用于在后台运行它)

例子:

假设您有一台 Ubuntu 机器,IP 为:192.168.0.115

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

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
Run Code Online (Sandbox Code Playgroud)

至于暴露您的服务,Deployment可以

  • 使用以下命令:
    • $ 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   
Run Code Online (Sandbox Code Playgroud)

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

  • 直接与$ kubectl port-forward.
  • 将流量引导至Ingress控制器。

直接访问

Service您可以直接公开您的内容而不使用Ingress

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

剖析上面的命令:

  • --address=0.0.0.0- 暴露在本地主机之外
  • deployment/nginx- 资源/资源名称
  • 10000:80- 主机上的端口/pod 上用于将流量发送到的端口

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

您需要登录 root 并复制.kube/config/root/目录或指定在哪里kubectl查找配置!

运行上述命令后,您应该能够运行:

  • 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
Run Code Online (Sandbox Code Playgroud)

将流量引导至Ingress控制器

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

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

  • 创建Ingress资源(就像您所做的那样)。
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
  name: ingress
spec:
  rules:
  - host:
    http:
      paths:
      - path: /
        backend:
          serviceName: nginx
          servicePort: 80
Run Code Online (Sandbox Code Playgroud)
  • 将流量转发到Ingress控制器!

Ingress控制器收到流量后会将其进一步转发(到您的Service,然后到Pod

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

  • 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- 控制器所在的Deployment命名Ingress空间
  • deployment/ingress-nginx-controller- 资源/资源名称
  • 80:80- 主机上的端口/pod 上用于将流量发送到的端口

命令$ kubectl port-forward将生成:

Forwarding from 0.0.0.0:80 -> 80 # AT THE START 
Handling connection for 80 # CURL FROM 192.168.0.2
Run Code Online (Sandbox Code Playgroud)

我还鼓励您使用不同的--driver产品,例如 Virtualbox。您将能够在没有$ kubectl port-forward(NAT)的情况下公开您的应用程序。

其他资源: