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  需要负载均衡器的 支持,一般云服务商都有提供










猜你喜欢

转载自blog.51cto.com/sdsca/2422193
今日推荐