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

Kubernetes 入門 #4/kubernetes-sapporo-k8ssa-number-4

Kubernetes 入門 #4/kubernetes-sapporo-k8ssa-number-4

Hiroki Matsumoto

September 13, 2019
Tweet

More Decks by Hiroki Matsumoto

Other Decks in Technology

Transcript

  1. Kubernetes Sapporo for Beginners
    Kubernetes 入門 #4
    ~ Custom Resources & Custom Controllers ~

    View Slide

  2. Kubernetes Sapporo for Beginners
    自己紹介
    松本 宏紀 ( まつもと ひろき )
    ● Kubernetes Sapporo for Beginners主催者。
    ● チーフ・アーキテクト
    ● オフショアラボ・チームリーダー
    ● デブサミ2019「Spring Bootでマイクロサービス作って苦労したお話」登壇
    ● Google Cloud Next ‘19 in Tokyo 「大規模エンタープライズ システムをマイクロ サービスで刷新。その開発プロセス再定義まで
    道のり」登壇
    Twitter:@hirokimatsumo13

    View Slide

  3. Kubernetes Sapporo for Beginners
    今日のお話
    Custom ResourcesとCustom Controllersの利用シーン

    View Slide

  4. Kubernetes Sapporo for Beginners
    Custom Resources
    Kubernetesは独自のリソース(オブジェクト)を定義する事ができます。
    引用元:https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/
    Custom Resources
    A resource is an endpoint in the Kubernetes API that stores a collection of API objects of a certain kind. For example, the built-in pods
    resource contains a collection of Pod objects.
    A custom resource is an extension of the Kubernetes API that is not necessarily available in a default Kubernetes installation. It represents
    a customization of a particular Kubernetes installation. However, many core Kubernetes functions are now built using custom resources,
    making Kubernetes more modular.
    Custom resources can appear and disappear in a running cluster through dynamic registration, and cluster admins can update custom
    resources independently of the cluster itself. Once a custom resource is installed, users can create and access its objects using kubectl,
    just as they do for built-in resources like Pods.

    View Slide

  5. Kubernetes Sapporo for Beginners
    Custom Controllers
    Custom Resourceに対するリソースの振る舞いを提供する事ができるようになります。
    Operator Patternは、Custom ResourcesとCustom Controllersによって実現されます。
    Custom controllers
    On their own, custom resources simply let you store and retrieve structured data. When you combine a custom resource with a custom
    controller, custom resources provide a true declarative API.
    A declarative API allows you to declare or specify the desired state of your resource and tries to keep the current state of Kubernetes
    objects in sync with the desired state. The controller interprets the structured data as a record of the user’s desired state, and continually
    maintains this state.
    You can deploy and update a custom controller on a running cluster, independently of the cluster’s own lifecycle. Custom controllers can
    work with any kind of resource, but they are especially effective when combined with custom resources. The Operator pattern combines
    custom resources and custom controllers. You can use custom controllers to encode domain knowledge for specific applications into an
    extension of the Kubernetes API.
    引用元:https://kubernetes.io/docs/concepts/extend-kubernetes/api-extension/custom-resources/#custom-controllers

    View Slide

  6. Kubernetes Sapporo for Beginners
    Operator Pattern
    リソースに対する運用の知識を、 Kubernetes APIとしてソフトウェア化する方法です。
    A Site Reliability Engineer (SRE) is a person that operates an application by writing software. They are an engineer, a developer, who knows
    how to develop software specifically for a particular application domain. The resulting piece of software has an application's operational
    domain knowledge programmed into it.
    Our team has been busy in the Kubernetes community designing and implementing this concept to reliably create, configure, and manage
    complex application instances atop Kubernetes.
    We call this new class of software Operators. An Operator is an application-specific controller that extends the Kubernetes API to create,
    configure, and manage instances of complex stateful applications on behalf of a Kubernetes user. It builds upon the basic Kubernetes
    resource and controller concepts but includes domain or application-specific knowledge to automate common tasks..
    引用元:https://coreos.com/blog/introducing-operators.html

    View Slide

  7. Kubernetes Sapporo for Beginners
    How to use
    ホワイトリスト管理がめんどくさい。
    元々はterraformでインフラチームでIaC化してた。
    ● ホワイトリストは開発側で管理したい。依頼の手間がかかる。
    ● GKEのノードも変更があったら勝手にその IPアドレスを登録/削除してほしい。めんどくさい。
    ※諸事情によりPrivate Cluster + Cloud NAT化できない。
    ※とある環境は、コスト削減のため 18:00〜08:00は停止してる。IPが毎日変わる。
    ● この作業は楽しくない。さっさと終わらせたい。
    GCPにおけるホワイトリスト管理は、ネットワーク的な部分は、 Cloud Firewall Rulesで制御できる。
    L7/L4ロードバランサ(Backend Service)に対しては、Cloud Armorで制御ができる。

    View Slide

  8. Kubernetes Sapporo for Beginners
    How to use
    リソースに対する運用の知識を
    Kubernetes APIとしてソフトウェア化
    イベント発生 状況判断 期待値への変更

    View Slide

  9. Kubernetes Sapporo for Beginners
    How to use
    Security Policyをリソース化しちゃう。
    Security Policy
    Cloud Armor
    <>
    Security
    Policy
    Custom Controller
    Controller
    Custom Resource
    Security Policy
    Healing
    Scale Out/In
    Version Up
    パートナーさんの増減
    開発オフィスの追加
    ...
    ここに、運用知識を蓄積す
    る。

    View Slide

  10. Kubernetes Sapporo for Beginners
    How to use
    実際に動かしてみる

    View Slide

  11. Kubernetes Sapporo for Beginners
    How to develop
    Kubebuilderを使って開発。こんな感じになった。
    Security Policy
    Cloud Armor
    <>
    Security
    Policy
    Custom Controller
    Controller
    securitypolicy_types.go
    Security Policy
    securitypolicy_controller.go
    securitypolicy_nodecontroller.go
    gce_securitypolicy.go
    公式:https://book.kubebuilder.io/

    View Slide

  12. Kubernetes Sapporo for Beginners
    How to develop
    kube-apiserver
    etcd
    custom controller
    controller-runtime
    client-go
    kubectl GCP Security Policy API
    実際開発する際、Controller-RuntimeのGoDoc見る事がしばしば…。
    any request
    - create
    - delete
    - get
    any request
    - watch
    - get
    client-go
    kube-controller-manager
    - Node Controller node event

    View Slide

  13. Kubernetes Sapporo for Beginners
    How to develop
    必要となるCPU,Memory
    クラスタにほんのちょっとのリソース確保するだけで動く。
    人が対応するよりも、安い、早い、安心。
    image: hirokimatsumoto/security-policy-operator:0.2.1
    resources:
    limits:
    cpu: 100m
    memory: 30Mi
    requests:
    cpu: 100m # 100mも要らないかも。
    memory: 20Mi

    View Slide

  14. Kubernetes Sapporo for Beginners
    How to develop
    インストール
    ● 通常はhelm。めんどくさいので、全部入り yamlを作ってる。
    ( gitlab-runnerで特定のdocker imageで動かしてるけどhelm入りimage作ってない )
    gitlab-ci.yml
    stages:
    - dry-run
    - apply
    image: asia.gcr.io/${project-id}/gitlab-runner:latest
    kubernetes-dry-run:
    stage: dry-run
    tags:
    - shared-docker
    script:
    - "kubectl diff -f kubernetes/ || echo changed "
    only:
    - /^feature\/.*$/
    - master
    kubernetes-apply:
    stage: apply
    tags:
    - shared-docker
    script:
    - "kubectl apply -f kubernetes/"
    only:
    - master
    リポジトリの構成
    xxxxx / # project group
    infra # gitlab project (git repository)
    terraform/ # Terraform configuration
    kubernetes/ # Kubernetes configuration
    .gitlab-ci.yml # CI/CD configuration

    View Slide

  15. Kubernetes Sapporo for Beginners
    Where are custom controllers used
    GCP
    ● Config Connector
    ● Backend Config
    Other
    ● OperatorHub.io
    ○ AWS Service Operator
    ○ Elasticsearch Cloud on Kubernetes

    View Slide

  16. Kubernetes Sapporo for Beginners
    その他知っておきたい事
    Service Catalog
    Open Service Broker APIの仕様に準ずるService Brokerを利用して、クラウドのマネージドサービスを管理する仕組
    み。
    引用元:https://kubernetes.io/docs/concepts/extend-kubernetes/service-catalog/

    View Slide

  17. Kubernetes Sapporo for Beginners
    その他知っておきたい事
    ● Operator SDK
    Red Hatさんの記事(日本語)が入りやすい気がします。
    ● Kubebuilder
    ● Kubernetes 1.16 Change Log
    #79604 CRDが、v1beta1からv1になります。定義が色々と変わります。

    View Slide

  18. Kubernetes Sapporo for Beginners
    Thank you !!

    View Slide