ReplicaSet的配置以下vim
apiVersion: apps/v1 kind: ReplicaSet metadata: name: myapp namespace: default spec: replicas: 2 selector: matchLabels: app: myapp release: canary template: metadata: name: myapp-pod labels: app: myapp release: canary environment: qa spec: containers: - name: myapp-container image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80
在這個ReplicaSet中,咱們設置了兩個label,app=myapp, release=canary
, 建立rs後,查看api
[root@master manifests]# kubectl create -f rs-demo.yaml --dry-run replicaset.apps/myapp created (dry run) [root@master manifests]# kubectl create -f rs-demo.yaml replicaset.apps/myapp created [root@master manifests]# kubectl get pods NAME READY STATUS RESTARTS AGE myapp-bj795 1/1 Running 0 18s myapp-blmx8 1/1 Running 0 18s [root@master manifests]# vim rs-demo.yaml [root@master manifests]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS myapp-b8sb5 1/1 Running 0 5m app=myapp,environment=qa,release=canary myapp-blmx8 1/1 Running 0 8m app=myapp,environment=qa,release=canary [root@master manifests]# kubectl get rs NAME DESIRED CURRENT READY AGE myapp 2 2 2 39s
ReplicaSet會經過label app=myapp, release=canary
來篩選Pod,而且設定的符合該label的Pod副本應該爲2個。app
pod-demo.yaml文件的內容以下:frontend
apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: default labels: app: myapp tier: frontend spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 - name: https containerPort: 443 - name: busybox image: busybox:latest #ImagePullPolicy: IfNotPresent command: - "/bin/sh" - "-c" - "sleep 3600"
此時,咱們建立另外一個pod測試
[root@master manifests]# kubectl create -f pod-demo.yaml pod/pod-demo created [root@master manifests]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS myapp-b8sb5 1/1 Running 0 5m app=myapp,environment=qa,release=canary myapp-blmx8 1/1 Running 0 7m app=myapp,environment=qa,release=canary pod-demo 2/2 Running 0 11s app=myapp,tier=frontend
能夠看到,pod-demo的label爲 app=myapp,tier=frontend
,咱們嘗試給pod-demo添加一個label release=canary
,看下會出現什麼效果。spa
[root@master manifests]# kubectl label pods pod-demo release=canary pod/pod-demo labeled [root@master manifests]# kubectl get pods --show-labels NAME READY STATUS RESTARTS AGE LABELS myapp-b8sb5 1/1 Running 0 5m app=myapp,environment=qa,release=canary myapp-blmx8 1/1 Running 0 8m app=myapp,environment=qa,release=canary pod-demo 2/2 Terminating 0 34s app=myapp,release=canary,tier=frontend
能夠發現: pod-demo的狀態已經爲 Terminating。 說明ReplicaSet經過標籤選擇器獲取到了三個副本,而後預期的副本數被設定爲2,它隨機殺掉了一個。code
應該是隨機的, 不過,我測試了幾回,都是把新建的pod-demo殺掉了。get