Writing a Kubernetes operator from scratch

image







In this article, we'll take a closer look at how to create your own Kubernetes operator from scratch. Operators are plug-ins that use custom resources (kind) to manage applications. Read the official documentation for details .







โ€” HelloApp. HelloApp, Kubernetes.







image







kind: HelloApp โ€” (Custom Resource Definition, CRD), ( ). ( ).









, , Go. .







() , Kubernetes API.







Kubernetes:







image







Kubernetes โ€” . API ( API). , , API Kubernetes . . . Go โ€” Kubernetes, , .









:







  1. Go lang (1.16)
  2. Operator SDK (1.5)
  3. Kind
  4. Visual Studio Code Go




:







1:

2:

3: CRD

4: CRD

5:

6:

7:







1:



, Operator SDK. Operator SDK 1.5. SDK, . .







$ mkdir demo-operator
$ cd demo-operator
$ operator-sdk init --domain anupam.com --repo 
github.com/anupamgogoi/demo-operator
      
      





API



$ operator-sdk create api --group apps --version v1 --kind HelloApp --resource --controller
      
      





.







image







:







  1. Makefile , , . make help, .
  2. . , .
  3. . .
  4. .
  5. , . .


2:



. CRD (), () kind HelloApp , spec.size.







image







. spec : image () size (). spec, [4] .







image







. . โ€” HelloApp , . , spec.size, โ€” . . API .







image







Reconciler, Reconcile. Reconcile , CRUD (create read update delete โ€” ยซ ยป, . ), (kind) (spec). , .













main.go



main.go Operator-SDK . , . main.go.







main.go:







image







ctrl.GetConfigOrDie() Kubernetes ~/.kube/config . ~/.kube/config :







image







IP , . . . GetConfigOrDie() , ctrl.NewManager() . API API Kubernetes. . main.go, .







kubectl CLI API API Kubernetes. :







$ kubectl get nodes --v=8
      
      





image







3: CRD



, HelloApp . CRD. demo-operator :







$ make manifests
      
      





CRD

~/demo-operator/config/crd/bases







image







~/demo-operator/config/samples .







image







4: CRD



, . kind, .







$ kind create cluster --name k8s
      
      





CRD :







$ make install
      
      





~/demo-operator/config/crd/bases







$ kubectl apply -f .
      
      





.







5:



. Kubernetes,







$ cd demo-operator
$ go run main.go
      
      





:







image







โ€” samples :







$ kubectl create ns test
$ kubectl apply -f apps_v1_helloapp.yaml -n test
      
      





, test:







$ kubectl get all -n test
      
      





image







:







$ kubectl get HelloApp -n test
      
      





image







โ€” !







6:



โ€” , .







, -, CRD , 4 ( CRD).







, k8s . Go Visual Studio Code . Run โ†’ Start Debugging VS Code, .







image







โ€” :







image







, ~/demo-operator/config/samples

CRD.







$ kubectl apply -f apps_v1_helloapp.yaml -n test
      
      





Reconcile, Reconcile, . Reconcile. , , .







7:



โ€” (YAML) docker- . :







  1. .
  2. .
  3. , API Kubernetes.
  4. 2.
  5. , .




image







~/demo-operator/config/rbac . . ?







.







image







:







  1. Get, HelloApp. , Get HelloApp, API apps.anupam.com.
  2. Get, Deployment. , Deployment Kubernetes API apps.
  3. Update Deployment, API apps.


, +.







//+kubebuilder:rbac:groups=apps.anupam.com,resources=helloapps,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps.anupam.com,resources=helloapps/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=apps.anupam.com,resources=helloapps/finalizers,verbs=update
      
      





. Kubernetes . controller-gen CLI (CRD, RBAC . .).







make manifests. , , โ€” CRD, , . .







Docker-



โ€” docker- Kubernetes.







, Dockerfile, Operator-SDK. .







image







docker-:







$ make docker-build IMG=anupamgogoi/demo-operator:latest
      
      





docker. registry :







make docker-push IMG=anupamgogoi/demo-operator:latest
      
      





.









dist 5 , .







image







1-Namespace.yaml ( ). . 2, 3 4 rbac. 5 crd/bases. , 6-Controller.yaml, . . .







image







.









Kubernetes CentOS. . . - Kubernetes . .







dist - , kubectl apply .







image







demo-operator-system. , .







image







. CRD. CRD โ€” helloapps.apps.anupam.com, 5-apps.anupam.com_helloapps.yaml.







, kind, HelloApp. - , HelloApp.







image







, HelloApp ( ) . Kubernetes.







, HelloApp.







$ kubectl get all -n test
      
      





!







image







, (size=1).







, IP .







image







. .









Kubernetes Operator-SDK. Go, . !








All Articles