k8s cron

Overview

With regard to scheduled tasks, there are a variety of options such as third-party packages, server cron, k8s cronjob, where only k8s cronjob, others can learn from the relevant third-party package information themselves.

2. Project address

Project address:https://github.com/Mikaelemmm/zerok8scron

We’ve integrated cobra,k8s cronjob directly executes job name at each schedule

3. Key code analysis

main.go

  1. package main
  2. import (
  3. "zerok8scron/internal/cmd"
  4. )
  5. func main() {
  6. cmd.Execute()
  7. }

internal/cmd/root.go

  1. package cmd
  2. import (
  3. "github.com/spf13/cobra"
  4. "github.com/zeromicro/go-zero/core/conf"
  5. "os"
  6. "zerok8scron/internal/config"
  7. "zerok8scron/internal/logic"
  8. "zerok8scron/internal/svc"
  9. )
  10. const (
  11. codeFailure = 1
  12. )
  13. var (
  14. confPath string
  15. rootCmd = &cobra.Command{
  16. Use: "cron",
  17. Short: "exec cron job",
  18. Long: "exec cron job",
  19. }
  20. // all job ...
  21. helloJob = &cobra.Command{
  22. Use: "hello",
  23. Short: "print 'hello SvcName' once per minute",
  24. RunE: logic.Hello,
  25. }
  26. // add more job , wait for you.....
  27. )
  28. // Execute executes the given command
  29. func Execute() {
  30. if err := rootCmd.Execute(); err != nil {
  31. os.Exit(codeFailure)
  32. }
  33. }
  34. func init() {
  35. // init config
  36. cobra.OnInitialize(initConfig)
  37. rootCmd.PersistentFlags().StringVar(&confPath, "config", "etc/cron.yaml", "config file (default is $HOME/.cobra.yaml)")
  38. // add subcommand
  39. rootCmd.AddCommand(helloJob)
  40. }
  41. func initConfig() {
  42. var c config.Config
  43. conf.MustLoad(confPath, &c)
  44. svc.InitSvcCtx(c)
  45. }

internal/logic/hello.go

  1. package logic
  2. import (
  3. "fmt"
  4. "github.com/spf13/cobra"
  5. "zerok8scron/internal/svc"
  6. )
  7. // Hello print "hello SvcName" once per minute
  8. func Hello(_ *cobra.Command, _ []string) error {
  9. fmt.Printf("srvName : %s , hello \n", svc.GetSvcCtx().Config.Name)
  10. return nil
  11. }

3.1 Implementation schedule

It can be seen that rootCmd is the primary command, helloJob is one of our own tasks to schedule, and the method to be executed after the scheduler is the method in the logo.go.

If more tasks need to be added, we will continue to add them directly below and add a corresponding implementation in the logic.

3.2 How to initialize configuration

We initialized the configuration before executing it, see that you may be familiar here. We place the initialization configuration of the go-zero default in the main configuration here

We use the default profile as etc/cron.yaml

4. Operational schedule

4.1 Local execution once

  1. $ go run main.go hello

4.2 Executed once in docker

  1. $ goctl docker -go main.go #创建dockerfile,如果你用上面的项目,项目中已经创建好可以省略
  2. $ docker build -t zerok8scron:v1 . # 构建镜像,如果你用上面的项目,项目中已经创建好可以省略
  3. $ docker run zerok8scron:v1 hello #运行即可

4.3 k8s with cronjob scheduling once a minute

cronjob.yaml

  1. apiVersion: batch/v1
  2. kind: CronJob
  3. metadata:
  4. name: hello
  5. spec:
  6. schedule: "*/1 * * * *"
  7. jobTemplate:
  8. spec:
  9. template:
  10. spec:
  11. containers:
  12. - name: hello
  13. image: zerok8scron:v1
  14. args:
  15. - hello
  16. restartPolicy: OnFailure

Run

  1. $ kubectl apply -f cronjob.yaml

Then you can view the cronjob state and output