使用 Kubernetes 和 minikube 在端口 80 上公开一个 Web 应用程序
Expose a web app on port 80 using Kubernetes and minikube
我正在使用笔记本电脑(无云)学习 Kubernetes。我按照 kubernetes(.)io 上的文档开始使用 minikube。我不确定我错过了什么,因为我只能使用高 TCP 端口 32744 而不是标准端口 80 访问我的网络应用程序。我希望能够通过访问 http:// 使用网络浏览器访问我的网络应用程序IP地址。
这是我的deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nodejs
template:
metadata:
labels:
app: nodejs
spec:
containers:
- name: nodeapp
image: localregistry/nodeapp:1.0
ports:
- containerPort: 3000
$ minikube service webapp-deployment
|-----------|-------------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|-------------------|-------------|---------------------------|
| default | webapp-deployment | 3000 | http://192.168.64.2:32744 |
|-----------|-------------------|-------------|---------------------------|
您可以为此使用 kubectl port-forward
。
$ kubectl port-forward -n <namespace> pod/mypod 8888:5000
它有什么作用?
本地监听8888端口,转发到pod中的5000端口。
注意:您也可以使用 port-forward
与 k8s 服务一起使用 kubectl port-forward svc/<service-name> PORT:PORT
。
这就是 Kubernetes 的工作原理。
3000
是容器中的端口,32744
是 NodePort where your application got exposed. There are multiple reasons for this, one of them is that port 80
and 443
are standard reserved ports for web services and Kubernetes needs to be able to run many containers and services. Another reason is that ports 0-1024
are restricted to root
only on *nix systems .
如果您真的想在本地计算机的 80
端口上提供服务,我只需设置 Nginx 之类的东西并将流量代理到 http://192.168.64.2:32744
# nginx.conf
...
location / {
proxy_set_header Accept-Encoding "";
proxy_pass http://192.168.64.2:32744;
}
...
或者您可以按照此处建议的其他答案从不受限制的本地 post 执行 port-forward
。
✌️
我正在使用笔记本电脑(无云)学习 Kubernetes。我按照 kubernetes(.)io 上的文档开始使用 minikube。我不确定我错过了什么,因为我只能使用高 TCP 端口 32744 而不是标准端口 80 访问我的网络应用程序。我希望能够通过访问 http:// 使用网络浏览器访问我的网络应用程序IP地址。
这是我的deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: webapp-deployment
spec:
replicas: 2
selector:
matchLabels:
app: nodejs
template:
metadata:
labels:
app: nodejs
spec:
containers:
- name: nodeapp
image: localregistry/nodeapp:1.0
ports:
- containerPort: 3000
$ minikube service webapp-deployment
|-----------|-------------------|-------------|---------------------------|
| NAMESPACE | NAME | TARGET PORT | URL |
|-----------|-------------------|-------------|---------------------------|
| default | webapp-deployment | 3000 | http://192.168.64.2:32744 |
|-----------|-------------------|-------------|---------------------------|
您可以为此使用 kubectl port-forward
。
$ kubectl port-forward -n <namespace> pod/mypod 8888:5000
它有什么作用?
本地监听8888端口,转发到pod中的5000端口。
注意:您也可以使用 port-forward
与 k8s 服务一起使用 kubectl port-forward svc/<service-name> PORT:PORT
。
这就是 Kubernetes 的工作原理。
3000
是容器中的端口,32744
是 NodePort where your application got exposed. There are multiple reasons for this, one of them is that port 80
and 443
are standard reserved ports for web services and Kubernetes needs to be able to run many containers and services. Another reason is that ports 0-1024
are restricted to root
only on *nix systems .
如果您真的想在本地计算机的 80
端口上提供服务,我只需设置 Nginx 之类的东西并将流量代理到 http://192.168.64.2:32744
# nginx.conf
...
location / {
proxy_set_header Accept-Encoding "";
proxy_pass http://192.168.64.2:32744;
}
...
或者您可以按照此处建议的其他答案从不受限制的本地 post 执行 port-forward
。
✌️