微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

K8s 集群pod 的几种访问模式

k8s  访问pod  和service  主要有以下几种方式

hostNetwork

hostPort

NodePort

LoadBalancer

Ingress

hostNetwork 主机网络模式 ,相当于 docker run --net=host 

示例演示 

apiVersion: v1
kind: Pod
Metadata:
  name: Nginx-host
spec:
  hostNetwork: true
  containers:
    - name: Nginx-host
      image: Nginx

 kubectl create -f hostNetwork.yaml

image.png

查看pod 的ip 为 node 节点的ip,选择这种网络模式 pod  调度 ,pod  会根据节点选择不同的node ip 发生变化,port 端口,需要保持不与宿主机上的port 端口发生冲突。

hostport 模式 

hostPort是直接将容器的端口与所调度的节点上的端口路由,这样用户就可以通过宿主机的IP加上来访问Pod了

示例演示

apiVersion: v1
kind: Pod
Metadata:
  name: Nginx-port
spec:
  hostNetwork: true
  containers:
    - name: Nginx-host
      image: Nginx
      ports: 
        - containerPort: 80
          hostPort: 80

kubectl create -f hostPort.yaml

image.png

image.png

curl 192.168.222.250:80/index.html

pod 被调度不同节点,ip 就会发生变化,需要维护pod  与宿主机间的对应关系

NodePort 模式

NodePort 模式 k8s service 认情况使用 cluster IP 的方式, 这样service 就会产生一个Cluster IP 的模式,这个cluster ip 认只能在集群内部访问,想要在外部直接访问 service ,需要将 service type 的类型修改为 nodePort 模式 , nodePort值,范围是30000-32767

示例如下

apiVersion: v1
kind: Pod
Metadata:
  name: Nginx
  labels:
    name: Nginx
spec:
  containers:
    - name: Nginx
      image: Nginx
      ports:
        - containerPort: 80
---
kind: Service
apiVersion: v1
Metadata:
  name: Nginx
spec:
  type: NodePort
  ports: 
    - port: 80
      nodePort: 30008  #  nodePort值,范围是30000-32767
  selector:
    name: Nginx

kubectl apply  -f Node_pod.yaml

image.png

image.png

访问pod 的三种方式

集群内部访问

pod ip  :curl 10.244.1.245

ClusterIP :curl 10.1.250.193

集群外部访问

NodePort: master:

http://192.168.222.240:30008 # 使用 NodePort 模式会在 master 和node 节点上新开一个端口 ,使用多个NodePort 模式需要维护好 端口的对应关系,防止出现端口冲突

image.png

LoadBalancer  需要负载均衡器的 支持,一般云服务商都有提供

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐