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
87
NotebookLMが変えるエンジニアの情報整理術
waytkheming
0
170
gRPC入門
waytkheming
0
150
Other Decks in Programming
See All in Programming
私なりのハーネスエンジニアリング ~独自の言語処理系の開発を通して~
seosoft
0
200
オーケストレーション記述言語 Katari
yukikurage
0
250
Python Lambdaにおけるテストしやすい設計とAI支援の活用 / Designing Testable Python Lambda Functions with AI
kitano_yuichi
1
310
社内AIエージェント「Askワンバン」における コストチューニングの取り組み
t0yohei
0
170
SODA - FACT BOOK(JP)
sodainc
1
40k
TS 7.0 で baseUrl が使えなくなった今、パスエイリアスをどうするか
bicstone
2
590
느린 네트워크와 저사양 디바이스 사용자를 위한 웹뷰 로딩 시간 개선기
kmkim
1
220
Lambda durable functionsでHuman in the Loopを楽に…楽に……
har1101
2
300
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
170
自作Discord音声botをLambda MicroVMsに移行した話 / Migrating My Discord Voice Bot to Lambda MicroVMs
akihisaikeda
1
310
はじめてのPLATEAU
nokonoko1203
1
640
Goコンパイラを作ってGoをもっと理解する / Build a Go compiler to better understand Go
ikura_hamu
0
130
Featured
See All Featured
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
203
77k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
1k
Design in an AI World
tapps
1
390
Navigating Weather and Climate Data
rabernat
0
590
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
980
JAMstack: Web Apps at Ludicrous Speed - All Things Open 2022
reverentgeek
1
680
Practical Orchestrator
shlominoach
192
12k
State of Search Keynote: SEO is Dead Long Live SEO
ryanjones
0
350
How to make the Groovebox
asonas
2
2.5k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
2
3.8k
brightonSEO & MeasureFest 2025 - Winning Strategies for Black Friday CRO & PPC - Christian Goodrich
cargoodrich
3
890
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で快適なバッチライフを!
ご清聴ありがとうございました!