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
Kubernetes入門#2 CronJob / Kubernetes Getting Sta...
Search
hi-rose
November 22, 2018
Programming
600
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Kubernetes入門#2 CronJob / Kubernetes Getting Started#2 CronJob
Kubernetes Sapporo for Beginners #k8ssa
hi-rose
November 22, 2018
More Decks by hi-rose
See All by hi-rose
20231118_過去の叡智から学ぶ効率的なアウトプット術
hirose39
0
180
Kubernetes入門#5 The pod resource and QoS / Kubernetes Getting Started#5
hirose39
1
370
Kubernetes入門#4 About Kubernetes Uses With GKE & EKS
hirose39
1
370
Kubernetes入門#3 About ConfigMap and Secret / Kubernetes Getting Started#3
hirose39
1
630
Kubernetes入門#1 運用例の紹介 / Kubernetes Getting Started#1 Case Introduction for k8ssa
hirose39
0
310
Other Decks in Programming
See All in Programming
WebMCP Challenge に星空観察アプリで参加した話
okajun35
0
180
GraphRAGのKnowledge Graphを 直接!見る/View-GraphRAG's-KnowledgeGraph-directly!
tyumugi1113
1
340
FreeBSDでZabbixを動かす
kenkino
0
310
cdk deploy JawsSonic #MARATHONしながらAWSリソースをデプロイしてみよう
akihisaikeda
2
140
Intent as Code
shoppingjaws
6
1k
AHC070解法紹介
eijirou
0
120
thread_parallel_with_free-threaded_Python_and_NumPy.pdf
riku_sakamoto
0
350
Augmenting AI with the Power of Jakarta EE
ivargrimstad
0
220
SONY CISC-NEWS NWS-1750 + NWB-225 フレームバッファの NetBSD/news68k ドライバ実装 / OSC2026Hiroshima
tsutsui
0
120
WebAssembly in Android Apps 〜 WASMはJNIの夢を見るか
keiji
1
110
新卒PdEのリアル
ryu1013
1
500
個人開発基盤をまるごとCloudflareに引っ越して爆速で総合的体験を向上させた話
tinykitten
0
200
Featured
See All Featured
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
Practical Tips for Bootstrapping Information Extraction Pipelines
honnibal
25
2.1k
How People are Using Generative and Agentic AI to Supercharge Their Products, Projects, Services and Value Streams Today
helenjbeal
1
320
Design of three-dimensional binary manipulators for pick-and-place task avoiding obstacles (IECON2024)
konakalab
0
600
Paper Plane
katiecoart
PRO
4
53k
Test your architecture with Archunit
thirion
2
2.4k
Game over? The fight for quality and originality in the time of robots
wayneb77
1
280
The Cost Of JavaScript in 2023
addyosmani
55
10k
SEO in 2025: How to Prepare for the Future of Search
ipullrank
3
3.8k
Deep Space Network (abreviated)
tonyrice
0
310
Exploring anti-patterns in Rails
aemeredith
4
510
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
330
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