k8s基础入门之常见的控制器

1 常用的控制器

controllers:在集群上管理和运行容器的对象,通过label-selector相关联,pod通过控制器实现应用的运维,如伸缩,滚动升级等。 控制器分为如下几种
  • Deployment
  • DaemonSet
  • job/cronjob

1.1 Deployment控制器

功能
  • 部署无状态应用
  • 管理pod和replicaset(控制副本数量)
  • 具备上线部署,副本设定,滚动升级,回滚等功能
  • 提供声明式更新,例如只更新一个新的image
应用场景: web服务,微服务。 例子
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: default
spec:
   replicas: 3
   selector:
    matchLabels:
      app: nginx
   template:
    metadata:
      labels:
       app: nginx
spec:
   containers:
   - name: nginx
     image: nginx:latest
     ports:
     - containerPort: 80

声明式只更新images
kubectl set image deployment(资源类型) web(资源类型名称) nginx=nginx:1.16

更新的时候是先启动一个新的pod 然后关闭老的pod,升级方式为滚动更新

1.2 DaemonSet控制器

DaemonSet 功能
  • 在每一个node启动一个pod
  • 新加入的Node也会自动运行一个pod
应用场景
  • agent 类似监控

1.3 job&cronjob控制器

job分为普通任务(job),定时任务(crontab).
  • 一次性任务
  • 定时任务
应用场景: 离线数据处理

实例

生成使用job控制器的yaml
[root@k8s-master01 ~]# kubectl create job pi --image=perl --dry-run -o yaml
W0227 14:23:20.487830   58985 helpers.go:535] --dry-run is deprecated and can be replaced with --dry-run=client.
apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  name: pi
spec:
  template:
    metadata:
      creationTimestamp: null
    spec:
      containers:
      - image: perl
        name: pi
        resources: {}
      restartPolicy: Never
status: {}

[root@k8s-master01 ~]# cat jobs.yaml 
apiVersion: batch/v1
kind: Job
metadata:
  creationTimestamp: null
  name: pi
spec:
  template:
    metadata:
      creationTimestamp: null
    spec:
      containers:
      - image: perl
        name: pi
        command: ["perl", "-Mbignum=bpi", "-wle", "print bpi(2000)"]
      restartPolicy: Never
   backoffLimit: 4  # 指定重试的次数

执行
[root@k8s-master01 ~]# kubectl apply -f jobs.yaml 
job.batch/pi created
[root@k8s-master01 ~]# kubectl get pods
NAME                    READY   STATUS              RESTARTS   AGE
nginx-f89759699-gv5ms   1/1     Running             1          70d
pi-jb2bq                0/1     ContainerCreating   0          8s
pod-dianduqi            1/1     Running             0          35d
如下临时任务已执行完成
[root@k8s-master01 ~]# kubectl get pods
NAME                    READY   STATUS      RESTARTS   AGE
nginx-f89759699-gv5ms   1/1     Running     1          70d
pi-jb2bq                0/1     Completed   0          69s
pod-dianduqi            1/1     Running     0          35d

查看日志可以看到执行的结果
[root@k8s-master01 ~]# kubectl logs pi-jb2bq
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303819644288109756659334461284756482337867831652712019091456485669234603486104543266482133936072602491412737245870066063155881748815209209628292540917153643678925903600113305305488204665213841469519415116094330572703657595919530921861173819326117931051185480744623799627495673518857527248912279381830119491298336733624406566430860213949463952247371907021798609437027705392171762931767523846748184676694051320005681271452635608277857713427577896091736371787214684409012249534301465495853710507922796892589235420199561121290219608640344181598136297747713099605187072113499999983729780499510597317328160963185950244594553469083026425223082533446850352619311881710100031378387528865875332083814206171776691473035982534904287554687311595628638823537875937519577818577805321712268066130019278766111959092164201989380952572010654858632788659361533818279682303019520353018529689957736225994138912497217752834791315155748572424541506959508295331168617278558890750983817546374649393192550604009277016711390098488240128583616035637076601047101819429555961989467678374494482553797747268471040475346462080466842590694912933136770289891521047521620569660240580381501935112533824300355876402474964732639141992726042699227967823547816360093417216412199245863150302861829745557067498385054945885869269956909272107975093029553211653449872027559602364806654991198818347977535663698074265425278625518184175746728909777727938000816470600161452491921732172147723501414419735685481613611573525521334757418494684385233239073941433345477624168625189835694855620992192221842725502542568876717904946016534668049886272327917860857843838279679766814541009538837863609506800642251252051173929848960841284886269456042419652850222106611863067442786220391949450471237137869609563643719172874677646575739624138908658326459958133904780275901
[root@k8s-master01 ~]# 

查看任务
[root@k8s-master01 ~]# kubectl get job #可以查看所有的任务
NAME   COMPLETIONS   DURATION   AGE
pi     1/1           45s        103m

定时任务实例
crontabjob.yaml 
apiVersion: batch/v1beta1
kind: CronJob
metadata:
  name: hello
spec:
  schedule: "*/1 * * * *"
  jobTemplate:
    spec:
      template:
        spec:
          containers:
          - name: hello
            image: busybox
            imagePullPolicy: IfNotPresent
            args:
            - /bin/sh
            - -c
            - date; echo Hello from the Kubernetes cluster
          restartPolicy: OnFailure

执行 
[root@k8s-master01 ~]# kubectl apply -f crontabjob.yaml
cronjob.batch/hello created

[root@k8s-master01 ~]# kubectl get pods -w  
NAME                     READY   STATUS      RESTARTS   AGE
hello-1614414660-99mlq   0/1     Completed   0          53s
nginx-f89759699-gv5ms    1/1     Running     1          70d
pi-jb2bq                 0/1     Completed   0          123m
pod-dianduqi             1/1     Running     0          35d
hello-1614414720-l6t2l   0/1     Pending     0          0s
hello-1614414720-l6t2l   0/1     Pending     0          0s
hello-1614414720-l6t2l   0/1     ContainerCreating   0          0s
hello-1614414720-l6t2l   0/1     Completed           0          1s

[root@k8s-master01 ~]# kubectl get jobs
NAME               COMPLETIONS   DURATION   AGE
hello-1614414660   1/1           3s         2m14s
hello-1614414720   1/1           1s         74s
hello-1614414780   1/1           1s         14s

#会每一分钟启动一次pod 

查看日志 
[root@k8s-master01 ~]# kubectl logs hello-1614414660-99mlq
Sat Feb 27 08:31:10 UTC 2021
Hello from the Kubernetes cluster

其他参数
在spec下面添加如下2条内容,自定义历史记录限制
successfulJobsHistoryLimit: 5   #成功保留次数
failedJobsHistoryLimit: 10      #失败保留次数

参考地址: https://kubernetes.io/zh/docs/concepts/workloads/controllers/cron-jobs/
  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin
avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: