1、Pod控制器的类型
(1)ReplicaSet(是ReplicationController的替代品)
(2)Deployment(工作在ReplicaSet之上,即Deployment管理ReplicaSet,再由ReplicaSet去管理Pod)—用来管理无状态应用的最好的控制器。
(3)DaemonSet
(4)Job
(5)CronJob
(6)StatefluSet—用来管理无状态应用的最好的控制器。
(7)正是因为StatefulSet管理Pod难度相当大,所以就引入了下面的东西
2、Deployment和DaemonSet的共同特点
3、Job、CronJob与Deployment、DaemonSet的不同点
4、Helm—Kubernetes包管理器。类似于yum。
[root@master manifests]# vim rs-demo.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myapp
namespace: default
spec:
replicas: 2
selector:
matchLabels: #基于等值关系的标签选择器
app: myapp
release: canary
template:
metadata:
name: myapp-pod #这里的name配置没有什么实际的意义(不会生效),Pod的名字默认是以ReplicaSet的名字myapp开头,并在后面紧跟"-和一串随机的数字"。这里不用配置namspace,这是因为容器的namespace必须与上面ReplicaSet里面的namespace相同
labels: #这里容器的标签必须包含上面replicaSet中的标签选择器中定义的标签
app: myapp
release: canary
environment: qa
spec:
containers:
- name: myapp-container
image: nginx:latest
ports:
- name: http
containerPort: 80
[root@master manifests]# kubectl create -f rs-demo.yaml
[root@master manifests]# kubectl get rs
NAME DESIRED CURRENT READY AGE
myapp 2 2 2 4m49s
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-2p9mt 1/1 Running 0 7m5s
myapp-qcn2c 1/1 Running 0 7m5s
[root@master manifests]# kubectl describe pods myapp-qcn2c #随便查看其中的一个Pod
IP: 10.244.1.10
[root@master manifests]# curl 10.244.1.10 #访问这个Pod的地址,可以看到nginx的默认页
Welcome to nginx!
Welcome to nginx!
If you see this page, the nginx web server is successfully installed and
working. Further configuration is required.
For online documentation and support please refer to
nginx.org.
Commercial support is available at
nginx.com.
Thank you for using nginx.
1、演示ReplicaSet维护期望副本数的场景
下面构造Pod的个数小于ReplicaSet中定义的2
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-2p9mt 1/1 Running 0 12m
myapp-qcn2c 1/1 Running 0 12m
[root@master manifests]# kubectl delete pods myapp-2p9mt #删除容器myapp-2p9mt
pod "myapp-2p9mt" deleted
[root@master manifests]# kubectl get pods #可以看到自动生成了一个myapp-m9rlw
NAME READY STATUS RESTARTS AGE
myapp-m9rlw 1/1 Running 0 27s
myapp-qcn2c 1/1 Running 0 12m
下面构造Pod的个数大于ReplicaSet中定义的2
[root@master manifests]# kubectl create -f pod-demo.yaml #新增一个pod-demo的容器
[root@master manifests]# kubectl get pods --show-labels #查看Pod的标签
NAME READY STATUS RESTARTS AGE LABELS
myapp-m9rlw 1/1 Running 1 12h app=myapp,environment=qa,release=canary
myapp-qcn2c 1/1 Running 1 12h app=myapp,environment=qa,release=canary
pod-demo 2/2 Running 0 23s app=myapp,tier=frontend
[root@master manifests]# kubectl label pods pod-demo release=canary #将pod-demo的标签设置为ReplicaSet所设置的变迁选择器中的标签(app=myapp,release=canary),以此来构造Pod的个数大于2的情况。
[root@master manifests]# kubectl get pods --show-labels #查看Pod的状态,可以看到pod-demo的状态为Terminating。(ReplicaSet随机删除掉多余的Pod)
NAME READY STATUS RESTARTS AGE LABELS
myapp-m9rlw 1/1 Running 1 12h app=myapp,environment=qa,release=canary
myapp-qcn2c 1/1 Running 1 12h app=myapp,environment=qa,release=canary
pod-demo 2/2 Terminating 0 50s app=myapp,release=canary,tier=frontend
[root@master manifests]# kubectl get pods --show-labels #几分钟后,可以看到pod-demo挂掉了
NAME READY STATUS RESTARTS AGE LABELS
myapp-m9rlw 1/1 Running 1 12h app=myapp,environment=qa,release=canary
myapp-qcn2c 1/1 Running 1 13h app=myapp,environment=qa,release=canary
2、演示ReplicaSet自动扩缩容的场景
下面构造自动扩容的场景
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-m9rlw 1/1 Running 1 13h
myapp-qcn2c 1/1 Running 1 13h
[root@master manifests]# kubectl edit rs myapp #将里面的replicas修改为3
spec:
replicas: 3
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-hj5vg 1/1 Running 0 17s
myapp-m9rlw 1/1 Running 1 13h
myapp-qcn2c 1/1 Running 1 13h
下面构造自动缩容的场景
[root@master manifests]# kubectl get pods
NAME READY STATUS RESTARTS AGE
myapp-hj5vg 1/1 Running 0 17s
myapp-m9rlw 1/1 Running 1 13h
myapp-qcn2c 1/1 Running 1 13h
[root@master manifests]# kubectl edit rs myapp #将replicas的值设置为2
spec:
replicas: 2
[root@master manifests]# kubectl get pods #rs这个ReplicaSet会自动杀掉一个pod
NAME READY STATUS RESTARTS AGE
myapp-m9rlw 1/1 Running 1 13h
myapp-qcn2c 1/1 Running 1 13h
3、演示ReplicaSet手动更新升级(所谓升级就是改Pod所管理的容器的镜像版本、或是修改其他的一些信息)的场景
[root@master manifests]# kubectl get rs -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp 2 2 2 13h myapp-container nginx:latest app=myapp,release=canary
[root@master manifests]# kubectl edit rs myapp #修改image中的版本
spec:
containers:
- image: nginx:v1
[root@master manifests]# kubectl get rs -o wide #可以看到nginx:latest变成了nginx:v1
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
myapp 2 2 2 13h myapp-container nginx:v1 app=myapp,release=canary
[root@master manifests]# kubectl get pods -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-m9rlw 1/1 Running 1 13h 10.244.2.21 node02
myapp-qcn2c 1/1 Running 1 13h 10.244.1.12 node01
[root@master manifests]# curl 10.244.2.21 #访问myapp-m9rlw所管理的容器,可以访问到。这是因为该pod还是未更新升级之前的pod(镜像版本为nginx:latest,而不是nginx:v1),可以正常访问。
...
Welcome to nginx!
...
[root@master manifests]# kubectl delete pods myapp-qcn2c #删除myapp-qcn2c
pod "myapp-qcn2c" deleted
[root@master manifests]# kubectl get pods -o wide #可以看到新生成的myapp-jcrrw的状态为ImagePullBackOff,而不是Running。这是因为我上面修改之后的nginx:v1镜像不存在。
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
myapp-jcrrw 0/1 ImagePullBackOff 0 4m13s 10.244.1.14 node01
myapp-m9rlw 1/1 Running 1 13h 10.244.2.21 node02
4、ReplicaSet手动更新升级的三种方法
(1)ReplicaSet更新升级时,手动删除一个Pod,ReplicaSet再自动创建一个Pod,就这样完成手动更新升级的过程;
(2)蓝绿更新的方法1:在ReplicaSet1更新升级之前,创建一个ReplicaSet2,ReplicaSet1和ReplicaSet2都由Service1来管理,创建好ReplicaSet2之后,停掉ReplicaSet1;
(3)蓝绿更新的方法2:在ReplicaSet1更新升级之前,创建一个ReplicaSet2然后让Service1来管理ReplicaSet2,而不去管理ReplicaSet1;