1:用的最多的是nodePort,如下nginx的service,將type設置成NodePort,同時nodePort設置成30010(k8s為了不與宿主機的端口沖突,默認限制了30000以下的端口)
這樣通過任何一個節點IP+30010就可以訪問nginx
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即可
3:hostPort,通過訪問宿主機IP+8081端口訪問,但是每台只能起一個pod,不然端口會發生沖突,也沒有service進行負載俊很
apiVersion: v1 kind: Pod metadata: name: webapp labels: app: webapp spec: containers: - name: webapp image: kubeguide/tomcat-app:v2 ports: - containerPort: 8080 hostPort: 8081