kubernetes "hostNetwork: true",這是一種直接定義Pod網絡的方式。如果在POD中
使用"hostNetwork: true"配置網絡,pod中運行的應用程序可以直接看到宿主主機的網
絡接口,宿主機所在的局域網上所有網絡接口都可以訪問到該應用程序及端口。
示例:
apiVersion: apps/v1 kind: Deployment metadata: name: nginx spec: replicas: 2 selector: matchLabels: app: nginx template: metadata: labels: app: nginx spec: hostNetwork: true # 使用主機網絡 dnsPolicy: ClusterFirstWithHostNet # 該設置是使POD使用k8s的dns,dns配置在/etc/resolv.conf文件中 # 如果不加,pod默認使用所在宿主主機使用的DNS,這樣會導致容器 # 內不能通過service name訪問k8s集群中其他POD containers: - name: nginx image: nginx:1.7.9 #ports: # - name: metrics # hostPort: 80 # containerPort: 80 --- apiVersion: v1 kind: Service metadata: name: nginx spec: selector: app: nginx type: NodePort ports: - name: http port: 80 targetPort: 80 protocol: TCP nodePort: 30080
80端口是使用pod的"hostNetwork: true"方式暴露的,30080端口是使用service的"type: NodePort"方式暴露的。