Deploy the CRD to K8S
make install
Log:
kustomize build config/crd | kubectl apply -f -customresourcedefinition.apiextensions.k8s.io/demoes.tutorial.demo.com created
Check it out
[root@paas-m-k8s-master-1 demo-operator]# kubectl api-resources | grep demo
demoes tutorial.demo.com true Demo
Yes, yes.
编译运行controller
make run
Keep the window open
。。。 The logs are as follows
go run ./main.go
I0314 14:37:11.802311 20314 request.go:665] Waited for 1.037997549s due to client-side throttling, not priority and fairness, request: GET:https://apiserver.cluster.local:6443/apis/rbac.istio.io/v1alpha1?timeout=32s
2024-03-14T14:37:13.055+0800 INFO controller-runtime.metrics metrics server is starting to listen {"addr": ":8080"}
2024-03-14T14:37:13.055+0800 INFO setup starting manager
2024-03-14T14:37:13.055+0800 INFO starting metrics server {"path": "/metrics"}
2024-03-14T14:37:13.055+0800 INFO controller.demo Starting EventSource {"reconciler group": "tutorial.demo.com", "reconciler kind": "Demo", "source": "kind source: /, Kind="}
2024-03-14T14:37:13.055+0800 INFO controller.demo Starting Controller {"reconciler group": "tutorial.demo.com", "reconciler kind": "Demo"}
2024-03-14T14:37:13.258+0800 INFO controller.demo Starting workers {"reconciler group": "tutorial.demo.com", "reconciler kind": "Demo", "worker count": 1}
Create a CRD instance
There is a default resource description file in the samples directory tutorial_v1_demo.yaml
We can use it for deployment testing
apiVersion: tutorial.demo.com/v1
kind: Demo
metadata:
namespace: demo
name: demo-sample
spec:
# TODO(user): Add fields here
image: nginx:1.22
svcName: demo-ng
replicas: 3
At present, there is no custom demo crd instance, and there are no pods under the demo nameSpace
# kubectl -n demo get demo
No resources found in default namespace.
# kubectl -n demo get pod
No resources found in demo namespace.
Let's apply this demo crd
# kubectl apply -f config/samples/tutorial_v1_demo.yaml
View custom resources
# kubectl get demo -n demo
NAME AGE
demo-sample 12s
controller的日志
View resources
# kubectl -n demo get pod
NAME READY STATUS RESTARTS AGE
demo-ng-6df8f7c68f-4mg9n 1/1 Running 0 4m33s
demo-ng-6df8f7c68f-699bv 1/1 Running 0 4m33s
demo-ng-6df8f7c68f-n6zkc 1/1 Running 0 4m33s
I saw that all 3 pods were created
验证通过patch修改podNum,来增减pod的数量
kubectl -n demo patch demo demo-sample --type merge --patch '{"spec": {"replicas": 5}}'
Automatically added to 5
Minus it
kubectl -n demo patch demo demo-sample --type merge --patch '{"spec": {"replicas": 2}}'
controller中也会看到对应的日志