k8s nodeport原理
一、背景介绍
在Kubernetes(K8S)中,NodePort是一种服务类型,可以将服务公开到集群外部,允许外部客户端连接到服务,NodePort将端口暴露到每个集群节点上,通过节点的IP和NodePort提供访问。
二、NodePort原理
NodePort的实现原理是通过在每个节点上创建一个特定范围内的端口,如30000-32767之间,当外部客户端请求到达时,集群中的负载均衡器将流量转发到相应的NodePort上的pod.三、实现步骤
- 创建Deployment
- 创建Service
- 检查Service详情
yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
步骤二:创建Service:创建一个Service并指定其类型为NodePort,将外部流量导向Deployment中的Pod,以下是Service的示例yaml文件:
yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-service
spec:
type: NodePort
ports:
- port: 80
targetPort: 80
nodePort: 30080
selector:
app: nginx
步骤三:检查Service详情:查看创建的Service的详细信息,包括集群IP、NodePort等信息
bash
kubectl get services nginx-service

浙公网安备 33010602011771号