一起来学k8s 08.Replication Controller 和 Replica Set
Replication Controller Replica Set如果我们都人工的去解决遇到的pod重启问题,似乎又回到了以前刀耕火种的时代了是吧,如果有一种工具能够来帮助我们管理Pod就好了,Pod不够了自动帮我新增一个,Pod挂了自动帮我在合适的节点上重新启动一个Pod,这样是不是遇到重启问题我们都不需要手动去解决了。幸运的是,Kubernetes就为我们提供了这样的资源对象:Rep...
Replication Controller Replica Set
如果我们都人工的去解决遇到的pod重启问题,似乎又回到了以前刀耕火种的时代了是吧,如果有一种工具能够来帮助我们管理Pod
就好了,Pod
不够了自动帮我新增一个,Pod
挂了自动帮我在合适的节点上重新启动一个Pod
,这样是不是遇到重启问题我们都不需要手动去解决了。
幸运的是,Kubernetes
就为我们提供了这样的资源对象:
- Replication Controller:用来部署、升级
Pod
- Replica Set:下一代的
Replication Controller
- Deployment:可以更加方便的管理
Pod
和Replica Set
环境
192.168.48.101 master01
192.168.48.201 node01
192.168.48.202 node02
Replication Controller
Replication Controller
简称RC
,RC
是Kubernetes
系统中的核心概念之一,简单来说,RC
可以保证在任意时间运行Pod
的副本数量,能够保证Pod
总是可用的。如果实际Pod
数量比指定的多那就结束掉多余的,如果实际数量比指定的少就新启动一些Pod
,当Pod
失败、被删除或者挂掉后,RC
都会去自动创建新的Pod
来保证副本数量,所以即使只有一个Pod
,我们也应该使用RC
来管理我们的Pod
。
编写myapp-rc.yaml
vim myapp-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
name: myapp-rc
labels:
app: myapp
type: rc
spec:
replicas: 2
selector:
app: myapp
type: rc
template:
metadata:
name: myapp-pod
labels:
app: myapp
type: rc
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
上面的YAML
文件:
- kind:
ReplicationController
- spec.replicas: 指定
Pod
副本数量,默认为1 - spec.selector:
RC
通过该属性来筛选要控制的Pod
- spec.template: 这里就是我们之前的
Pod
的定义的模块,但是不需要apiVersion
和kind
了 - spec.template.metadata.labels: 注意这里的
Pod
的labels
要和spec.selector
相同,这样RC
就可以来控制当前这个Pod
了。
创建myapp-rc.yaml
[root@master01 deploy_yaml]# kubectl apply -f myapp-rc.yaml
replicationcontroller/myapp-rc created
[root@master01 deploy_yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo-deploy-8675c97685-vhncn 1/1 Running 0 8d
demo-deploy-8675c97685-w7md2 1/1 Running 0 8d
demo-rc-l9c8r 1/1 Running 0 4m49s
demo-rc-qvdd7 1/1 Running 0 4m49s
myapp-rc-hzsxm 1/1 Running 0 7s
myapp-rc-w5k7r 1/1 Running 0 7s
查看rc
[root@master01 deploy_yaml]# kubectl get rc
NAME DESIRED CURRENT READY AGE
demo-rc 2 2 2 6m18s
myapp-rc 2 2 2 96s
查看具体信息
[root@master01 deploy_yaml]# kubectl describe rc myapp-rc
Name: myapp-rc
Namespace: default
Selector: app=myapp,type=rc
Labels: app=myapp
type=rc
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"v1","kind":"ReplicationController","metadata":{"annotations":{},"labels":{"app":"myapp","type":"rc"},"name":"myapp-rc","nam...
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=myapp
type=rc
Containers:
myapp:
Image: ikubernetes/myapp:v1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 2m8s replication-controller Created pod: myapp-rc-hzsxm
Normal SuccessfulCreate 2m8s replication-controller Created pod: myapp-rc-w5k7r
修改yaml下Pod
的副本数量为3:
vim myapp-rc.yaml
....
replicas: 3
[root@master01 deploy_yaml]# kubectl apply -f myapp-rc.yaml
replicationcontroller/myapp-rc configured
[root@master01 deploy_yaml]# kubectl get rc
NAME DESIRED CURRENT READY AGE
demo-rc 2 2 2 8m48s
myapp-rc 3 3 3 4m6s
[root@master01 deploy_yaml]#
滚动升级
[root@master01 deploy_yaml]# kubectl rolling-update myapp-rc --image=ikubernetes/myapp:v2
Command "rolling-update" is deprecated, use "rollout" instead
Created myapp-rc-df879e42cfe882821bb7b680abe0f890
Scaling up myapp-rc-df879e42cfe882821bb7b680abe0f890 from 0 to 3, scaling down myapp-rc from 3 to 0 (keep 3 pods available, don't exceed 4 pods)
Scaling myapp-rc-df879e42cfe882821bb7b680abe0f890 up to 1
Scaling myapp-rc down to 2
Scaling myapp-rc-df879e42cfe882821bb7b680abe0f890 up to 2
Scaling myapp-rc down to 1
Scaling myapp-rc-df879e42cfe882821bb7b680abe0f890 up to 3
Scaling myapp-rc down to 0
Update succeeded. Deleting old controller: myapp-rc
Renaming myapp-rc-df879e42cfe882821bb7b680abe0f890 to myapp-rc
replicationcontroller/myapp-rc rolling updated
Replication Set
Replication Set
简称RS
,随着Kubernetes
的高速发展,官方已经推荐我们使用RS
和Deployment
来代替RC
了,实际上RS
和RC
的功能基本一致,目前唯一的一个区别就是RC
只支持基于等式的selector
(env=dev或environment!=qa),但RS
还支持基于集合的selector
(version in (v1.0, v2.0)),这对复杂的运维管理就非常方便了。
kubectl
命令行工具中关于RC
的大部分命令同样适用于我们的RS
资源对象。不过我们也很少会去单独使用RS
,它主要被Deployment
这个更加高层的资源对象使用,除非用户需要自定义升级功能或根本不需要升级Pod
,在一般情况下,我们推荐使用Deployment
而不直接使用Replica Set
。
最后我们总结下关于RC
/RS
的一些特性和作用吧:
- 大部分情况下,我们可以通过定义一个
RC
实现的Pod
的创建和副本数量的控制 RC
中包含一个完整的Pod
定义模块(不包含apiversion
和kind
)RC
是通过label selector
机制来实现对Pod
副本的控制的- 通过改变
RC
里面的Pod
副本数量,可以实现Pod
的扩缩容功能 - 通过改变
RC
里面的Pod
模板中镜像版本,可以实现Pod
的滚动升级功能(但是不支持一键回滚,需要用相同的方法去修改镜像地址)
编写myapp-rs.yaml
vim myapp-rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp-replicaset
namespace: default
labels:
app: myapp
type: replicaset
spec:
replicas: 2
selector:
matchLabels:
app: myapp
type: replicaset
template:
metadata:
name: myapp-pod
labels:
app: myapp
type: replicaset
spec:
containers:
- name: myapp
image: ikubernetes/myapp:v1
ports:
- name: http
containerPort: 80
创建myapp-rs.yaml
[root@master01 deploy_yaml]# kubectl apply -f myapp-rs.yaml
replicaset.apps/myapp-replicaset created
[root@master01 deploy_yaml]# kubectl get pod
NAME READY STATUS RESTARTS AGE
demo-deploy-8675c97685-vhncn 1/1 Running 0 8d
demo-deploy-8675c97685-w7md2 1/1 Running 0 8d
demo-rc-l9c8r 1/1 Running 0 23m
demo-rc-qvdd7 1/1 Running 0 23m
myapp-rc-brbm2 1/1 Running 0 4m14s
myapp-rc-c78nt 1/1 Running 0 4m14s
myapp-rc-lkbxw 1/1 Running 0 4m14s
myapp-replicaset-h4g92 1/1 Running 0 8s
myapp-replicaset-l24ff 1/1 Running 0 8s
查看rs
[root@master01 deploy_yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
demo-deploy-8675c97685 2 2 2 8d
myapp-replicaset 2 2 2 48s
查看具体信息
[root@master01 deploy_yaml]# kubectl describe rs myapp-replicaset
Name: myapp-replicaset
Namespace: default
Selector: app=myapp,type=replicaset
Labels: app=myapp
type=replicaset
Annotations: kubectl.kubernetes.io/last-applied-configuration:
{"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"labels":{"app":"myapp","type":"replicaset"},"name":"myapp-replic...
Replicas: 2 current / 2 desired
Pods Status: 2 Running / 0 Waiting / 0 Succeeded / 0 Failed
Pod Template:
Labels: app=myapp
type=replicaset
Containers:
myapp:
Image: ikubernetes/myapp:v1
Port: 80/TCP
Host Port: 0/TCP
Environment: <none>
Mounts: <none>
Volumes: <none>
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal SuccessfulCreate 5m45s replicaset-controller Created pod: myapp-replicaset-h4g92
Normal SuccessfulCreate 5m45s replicaset-controller Created pod: myapp-replicaset-l24ff
修改yaml下Pod
的副本数量为3:
vim myapp-rs.yaml
.....
replicas: 3
[root@master01 deploy_yaml]# kubectl apply -f myapp-rs.yaml
replicaset.apps/myapp-replicaset configured
[root@master01 deploy_yaml]# kubectl get rs
NAME DESIRED CURRENT READY AGE
demo-deploy-8675c97685 2 2 2 8d
myapp-replicaset 3 3 3 6m11s
[root@master01 deploy_yaml]#
更多推荐
所有评论(0)