3.k8s资源控制器rs Deployment Job
k8s资源控制器
#控制器类型
ReplicaSet #rs,确保pod副本数,rs已替代rc
Deployment #管理rs,升级、回滚、扩容pod
DaemonSet #在每个节点运行一个Pod
StateFulSet #解决有状态服务,Pod重新调度后PodName和HostName不变
Job/CronJob #批处理任务/定时任务
HPA #按CPU使用率或自定义metrics扩展Pod数量 Horizontal Pod Autoscaling
#rs实例
#rs-demo.yaml
apiVersion: extensions/v1beta1
kind: ReplicaSet
metadata:
name: rs-demo
spec:
replicas: 2 #副本数量
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx-rs
image: alivv/nginx:node
ports:
- containerPort: 80
#创建
kubectl create -f rs-demo.yaml
#查看rs
kubectl get rs
kubectl describe rs rs-demo
#查看pod
kubectl get pod
kubectl get pod --show-labels
#删除所有pod
kubectl delete pod --all
#再次查看,发现pod存在,保持在副本数量
kubectl get pod
#删除
kubectl delete -f rs-demo.yaml
#Deployment实例
Deployment -> 创建ReplicaSet(rs) -> 创建Pod
#nginx-deployment.yaml
apiVersion: apps/v1beta1
kind: Deployment
metadata:
name: nginx-deploy
labels:
app: nginx-demo
spec:
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: alivv/nginx:node
ports:
- containerPort: 80
#创建( --record参数记录命令,方便后续查看变化)
kubectl apply -f nginx-deployment.yaml --record
#查看
kubectl get deployments
kubectl get rs
kubectl get pod --show-labels
#扩容
kubectl scale deployment nginx-deploy --replicas 5
#Deployment自动扩展
#kubectl autoscale deployment nginx-deployment --min=3 --max=9 --cpu-percent=80
#更新镜像
kubectl set image deployment/nginx-deploy nginx=nginx:alpine
#回滚到前一个版本
kubectl rollout undo deployment/nginx-deploy
#查看回滚状态(回滚完成返回值为0 echo $?)
kubectl rollout status deployment/nginx-deploy
#查看历史rs
kubectl get rs
#查看回滚历史
kubectl rollout history deployment nginx-deploy
#回滚到指定版本
#kubectl rollout undo deployment nginx-deploy --to-revision=2
#生产建议设置Deployment的.spec.revisionHistoryLimit限制保留revision历史记录数量
Deployment更多参数 https://www.kubernetes.org.cn/deployment
#DaemonSet实例
#nginx-ds.yaml
kind: DaemonSet
apiVersion: apps/v1
metadata:
name: deamonset-example
labels:
app: daemonset
spec:
selector:
matchLabels:
name: deamonset-example
template:
metadata:
labels:
name: deamonset-example
spec:
containers:
- name: nginx
image: alivv/nginx:node
#部署
kubectl create -f nginx-ds.yaml
#查看启动node
kubectl get pods -o wide
#删除
kubectl delete -f nginx-ds.yaml
#Job实例
可选参数
RestartPolicy 仅支持Never或OnFailure
spec.completions 需要成功运行Pod个数,默认1
spec.parallelism 运行Pod个数,默认1
spec.activeDeadlineSeconds 失败Pod重试最大时间
#job-demo.yaml
apiVersion: batch/v1
kind: Job
metadata:
name: job-demo
spec:
template:
metadata:
name: job-demo
spec:
restartPolicy: Never
containers:
- name: job-test
image: busybox
command: ["bin/sh", "-c", "for n in $(seq 1 9);do echo $n;done"]
#创建
kubectl create -f job-demo.yaml
#查看
kubectl get job
kubectl get pod
kubectl log $(kubectl get pod |awk '/job-demo/{print $1}')
#删除
kubectl delete -f job-demo.yaml
#cronjob实例
cronjob -> 创建job -> 创建Pod
schedule: "* * * * *" 分 时 日 月 周,相当于Linux定时任务
.spec.startingDeadlineSeconds 启动Job期限,默认没限制
.spec.successfulJobsHistoryLimit 保留完成job数,默认3
.spec.failedJobsHistoryLimit 保留失败job数,默认1
.spec.concurrencyPolicy 并发策略,默认Allow,Forbid禁止,Replace关闭旧的运行新Job
#cronjob-demo.yaml
kind: CronJob
apiVersion: batch/v1beta1
metadata:
name: cronjob-demo
spec:
schedule: "*/1 * * * *" #每分钟执行
jobTemplate:
spec:
template:
spec:
restartPolicy: OnFailure
containers:
- name: cronjob-demo
image: busybox
args:
- /bin/sh
- -c
- echo -n $(hostname) 'cronjob demo ';date +%T
#创建
kubectl create -f cronjob-demo.yaml
#查看
kubectl get cronjob
kubectl get job
kubectl get pod
kubectl get pod |awk '/cronjob-demo/{print "kubectl logs "$1}' |bash
#删除
kubectl delete -f cronjob-demo.yaml
Blog地址 https://www.cnblogs.com/elvi/p/11755712.html
本文git地址 https://gitee.com/alivv/k8s/tree/master/notes
本文来自博客园,作者:blog-elvin-vip,转载请注明原文链接:https://www.cnblogs.com/elvi/p/11755712.html