Operator reading and writing with Operator SDK

4cb5b950cfd0eabf5d6b828c951d4549?s=47 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

4cb5b950cfd0eabf5d6b828c951d4549?s=128

loftkun

January 17, 2020
Tweet

Transcript

  1. Operator reading and writing Operator SDK & 2020/1/17 # 

    node-3 @loftkun
  2. About Me @loftkun ( _F G7 ) èáòP2 j ÑòàÔãêÙÜáËòæNŒ

    20191b)D : 10 ¹¡¸µ¯§d #¹¡¸µ¯§ node-1 #¹¡¸µ¯§ node-2 #¹¡¸µ¯§ virtual-kubelet-1 #¹¡¸µ¯§ node-3
  3. Motivation • #–Prometheus Operator¶^HWÇ"½•ÇLT¥»¥ª • #·Operator¶-w“´gf¥¯¥¥»§

  4. Environment 28 0 36 . 28 3 4 6 836

    : / (80 : 1.2./ : 62 8 7 : 67.32 :
  5. Agenda Operator is * Reading Writing

  6. Operator is *

  7. 

  8. hƒ https://kubernetes.io/docs/concepts/overview/components/  !  #

  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
  10. hƒ 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
  11. Operator is *

  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
  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 ¶
  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
  15. Reading

  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)
  17. operator-framework/operator-sdk-samples • Operator SDK ° :¤ÅªOperator¶Ñðãì (Memcached Go Operator) CRD

    Reconcile https://github.com/operator-framework/operator-sdk-samples
  18. Writing

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

  20. operator-framework/getting-started 1. $' " 2. CRD ) 3.  )

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

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

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

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

  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¥¯šÄ—
  26. 2. CRDLJ *_types.goÇq’¥ª(·–ëÖòÔ¶ÐòÝÇ]:§Ä https://github.com/operator-framework/getting-started

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

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

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

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

  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‰§Ä—
  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‰§Ä—
  33. 3. ControllerLJ ‡¤ÅªÖòÔ ( pkg/controller/memcached/mem cached_controller.go ) https://github.com/operator-framework/getting-started ( p¡

    ) Reconcile\¶scaffold CR¶ÌâÓÊÏÜÇ3
  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œÄ
  35. 3. ControllerLJ https://github.com/operator-framework/getting-started 1 DeploymentÇ : m Ÿ ÍÔ×æëÖòÔ¶X8'°– ¦!–

    ¦ßòæÔåòÔ´ :¥¯šÄ Image·memcached-alpine
  36. 3. ControllerLJ https://github.com/operator-framework/getting-started 1 DeploymentÇ : r.Client·sigs.k8s.io/controller-runtime

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

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

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

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

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

  42. 4 build &  i€ ÛãîÉ https://github.com/operator-framework/getting-started

  43. 4 build &  i€ ÛãîÉ https://github.com/operator-framework/getting-started

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

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

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

  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¥¯šÄ

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

  49. »±¾ • Operator = Custom Resource + Custom Controller –

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

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