现在,再去了解一个新的定义方式。
了解之前,先将刚刚的 pod 删除
[root@kube-node01 yaml]$ kubectl delete -f rc_nginx.yml
replicationcontroller "nginx" deleted
[root@kube-node01 yaml]$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-99nns 0/1 Terminating 0 6m
nginx-c5nk4 0/1 Terminating 0 49m
nginx-fjnrn 0/1 Terminating 0 6m
nginx-r2zcm 0/1 Terminating 0 49m
[root@kube-node01 yaml]$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-fjnrn 0/1 Terminating 0 6m
[root@kube-node01 yaml]$ kubectl get pods
No resources found.
可以看到 pod 会慢慢的消失掉,有点淡入淡出的那种感觉哈。
那么新的定义类型叫做ReplicaSet。
其实这个定义方式与上边介绍的ReplicationController非常像,官网这样介绍:
ReplicaSet 是下一代复制控制器。现在 ReplicaSet 和 Replication Controller 之间的唯一区别是选择器支持。ReplicaSet 支持标签用户指南中描述的新的基于集合的选择器要求, 而 Replication Controller 仅支持基于等同的选择器要求。
同样是定义一个 yaml 文件,如下:
[root@kube-node01 yaml]$ cat rs_nginx.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: nginx
labels:
tier: frontend
spec:
replicas: 3
selector:
matchLabels:
tier: frontend
template:
metadata:
name: nginx
labels:
tier: frontend
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
创建 pod:
[root@kube-node01 yaml]$ kubectl create -f rs_nginx.yml
replicaset.apps "nginx" created
[root@kube-node01 yaml]$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-4vp95 1/1 Running 0 8s
nginx-9xxp6 0/1 ContainerCreating 0 8s
nginx-f8vfg 1/1 Running 0 8s
[root@kube-node01 yaml]$ kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx 3 3 1 4s
关于扩展等,与上边的一样
[root@kube-node01 yaml]$ kubectl scale rs nginx --replicas=2
replicaset.extensions "nginx" scaled
[root@kube-node01 yaml]$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-4vp95 1/1 Running 0 1m
nginx-9xxp6 0/1 Terminating 0 1m
nginx-f8vfg 1/1 Running 0 1m
[root@kube-node01 yaml]$ kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx 2 2 2 1m
[root@kube-node01 yaml]$ kubectl scale rs nginx --replicas=5
replicaset.extensions "nginx" scaled
[root@kube-node01 yaml]$ kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-4vp95 1/1 Running 0 2m
nginx-bbxpk 0/1 ContainerCreating 0 2s
nginx-cg8ts 0/1 ContainerCreating 0 2s
nginx-f8vfg 1/1 Running 0 2m
nginx-sgvpd 0/1 ContainerCreating 0 2s
[root@kube-node01 yaml]$ kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx 5 5 3 2m
[root@kube-node01 yaml]$ kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx 5 5 4 2m
[root@master replicas-set]$kubectl get rs
NAME DESIRED CURRENT READY AGE
nginx 5 5 5 2m
这个知识点就到这里,可以根据以上的一些小实践,理解其工作方式
网友评论