A Kubernetes operator built with Operator SDK and deployed via Operator Lifecycle Manager (OLM).
This operator manages two deployments (web-application and data-db) through a Custom Resource Definition (CRD) called K8soperator.
- Kubernetes cluster with OLM installed
- Docker registry access
- Tools:
operator-sdk,opm,kubectl,docker
Custom Resource (CR) → CRD → Operator Controller → Managed Resources
operator-sdk init --domain=mydomain.com --repo=github.com/user/k8soperatoroperator-sdk create api --group=apps --version=v1alpha1 --kind=K8soperator --resource --controllerEdit api/v1alpha1/k8soperator_types.go:
type K8soperatorSpec struct {
Replicas int32 `json:"replicas,omitempty"`
}make generate manifestsmake build docker-build docker-push IMG=<registry>/k8soperator:v0.1.2make bundle VERSION=0.1.2
make bundle-build bundle-push BUNDLE_IMG=<registry>/k8soperator-bundle:v0.1.2make catalog-render catalog-build catalog-push INDEX_IMG=<registry>/k8soperator-index:v0.1.2# Create CatalogSource
kubectl apply -f catalogsource.yaml
# Create OperatorGroup
kubectl apply -f - <<EOF
apiVersion: operators.coreos.com/v1
kind: OperatorGroup
metadata:
name: k8soperator-operatorgroup
namespace: default
spec:
targetNamespaces:
- default
EOF
# Create Subscription
kubectl apply -f - <<EOF
apiVersion: operators.coreos.com/v1alpha1
kind: Subscription
metadata:
name: k8soperator-subscription
namespace: default
spec:
channel: stable
name: k8soperator
source: k8soperator-catalog
sourceNamespace: olm
EOF- Bundle Creation: CRD manifests are packaged in the operator bundle
- CSV Definition: ClusterServiceVersion references the CRD in
spec.customresourcedefinitions.owned - OLM Installation: When subscription is created, OLM:
- Reads the CSV from catalog
- Installs CRDs defined in CSV
- Deploys operator controller
- Manages lifecycle
Create a Custom Resource:
apiVersion: apps.mydomain.com/v1alpha1
kind: K8soperator
metadata:
name: k8soperator-sample
namespace: default
spec:
replicas: 1# Clean install
./clean-reinstall.sh
# Manual deploy
./deploy-olm.sh# Check operator status
kubectl get csv,subscription,operatorgroup -n default
# Check CRD
kubectl get crd k8soperators.apps.mydomain.com
# Check managed resources
kubectl get k8soperators,deployments,services -n defaultapi/v1alpha1/k8soperator_types.go- CRD schema definitionconfig/crd/bases/- Generated CRD manifestsconfig/manifests/bases/k8soperator.clusterserviceversion.yaml- CSV templatebundle/- OLM bundle artifactscatalog/- Catalog index configuration
kubectl delete subscription k8soperator-subscription -n default
kubectl delete catalogsource k8soperator-catalog -n olmThis project is licensed under the Apache License 2.0 - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.
Vasudev Chavan