Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Kubernetes入門#2 CronJob / Kubernetes Getting Sta...
Search
hi-rose
November 22, 2018
Programming
1
560
Kubernetes入門#2 CronJob / Kubernetes Getting Started#2 CronJob
Kubernetes Sapporo for Beginners #k8ssa
hi-rose
November 22, 2018
Tweet
Share
More Decks by hi-rose
See All by hi-rose
20231118_過去の叡智から学ぶ効率的なアウトプット術
hirose39
0
150
Kubernetes入門#5 The pod resource and QoS / Kubernetes Getting Started#5
hirose39
1
340
Kubernetes入門#4 About Kubernetes Uses With GKE & EKS
hirose39
1
350
Kubernetes入門#3 About ConfigMap and Secret / Kubernetes Getting Started#3
hirose39
1
580
Kubernetes入門#1 運用例の紹介 / Kubernetes Getting Started#1 Case Introduction for k8ssa
hirose39
0
280
Other Decks in Programming
See All in Programming
After go func(): Goroutines Through a Beginner’s Eye
97vaibhav
0
380
Building, Deploying, and Monitoring Ruby Web Applications with Falcon (Kaigi on Rails 2025)
ioquatix
4
2.1k
Things You Thought You Didn’t Need To Care About That Have a Big Impact On Your Job
hollycummins
0
220
コードとあなたと私の距離 / The Distance Between Code, You, and I
hiro_y
0
160
Goで実践するドメイン駆動開発 AIと歩み始めた新規プロダクト開発の現在地
imkaoru
4
830
デミカツ切り抜きで面倒くさいことはPythonにやらせよう
aokswork3
0
230
Go Conference 2025: Goで体感するMultipath TCP ― Go 1.24 時代の MPTCP Listener を理解する
takehaya
9
1.7k
Swift Concurrency - 状態監視の罠
objectiveaudio
2
520
Writing Better Go: Lessons from 10 Code Reviews
konradreiche
0
1.1k
なぜGoのジェネリクスはこの形なのか? Featherweight Goが明かす設計の核心
ryotaros
7
1.1k
『毎日の移動』を支えるGoバックエンド内製開発
yutautsugi
2
240
SpecKitでどこまでできる? コストはどれくらい?
leveragestech
0
740
Featured
See All Featured
Intergalactic Javascript Robots from Outer Space
tanoku
273
27k
How STYLIGHT went responsive
nonsquared
100
5.8k
Code Reviewing Like a Champion
maltzj
526
40k
Side Projects
sachag
455
43k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
48
9.7k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
34
6.1k
The Language of Interfaces
destraynor
162
25k
A designer walks into a library…
pauljervisheath
209
24k
Fireside Chat
paigeccino
40
3.7k
Principles of Awesome APIs and How to Build Them.
keavy
127
17k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
15k
Fashionably flexible responsive web design (full day workshop)
malarkey
407
66k
Transcript
Kubernetes Sapporo for Beginners Kubernetes 入門 #2 ~ ハンズオン:こんなのもあるよ!CronJobを動かしてみよう~
Kubernetes Sapporo for Beginners 自己紹介 廣瀬 亮輔(Ryosuke Hirose)/@hi-rose • 最近は家の近くにできたジムに通うのがマイブーム •
旭川高専卒業後、札幌で今の会社に就職してシステム開発 ◦ 言語はC#、Java、delphiなど ◦ ミドルウェアはSQLServer、Oracle、Apache、Tomcatなど • 2017年4月からアーキテクト部署となりクラウドシステムのバックエンド系基盤機能開発に ◦ Kubernetesもそのころから利用 ▪ 普段GCPで使用しているため、 GCP実装ベースでの理解が中心となります ◦ データマイグレーション系サービスのプロジェクトリーダー 2
Kubernetes Sapporo for Beginners 今日のゴール • CronJobがどんなものかわかるようになる • CronJobを動かしたことがある人になる 3
Kubernetes Sapporo for Beginners 事前準備 このイベントのひとつ前のセッション (Step1)ができていれば Kubernetesの実行環境としては問題ありません。 $ kubectl
apply -f Step1-ApplicationDeployment/statefulsets 注意事項! コマンドは hands-onリポジトリをcloneした場所をカレントディレクトリとして記載しています。 本セッションではひとつ前のセッションで使用した greet APIを 呼び出すため、リソースを削除している場合は以下のコマンドで起動してください。 4
Kubernetes Sapporo for Beginners CronJobについて Cronとは 特定のJob(コマンドやスクリプト )をスケジューリングして自動実行するための仕組み。 Windowsでいうとタスクスケジューラーでも同じようなことが可能。 */5 5-9 * * * execute.sh
分 時 日 月 曜日 実行コマンド 例)毎日5時~9時台に5分おきにexecute.shを実行する定義。 5
Kubernetes Sapporo for Beginners CronJobについて2 CronJobとは KubernetesにJobという処理の終了を前提としたバッチ的処理を行うためのワークロードが存在。 このJobをCron形式でスケジューリングして実行することが可能。 Master Node
Scheduler APIs kubelet Container Runtime Pod(Job) 6
Kubernetes Sapporo for Beginners 今回のハンズオン構成 Node cron-test(Pod) greet(Pod) 今回の構成では、CronJobのPod「cron-test」から「greet」Podをスケジューリングで定期的に呼び出すようにし ています。
次ページより実際にその時の動きを見ていきます。 7
Kubernetes Sapporo for Beginners Let's ハンズオン STEP.1 CronJobのワークロードを作成します。 $ kubectl
apply -f Step2-Cronjob/cronjob Cronjob.batch “cron-test” created STEP.2 実行状態を確認します。 $ kubectl get pod -w NAME READY STATUS RESTARTS AGE greet-0 2/2 Running 0 10m cron-test-1542618600-t8xjr 0/1 Pending 0 0s cron-test-1542618600-t8xjr 0/1 Pending 0 0s cron-test-1542618600-t8xjr 0/1 ContainerCreating 0 0s cron-test-1542618600-t8xjr 0/1 Completed 0 3s Completedが出たら実行完了 8
Kubernetes Sapporo for Beginners Let's ハンズオン2 STEP.3 実行結果はlogsコマンドで確認します。 $ kubectl
logs cron-test-1542618600-t8xjr “Hello” APIの実行結果が取得できている。 9
Kubernetes Sapporo for Beginners 中身の説明 apiVersion: batch/v1beta1 kind: CronJob metadata:
name: cron-test spec: concurrencyPolicy: Replace schedule: "*/1 * * * *" jobTemplate: spec: template: metadata: labels: app: cron-test spec: restartPolicy: Never containers: - name: cron-test image: k8ssa/hands-on-alpine-cronjob:latest imagePullPolicy: IfNotPresent resources: requests: cpu: 100m memory: 100Mi limits: cpu: 100m memory: 100Mi args: - sh - -c - curl -s http://greet:30000/hello | jq '.Message' 既に実行中のCronjobがあった場 合の動作 Allow:制限なし Replace:停止して置き換え Forbid:次のJobを実行しない Cron形式のスケジューリング ※時刻はUTC指定 greet API呼び出し結果jsonをjq により要素抜き出し ※実行イメージはalpine linuxにcrulとjqコマンドを入れたものを勉強会用に作成し、Dockerhubにアップ。 https://hub.docker.com/r/k8ssa/hands-on-alpine-cronjob/~/dockerfile/ POD障害時のポリシー Never:新規PODを作成し再実行 OnFailure:同一PODで再実行 ※ 10
Kubernetes Sapporo for Beginners Let's ハンズオン3 STEP.4 しばらくしてpodを取得すると、Completedとなっている実行履歴が複数出てきます。 $ kubectl
get pod cron-test-1542618600-t8xjr 0/1 Completed 0 2m cron-test-1542618600-azc9e 0/1 Completed 0 1m cron-test-1542618600-zbnbk 0/1 Completed 0 37s Historyが残っていればkubectl logsにより確認ができるが、 保存数に限りがあるため、別途ログ収集基盤を用意しておいたほうが良い。 実行履歴は定義Yamlにより設定可能です。 spec.successfulJobsHistoryLimit … 成功したJobを保存する数 spec.failedJobsHistoryLimit … 失敗したJobを保存する数 11
Kubernetes Sapporo for Beginners STEP.6 しばらくしてpodを取得すると、実行が停止していることがわかります。 $ kubectl get pod
cron-test-1542618600-t8xjr 0/1 Completed 0 5m cron-test-1542618600-azc9e 0/1 Completed 0 4m cron-test-1542618600-zbnbk 0/1 Completed 0 3m Let's ハンズオン4 STEP.5 CronJobの一時停止を行うことができます。 $ kubectl patch cronjob cron-test -p '{"spec":{"suspend":true}}' Cronjob.batch “cron-test” patched ※Windowsはこちら patchで変更差分を当てていますがYamlを変更してApplyでもOK $ kubectl patch cronjob cron-test -p "{\"spec\":{\"suspend\":true}}" 12
Kubernetes Sapporo for Beginners 補足 運用中にスケジュールを管理する Kubernetes Masterが動作を停止していた場合など、実行開始時間を過ぎ てしまう場合が想定されます。 その場合に実行遅延を許容する設定が可能です。
例)10分以内の実行遅延であればその Jobを実行する spec.startingDeadlineSeconds: 600 13
Kubernetes Sapporo for Beginners お片付け STEP.7 定義を削除します。 $ kubectl delete
-f Step2-Cronjob/cronjob $ kubectl delete -f Step1-ApplicationDeployment/statefulsets $ kubectl delete persistentvolumeclaim/greet-storage-greet-0 $ kubectl delete persistentvolumeclaim/greet-storage-greet-1 14
Kubernetes Sapporo for Beginners CronJobの利点と利活用 • 利点 ◦ Kubernetes内で完結して仕組みを構築することが可能 ▪
Yamlファイルによる一元管理 ▪ 別マシンを立てる必要がない • 運用コストや、そのマシンの死活リスクの面でメリット ◦ JobごとにCPU,Memoryリソースを確保して実行が可能 ◦ 通常のCronとは違い、エラー時の再実行など、ある程度動作制御が可能 ◦ Kubernetesでの実行成否確認を行うことができる ▪ コンテナ内でCronプロセスを動かす場合、実行結果を別の場所に残す必要がある • 活用ポイント ◦ 夜間バッチ的な処理 ◦ バックアップ ◦ 不要データの整理 ◦ 一括データ処理のトリガ ◦ 時報トリガ 15
Kubernetes Sapporo for Beginners 注意点 • β機能です • 場合によってJobが実行されないことや、複数の Jobが同時に実行される可能性があります。
◦ 実行されない部分は設定によりある程度回避できる。 ◦ 複数実行は処理側で冪等性を担保する必要がある。 • 詳細は公式ページにある制限事項を参照。 16
Kubernetes Sapporo for Beginners 質疑応答 お聞きいただきありがとうございました。 何でも気軽にご質問くださいませ! 17