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
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
全ての永続トークンを、生まれる前に消し去りたい!
sylph01
2
1k
Long Living Tokens, Die, Die, Die! (or: The One Ring is Banned in Modern)
sylph01
0
330
エージェントを使い続けるためのメタワーク / Meta-work for keeping using the agent
rkaga
1
310
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
0
110
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
180
dotNET_vulnclicker_intro.pdf
deka_morita
0
150
Kotlinの15年とServer-Side Kotlin
n_takehata
1
160
見えないものを探る要求要件定義に必要な基本的思考 / invisible-requirement-thinking
minodriven
15
8.6k
AIで開発が速くなった先で、開発チームは何を目指すか
shihochan
1
1.7k
[Dev Group Paris Octobre 26] Agentforce, SIforce, Claudeforce, Slackbot… on s'y retrouve ?
nabondance
0
110
[ハンズオン]AIへの指示だけで「五目並べ」を作ってみよう
satoshi256kbyte
1
380
なぜCTOを降りてFDEを選んだのか?〜なぜプロダクト企業がFDEで顧客の現場に踏み込むのか〜
gonta
1
220
Featured
See All Featured
First, design no harm
axbom
PRO
2
1.3k
For a Future-Friendly Web
brad_frost
184
10k
A designer walks into a library…
pauljervisheath
211
25k
Keith and Marios Guide to Fast Websites
keithpitt
413
23k
Crafting Experiences
bethany
1
390
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
370
The Illustrated Children's Guide to Kubernetes
chrisshort
51
53k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
460
The Mindset for Success: Future Career Progression
greggifford
PRO
0
540
The browser strikes back
jonoalderson
0
1.8k
Accessibility Awareness
sabderemane
1
250
Utilizing Notion as your number one productivity tool
mfonobong
4
620
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で快適なバッチライフを!
ご清聴ありがとうございました!