文章目录 控制器含义解释 pod的标签与注释 ReplicaController ReplicaSet Deployments DaemonSet Job Cronjob
控制器含义解释
空调遥控器知道吧ReplicationController: ReplicationController确保在任何时候都运行指定数量的pod副本。换句话说,一个ReplicationController确保一个pod或一组同类的pod总是处于可用状态。ReplicaSet: ReplicaSet的目的是维护在任何给定时间运行的一组稳定的pod副本。因此,它通常用于保证指定数量的相同pod的可用性。Deployments: 部署针对Pods和ReplicaSets提供了宣告式的更新。StatefulSets: StatefulSet常用于管理有状态的应用。DaemonSet: 它是确保所有的节点或一些节点都运行一个Pod的复本。
pod的标签与注释
标签就是名字的意思
kubectl run httpd \
--image=httpd:2.4 \
--replicas=1 \
--labels="ver=2,env=prod"查看标签
kubectl get deployment --show-labelskubectl label deployments httpd "canary=true"
kubectl get deployment -L canary
移除
kubectl get deployment -L canary
显示标签 kubectl get pods --show-labels选择标签为某个值
kubectl get pod --selector="ver=2"标签是抓取pod的唯一依据
ReplicaController
管理它自己管理的pod数量保持一致[root@k8s-01 chapter04]# cat web-rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:name: web
spec:replicas: 3selector:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginxports:- containerPort: 80[root@k8s-01 chapter04]# kubectl create -f web-rc.yaml
replicationcontroller/web created
[root@k8s-01 chapter04]# kubectl get pod
NAME READY STATUS RESTARTS AGE
httpd 1/1 Running 0 22m
httpd-1 1/1 Running 0 21m
nginx-97499b967-jzxwg 1/1 Running 0 14h
web-5bq9k 0/1 ContainerCreating 0 4s
web-rvbmm 0/1 ContainerCreating 0 4s
web-x6wxd 0/1 ContainerCreating 0 4s
[root@k8s-01 chapter04]# kubectl get rc
NAME DESIRED CURRENT READY AGE
web 3 3 0 14s
[root@k8s-01 chapter04]# kubectl get replicationController
NAME DESIRED CURRENT READY AGE
web 3 3 1 27s如果删除一个pod ,会自己新增一个pod
kubectl delete pod/web-x6wxd新增一个标签看看
[root@k8s-01 chapter04]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-97499b967-jzxwg 1/1 Running 0 14h app=nginx,pod-template-hash=97499b967
web-rftd7 1/1 Running 0 4m16s app=nginx
web-rvbmm 1/1 Running 0 7m35s app=nginx
web-x6wxd 1/1 Running 0 7m35s app=nginx[root@k8s-01 chapter04]# kubectl label pod web-rftd7 type=special
pod/web-rftd7 labeled
[root@k8s-01 chapter04]# kubectl get pod --show-labels
NAME READY STATUS RESTARTS AGE LABELS
nginx-97499b967-jzxwg 1/1 Running 0 14h app=nginx,pod-template-hash=97499b967
web-rftd7 1/1 Running 0 5m20s app=nginx,type=special
web-rvbmm 1/1 Running 0 8m39s app=nginx
web-x6wxd 1/1 Running 0 8m39s app=nginx覆盖过去
kubectl label pod web-rftd7 app=nginx1 --overwrite添加pod的标签
# kubectl label pod web-z8stj type=special更改pod的标签
# kubectl label pod web-z8stj app=foo --overwrite再次列出pod
# kubectl get pods –L app修改rc的数量为2个
[root@k8s-01 chapter04]# kubectl get rc
NAME DESIRED CURRENT READY AGE
web 3 3 3 17m
[root@k8s-01 chapter04]# kubectl edit rc web
replicationcontroller/web edited
[root@k8s-01 chapter04]# kubectl get rc
NAME DESIRED CURRENT READY AGE
web 2 2 2 18m修改里面的数量为2 个水平扩缩容
[root@k8s-01 chapter04]# kubectl scale rc web --replicas=6
replicationcontroller/web scaled
[root@k8s-01 chapter04]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx-97499b967-jzxwg 1/1 Running 0 14h
web-fhg2l 0/1 ContainerCreating 0 4s
web-nsxsh 0/1 ContainerCreating 0 4s
web-pcwkz 0/1 ContainerCreating 0 4s
web-rvbmm 1/1 Running 0 19m
web-vj48d 0/1 ContainerCreating 0 4s
web-x6wxd 1/1 Running 0 19mkubectl delete rc web ,删除rcRC (ReplicaController) 通过标签来控制pod的,改变pod的标签,pod会进行变化
ReplicaSet
针对rc而言,选择更加灵活kubectl describe rs[root@k8s-01 chapter04]# cat web-replicaset.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:name: web
spec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx生产环境用rs多些
Deployments
Deployment为Pods和ReplicaSets提供声明式的更新。
在部署中描述一个期望的状态,部署控制器以某个控制的速率改变实际的状态到期望的状态控制更新的节奏和速率[root@k8s-01 chapter04]# cat nginx-deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:name: nginx-deploymentlabels:app: nginx
spec:replicas: 3selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.7.9ports:- containerPort: 80创建
# kubectl apply –f nginx-deployment.yaml查看部署
# kubectl get deployment查看部署的状态
# kubectl rollout status deployment.v1.apps/nginx-deployment查看通过部署创建的rs
# kubectl get rs查看pod自动产生的labels
# kubectl get pods –show-labels更新nginx:1.7.9为nginx:1.9.1
# kubectl –record deployment.apps/nginx-deployment set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1
或者:
# kubectl set image deployment/nginx-deployment nginx=nginx:1.9.1 –record
或者:
# kubectl edit deployment.v1.apps/nginx-deployment查看更新的状态
# kubectl rollout status deployment.v1.apps/nginx-deployment通过以下命令查看部署
# kubectl get deployment查看部署的rs
# kubectl get rs查看新的pods
# kubectl get pods查看部署的详情
# kubectl describe deployments回滚部署
更新一个不存在的镜像
# kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.91 –record=true通过以下命令查看更新卡住了
# kubectl rollout status deployment.v1.apps/nginx-deployment查看部署的rs
# kubectl get rs查看部署的pods
# kubectl get pods查看部署的描述信息
# kubectl describe deployment查看部署的版本
# kubectl rollout history deployment.v1.apps/nginx-deployment查看每个版本的详情
# kubectl rollout history deployment.v1.apps/nginx-deployment –revision=2回退到先前的版本
# kubectl rollout undo deployment.v1.apps/nginx-deployment回退到特定的版本
# kubectl rollout undo deployment.v1.apps/nginx-deployment --to-revision=2检查回退是否成功
# kubectl get deployment nginx-deployment部署的扩容
扩容副本数量为10
# kubectl scale deployment.v1.apps/nginx-deployment --replicas=10如果启用了水平的pod自动扩容可以执行如下:
# kubectl autoscale deployment.v1.apps/nginx-deployment --min=10 --max=15 --cpu-percent=80扩容的比例和刻度
确保部署的10个复本处于运行
# kubectl get deploy更新一个不存在的镜像,并查看结果
# kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:sometag
# kubectl get rs更新复本的数量为15个,再次查看结果,然后删除本次部署
# kubectl scale deployment.v1.apps/nginx-deployment --replicas=15
# kubectl get deployment
# kubectl get rs
# kubectl delete deploy nginx-deployment暂停和继续部署
创建部署
# kubectl create –f nginx-deployment.yaml暂停更新
# kubectl rollout pause deployment.v1.apps/nginx-deployment更新镜像
# kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.9.1查看是否有新的更新开始
# kubectl rollout history deployment.v1.apps/nginx-deployment恢复部署
# kubectl rollout resume deployment.v1.apps/nginx-deployment
DaemonSet
使用DaemonSet在每个节点上运行一个pod
DaemonSet确保创建尽可能多的pod,并将每个pod部署到自己的节点上。使用DaemonSet仅在特定的节点上运行Pods
这可以通过在pod模板中指定node-selector属性来完成的,它是DaemonSet定义的一部分[root@k8s-01 chapter04]# cat ssd-monitor-daemonset.yaml
apiVersion: apps/v1
kind: DaemonSet
metadata:name: ssd-monitor
spec:selector:matchLabels:app: ssd-monitortemplate:metadata:labels:app: ssd-monitorspec:nodeSelector:disk: ssdcontainers:- name: mainimage: luksa/ssd-monitor[root@k8s-01 chapter04]# kubectl get pod -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-97499b967-jzxwg 1/1 Running 0 16h 10.244.1.2 k8s-02 <none> <none>打标签
kubectl label node k8s-02 disk=ssdkubectl get ds
kubectl get pods打完标签后指哪打哪
Job
这种是运行一次,直到完成在Job完成后,它并不会删除Pod,允许我们可以查看它的日志在Job中运行多个Pod实例
Job可以配置为创建多个Pod实例,依并行或有序的方式运行它们。按顺序来运行Job
如果需要一个job运行多次,可以将completions设置为希望job的Pod运行多次。
配置请参考文件:multi-completion-batch-job.yaml以并行的方式运行job pods.
如果让job并行运行多个pods,可以使用parallelism指定并运行pod的数量。
配置请参考文件:multi-completion-batch-job2.yaml限制Job pod的完成时间
通过设置activedeadlinesecond属性 [root@k8s-01 chapter04]# cat multi-completion-batch-job.yaml
apiVersion: batch/v1
kind: Job
metadata:name: multi-completion-batch-job
spec:completions: 5template:metadata:labels:app: batch-jobspec:restartPolicy: OnFailurecontainers:- name: mainimage: luksa/batch-job
[root@k8s-01 chapter04]# cat multi-completion-batch-job2.yaml
apiVersion: batch/v1
kind: Job
metadata:name: multi-completion-batch-job2
spec:completions: 5parallelism: 2template:metadata:labels:app: batch-jobspec:restartPolicy: OnFailurecontainers:- name: mainimage: luksa/batch-job
Cronjob
这个是定时计划任务,周期性的[root@k8s-01 chapter04]# cat batch-job-every-fifteen-minutes.yaml
apiVersion: batch/v1beta1
# API group的版本是v1beat1,这里不要写错
kind: CronJob
metadata:name: batch-job-every-fifteen-minutes
spec:
# 该任务将会在每小时的0,15,30,45分运行schedule: "0,15,30,45 * * * *"jobTemplate:spec:template:metadata:labels:app: periodic-batch-job-every-fifteen-minutesspec:restartPolicy: OnFailurecontainers:- name: mainimage: luksa/batch-job
[root@k8s-01 chapter04]# cat cronjob.yaml
apiVersion: batch/v1beta1
kind: CronJob
metadata:name: hello
spec:schedule: "*/1 * * * *"jobTemplate:spec:template:spec:containers:- name: helloimage: busyboxargs:- /bin/sh- -c- date; echo Hello from the kubernetes clusterrestartPolicy: OnFailure使用以下命令获取它的状态
# kubectl get cronjob hello也可以通过以下命令获取它的状态
# kubectl get jobs --watch可以通过以下命令查看上次调度的时间
# kubectl get conrjob hello删除cronjob
# kubectl delete cronjob hello
参考文档
https://edu.csdn.net/course/detail/27762?spm=1003.2449.3001.8295.2