Upgrade to Pro — share decks privately, control downloads, hide ads and more …

oc observe コマンドで始める Kubernetes コントローラ開発 / Get started developing Kubernetes controller with oc observe

oc observe コマンドで始める Kubernetes コントローラ開発 / Get started developing Kubernetes controller with oc observe

OpenShift Meetup Tokyo #3 (2019/3/29)
https://openshift.connpass.com/event/122443/

Kazuki Suda

March 28, 2019
Tweet

More Decks by Kazuki Suda

Other Decks in Technology

Transcript

  1. OpenShift Meetup Tokyo #3 (2019/3/28) Kazuki Suda <[email protected]> @superbrothers oc

    observe コマンドではじめる
 Kubernetes コントローラ開発
  2. @superbrothers Kubernetes コントローラとは何か ▶ kubelet + Pods の追加/削除を監視してコンテナを操作する ▶ kube-proxy

    (iptables) + Services の追加/削除/変更を監視して、iptables のルールを操作する ▶ kube-controller-manager + Deployments, CronJobs などの追加/削除/変更を監視して、
 オブジェクトを操作する
  3. @superbrothers Kubernetes コントローラ/Operator 開発ツール ▶ kubernetes/client-go, kubernetes/code-generator ▶ kubernetes-sigs/controller-runtime, kubernetes/code-generator

    ▶ operator-framework/operator-sdk ▶ kubernetes-sigs/kubebuilder ▶ GoogleCloudPlatform/metacontroller ▶ oc observe コマンド
  4. @superbrothers oc observe とは何か oc コマンドのサブコマンド ▶ Kubernetes オブジェクトの追加/削除/変更のイベントをトリガに
 任意のスクリプトファイルを呼び出す

    ▶ oc コマンドのオリジナルで kubectl には存在しない + OpenShift だけでなく、通常の Kubernetes でも利⽤できる ❗ 私たちは、PoC 実装⽤に Kubernetes 上で利⽤しています $ oc observe namespaces -- ./set_owner.sh
  5. @superbrothers apiVersion: apps/v1 kind: Deployment metadata: name: mycontroller spec: selector:

    matchLabels: app: mycontroller template: metadata: labels: app: mycontroller spec: containers: - name: oc image: openshift/origin-cli:v3.11 command: - /bin/bash - -xec - | cat <<'SCRIPT' >./set_owner.sh #!/bin/bash if [[ "$(oc get namespace "$1" -o 'jsonpath={.metadata.annotations.owner}')" == "" ]]; then oc annotate namespace "$1" "owner=bob" fi SCRIPT chmod +x ./set_owner.sh oc observe namespaces -- ./set_owner.sh
  6. @superbrothers より詳しい oc observe コマンドの使い⽅ $ oc observe -h Observe

    changes to resources and take action on them This command assists in building scripted reactions to changes that occur in Kubernetes or OpenShift resources. This is frequently referred to as a 'controller' in Kubernetes and acts to ensure particular conditions are maintained. On startup, observe will list all of the resources of a particular type and execute the provided script on each one. Observe watches the server for changes, and will reexecute the script for each update. Observe works best for problems of the form "for every resource X, make sure Y is true". Some examples of ways observe can be used include: * Ensure every namespace has a quota or limit range object * Ensure every service is registered in DNS by making calls to a DNS API * Send an email alert whenever a node reports 'NotReady' * Watch for the 'FailedScheduling' event and write an IRC message * Dynamically provision persistent volumes when a new PVC is created * Delete pods that have reached successful completion after a period of time. The simplest pattern is maintaining an invariant on an object - for instance, "every namespace should have an annotation that indicates its owner". If the object is deleted no reaction is necessary. A variation on that pattern is creating another object: "every namespace should have a quota object based on the resources allowed for an owner". $ cat set_owner.sh #!/bin/sh