kubernetes 提供了多种控制器,控制器能够对pod 进行部署、滚动升级、回滚、灰度发布、自愈
先了解下控制
kubectl run niginx21--imang=nginx
# 查看描述
kubect describe pod niginx21
nginx21 上级控制器为: ReplicaSet/nginx21-54bbb4b57
kubectl describe ReplicaSet/nginx21-54bbb4b57
ReplicaSet/nginx21-54bbb4b57
上级控制器 Deployment/nginx21
kubectl describe Deployment/nginx21
层层查找控制器,顶层是 Deploument
。kubernetes 节点升级、回滚、自愈等最终由顶级控制器控制
apiVersion: v1
kind: Pod
metadata:
name: pod-22
labels:
pod: pod-label
spec:
containers:
- image: nginx
name: ng-pod
status: {}
kubectl describe pod pod-22
# 扩容 nginx-21
[root@guanzc-130 kubernetes]# kubectl scale --replicas=3 deployment/nginx21
deployment.apps/nginx21 scaled
# 查看
[root@guanzc-130 kubernetes]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx01-7fbfd5976c-qb2fs 1/1 Running 2 2d10h
nginx21-54bbb4b57-bpdnc 1/1 Running 0 14m
nginx21-54bbb4b57-brcps 1/1 Running 0 14m
nginx21-54bbb4b57-v6rv2 1/1 Running 0 50m
pod-20 1/1 Running 1 37h
pod-21 1/1 Running 1 37h
pod-22 1/1 Running 1 37h
# 删除pod-22
[root@guanzc-130 kubernetes]# kubectl delete pod pod-22
pod "pod-22" deleted
# 删除nginx-21
[root@guanzc-130 kubernetes]# kubectl delete pod pod-22
pod "pod-22" deleted
[root@guanzc-130 kubernetes]# kubectl delete pod nginx21-54bbb4b57-bpdnc
pod "nginx21-54bbb4b57-bpdnc" deleted
[root@guanzc-131 ~]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx01-7fbfd5976c-qb2fs 1/1 Running 2 2d10h
nginx21-54bbb4b57-brcps 1/1 Running 0 17m
nginx21-54bbb4b57-qg2gf 0/1 ContainerCreating 0 13s
nginx21-54bbb4b57-v6rv2 1/1 Running 0 54m
pod-20 1/1 Running 1 37h
pod-21 1/1 Running 1 37h
[root@guanzc-131 ~]#
[root@guanzc-130 kubernetes]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx01-7fbfd5976c-qb2fs 1/1 Running 2 2d10h
nginx21-54bbb4b57-brcps 1/1 Running 0 18m
nginx21-54bbb4b57-qg2gf 1/1 Running 0 37s
nginx21-54bbb4b57-v6rv2 1/1 Running 0 55m
pod-20 1/1 Running 1 37h
pod-21 1/1 Running 1 37h
[root@guanzc-130 kubernetes]#
单pod 永远被删除,受控制器管理,删除后自愈,保证副本为3
- 由控制器控制的pod, 有自愈能力,pod 挂掉还是删除pod,总能保证节点副本数与配置相同(副本由ReplicaSet保证)
- 单pod不受任何控制器控制,一旦pod 挂掉,不会再恢复。不建议使用单pod 部署应用
- 副本优先级受 Deployment 控制
Deployment
自愈、滚动更新、副本、灰度发布、回滚等
ReplicaSet
ReplicaSet 常用于pod 自愈,官方建议用 Deployment替代
ReplicationController
功能与ReplicaSet 类似,官方建议用 Deployment替代
StatefulSet
有状态副本集
DaemonSet
daemon方式,每个节点都有
CronJob
定时任务
Jobs
任务
Garbage Collection
垃圾回收
TTL Controller for Finished Resources
time to live;自动清理Job
kubectl get all
#获取yaml,
kubectl get replicaset.apps/nginx01-7fbfd5976c -o yaml
nginx-rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-pod
spec:
replicas: 3
selector:
matchLabels:
pod-label: nginx-rs-test
template:
metadata:
labels:
pod-label: nginx-rs-test
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx-rs-01
kubectl apply -f nginx-rs.yaml
# 查看
kubectl get pod
使用 kind: ReplicaSet
部署,只产生了 replicaset.apps
控制器
执行删除操作,pod 会被删除,会自动创建,保持配置的3个副本
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: rs-pod
spec:
replicas: 3
selector:
matchLabels:
pod-label: nginx-rs-test
template:
metadata:
labels:
pod-label: nginx-rs-test
spec:
containers:
- image: tomcat:8
imagePullPolicy: Always
name: nginx-rs-01
kubectl apply -f nginx-rs.yaml
镜像修改为tomcat,升级后镜像为 nginx,并未生效。
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: nginx01
name: nginx01
spec:
replicas: 3
selector:
matchLabels:
run: nginxhh
template:
metadata:
creationTimestamp: null
labels:
run: nginxhh
spec:
containers:
- image: nginx
name: nginxhh
status: {}
kubectl apply -f ngnix-deploy.yml
# 查看
kubectl get pod