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. 1 0 @


    1

    View Slide






  2. 2

    View Slide

  3. Copyright 2019 Red Hat K.K.
    .
    ( ) .
    !
    3

    View Slide

  4. Copyright 2019 Red Hat K.K.
    !
    4

    View Slide

  5. Copyright 2019 Red Hat K.K.


    !
    5

    View Slide

  6. 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

    View Slide

  7. Copyright 2019 Red Hat K.K.


    7

    View Slide

  8. Copyright 2019 Red Hat K.K.


    8

    View Slide

  9. Copyright 2019 Red Hat K.K.
    )
    ( )
    https://qiita.com/capsmalt/items/496942c823f5cf0f307d
    9

    View Slide




  10. 10

    View Slide



  11. 11

    View Slide


  12. 12

    View Slide

  13. 8 /
    /
    )
    (/ . /
    @
    K L
    I
    /
    13

    View Slide

  14. Cloud Native Meetup Tokyo Cloud Native Developer JP
    )
    () )
    14

    View Slide

  15. https://openshift.connpass.com/
    OpenShift UserGroup
    15

    View Slide

  16. 1 0 @


    16

    View Slide

  17. Copyright 2019 Red Hat K.K.

    17

    View Slide

  18. Copyright 2019 Red Hat K.K.
    8 K
    8=
    C
    + )
    +
    (+ +

    /
    18

    View Slide

  19. Copyright 2019 Red Hat K.K. 19

    View Slide

  20. Copyright 2019 Red Hat K.K.
    ?
    20

    View Slide

  21. Today’s Agenda
    1
    2
    3
    Kubernetes Core
    Why / What Operator
    How to get Operator
    21

    View Slide

  22. Copyright 2019 Red Hat K.K. 22

    View Slide

  23. Copyright 2019 Red Hat K.K.
    (
    )
    )
    )
    )
    )
    )
    )
    ) K ( !%$


    ("'
    23

    View Slide

  24. Copyright 2019 Red Hat K.K.
    N M N P3 N 1 P
    2
    a a
    24

    View Slide

  25. Copyright 2019 Red Hat K.K.

    25

    View Slide

  26. Copyright 2019 Red Hat K.K.
    . 3 :
    ) ) )
    ( ( :
    N
    3
    26

    View Slide

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

    View Slide

  28. Copyright 2019 Red Hat K.K.



    28

    View Slide

  29. Copyright 2019 Red Hat K.K.





    29

    View Slide

  30. Copyright 2019 Red Hat K.K.
    )( (
    ) )
    )
    (






    30

    View Slide

  31. Copyright 2019 Red Hat K.K.
    ( ( ) (
    -
    ( (
    -
    ) ( ) (
    31

    View Slide

  32. Copyright 2019 Red Hat K.K.
    o .- . -

    • . -
    • .-

    • .

    32

    View Slide

  33. Copyright 2019 Red Hat K.K.
    " - . - -
    - -
    33

    View Slide

  34. Copyright 2019 Red Hat K.K.



    34

    View Slide

  35. 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

    View Slide

  36. Copyright 2019 Red Hat K.K. 36

    View Slide

  37. Copyright 2019 Red Hat K.K. 37

    View Slide

  38. Copyright 2019 Red Hat K.K.
    https://www.cncf.io/projects/
    38

    View Slide

  39. Copyright 2019 Red Hat K.K.
    + +
    39

    View Slide

  40. Copyright 2019 Red Hat K.K.
    M DCAN I G FB
    40

    View Slide

  41. Copyright 2019 Red Hat K.K.
    !
    41

    View Slide

  42. 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

    View Slide

  43. Copyright 2019 Red Hat K.K.


    / b gpim : a
    K a lc nhe
    od b k


    - . - // / / / - . / - -
    43

    View Slide

  44. 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

    View Slide

  45. Copyright 2019 Red Hat K.K.

    45

    View Slide

  46. Copyright 2019 Red Hat K.K.

    46

    View Slide

  47. Copyright 2019 Red Hat K.K.

    47

    View Slide

  48. Copyright 2019 Red Hat K.K.
    8 K
    8=
    C
    + )
    +
    (+ +

    /
    48

    View Slide

  49. Copyright 2019 Red Hat K.K.


    o ) )
    o
    o
    o J P (
    o








    or
    49

    View Slide

  50. Copyright 2019 Red Hat K.K.
    . 4 1
    o
    o
    o O
    o
    o
    o .
    o S
    o
    o
    o
    50

    View Slide

  51. Copyright 2019 Red Hat K.K.




    51

    View Slide

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

    View Slide

  53. Copyright 2019 Red Hat K.K.
    ● ()P
    ● ()P
    ● P ()
    ● C (
    ()
    RNO
    (
    ) ) /
    53

    View Slide

  54. Copyright 2019 Red Hat K.K. 54

    View Slide



  55. 55

    View Slide

  56. Copyright 2019 Red Hat K.K.
    . . / - / - - . --. / -. / -/
    56

    View Slide

  57. Copyright 2019 Red Hat K.K.
    /66 5 . 7 4. 6/ 1 6-15
    . 7 4.
    57

    View Slide

  58. Copyright 2019 Red Hat K.K.
    :

    . - / 1-81 - . 1 1.. /
    58

    View Slide

  59. 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

    View Slide

  60. - . - /
    .

    /
    60

    View Slide

  61. / - / - . ./ . .
    / . . . ./ .
    61

    View Slide

  62. Copyright 2019 Red Hat K.K.
    o P L
    o C L
    o ( R
    o ) / / L A
    o I O
    62

    View Slide

  63. Copyright 2019 Red Hat K.K.


    2.3 413
    D C
    D 1 431 .3 C
    2.3 413 1 5
    C5
    63

    View Slide

  64. Copyright 2019 Red Hat K.K.
    $ operator-sdk new memcached-operator
    ( ) ( . 1
    64

    View Slide

  65. 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

    View Slide

  66. Copyright 2019 Red Hat K.K.
    ) D .
    $ operator-sdk generate k8s
    $ operator-sdk generate openapi
    2
    ( 2AC
    66

    View Slide

  67. 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

    View Slide

  68. 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

    View Slide

  69. 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

    View Slide

  70. 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

    View Slide

  71. 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

    View Slide

  72. 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

    View Slide

  73. 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

    View Slide

  74. 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

    View Slide

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

    View Slide

  76. 1
    - ) (
    -- - . / .
    76

    View Slide

  77. /- / / . /. . -


    77

    View Slide

  78. - ) (
    .
    78

    View Slide

  79. Copyright 2019 Red Hat K.K.
    / . ./ - . - ./ . /
    79

    View Slide

  80. )
    (.
    80

    View Slide

  81. 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

    View Slide

  82. Copyright 2019 Red Hat K.K.


    /
    /
    /
    / /
    /
    82

    View Slide

  83. Copyright 2019 Red Hat K.K. 83

    View Slide

  84. Copyright 2019 Red Hat K.K.
    . ./
    84

    View Slide

  85. Copyright 2019 Red Hat K.K. 85

    View Slide

  86. 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

    View Slide

  87. Copyright 2019 Red Hat K.K. 87

    View Slide

  88. Copyright 2019 Red Hat K.K.
    +
    88

    View Slide

  89. Copyright 2019 Red Hat K.K.
    ( )
    + =
    “create new
    operator”





    Kubernetes
    Operator
    Operator
    SDK
    DEV
    Deployment
    Role
    ClusterRole
    RoleBinding
    ClusterRoleBinding
    ServiceAccount
    CustomResourceDefinition
    89

    View Slide

  90. Copyright 2019 Red Hat K.K.
    ( )
    + =
    “create new
    operator”







    +
    Kubernetes
    Operator
    Operator
    SDK
    DEV
    90

    View Slide

  91. Copyright 2019 Red Hat K.K.
    ) (
    ( )
    ( (
    91

    View Slide

  92. - . - /
    .

    /
    92

    View Slide

  93. Copyright 2019 Red Hat K.K.
    .
    E
    =
    D
    / +
    +
    D
    93

    View Slide

  94. Copyright 2019 Red Hat K.K.







    ( )






    94

    View Slide

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

    View Slide

  96. Copyright 2019 Red Hat K.K.
    A . / . =
    =
    . / =
    /
    A/ =
    =
    U
    RS
    DI
    E
    M
    N
    =
    D
    I
    A = /
    / = +
    96

    View Slide

  97. 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

    View Slide

  98. 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

    View Slide

  99. 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

    View Slide

  100. Copyright 2019 Red Hat K.K.

    100

    View Slide

  101. Copyright 2019 Red Hat K.K.
    8 K
    8=
    C
    + )
    +
    (+ +

    /
    101

    View Slide

  102. Copyright 2019 Red Hat K.K.
    ) (
    )
    )
    ) ).
    102

    View Slide

  103. Copyright 2019 Red Hat K.K. 103

    View Slide

  104. Copyright 2019 Red Hat K.K.

    104

    View Slide






  105. 105

    View Slide


  106. 106

    View Slide