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

Operator 101

capsmalt
December 20, 2019

Operator 101

Operator 101 〜Operatorに手足をつっこんでみようか。レッツエンジョイ〜
@capsmalt (Kazufumi Saito), Red Hat K.K.

2019/12/20
OpenShift.Run

11/28CNDKの資料を少しアップデートした内容です。あくまで101ではありますが,誰かしらのお役に立てると嬉しいです。

capsmalt

December 20, 2019
Tweet

More Decks by capsmalt

Other Decks in Technology

Transcript

  1. l eo T i T @ N T aI F

    I 1 4 3 2 d N # NF T C I I T M F F 66 777 60 4. 6 2 6 46 / 1 7 60/ 66 777 60 4. 6 2 6 46 / 1 7 60/ 66 777 60 4. 6 2 6 46 / 1 7 60/ 66 777 60 4. 6 2 6 46 / 1 7 60/ 6
  2. Copyright 2019 Red Hat K.K. 8 K 8= C +

    ) + (+ +  /    18
  3. Today’s Agenda 1 2 3 Kubernetes Core Why / What

    Operator How to get Operator 21
  4. Copyright 2019 Red Hat K.K. ( ) ) ) )

    ) ) ) ) K ( ! %$       &# ("'   23
  5. Copyright 2019 Red Hat K.K. 3 N ( ( :

    N ( 2 3 2 . ) ( ) ) ) 3 c e` c K ad b : i 3 27
  6. Copyright 2019 Red Hat K.K.     

           29
  7. Copyright 2019 Red Hat K.K. )( ( ) ) )

    (          30
  8. Copyright 2019 Red Hat K.K. o .- . - •

    • . - • .- • • . • 32
  9. Copyright 2019 Red Hat K.K. Cloud Infrastructure Resources Hardware, Virtualization,

    Cloud and Service Provider Certifications CI/CD Workload Serverless Workload Machine Learning Workload IoT Workload Workload Abstraction Any Applications Resource Abstraction Any Resources Company Core Value? (*Not Kubernetes) 8 C 8 => K ) ( ) 35
  10. Copyright 2019 Red Hat K.K. 2 7 00 - ,2

    , 0 : , 7, S 7 IE LC A S EC LCH S LMI S LCLP M CLP KK S II 1H CLH C A E LK S LH A KCKL S ,A KK A KK L A LCH S -HKL H L C A K S MCE IEHP 1 LBH HEHAP S E L H 1H CLH C A E LK S 1 L C A B A D S E L H M CLP - C A S A - C A S M CLP LC C LCH K S 2 LOH D HEC P S CK KL HN P S KHM A L LCH S IA L B S E L H IA L B S A IA L B S II IA L B S M CLP L B K S H LC MHMK M CLP C A S 1MELC NC H L HEEHML S , L I CK H L C ACKL P S EMKL II ,E KLC CLP S 1H CLH E L C L S 0HA AA A LCH c R RV g eb kl i faVh T dUYT HM 7B 2 O L D 7B L L H LB /M L K , HKPKL MAMKL & % 42
  11. Copyright 2019 Red Hat K.K.    / b

    gpim : a K a lc nhe od b k     - . - // / / / - . / - - 43
  12. Copyright 2019 Red Hat K.K. Cloud Infrastructure Resources Hardware, Virtualization,

    Cloud and Service Provider Certifications CI/CD Workload Serverless Workload Machine Learning Workload IoT Workload Workload Abstraction Any Applications Resource Abstraction Any Resources Company Core Value? (*Not Kubernetes) C V 8 => K ) ( ) 8 K C 8 44
  13. Copyright 2019 Red Hat K.K. 8 K 8= C +

    ) + (+ +  /    48
  14. Copyright 2019 Red Hat K.K.     o

    ) ) o o o J P ( o         or  49
  15. Copyright 2019 Red Hat K.K. . 4 1 o o

    o O o o o . o S o o o 50
  16. Copyright 2019 Red Hat K.K. ) • N ) (

    P • N ) ( P • R ) ( O • ) ) ) O ) ) / 52
  17. Copyright 2019 Red Hat K.K. • ()P • ()P •

    P () • C ( () RNO ( ) ) / 53
  18. Copyright 2019 Red Hat K.K. . . / - /

    - - . --. / -. / -/ 56
  19. Copyright 2019 Red Hat K.K. G s : / -

    - - . : / .B/ G HP G Sb km .B/ HPon dg l pr KO - a iu . Ct efD hc . / . . A . 59
  20. / - / - . ./ . . / .

    . . ./ . 61
  21. Copyright 2019 Red Hat K.K. o P L o C

    L o ( R o ) / / L A o I O 62
  22. Copyright 2019 Red Hat K.K.   2.3 413 D

    C D 1 431 .3 C 2.3 413 1 5 C5 63
  23. Copyright 2019 Red Hat K.K. $ operator-sdk add api --api-

    version=cache.example.com/v1alpha1 --kind=Memcached (pkg/apis/cache/v1alpha1/memcached_types.g D A . C 2 type MemcachedSpec struct { // Size is the size of the memcached deployment Size int32 `json:"size"` } type MemcachedStatus struct { // Nodes are the names of the memcached pods // +listType=set Nodes []string `json:"nodes"` } 65
  24. Copyright 2019 Red Hat K.K. ) D . $ operator-sdk

    generate k8s $ operator-sdk generate openapi 2 ( 2AC 66
  25. Copyright 2019 Red Hat K.K. $ operator-sdk add controller --api-

    version=cache.example.com/v1alpha1 --kind=Memcached err := c.Watch( &source.Kind{Type: &cachev1alpha1.Memcached{}}, &handler.EnqueueRequestForObject{}, ) (pkg/controller/memcached/memcached_controlle A C . I 3 err := c.Watch( &source.Kind{Type: &appsv1.Deployment{}}, &handler.EnqueueRequestForOwner{ IsController: true, OwnerType: &cachev1alpha1.Memcached{}}, ) https://github.com/operator-framework/operato sdk/blob/master/example/memcached- operator/memcached_controller.go.tmpl 67
  26. Copyright 2019 Red Hat K.K. func (r *ReconcileMemcached) Reconcile(request reconcile.Request)

    (reconcile.Result, error) { // Lookup the Memcached instance for this reconcile request memcached := &cachev1alpha1.Memcached{} err := r.client.Get(context.TODO(),request.NamespacedName, memcached) ... } )) . ()( ( 3 68
  27. Copyright 2019 Red Hat K.K. $ operator-sdk build quay.io/capsmalt/memcached-operator:v0.0.1 )4

    - C D $ docker push quay.io/capsmalt/memcached-operator:v0.0.1 serviceAccountName: memcached-operator containers: - name: memcached-operator # Replace this with the built image name image: quay.io/capsmalt/memcached-operator:v0.0.1 command: - memcached-operator imagePullPolicy: Always (deploy/operator.yaml) $ kubectl create -f deploy/crds/cache.example.com_memcacheds_crd.yaml & ( .-. 4 - C D I A 69
  28. Copyright 2019 Red Hat K.K. $ kubectl create -f deploy/operator.yaml

    ( 4 ) BDA C $ kubectl create -f deploy/service_account.yaml $ kubectl create -f deploy/role.yaml $ kubectl create -f deploy/role_binding.yaml & C ( 4 ) . 4 C $ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE memcached-operator 1 1 1 1 1m $ kubectl get pod NAME READY STATUS RESTARTS AGE memcached-operator-7d76948766-nrcp7 1/1 Running 0 1m 70
  29. Copyright 2019 Red Hat K.K. apiVersion: "cache.example.com/v1alpha1" kind: "Memcached" metadata:

    name: "example-memcached" spec: size: 3 (deploy/crds/cache.example.com_v1alpha1_Memcached_cr.yaml) $ kubectl apply -f deploy/crds/cache.example.com_v1alpha1_memcached_cr.yaml 71
  30. Copyright 2019 Red Hat K.K. D .)() 5 1 .

    C $ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE memcached-operator 1 1 1 1 2m example-memcached 3 3 3 3 1m $ kubectl get pods NAME READY STATUS RESTARTS AGE example-memcached-5cc7c78d6-g6h6d 1/1 Running 0 2m example-memcached-5cc7c78d6-g5k7v 1/1 Running 0 2m example-memcached-5cc7c78d6-m7vn7 1/1 Running 0 2m memcached-operator-7gd6csgfd6-vvjqk 1/1 Running 0 3m 72
  31. Copyright 2019 Red Hat K.K. ( 53- 2 . (

    ) apiVersion: "cache.example.com/v1alpha1" kind: "Memcached" metadata: name: "example-memcached" spec: size: 5 $ kubectl apply -f deploy/crds/cache.example.com_v1alpha1_memcached_cr.yaml $ kubectl get deployment NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE example-memcached 5 5 5 5 10m 73
  32. Copyright 2019 Red Hat K.K. $ operator-sdk new memcached-operator \

    --api-version=cache.example.com/v1alpha1 \ --kind=Memcached --type=ansible Creates: • Ansible Role • Mapping File (watches.yaml) • Custom Resource Definition • Deploy manifest for the new Operator # watches.yaml --- - version: v1alpha1 group: cache.example.com kind: Memcached playbook: /path/to/playbook 74
  33. Copyright 2019 Red Hat K.K. / .- ./ . -

    . ./ . -/ - . - - ./ . 75
  34. Copyright 2019 Red Hat K.K. . . , , -

    . . . - h yb , O fc HGu d sp t glrn 4 . . K A m w e AW RM i S a W AW o 81
  35. Copyright 2019 Red Hat K.K.     

      / / / / / / 82
  36. Copyright 2019 Red Hat K.K. A L H I ,

    ( , (( , ((( , (/ , / ( A BA A A B A A , A B B A B ) A B B ( A A A D BA , A F A A BA AB A A B AB 41 $+) /$.'/ 5092 "=3 & !)/ $ -=3 % !$ 41+ !+ - ;: < $+ )/*# , % '+* ./  %  41 , ,$ ( />86 7(+/ % 86
  37. Copyright 2019 Red Hat K.K. ( ) + = “create

    new operator”   Kubernetes Operator Operator SDK DEV Deployment Role ClusterRole RoleBinding ClusterRoleBinding ServiceAccount CustomResourceDefinition 89
  38. Copyright 2019 Red Hat K.K. ( ) + = “create

    new operator”         + Kubernetes Operator Operator SDK DEV 90
  39. Copyright 2019 Red Hat K.K.     

                   ( )           94
  40. Copyright 2019 Red Hat K.K. < - - + .

    + . - - - $ :- - : - - .+ . . . + R A NEI M ID E += ./ = A N - - - - - $ - - - - + +/. > - - 95
  41. Copyright 2019 Red Hat K.K. A . / . =

    = . / = / A/ = = U RS DI E M N = D I A = / / = + 96
  42. Copyright 2019 Red Hat K.K. ) ( -( C E

    C : A ) A DDD C E I S C E C : A ) Q N TM - ) ( -( c ye / A hl / A ye e g P : Aa / A ye Q / A ye Q s c / A ye Qu i V O / A w Poe / A ye t l ud g . p / A Q i r e ng g , C . : C . : s m . J Q LO M s m C E :A CA . C ( :A / A 97
  43. Copyright 2019 Red Hat K.K. 2 3 13 2 3

    13 2 3 13 2 3 13 2 3 13 3.2 .10         2 3 13 . 0 3 98
  44. Copyright 2019 Red Hat K.K. Automated Dependency Resolution My Operator

    v1.1.2 require s requires Jaeger Operator jaeger.jaegertracing.io /v1 CockroachDB Operator cockroachdb.charts.helm.k8s.io/v1alpha 1 resolves to resolves to Operator Framework  installed by installed by         99
  45. Copyright 2019 Red Hat K.K. 8 K 8= C +

    ) + (+ +  /    101