HostPort

(1)hostport是将pod的端口映射到宿主机上。
(2)nodeport是将service的端口映射到集群中的每个宿主机上。

HostPort NodePort 虽然可以给 pod 提供节点级别的 porxy,但是如果对于一个daemonset,采用 NodePort 方式来申明节点端口就不这么直观了,这个时候就可以使用pod的 hostport 来直接在pod的节点上暴露端口。 apiVersion: apps
/v1 kind: DaemonSet metadata: labels: app: tools-test name: tools-test spec: selector: matchLabels: app: tools-test template: metadata: labels: app: tools-test spec: containers: - command: - python - -m - http.server ports: - containerPort: 8000 # When you bind a Pod to a hostPort, it limits the number of places the Pod can be scheduled, because each <hostIP, hostPort, protocol> combination must be unique hostPort: 10000 name: http protocol: TCP image: python:3.9.5 imagePullPolicy: IfNotPresent name: tools-jupyter resources: {} 设置完就可以了。 那 hostport 的实现原理是怎么样的呢?是不是也是开了一个程序监听?通过到节点执行 netstat -anp| grep 10000,发现并没有启动一个监听程序,那说明很可能走的是iptables,我们看看 iptables 的 nat 表: $ iptables -S -t nat | grep CNI-DN-9c969028fa2789c46c080 # Warning: iptables-legacy tables present, use iptables-legacy to see them root@kubeflow-worker:/# iptables -S -t nat -N CNI-HOSTPORT-SETMARK -N CNI-HOSTPORT-MASQ -N CNI-HOSTPORT-DNAT -N CNI-DN-2a6b9967ebd3cd7a5eeb3 -A PREROUTING -m addrtype --dst-type LOCAL -j CNI-HOSTPORT-DNAT -A POSTROUTING -m comment --comment "CNI portfwd requiring masquerade" -j CNI-HOSTPORT-MASQ -A OUTPUT -m addrtype --dst-type LOCAL -j CNI-HOSTPORT-DNAT -A CNI-HOSTPORT-SETMARK -m comment --comment "CNI portfwd masquerade mark" -j MARK --set-xmark 0x2000/0x2000 -A CNI-HOSTPORT-MASQ -m mark --mark 0x2000/0x2000 -j MASQUERADE -A CNI-HOSTPORT-DNAT -p 6 -m comment --comment "dnat name: \"kindnet\" id: \"07dbe45d8e31690c76141e5e16132b086f6a2d5b63216584009b990a7c08552c\"" -m multiport --dports 10000 -j CNI-DN-2a6b9967ebd3cd7a5eeb3 -A CNI-DN-2a6b9967ebd3cd7a5eeb3 -s 10.244.1.155/32 -p 6 -m tcp --dport 10000 -j CNI-HOSTPORT-SETMARK -A CNI-DN-2a6b9967ebd3cd7a5eeb3 -s 127.0.0.1/32 -p 6 -m tcp --dport 10000 -j CNI-HOSTPORT-SETMARK -A CNI-DN-2a6b9967ebd3cd7a5eeb3 -p 6 -m tcp --dport 10000 -j DNAT --to-destination 10.244.1.155:8000 iptables 用法参考: 这里目标 pod 的 ip 是 10.244.1.155,通过 iptables 可以看到,通过CNI-HOSTPORT-DNAT匹配目标端口 10000, jump 到 CNI-DN-2a6b9967ebd3cd7a5eeb3 chain,CNI-DN-2a6b9967ebd3cd7a5eeb3 对于源IP不等于10.244.1.155/32和127.0.0.1/32 的流量转到 10.244.1.155:8000,这个地址就是目标 pod 对应的 containerPort。 注意:按照官方文档说的,除非绝对必要,否则不要为 Pod 指定 hostPort。 将 Pod 绑定到hostPort时,它会限制 Pod 可以调度的位置数,因为每个 <hostIP, hostPort, protocol>组合必须是唯一的。 如果您没有明确指定 hostIP 和 protocol,Kubernetes 将使用 0.0.0.0 作为默认 hostIP 和 TCP 作为默认 protocol,请在使用 hostPort 之前考虑使用 NodePort 服务。

 

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx0414
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx0414
  template:
    metadata:
      labels:
        app: nginx0414
    spec:
      hostNetwork: true
      dnsPolicy: ClusterFirstWithHostNet
      containers:
      - name: nginx0414
        image: nginx:1.7.9
        ports:
        - name: metrics
          hostPort: 80
          containerPort: 80

 

posted @ 2023-04-14 17:48  滴滴滴  阅读(127)  评论(0编辑  收藏  举报