$30 off During Our Annual Pro Sale. View Details »

Operator reading and writing with Operator SDK

loftkun
January 17, 2020

Operator reading and writing with Operator SDK

2020/01/17 #ふくばねてす node-3 - connpass
https://fukubernetes.connpass.com/event/159429/

image quality is low because of upload size limits , so I recommend to look at slideshare below.

slideshare : Operator reading and writing ( Operator SDK 編 )
https://www.slideshare.net/ssuserb989ce/operator-reading-and-writing-operator-sdk

loftkun

January 17, 2020
Tweet

More Decks by loftkun

Other Decks in Technology

Transcript

  1. Operator reading and writing
    Operator SDK &
    2020/1/17 # node-3
    @loftkun

    View Slide

  2. About Me
    @loftkun ( _F G7 )
    èáòP2 j ÑòàÔãêÙÜáËòæNŒ
    20191b)D : 10
    ¹¡¸µ¯§d
    #¹¡¸µ¯§ node-1
    #¹¡¸µ¯§ node-2
    #¹¡¸µ¯§ virtual-kubelet-1
    #¹¡¸µ¯§ node-3

    View Slide

  3. Motivation
    • #–Prometheus Operator¶^HWÇ"½•ÇLT¥»¥ª
    • #·Operator¶-w“´gf¥¯¥¥»§

    View Slide

  4. Environment
    28 0 36 .
    28
    3
    4 6 836 :
    / (80 :
    1.2./ : 62 8 7 : 67.32 :

    View Slide

  5. Agenda
    Operator is *
    Reading
    Writing

    View Slide

  6. Operator is *

    View Slide


  7. View Slide


  8. https://kubernetes.io/docs/concepts/overview/components/

    !

    #

    View Slide

  9. Operator is
    õ
    built-in controller ¶
    43 73 6( 3286300 6 4/, (3286300 6
    / 62 8 7 / 62 8 7 40.( 8 6 40.( 7 8 6 40.( 7 8 ,3
    132 8 ) 132 ) 132 (3286300 6 ,3
    403 1 28 ) 403 1 28 ) 403 1 28 (3286300 6 ,3
    43) 837( 0 6 -36. 328 0 ,3

    View Slide


  10. 1. e{ ( watch )
    resource¶ÉäðÜ(Add/Update/Delete)Çwatch
    2. ‚E ( reconcile )
    1. ÉäðÜU¶ reconcile.Requests ŸÜëÎò
    2. ÌâÓÊÏܶSpec(™Äº Y9)±ÒÔÚæ¶Y9Ÿ
    u§Äži€
    3. u¥³š(–5x´6¦ª‚EÇv›
    5x³ÌâÓÊÏÜÇAdd/Update/Delete§Ä—
    https://kubernetes.io/docs/concepts/overview/components/
    .}c~,´&®šªt04SsŸ-w¤Å¯šÄ
    e{(watch)±‚E(reconcile)Çv›¶ŸController

    View Slide

  11. Operator is *

    View Slide

  12. Operator is
    õ
    Operator = Custom Resource + Custom Controller
    https://kubernetes.io/docs/concepts/extend-kubernetes/operator/
    • CR ( Custom Resource )
    – Deployment¶Á›³ built-in³resource°·³¡–Zt´,r¥ª resource
    – CRDó Custom Resource Definition ô °,r° Ä
    • Custom Controller
    – CRÇe{(watch)–‚E(reconcile)¥“ÇyĪ¾´–Zt´-w¥ªcontroller
    – &Nc³Ã·Deployment controller¶Á›³ built-in³controller± R
    • watch À reconcile Çv›
    ¶=Q´ÁĊ^(Operation)Çt§Ä¶ŸOperator

    View Slide

  13. Operator is
    õ
    built-in controller ¶ ( @ )
    43 73 6( 3286300 6 4/, (3286300 6
    / 62 8 7 / 62 8 7 40.( 8 6 40.( 7 8 6 40.( 7 8 ,3
    132 8 ) 132 ) 132 (3286300 6 ,3
    403 1 28 ) 403 1 28 ) 403 1 28 (3286300 6 ,3
    43) 837( 0 6 -36. 328 0 ,3
    43 7831 73 6( 3286300 6
    34 6 836 6 1 36/ 34 6 836 7)/ 7 140 7
    1( (- ) 4 6 836
    1( (- ) (3286300 6 1 1( (- ) 1 1( (- ) (3286
    300 6 ,3
    (36 37 4631 8- 7 34 6 836 631 8- 7
    0 681 2 , 6 8(
    4631 8- 7 34 6 836 ,3
    36 (0 1 750 34 6 836 0 78 6
    (/ 4
    7836 8(
    4/, (3286300 67
    (0 78 6 (328630 ,3
    (/ 4 34 6 836 (3286300 6 ,3
    6 7836 34 6 836 (3286300 6 ,3
    Operator ¶

    View Slide

  14. -w=T
    • êÉâêëǛ
    – client-go , code-generator ³²
    • built –in ³ÐðÜîòêò·£Å¶êÉâêë°-w¤Å¯šÄ
    • ´·coreos/prometheus-operator³²¿£¶=T°-w¤Å¯šÄÁ›«
    • áíòæïòÏǛ
    – kubernetes-sigs/kubebuilder
    – operator-framework/operator-sdk
    • OLM ( Operator LifeCycle Manager )
    • ©¶
    – KUDO
    – Metacontroller

    View Slide

  15. Reading

    View Slide

  16. ½k<
    Operator = Custom Resource + Custom Controller ³¶°–
    • CRD ( Custom Resource Definition ô Ç ½
    – spec
    • reconcile loop°X¤ÅÄ8'
    – spec±[$¶ÌâÓÊÏܶY9ÇV†¥¯reconcile§Ä
    – status
    • reconcile loop°KG¤ÅÄ8'
    – [$¶ÌâÓÊÏܶY9 ( kubeclt describe °i€° Ä )
    • Reconcile ¶\ǁ½
    – Spec±[$¶ÒÔÚæ¶Y9¶V†
    – 5x´6¦¯ÌâÓÊÏܶA(Add/Update/Delete)

    View Slide

  17. operator-framework/operator-sdk-samples
    • Operator SDK °:¤ÅªOperator¶Ñðãì (Memcached Go Operator)
    CRD Reconcile
    https://github.com/operator-framework/operator-sdk-samples

    View Slide

  18. Writing

    View Slide

  19. Install Operator SDK
    https://github.com/operator-framework/operator-sdk/blob/master/doc/user/install-operator-sdk.md

    View Slide

  20. operator-framework/getting-started
    1. $'"
    2. CRD
    )
    3.
    )
    4. & %(
    https://github.com/operator-framework/getting-started

    View Slide

  21. 1. GzãîÓÊÏÜ:
    GzãîÓÊÏÜÇ]:§Ä
    https://github.com/operator-framework/getting-started
    Operator¶ÖòÔÐòݶscaffold(…()Ÿ° Ä

    View Slide

  22. 1. GzãîÓÊÏÜ:
    *C
    https://github.com/operator-framework/getting-started

    View Slide

  23. 1. GzãîÓÊÏÜ:
    GOROOTÇ~,§Ä±›»¡š¬ª
    https://github.com/operator-framework/getting-started

    View Slide

  24. 2. CRDLJ
    CRD¶scaffoldLJ§Ä
    https://github.com/operator-framework/getting-started

    View Slide

  25. 2. CRDLJ
    CRD¶scaffoldÇq’§Ä
    https://github.com/operator-framework/getting-started
    specÇq’
    ñ;˜·kubectl create°CR]:J´Ç?,
    ° Ä
    ñOperatorŸreconcile ¶‘´X§Ä
    NØéòÜëÈì°·int%¶size(íãëÍD)Ç,r¥¯š
    ė
    statusÇq’
    ñOperatorŸ[$¶Y9Ç~,§Ä
    ñ;˜·kubectl describe°i€° Ä
    NØéòÜëÈì°·string¶±¥¯pod!¶ëÔÜÇ
    ,r¥¯šÄ—

    View Slide

  26. 2. CRDLJ
    *_types.goÇq’¥ª(·–ëÖòÔ¶ÐòÝÇ]:§Ä
    https://github.com/operator-framework/getting-started

    View Slide

  27. 2. CRDLJ
    OpenAPI validations Ç]:§Ä
    https://github.com/operator-framework/getting-started

    View Slide

  28. 2. CRDLJ
    ]:¤ÅªCRD¶yaml
    https://github.com/operator-framework/getting-started
    Memcached±š›kind·built-in³
    resouce´·³š
    £¶CRDÇÏêÔ×´‹^§Ä±–
    kind:MemcachedŸœÄÁ›´³Ä

    View Slide

  29. 2. CRDLJ
    ]:¤ÅªCRD¶yaml ( ­® >l )
    https://github.com/operator-framework/getting-started
    spec¶validation
    status¶validation

    View Slide

  30. 3. ControllerLJ
    Controller¶scaffoldLJ§Ä
    https://github.com/operator-framework/getting-started

    View Slide

  31. 3. ControllerLJ
    ‡¤ÅªÖòÔ
    ( pkg/controller/memcached/mem
    cached_controller.go )
    https://github.com/operator-framework/getting-started
    CRÇwatch
    PodÇwatch
    £¶Á›´scaffold±¥¯·PodÇwatch
    §ÄÐòݟ]:¤Åė
    DeploymentÇwatch§ÄÁ›B‰§Ä—

    View Slide

  32. 3. ControllerLJ
    https://github.com/operator-framework/getting-started
    ‡¤ÅªÖòÔ
    ( pkg/controller/memcached/mem
    cached_controller.go )
    CRÇwatch
    PodÇwatch
    £¶Á›´scaffold±¥¯·PodÇwatch
    §ÄÐòݟ]:¤Åė
    DeploymentÇwatch§ÄÁ›B‰§Ä—

    View Slide

  33. 3. ControllerLJ
    ‡¤ÅªÖòÔ
    ( pkg/controller/memcached/mem
    cached_controller.go )
    https://github.com/operator-framework/getting-started
    ( p¡ )
    Reconcile\¶scaffold
    CR¶ÌâÓÊÏÜÇ3

    View Slide

  34. 3. ControllerLJ
    ‡¤ÅªÖòÔ
    ( pkg/controller/memcached/mem
    cached_controller.go )
    https://github.com/operator-framework/getting-started
    Reconcile\¶scaffold
    PodÇ]:§Ä
    £¶Á›´scaffold·–PodÇ]:
    §ÄÐòÝ°™Ä—(image·
    busybox)
    DeploymentÇ]:§ÄÁ›ÐòÝ
    ´/¥MœÄ

    View Slide

  35. 3. ControllerLJ
    https://github.com/operator-framework/getting-started
    1 DeploymentÇ:
    m Ÿ ÍÔ×æëÖòÔ¶X8'°–
    ¦!– ¦ßòæÔåòÔ´
    :¥¯šÄ
    Image·memcached-alpine

    View Slide

  36. 3. ControllerLJ
    https://github.com/operator-framework/getting-started
    1 DeploymentÇ:
    r.Client·sigs.k8s.io/controller-runtime

    View Slide

  37. 3. ControllerLJ
    https://github.com/operator-framework/getting-started
    2 DeploymentÇKG
    Ÿu§Äži€¥–u¥³š(·KG§Ä
    – CR¶spec°,r¥ªsize
    – Deployment¶size(replicaD)

    View Slide

  38. 3. ControllerLJ
    https://github.com/operator-framework/getting-started
    3 CR¶status(describe°yÅÄÀ­)ÇKG(pod!ëÔÜÇL¡)

    View Slide

  39. 3. ControllerLJ
    https://github.com/operator-framework/getting-started
    3 CR¶status(describe°yÅÄÀ­)ÇKG(pod!ëÔÜÇL¡)
    Status¶Nodes´
    node!¶ëÔÜÇ
    L¢¯šÄ

    View Slide

  40. 4 build & i€
    CRDÇAPI Server´bŽ§Ä
    https://github.com/operator-framework/getting-started
    ( ` )

    View Slide

  41. 4 build & i€
    ÐðÚÞÉçòÓ¶build & push
    https://github.com/operator-framework/getting-started

    View Slide

  42. 4 build & i€
    󋔃
    https://github.com/operator-framework/getting-started

    View Slide

  43. 4 build & i€
    󋔃
    https://github.com/operator-framework/getting-started

    View Slide

  44. 4 build & i€
    ÍÔ×æëÖòÔÇ]:§Ä
    https://github.com/operator-framework/getting-started

    View Slide

  45. 4 build & i€
    iž´podŸ3­:¤Åª
    https://github.com/operator-framework/getting-started
    Operator¶custom controllerŸwatch¥–reconcile¥ªoO
    Deployment¶repliaDÇ3´KG¥¯šÄ

    View Slide

  46. 4 build & i€
    ÍÔ×æëÖòÔ¶sizeÇ4´¥¯¼Ä
    https://github.com/operator-framework/getting-started

    View Slide

  47. 4 build & i€
    iž´podŸ4­´³¬ª
    https://github.com/operator-framework/getting-started
    Operator¶custom controllerŸwatch¥–reconcile¥ªoO
    Deployment¶repliaDÇ3žÂ4´KG¥¯šÄ

    View Slide

  48. Tips
    OperatorÇîòÍ찄° ėa&ÚÔÜÇmI¡#¨Ä
    https://github.com/operator-framework/getting-started
    £¶(–Operator·ÐðÚÞ°·³¡–go¶ãîÕÔ±¥¯îòÍ찄§Ä
    K8s±¶ˆ´·default° $HOME/.kube/config ŸÆÅÄ

    View Slide

  49. »±¾
    • Operator = Custom Resource + Custom Controller
    – ¶=Q´ÁĊ^(Operation)Çt
    • Reading
    – CRD ( Custom Resource Definition ) ǁ½
    –reconcile\ǁ½
    • Writing
    – Operator SDK ´ÁÄ-w=”Çn¥»¥ª

    View Slide

  50. »±¾
    • Operator = Custom Resource + Custom Controller
    – ¶=Q´ÁĊ^(Operation)Çt
    • Reading
    – CRD ( Custom Resource Definition ) ǁ½
    –reconcile\ǁ½
    • Writing
    – Operator SDK ´ÁÄ-w=”Çn¥»¥ª
    OperatorÇ+´\|¥»¥ª

    View Slide