當咱們建立pod時,僅僅是建立了pod,要爲其建立rc(ReplicationController),他纔會有固定的副本,而後爲其建立service,集羣內部才能訪問該pod,使用 NodePort 或者 LoadBalancer 類型的 Service,外部網絡也能夠訪問該pod;每一個 service 會建立出來一個虛擬 ip,經過訪問 vip:port 就能獲取服務的內容(內部訪問,由於這是一個vip,外部沒法訪問的)
建立service時,其配置文件中的selector:指定後端綁定的pod,例如serviceA的selector:
app:whoimi
env:dev
而後有一個podA,
labels:
app:whoimi
env:dev
而後有一個podB,
labels:
app:whoimi
env:dev
那麼咱們的serviceA 就會綁定podA,podB,綁定的pod的ip會填寫到serviceA的endpoint中,內部訪問(vip:port方式訪問)serviceA,根據serviceA的vip:port直接訪問,serviceA會隨機的將服務轉發給後端的pod(podA,podB)node
# cat nginx01.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment01 spec: selector: matchLabels: env: prod replicas: 1 template: metadata: labels: env: prod spec: containers: - name: nginx image: nginx:1.11 ports: - containerPort: 80
# cat nginx02.yaml apiVersion: apps/v1 kind: Deployment metadata: name: nginx-deployment02 spec: selector: matchLabels: env: prod replicas: 1 template: metadata: labels: env: prod spec: containers: - name: nginx image: nginx:1.11 ports: - containerPort: 80
# cat service-test.yaml apiVersion: v1 kind: Service metadata: name: nginx-service spec: selector: env: prod ports: - name: http protocol: TCP port: 80 targetPort: 80 nodePort: 30011 type: NodePort