1.pod控制器类别
ReplicaSet
deployment 主要
Horizontal Pod Autoscaler (hpa) 高可用 实现削峰填谷
DaemonSet 守护进厂类的任务
Job 执行一次任务,执行完之后就结束
Cronjob 周期性的执行任务
StatefulSet 管理有状态应用
2.具体控制器
------------------------------------------------pod控制器------------------------------------------------------
replicaset
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: myrs
namespace: dev
labels:
controller: rs
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
matchExpressions:
- {key: app,operator: In, Values: [nginx-pod]}
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: nginx
image: nginx:1.17.1
ports:
- containerPort: 80
--------------------------------------------
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-rs
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx-pod
template:
metadata:
labels:
app: nginx-pod
spec:
containers:
- name: my-nginx
image: nginx
--------------------------------------------
[root@node1 ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
my-rs 3 3 3 3m34s my-nginx nginx app=nginx-pod
--------------------------------------------
[root@node1 ~]# kubectl get pod -n dev
NAME READY STATUS RESTARTS AGE
my-rs-6wbdd 1/1 Running 0 5m
my-rs-cs6nr 1/1 Running 0 5m
my-rs-hzncc 1/1 Running 0 5m
--------------------------------------------
rs 扩缩容
kubectl edit rs my-rs -n dev 修改replicas即可
kubectl scale rs my-rs --replicas=2 -n dev
--------------------------------------------
镜像版本升级
kubectl edit rs my-rs -n dev 修改image
kubectl set image rs my-rs my-nginx=nginx:1.18.0 -n dev
删除rs
kubectl delete rs my-rs -n dev #首先把副本数设置成0,然后再删除
kubectl delete rs my-rs -n dev --cascade=false #只删除控制器,不删除pod
kubectl delete -f my-rs.yaml
---------------
deployment
支持replicaset的所有功能
支持版本回滚和版本回退
apiVersion: apps/v1
kind: Deployment
metadata:
name: mynginx
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
run: nginx
template:
metadata:
labels:
run: nginx
spec:
containers:
- image: nginx
imagePullPolicy: IfNotPresent
name: nginx
ports:
- containerPort: 80
protocol: TCP
kubectl apply -f yaml-nginx-deploy.yaml
kubectl delete -f yaml-nginx-deploy.yaml
查询对应的rs:
[root@node1 ~]# kubectl get rs -n dev -o wide
NAME DESIRED CURRENT READY AGE CONTAINERS IMAGES SELECTOR
mynginx-869c47cdd9 3 3 3 3m32s nginx nginx pod-template-hash=869c47cdd9,run=nginx
扩缩容:
kubectl scale deploy my-nginx --replicas=5 -n dev
kubectl edit deploy my-nginx -n dev
[root@node1 ~]# kubectl scale deploy mynginx --replicas=5 -n dev
deployment.apps/mynginx scaled
kubectl edit deploy mynginx -n dev
修改replicas
镜像升级:
重建更新:
滚动更新:(默认滚动更新) 新的起来了之后,旧的才会停止
-----
spec:
strategy:
type: Recreate
-----
spec:
strategy:
type: RollingUpdate
maxUnavailable: 25%
maxSurge: 25%
kubectl set image deploy mynginx nginx=nginx -n dev
kubectl get pod -n dev -w
滚动更新 镜像升级,创建新的rs,原来的rs会进行保留,成为上一个版本
版本回退
kubectl rollout
status history pause resume restart undo
查看状态
[root@node1 ~]# kubectl rollout status deploy mynginx -n dev
Waiting for deployment "mynginx" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "mynginx" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "mynginx" rollout to finish: 1 out of 2 new replicas have been updated...
Waiting for deployment "mynginx" rollout to finish: 1 old replicas are pending termination...
Waiting for deployment "mynginx" rollout to finish: 1 old replicas are pending termination...
deployment "mynginx" successfully rolled out
查看历史
kubectl rollout history deploy mynginx -n dev
kubectl apply -f yaml-nginx-deploy.yaml --record
查看历史
[root@node1 ~]# kubectl rollout history deploy mynginx -n dev
deployment.apps/mynginx
REVISION CHANGE-CAUSE
1
2
//回退版本to-revision 指定版本
[root@node1 ~]#
[root@node1 ~]#
[root@node1 ~]# kubectl rollout undo deployment mynginx --to-revision=1 -n dev
金丝雀发布
中间状态:灰度发布
kubectl set image deploy mynginx nginx=nginx:1.17.4 -n dev && kubectl rollout pause deploy mynginx -n dev
--------------------------------------------------hpa------------------------------------------
Horizontal Pod Autoscaler
手动调整改为自动调整,需要计算负载量
需要安装监控 Metrics-server
kubectl top node 查看资源使用信息
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
name: my-hpa
namespace: dev
spec:
minReplicas: 1
maxReplicase: 10
targetCPUUtilizationPercentage: 3
scaleTargeRef:
apiVersion: apps/v1
kind: Deployment
name: mynginx
--------------------------------------------------hpa------------------------------------------
网络部分cni
--------------------------------------------------DaemonSet(ds)------------------------------------------
保证集群中每个节点运行一个pod
每添加一个node,就会自动运行pod
其yaml格式同deploy
--------------------------------------------------DaemonSet------------------------------------------
--------------------------------------------------job------------------------------------------
批量处理一次性任务
job对应的pod的重启策略为Never,或者OnFailure
主要的配置项:
manualSelector: true
completions: 6 #指定运行的pod总数
parallelism:3 #并发执行总数
--------------------------------------------------Cronjob------------------------------------------
job任务创建之后立刻执行
cronjob以job为管理对象,类似cron表达式
在指定的时间点执行任务,重复运行
schedule: cron表达式,最短时间为分钟
concurrencyPolicy: 并发执行任务,前一次任务没有完成,是否执行下一次任务, Allow 允许(默认) Forbid 禁止 Replace 替换,取消当前的任务,执行新的任务
failedJobHistoryLimit: 为失败的任务保留的历史记录数目
successfulJobHistoryLimit: 为成功的任务保留历史记录数目
startingDeadlineSeconds: 启动任务错误的超时时间
jobTemplate: