1:用的最多的是nodePort,以下nginx的service,將type設置成NodePort,同時nodePort設置成30010(k8s爲了避免與宿主機的端口衝突,默認限制了30000如下的端口)node
這樣經過任何一個節點IP+30010就能夠訪問nginxnginx
apiVersion: v1 kind: Service metadata: name: my-nginx labels: run: my-nginx spec: ports: - port: 80 protocol: TCP nodePort: 30010 type: NodePort selector: run: my-nginx
2:loadbalancer模式,只有雲提供商支持才能夠使用。一樣設置type便可web
3:hostPort,經過訪問宿主機IP+8081端口訪問,可是每臺只能起一個pod,否則端口會發生衝突,也沒有service進行負載俊很api
apiVersion: v1 kind: Pod metadata: name: webapp labels: app: webapp spec: containers: - name: webapp image: kubeguide/tomcat-app:v2 ports: - containerPort: 8080 hostPort: 8081