k8s nodeport原理

一、背景介绍

  在Kubernetes(K8S)中,NodePort是一种服务类型,可以将服务公开到集群外部,允许外部客户端连接到服务,NodePort将端口暴露到每个集群节点上,通过节点的IP和NodePort提供访问。

二、NodePort原理

   NodePort的实现原理是通过在每个节点上创建一个特定范围内的端口,如30000-32767之间,当外部客户端请求到达时,集群中的负载均衡器将流量转发到相应的NodePort上的pod.

三、实现步骤

  1. 创建Deployment
  2. 创建Service
  3. 检查Service详情
  步骤一:创建Deployment:首先需要创建一个Deployment看来部署一个简单的应用,例如NGINX web服务器,下面是一个Deployment的示例yaml文件:
  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

四、总结:

  通过以上步骤们,成功实现了在Kubernetes中使用NodePort服务类型。NodePort允许外部流量通过映射到每个节点上的端口访问服务,从而扩展服务的访问范围
posted @ 2024-09-27 14:40  稻草人&梦想  阅读(199)  评论(0)    收藏  举报