K8s 集群pod 的几种访问模式


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


hostNetworkjava

hostPortnode

NodePortpython

LoadBalancernginx

Ingressdocker


hostNetwork 主机网络模式 ,至关于 docker run --net=host api


示例演示 网络

apiVersion: v1
kind: Pod
metadata:
  name: nginx-host
spec:
  hostNetwork: true
  containers:
    - name: nginx-host
      image: nginx

 kubectl create -f hostNetwork.yamlapp

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

相关文章
相关标签/搜索