mac 上学习k8s系列(27)Deployment Job CronJob

2022-08-02 19:35:10 浏览数 (3)

在学习完上一讲mac 上学习k8s系列(26)ReplicationController、ReplicaSet、StatefulSet中的ReplicationSet后,学习Depolyment就非常容易了。它只是在RS中增加了如下功能:1,可以查看Deployment的升级详细进度和状态2,可以使用回滚操作回滚到之前的任一版本3,每一次对Deployment的操作,都能够保存下来4,对于每一次升级都能够随时暂停和启动。整体来说就是运维更方便了。Job和CronJob对应的是任务和周期性任务。还是基于前面创建的apple:5678镜像来学习,由于该镜像没有装bash,所以学习Job的时候使用的是apline:3.13

1,Deployment

代码语言:javascript复制
apiVersion: apps/v1
kind: Deployment 
metadata:
  name: apple-deployment 
spec:
  replicas: 3 
  selector: #标签选择器
    matchLabels:
      app: apple 
  template:
    metadata:
      labels:
        app: apple 
    spec:
      containers:   
      - name: apple-app 
        image: apple:5678 
        ports:
        - containerPort: 5678

中间遇到的问题

代码语言:javascript复制
% kubectl apply -f deployment/Deployment.yaml
error: error validating "deployment/Deployment.yaml": error validating data: ValidationError(Deployment.spec): missing required field "selector" in io.k8s.api.apps.v1.DeploymentSpec; if you choose to ignore these errors, turn validation off with --validate=false

原因:我们创建deployment的时候必须指定selector

代码语言:javascript复制
 % kubectl apply -f deployment/Deployment.yaml
deployment.apps/apple-deployment created

查看下我们的deployment和pod

代码语言:javascript复制
% kubectl get deployment                     
NAME                       READY   UP-TO-DATE   AVAILABLE   AGE
apple-deployment           3/3     3            3           87s
代码语言:javascript复制
 % kubectl get pods                           
NAME                                        READY   STATUS      RESTARTS   AGE
apple-app                                   1/1     Running     18         102d
apple-deployment-fb8cfb965-bnjhr            1/1     Running     0          106s
apple-deployment-fb8cfb965-fftd8            1/1     Running     0          106s
apple-deployment-fb8cfb965-zznjr            1/1     Running     0          106s

当然,我们也可以更新镜像和回滚镜像

代码语言:javascript复制
kubectl set image deployment/apple-deployment apple-app=gorse_server:latest
deployment.apps/apple-deployment image updated
代码语言:javascript复制
% kubectl rollout history deployment/apple-deployment 
deployment.apps/apple-deployment 
REVISION  CHANGE-CAUSE
1         <none>
2         <none>

更新镜像后我们发现有两个版本的deployment,可以回滚掉刚才的更新

代码语言:javascript复制
%  kubectl rollout undo deployment/apple-deployment
deployment.apps/apple-deployment rolled back

当然我们也可以缩放我们的pod数量

代码语言:javascript复制
% kubectl scale deployment apple-deployment --replicas 1
deployment.apps/apple-deployment scaled

2,Job

代码语言:javascript复制
apiVersion: batch/v1
kind: Job
metadata:
  name: alpine-job
spec:
  template:
    metadata:
      name: alpine-job
    spec:
      restartPolicy: Never
      containers:
      - name: counter
        image: alpine:3.13
        command:
        - "bin/sh"
        - "-c"
        - "for i in 9 8 7 6 5 4 3 2 1; do echo $i; done"

这里需要注意的是Job的RestartPolicy仅支持Never和OnFailure两种,不支持Always。

代码语言:javascript复制
% kubectl apply -f deployment/job.yaml
job.batch/alpine-job created

3,CronJob

代码语言:javascript复制
apiVersion: batch/v1
kind: CronJob
metadata:
  name: alpine-cronjob
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          restartPolicy: OnFailure
          containers:
          - name: alpine-cronjob
            image: alpine:3.13
            args:
            - "bin/sh"
            - "-c"
            - "for i in 9 8 7 6 5 4 3 2 1; do echo $i; done"

CronJob里面多了个schedule 它的配置和crontab配置一样。可以用来执行周期性的任务。

0 人点赞