Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
GoとKubernetesを用いたバッチ開発のすすめ
Search
Sponsored
·
SiteGround - Reliable hosting with speed, security, and support you can count on.
→
To Kawa
April 23, 2022
Programming
2.5k
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
GoとKubernetesを用いたバッチ開発のすすめ
Go Conference 2022 Springでの発表資料です
https://gocon.jp/2022spring/ja/sessions/lt5/
To Kawa
April 23, 2022
More Decks by To Kawa
See All by To Kawa
Coding Agentを用いた仕様駆動開発を試す中で考えたこれからのエンジニアに重要な能力
waytkheming
0
82
NotebookLMが変えるエンジニアの情報整理術
waytkheming
0
160
gRPC入門
waytkheming
0
150
Other Decks in Programming
See All in Programming
ジョジョの世界をXRで再現する
mofurune
0
140
Androidだけじゃない、DroidKaigiで広がった私の技術スタック
akkeylab
0
120
外から家のPCの電源入れたい!をPHPで解決する
kawashimo0
1
120
コードレビューのボトルネックを"する側"と"される側"の両面から解消する
yub0n
2
1.5k
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8k
Verilogで学ぶCPU自作入門
uyuki234
8
4.1k
Everything will be SERVERLESS — 信じて運用した10年の経験値 / Everything Will be Serverless — Lessons Learned from 10 Years of Operational Experience
seike460
PRO
1
890
Starting & Sustaining Code-Based E2E Testing for Non-Coding QA Teams( #jasstniigata )
teyamagu
PRO
1
940
When benchmarks go bad - what I learned from measuring performance wrong
hollycummins
0
190
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
510
UnityでSystem.Net.WebSocketsなWebSocketサーバが動かないのでUnity Monoのコードを覗いてみた / about implementing websocket server with unity mono
drumath2237
1
570
プロダクトコードからライブラリの境界を見つける
elmetal
PRO
0
110
Featured
See All Featured
Optimising Largest Contentful Paint
csswizardry
37
4k
Why You Should Never Use an ORM
jnunemaker
PRO
61
10k
From π to Pie charts
rasagy
1
390
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
A better future with KSS
kneath
240
18k
Technical Leadership for Architectural Decision Making
baasie
3
600
Understanding Cognitive Biases in Performance Measurement
bluesmoon
32
3k
Taking LLMs out of the black box: A practical guide to human-in-the-loop distillation
inesmontani
PRO
3
2.4k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
WENDY [Excerpt]
tessaabrams
14
40k
Redefining SEO in the New Era of Traffic Generation
szymonslowik
1
470
Navigating Team Friction
lara
192
16k
Transcript
GoとKubernetesを用いた バッチ開発のすすめ @awakot Go Conference 2022 Spring
自己紹介 Toshiki Kawamura 株式会社Voicy BE→SRE Twitter: awakot_56 Github: awakot
今日の話題 1. Go×Kubernetesを用いたバッチ開発方法の概要 2. 開発運用して感じたメリットとTips
ここでのバッチ処理とは 任意のタイミングで対象データに対して まとめて実施される処理 例 ・任意のタイミングで対象ユーザーにメールを送るシステム ・月初に定期購読ユーザーを更新するシステム ・一定間隔で売上の集計を行うシステム
バッチシステムに関して - GoでCLIを作成し、Kubernetes上で実行 - バッチ処理を実行するCLIツールとしてメインで利用したのは spf13/cobraパッケージ - https://github.com/spf13/cobra - KubernetesやIstioなどでも使用されていて、スター数も2万以上
- Command構造体に名前などを定義するだけで簡単に実行可能 - オプションフラグやエイリアスなど、CLIに必要な要素を簡単に設定するこ とができる
CLIを作る
まずはサブコマンドを定義
サブコマンドを定義 type hogehogeOptions struct { Notice bool Dryrun bool }
func (opt *hogehogeOptions) Run(ctx context.Context, w io.Writer) error { //ここに必要な処理を書く return nil } func HogeCommand(ctx context.Context) *cobra.Command { opt := &hogehogeOptions {} cmd := &cobra.Command{ Use: “hoge”, Short: “ 処理の説明“, Run: func(cmd *cobra.Command, args [] string) { if err := opt.Run(ctx, cmd.OutOrStdout()); err != nil { panic(err) } }, } cmd.Flags().BoolVarP(&opt.Notice, “notice”, “”, opt.Notice, “notice”) cmd.Flags().BoolVarP(&opt.Dryrun, “dryrun”, “”, opt.Dryrun, “dryrun”) return cmd
ルートコマンドにサブコマンドを追加していく ctx := context.Background() rootCmd := &cobra.Command{Use: “batch”} rootCmd.AddCommand(subcmd.HogeCommand(ctx)) rootCmd.AddCommand(subcmd.FooCommand(ctx))
Kubernetesでバッチを実行する
CronJobの定義例(一部) apiVersion: batch/v1beta1 kind: CronJob metadata: name: name namespace: namespace
spec: schedule: “00 18 * * * ” concurrencyPolicy: Forbid jobTemplate: spec: template: spec: containers: - name: hogehoge image: <image名> args: - /app/batch - hoge restartPolicy: Never #以下環境変数など
Go×Kubernetesでバッチ開発をしてみて - 他アプリケーションと同じくKubernetes上で管理や監視できる - JobやCronJobの実行をYAMLで管理できる - アプリケーションと同じ言語で書け、共通処理はそのまま利用できるので開発効率が上がる - ゴルーチンを用いた並行処理の導入で実行時間を短縮
バッチ開発のTips
リトライや冪等性を担保する仕組みを導入する バッチ処理が時に失敗する可能性があり、そういった場合に備えてシステム内にリトライの仕 組みや、リトライ時に備えた処理の冪等性が担保される仕組みを導入しておくと、バッチ処理 が仮に失敗しても簡単にリカバリが効くようになる また、KubernetesのCronJobにおいては以下のようなコマンドを実行することでCronJobから 即時実行されるJobを生成することが可能 kubectl create job job-name
--from=cronjob/cronjob-name -n namespace
1つのバッチの責務を小さくする 例えば大量のデータを一気に処理する場合には、1つのバッチの処理する量を減らし、段階 に分けた処理の実行や分割など、一つのバッチ毎の責務を小さくすることでリトライ設計や失 敗時のリカバリがしやすく管理のしやすいものになる
ゴルーチンをうまく利用する バッチ処理では大量データを扱うようなものが多いので、ゴルーチンを用いて並行処理をする ことで比較的簡単に処理時間を短縮することが可能 ただ並行処理を不必要に乱用するとシステムの複雑性が上がるため、注意は必要。 sync.WaitGroupやerrgroup.Groupなどを用いて、ゴルーチンの管理を丁寧にする必要はあ る
オプションフラグを活用する cmd.Flags().BoolVarP(&opt.Notice, “notice”, “”, opt.Notice, “notice”) cmd.Flags().BoolVarP(&opt.Dryrun, “dryrun”, “”, opt.Dryrun,
“dryrun”) バッチの動作確認をする際などに利用できるオプションを追加すると動作確認が簡単 にできるようになり非常に便利 (例) - メールなどの通知の送信を伴うバッチにおいては、noticeフラグをつけた際にのみ通知 を送る - dryrunフラグをつけるとデータの更新がされず処理内容のログが出るだけにする
Go×Kubernetesで快適なバッチライフを!
ご清聴ありがとうございました!