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  需要負載均衡器的 支持,一般雲服務商都有提供










發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章