如何在 Kubernetes 中使用 NodePort 服务在 public Ip 上公开 nginx?
How to expose nginx on public Ip using NodePort service in Kubernetes?
我正在执行 kubectl create -f nginx.yaml
,它成功创建了 pods。但是 PODS 没有暴露在我实例的 Public IP 上。以下是我使用的 YAML,服务类型为 nodeport:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
name: nginx
spec:
type: NodePort
ports:
- port: 80
nodePort: 30080
name: http
- port: 443
nodePort: 30443
name: https
selector:
name: nginx
我的方法或上面的 YAML 文件在将部署时的 pod 暴露给 public IP 时有什么不正确的地方?
PS: 防火墙和 ACL 在所有 TCP
上对互联网开放
没有添加端点。在调试时我发现部署和服务之间的标签不匹配。因此,将标签类型从 "app" 更改为 "name" 并且有效。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
name: nginx
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
name: nginx
spec:
type: NodePort
ports:
- port: 80
nodePort: 30080
name: http
selector:
name: nginx
是对的。您的服务选择器与 Pod 标签不匹配。
如果您像 Jeel 已在此答案中添加的那样修复该问题
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
name: nginx
spec:
type: NodePort
ports:
- port: 80
nodePort: 30080
name: http
selector:
name: nginx
您的服务将在节点 IP 地址中公开。因为你的服务类型是 NodePort。
如果您的节点 IP 是 35.226.16.207
,您可以使用此 IP 和 NodePort
连接到您的 Pod
$ curl 35.226.16.207:30080
在这种情况下,您的节点必须具有 public IP。否则无法访问
第二个选项,可以创建LoadBalancer类型的服务
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
name: nginx
spec:
type: LoadBalancer
ports:
- port: 80
name: http
selector:
name: nginx
这将为您提供一个 public IP。
更多详情,check this
我正在执行 kubectl create -f nginx.yaml
,它成功创建了 pods。但是 PODS 没有暴露在我实例的 Public IP 上。以下是我使用的 YAML,服务类型为 nodeport:
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
name: nginx
spec:
type: NodePort
ports:
- port: 80
nodePort: 30080
name: http
- port: 443
nodePort: 30443
name: https
selector:
name: nginx
我的方法或上面的 YAML 文件在将部署时的 pod 暴露给 public IP 时有什么不正确的地方?
PS: 防火墙和 ACL 在所有 TCP
上对互联网开放没有添加端点。在调试时我发现部署和服务之间的标签不匹配。因此,将标签类型从 "app" 更改为 "name" 并且有效。
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
name: nginx
spec:
replicas: 3
selector:
matchLabels:
name: nginx
template:
metadata:
labels:
name: nginx
spec:
containers:
- name: nginx
image: nginx:1.7.9
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
name: nginx
spec:
type: NodePort
ports:
- port: 80
nodePort: 30080
name: http
selector:
name: nginx
如果您像 Jeel 已在此答案中添加的那样修复该问题
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
name: nginx
spec:
type: NodePort
ports:
- port: 80
nodePort: 30080
name: http
selector:
name: nginx
您的服务将在节点 IP 地址中公开。因为你的服务类型是 NodePort。
如果您的节点 IP 是 35.226.16.207
,您可以使用此 IP 和 NodePort
$ curl 35.226.16.207:30080
在这种情况下,您的节点必须具有 public IP。否则无法访问
第二个选项,可以创建LoadBalancer类型的服务
apiVersion: v1
kind: Service
metadata:
name: nginx
labels:
name: nginx
spec:
type: LoadBalancer
ports:
- port: 80
name: http
selector:
name: nginx
这将为您提供一个 public IP。
更多详情,check this