Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

Agenda Operator is * Reading Writing

Slide 6

Slide 6 text

Operator is *

Slide 7

Slide 7 text

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

Operator is *

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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 ¶

Slide 14

Slide 14 text

-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

Slide 15

Slide 15 text

Reading

Slide 16

Slide 16 text

½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)

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Writing

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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‰§Ä—

Slide 32

Slide 32 text

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‰§Ä—

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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œÄ

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

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

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

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

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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