创建ReplicaSet
ReplicaSet的配置如下
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后,查看
[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个。
pod-demo.yaml文件的内容如下:
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
,看下会出现什么效果。
[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,它随机杀掉了一个。
应该是随机的, 不过,我测试了几次,都是把新建的pod-demo杀掉了。
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。