如何从局域网中的其他机器访问服务? [英] How to access a service from other machine in LAN?
问题描述
你好,我正在用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 ...
免责声明!
- 您的
$ kubectl port-forward
应该在运行minikube的主机上运行. - 上面的命令将连续运行(
&
可用于在后台运行它)
- Your
$ kubectl port-forward
should be run on a host running minikube. - 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 usingIngress
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 localhostdeployment/nginx
- resource/resource_name10000: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 wherekubectl
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! -
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 theDeployment
ofIngress
controller resides indeployment/ingress-nginx-controller
- resource/resource-name80:80
- port on host machine/port on pod to send the traffic to
Ingress
控制器在收到流量后会进一步将其转发(到您的Service
然后到Pod
)Ingress
controller after receiving the traffic will forward it further (to yourService
and then toPod
)要将流量转发到您的
Ingress
控制器,请运行以下命令:To forward the traffic to your
Ingress
controller run this command:再次剖析以上命令:
命令
$ 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屋!
-