目录
一、replicaset
二、deployment
1、版本迭代
2、回滚
3、滚动更新策略
4、暂停与恢复
三、daemonset
四、statefulset
五、job
六、cronjob
一、replicaset
ReplicaSet用于保证指定数量的 Pod 副本一直运行
vim rs-example.yml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: replicaset-example
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
kubectl apply -f rs-example.yml
kubectl get pod --show-labels
replicaset是通过标签匹配pod
kubectl label pod replicaset-example-c7bs2 app=myapp --overwrite
kubectl get pod --show-labels
kubectl label pod replicaset-example-c7bs2 app=nginx --overwrite
kubectl get pod --show-labels
replicaset自动控制副本数量,pod可以自愈
kubectl delete pod replicaset-example-c7bs2
kubectl get pod --show-labels
回收资源
kubectl delete -f rs-example.yml
二、deployment
vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1
kubectl apply -f deployment-example.yaml
1、版本迭代
replicaset就是deployment的一个版本
2、回滚
vim deployment-example.yaml
kubectl apply -f deployment-example.yaml
kubectl get all
3、滚动更新策略
vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
minReadySeconds: 5
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
replicas: 3
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:v1
kubectl apply -f deployment-example.yaml
kubectl describe deployments.apps deployment-example
4、暂停与恢复
暂停,避免触发不必要的线上更新
kubectl rollout pause deployment deployment-example
vim deployment-example.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: deployment-example
spec:
minReadySeconds: 5
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
replicas: 6
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: nginx
resources:
limits:
cpu: 0.5
memory: 200Mi
requests:
cpu: 0.5
memory: 200Mi
kubectl apply -f deployment-example.yaml
调整副本数,不受影响
但是更新镜像和修改资源并没有触发更新
kubectl rollout history deployment deployment-example
恢复后开始触发更新
kubectl rollout resume deployment deployment-example
kubectl rollout history deployment deployment-example
回收
kubectl delete -f deployment-example.yaml
三、daemonset
Taints(污点)是一种标记,可以应用于节点,用于表示这个节点上的某些条件不适合运行某些 Pod。
vim daemonset-example.yml
apiVersion: apps/v1
kind: DaemonSet
metadata:
name: daemonset-example
spec:
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
tolerations:
- effect: NoSchedule
operator: Exists
containers:
- name: nginx
image: nginx
kubectl apply -f daemonset-example.yml
kubectl get pod -o wide
四、statefulset
当使用StatefulSet时,Pod的名称会包括一个索引,例如web-0,web-1,web-2等,这些索引是按顺序递增的,并且在Pod的生命周期中保持不变
StatefulSet保证每个Pod的名称在整个集群中是唯一的,例如,如果您有一个名为web-0的Pod,任何时候都不能重新创建一个叫做web-0的Pod。
vim statefulset.yaml
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: web
spec:
selector:
matchLabels:
app: nginx
serviceName: "nginx"
replicas: 3
minReadySeconds: 3
template:
metadata:
labels:
app: nginx
spec:
terminationGracePeriodSeconds: 10
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
name: web
kubectl apply -f statefulset.yaml
kubectl get pod
五、job
Job是用于处理一次性任务的控制器,它可以保证任务成功完成后自动停止。Job中包含一个或多个Pods,在Pods成功完成后便完成任务。如果Pods失败,则Job会重启Pods,直到任务完成。
提前将perl上传至harbor仓库
vim job.yml
apiVersion: batch/v1
kind: Job
metadata:
name: pi
spec:
completions: 6 //完成任务的Pod数量
parallelism: 2 //并行执行的Pod数量
template:
spec:
containers:
- name: pi
image: perl:5.34.0
command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
restartPolicy: Never //完成后不会重启
backoffLimit: 4 //当任务执行失败时,最多重试的次数
kubectl apply -f job.yml
六、cronjob
CronJob是用于处理周期性任务的控制器,它类似于传统的Cron定时任务,可以基于指定的时间或时间间隔来执行任务。CronJob会创建一个Job对象来运行任务,Job对象运行完成后自动被删除。如果Job对象出现错误,CronJob将会自动重启Job对象,直到任务成功完成。
vim cronjob.yml
apiVersion: batch/v1
kind: CronJob
metadata:
name: hello
spec:
schedule: "* * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: hello
image: busybox
imagePullPolicy: IfNotPresent
command:
- /bin/sh
- -c
- date; echo Hello from the Kubernetes cluster
restartPolicy: OnFailure
kubectl apply -f cronjob.yml
kubectl get all
kubectl get pod
kubectl delete -f cronjob.yml
每分钟的每一秒都会执行一次,使用busybox镜像来打印当前时间并输出"Hello from the Kubernetes cluster"