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
380
Kubernetes入門#4 About Kubernetes Uses With GKE & EKS
hirose39
1
370
Kubernetes入門#3 About ConfigMap and Secret / Kubernetes Getting Started#3
hirose39
1
640
Kubernetes入門#1 運用例の紹介 / Kubernetes Getting Started#1 Case Introduction for k8ssa
hirose39
0
310
Other Decks in Programming
See All in Programming
難しいけど、読めた。- OSSの入口に立った話。
sts11142
0
150
速習iPhone Duo対応
yuukiw00w
2
980
setup-vp GitLab対応の裏側
naokihaba
0
160
UPDATE をやめる — EF Core でマスタをバージョン管理する
panda728
PRO
0
1.2k
すこし踏み込む CancellationToken
htkym
2
1.7k
The Rails Doctrine Decade
koic
2
550
Streamlitで実現する自然言語データアプリ開発
ayumu_yamaguchi
1
350
GemmaをJevのように使ってみる / Use Gemma like Jev
kishida
5
760
Verilogで学ぶCPU自作入門
uyuki234
8
4.1k
Jetpack Compose Mechanisms
skydoves
2
680
コードで差別化できない時代の ソフトウェアエンジニアリング / Software Engineering in an Era Where Code Doesn’t Differentiate
kishida
2
150
Are APIs Still Relevant in the AI Era?
soyuka
0
450
Featured
See All Featured
From Legacy to Launchpad: Building Startup-Ready Communities
dugsong
0
350
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
540
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
390
Utilizing Notion as your number one productivity tool
mfonobong
4
610
More Than Pixels: Becoming A User Experience Designer
marktimemedia
3
560
Odyssey Design
rkendrick25
PRO
2
870
Principles of Awesome APIs and How to Build Them.
keavy
128
18k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Collaborative Software Design: How to facilitate domain modelling decisions
baasie
1
340
How to build an LLM SEO readiness audit: a practical framework
nmsamuel
2
940
Max Prin - Stacking Signals: How International SEO Comes Together (And Falls Apart)
techseoconnect
PRO
0
480
Lessons Learnt from Crawling 1000+ Websites
charlesmeaden
PRO
1
1.6k
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