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.
kind: HelloApp โ (Custom Resource Definition, CRD), ( ). ( ).
, , Go. .
() , Kubernetes API.
Kubernetes:
Kubernetes โ . API ( API). , , API Kubernetes . . . Go โ Kubernetes, , .
:
- Go lang (1.16)
- Operator SDK (1.5)
- Kind
- 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
.
:
2:
. CRD (), () kind HelloApp , spec.size.
. spec : image () size (). spec, [4] .
. . โ HelloApp , . , spec.size, โ . . API .
Reconciler, Reconcile. Reconcile , CRUD (create read update delete โ ยซ ยป, . ), (kind) (spec). , .
main.go
main.go Operator-SDK . , . main.go.
main.go:
ctrl.GetConfigOrDie() Kubernetes ~/.kube/config . ~/.kube/config :
IP , . . . GetConfigOrDie() , ctrl.NewManager() . API API Kubernetes. . main.go, .
kubectl CLI API API Kubernetes. :
$ kubectl get nodes --v=8
3: CRD
, HelloApp . CRD. demo-operator :
$ make manifests
CRD
~/demo-operator/config/crd/bases
~/demo-operator/config/samples .
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
:
โ samples :
$ kubectl create ns test $ kubectl apply -f apps_v1_helloapp.yaml -n test
, test:
$ kubectl get all -n test
:
$ kubectl get HelloApp -n test
โ !
6:
โ , .
, -, CRD , 4 ( CRD).
, k8s . Go Visual Studio Code . Run โ Start Debugging VS Code, .
โ :
, ~/demo-operator/config/samples
CRD.
$ kubectl apply -f apps_v1_helloapp.yaml -n test
Reconcile, Reconcile, . Reconcile. , , .
7:
โ (YAML) docker- . :
- .
- .
- , API Kubernetes.
- 2.
- , .
~/demo-operator/config/rbac . . ?
:
- Get, HelloApp. , Get HelloApp, API apps.anupam.com.
- Get, Deployment. , Deployment Kubernetes API apps.
- 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. .
docker-:
$ make docker-build IMG=anupamgogoi/demo-operator:latest
docker. registry :
make docker-push IMG=anupamgogoi/demo-operator:latest
.
dist 5 , .
1-Namespace.yaml ( ). . 2, 3 4 rbac. 5 crd/bases. , 6-Controller.yaml, . . .
.
Kubernetes CentOS. . . - Kubernetes . .
dist - , kubectl apply .
demo-operator-system. , .
. CRD. CRD โ helloapps.apps.anupam.com, 5-apps.anupam.com_helloapps.yaml.
, kind, HelloApp. - , HelloApp.
, HelloApp ( ) . Kubernetes.
, HelloApp.
$ kubectl get all -n test
!
, (size=1).
, IP .
. .
Kubernetes Operator-SDK. Go, . !