is Kubernetes? • Open source platform for managing containerized workloads and services • Containers, containers, containers • Name originates from Greek - helmsman or pilot • Google open-sourced in 2014 • Based on Borg - Google’ internal project • K_ _ _ _ _ _ _ _S -> k8s
is great about Kubernetes? • Scalability of workloads • Separation of workloads • Native stable resources (Deployments, Pods) • Extending the API - Custom Resources
is Kubernetes not? • Not a PaaS • Not limited to the types of apps supported • Not opinionated on: ◦ Deploying ◦ Run CI/CD ◦ Logging, monitoring or alerting
a controller? • Deployment has deployment controller • Reconciles given state • Pattern of controllers ◦ Non terminating loop that regulates the state of the system ◦ A control loop watches the shared state of the cluster via the API server and makes changes to move from the current to the desired state
an “operator”? • Non core Kubernetes controller • Makes use of CRDs - custom resource definition • Holds the knowledge of how an application needs to be deployed, managed and packaged • Reconcile loop • Extends the Kubernetes API to make the application a custom resource • Concept introduced by CoreOS
choose creating an operator? • Application uses declarative API • Resources are scoped to a namespace or a cluster • Encapsulate business logic • Build automation that watches for updates of Kubernetes objects • Create or update resources via the Kubernetes native API • Top level support from kubectl
just use a ConfigMap or Secret? Sometimes creating an operator is not needed • Existing well known config file format (e.g. mysql.cnf ) • Config file is used to just configure an application running in a Pod • No need to reconcile to the state
to build an operator • go language: ◦ client-go & co. ◦ operator-sdk ◦ kubebuilder • Other languages: ◦ Kubernetes python and java clients and others • Other tools: ◦ helm ◦ ansible
clients Pros: Cons: • Same as upstream controllers use • Stability of Kubernetes code • Versioning based on Kubernetes releases • Ability to finetune • Large ecosystem • No abstractions/helpers • A lot of inside knowledge to optimize correctly • New major version on every Kubernetes minor version release
Resource Event Handler puts an item to the workqueue • Workqueue consists of keys: ◦ <resource_namespace>/<resource_name> • Only one worker works on one item at a time • First In First Out
github.com/kubernetes-sigs/kubebuilder • SDK for building Kubernetes APIs using CRDs • Part of kubernetes-sigs organization • Uses controller-runtime under the hood • Doesn’t strictly advertise itself as operator builder tool • Creates go language controllers/operators
Example of a go operator $ mkdir kubebuilder-operator && cd kubebuilder-operator $ kubebuilder init --domain k8s.io --license apache2 --owner "The JOnTheBeach Audience" $ kubebuilder create api --group ships --version v1beta1 --kind Sloop $ # Edit the logic code $ pkg/controller/sloop/sloop_controller.go
github.com/operator-framework/operator-sdk • Aimed at creating operators • Part of operator-framework • Operator types you can create: ◦ go ◦ helm ◦ ansible • Testing framework • Metrics • Building and running commands
Example of a go operator $ operator-sdk new app-operator $ cd app-operator $ # Add a new API for the custom resource AppService $ operator-sdk add api --api-version=app.example.com/v1alpha1 --kind=AppService $ # Add a new controller that watches for AppService $ operator-sdk add controller --api-version=app.example.com/v1alpha1 --kind=AppService $ # Build operator $ operator-sdk build quay.io/example/app-operator
Example of a helm operator $ # Create a new helm operator project $ operator-sdk new nginx-operator --api-version=example.com/v1alpha1 --kind=Nginx --type=helm $ # Edit watches.yaml file to customize the operator logic
Example of an ansible operator $ # Create a new ansible based operator project: $ operator-sdk new memcached-operator --api-version=cache.example.com/v1alpha1 --kind=Memcached --type=ansible $ # Edit watches file to customize the logic
operator • `operator-sdk up local` - run • Log all the things! • Get logs of an operator ◦ `kubectl logs <pod-name>` ◦ Use CR name to label any pods the operator creates • `kubectl events -n NAMESPACE` • `Spec.Paused` Helpful tools and tips