Writing a Kubernetes Operator
metalbear.co
metalbear.co
> An Operator is an application-specific controller that extends the Kubernetes API to create, configure, and manage instances of complex stateful applications on behalf of a Kubernetes user. It builds upon the basic Kubernetes resource and controller concepts but includes domain or application-specific knowledge to automate common tasks.
This is the original definition of an operator [1]. People no use them for stateless things and domain specific work has taken off.
You can look at the Kubernetes docs [2] to see refinements on it...
> Kubernetes' operator pattern concept lets you extend the cluster's behaviour without modifying the code of Kubernetes itself by linking controllers to one or more custom resources. Operators are clients of the Kubernetes API that act as controllers for a Custom Resource.
[1] https://web.archive.org/web/20190113035722/https://coreos.co...
[2] https://kubernetes.io/docs/concepts/extend-kubernetes/operat...
controlling refers to directing or regulating the behavior of something, while operating refers to the actual execution or manipulation.
They watch for their CR changing and then act on it - usually by making requests to the API server to create/update/delete objects (e.g. a operator for a database might make Pod creation requests when it spots the FooDatabase CR being created)
In essence, a controller is like a general-purpose manager for built-in K8s resources, while an operator is a specialized manager tailored for specific applications and tasks. Operators are built on top of the controller pattern, so you can think of them as the next level up in the K8s automation!
but i will agree on that i have seen people develop controllers with the operator sdk where mostly the Kubernetes client libraries would have been more than enough.
mind clarifying?
My complaint is that I have seen orgs write operators for random stuff, often reinventing the wheel. Lot of operators in orgs are result of resume driven development. Having said that it often comes handy for complex orchestration.
- scaling up and down applications because of time instead of demand. or based on non metric based actions
- Extending kubernetes to understand your workload
- Automating configuration and management of complex applications
- Managing legacy applications that cannot be easily containerized or migrated to the cloud.
if you love k8s youll love operators
the list is endless!
Kubernetes is more like a way of doing things than a technology. Basically APIs all the way down. and thus the operators and controllers do deserve love.
Im not saying that you need an operator to change the dipers of a baby, but as far as a stack goes, k8s is the best i have ever worked with.
The only moderately justifiable operator I've ever seen is cert-manager. Even then, one wonders what it would be like if it just updated a Secret every 3 months based on a hard-coded config passed to it, and skipped the CRDs.
An operator is needed when you can't just deploy and forget about it. An example is the Prometheus operator, which will track annotations created by users to configure the scraping configuration of your Prometheus instances. Another example is cert-manager, which gets certificates into secrets based on Certificate and Ingress objects, renews them automatically before expiry, and does that by creating ingresses picked up by your ingress controller.
The advantage of an operator is that it will react to stuff happening in the cluster. The drawback is that it reacts to stuff happening, potentially doing unexpected things because changes happen at any time and you can't dry-run them. Another drawback is that they are usually global, so you can't run multiple versions at the same time for different namespaces (mainly because custom resource definitions are global).
Unfortunately many people think packaging an application = creating an operator, and that operator does nothing a chart couldn't do.
Or is can be actively harmful when they don't do any error checking whatsoever, causing it to be less accurate that `helm template` would be. Related, it's also one more thing to monitor because it can decide to start vomiting errors for whatever random reason
Edit: maybe we could shift left and ask the app developers to add certificate acquisition directly into the app source.
A certificate for service to service communication does not have to correspond to a public endpoint.
The problem is duplication of functionality within the cluster and increased complexity in the configuration of deployments. I want to configure a certificate acquisition process once, and plug those certificates into pods using a generic process based on annotations. I don’t want to configure it 6 times because each operator does things slightly differently. Each CRD introduces new complexity to the cluster. More opportunities for things to break. More stuff to read and learn about.
I think CRDs should be used to extend the platform functionality, not as a tool to simplify deployments. It feels a little like reworking the plumbing in your house to make the sink easier to install instead of using a wrench.
So yes -> you need an operator to dynamically change config of prometheus. If not -> all your changes wont matter because they will be not reflected in prom.
If all of your pods have the same scrape settings, there's not much benefit to the operator. But if each pod or set of pods needs custom scrape settings - say different scrape intervals or custom tag re-writes, the operator will let you define each set of scrape configs separately as kubernetes resources. You can store the definitions alongside the resources they scrape instead of having a single large complex config in your prometheus deployement. This would be especially beneficial if you have multiple projects owned by multiple teams, all scraped by the same Prometheus.
Granted I also don't use the operator, but I've looked into it. With complex enough deployments it would simplify things.
The same with annotations approach - its very limited.
The two features I need (and can get without the provider) are: 1. If I (manually, explicitly) change the configmap, prometheus can pick it up without being restarted. This is provided by the side-car container. 2. If new pods come online they are automatically detected and scraped by Prometheus. As long as they have the annotations saying: a) this pod should be scraped, b) scrape on this port, and c) scrape at this URL - then they will be scraped.
The main point of Prometheus operator is to federate access to Prometheus configuration so teams can manage configuration that the built-in Kubernetes service discovery doesn't let you control from an application side. Things like scrape interval, recording rules and alerts, etc. ServiceMonitors, PrometheusRule CRDs basically let app owners ship these aspects of monitoring with their applications, instead of having to have a hand-off between the service owners and the SRE teams managing the monitoring infrastructure.
The operator will do this for you, you just update the version of the broker in the CR spec, it notices, and then applies the change.
Likewise, some configuration options can be applied at runtime, some need the broker to be restarted to be applied, the operator knows which are which, and will again manage the process of a rolling restart if needed to apply the change.
You can also define topics and users as custom resources, so have a nice Gitops approach to declaring resources.
Disclaimer: In my past job, I've worked at Red Hat, who are sponsoring Strimzi
An example that has come up for me is an operator for a Kafka Schema Registry. This is a service that needs some credentials in a somewhat obscure format so it can communicate very directly with a Kafka broker. If the broker's certificates (or CA) are modified, then the Schema Registry needs to have new credentials generated, and needs to be restarted. But the registry shouldn't (obviously) have direct access to the broker's certificates. Instead, there's a more-privileged subsystem which orchestrates that dance; that's the operator.
Normally you would have a CI/CD pipeline setup that shit before deployment.
But thats super brittle and against GitOps. So you write custom CRDs you can store in repo that abstract away the complexity of the setup but still provide devs with srlf-service approaches to configure their app monitoring for them.
If you start thinking about self-service and gitops - operators become very useful for many things.
Also it's important for people reading this article - an apiservice (which this article talks about) is very rarely something that should be done. An operator is more appropriate for nearly all cases except for when you truly need your state stored outside of the internal Kubernetes etcd datastore.
> Operators are clients of the Kubernetes API that act as controllers for a Custom Resource.
In the mean time, one of the creators of the Operator Framework[0] built a bunch of useful patterns using generics that we used to build the SpiceDB Operator[1] called controller-idioms[2].
Does anyone know of other efforts to improve the status quo?
[0]: https://operatorframework.io
I'm hoping things are improving. Maybe I'll consider it for the next one I write.
It's frustrating to be a kubernetes admin but not have access to basic configuration options because the maintainers of even some very high-profile operators (looking at you, AWX) neglected to build in access to basic functionality.
In the latest release of the spicedb-operator[0], I added a feature that allows users to specify arbitrary patches over operator-managed resources directly in the API (examples in the link).
There are some other projects like Kyverno and Gatekeeper that try to do this generically with mutating webhooks, but embedding a `patches` API into the operator itself gives the operator a chance to ensure the changes are within some reasonable guardrails.
[0]: https://github.com/authzed/spicedb-operator/releases/tag/v1....
1 https://github.com/prometheus-operator/prometheus-operator/b...
I can see you don’t like operators and that’s fine. Many folks find it’s a useful pattern to follow
Operators are just the non-containerized daemons of the Kubernetes OS. We did all this work to run everything in neatly encapsulated containers, and then everyone wants to run stuff globally on the whole cluster. What's the point? Do we just containerize clusters and start over?
You run the operator itself in a pod, but you can only have one of them running at a time, hence they are global. You can run as many Alpine/Ubuntu/... containers as you want, and as many ElasticSearch or PostgreSQL as you want, in isolation, that's the beauty of containers. But you can only run one SpiceDB operator or one ElasticSearch operator, on the whole cluster. We're back to square one.
That said, I do think the direction that Kube has gone with CRDs, by leaning into making them more like built-in apis instead of allowing them to be differentiated as an external extension point has limited what you can do with them.
When ThirdPartyResrouces just carved out a group and a name in the API it was simple to have multiple copies and multiple versions running together in a cluster. But that has become more difficult with time, especially with ConversionWebhooks - a feature that forces all controllers for an API to agree not just on a version, but on the specific ways versions convert into each other.
Migrations can be run in containers (and they are, even with the operator), but it's actually a lot of work to run them at the right time, only once, with the right flags, in the right order, waiting for SpiceDB to reach a specific spot in phased migrations, etc.
Moving from v1.13.0 to v1.14.0 of SpiceDB requires a multi-phase migration to avoid downtime[0], as could any phased migration for any stateful workload. The operator will walk you through them correctly, without intervention. Users who aren't running on Kubernetes or aren't using the operator often have problems running these steps correctly.
The value is in this automation, but also in the API interface itself. RDS is just some automation and an API on top of EC2, and I think RDS has value over running postgres on EC2 myself directly.
As for helm charts, this is just my opinion, but I don't think they're a good way to distribute software to end users. The interface for a helm chart becomes polluted over time in the same way that most operator APIs become polluted over time, as more and more configuration is pulled up to the top. I think helm is better suited to managing configuration you write yourself to deploy on your own clusters (I realize I'm in the minority here).
[0]: https://github.com/authzed/spicedb/releases/tag/v1.14.0
Just curious, is this a limitation of the Operators framework, or that of your system's implementation? My knee-jerk reaction is that any implementation should absolutely not require opening ticket. After all, Amazon's API mandate happened 20 years ago, and Netflix followed suit to achieve phenomenal productivity for their engineers. I have a hard time imagining why any engineer would think that gatekeeping configuration with PR is a good idea(a UI with proper automation and approval process that hides generated PR for specific use cases is a different matter)
An operator (or its custom resource) can be configured by Kubernetes YAML/API and its upto the creator of the operator to specify the kind of configuration. If the operator creator did not specify options to set cpu/memory limits on the pods managed by the operator, then you can't do anything. You have to add that feature into the operator and then make a pull request and wait for it to be upstreamed.
Or fork it instead. Same thing for helm charts (except forking and patching them is easier than forking an operator).
I've seen just as much if not more issues with debugging the operator logic itself as with the main pods/deployments it was trying to manage.
So just from a practical point of view, I think it should be a last resort after everything else fails (helm charts, etc).
There really is no magic, is all there and with go the images are usually what? like 10 mb?
It's essential to have a solid understanding of Kubernetes architecture, concepts such as custom resources and controllers, and the tools and APIs available for working with Operators.
Dont use rust though, use and sdk like the operator sdk or kubebuilder. Its native to k8s and you will have a much easier time too.
It should be helpful if you are new to the Operator concept.
Operators are generally useful for handling domain-specific actions - for example, performing database backups, installing plugins on Moodle/Wordpress, etc. If you are looking for application deployment then a Helm chart should be sufficient.
In my years of working with Kubernetes I cannot shake the feeling that it's, basically, an MLM. It carefully obscures it's functionality by hiding behind opaque definitions. It doesn't really work, when push comes to shove. And, most importantly, it survives in a parasitic kind of way: by piggybacking on those who develop all kinds of extensions, be it operators, custom networking or storage plugins, authentication and so on.
My problem is I cannot find who stands at the top of the pyramid. There's Cloudnative Foundation, but all it does is selling certifications nobody really needs... so, that cannot possibly be it. No big name doesn't really benefit from this in an obvious way...
So... anyways, when I hear people argue about how to implement this or another extension of Kubernetes, it rings the same as when people argue about styles of agile, or code readability etc. nonsense. There isn't a good way. There is not acceptance criteria. The whole system is flawed to no end.
Written by a co-worker to help manage our databricks projects across clusters. Works wonderfully!!
1. Write a provider/extension/whatever for a tool like Terraform or Pulumi. I live in a world where the infrastructure doesn't move underneath my feet. I am the source of truth. I feel like I only need to reconcile changes when I make changes to my IaC repositories.
2. I could write something that exists in a control plane, like Kubernetes operators or Crossplane. I live in a world where I look at the world, find the delta between current state and desired state, then try to reconcile. This is an endless loop.
I feel like these are different approaches with the same goal. Why should I decide either way beyond tossing a coin?
Some use cases:
- an internal enterprise DNS system which is not standards-compliant with the world at large
- an internal certificate authority and certificate issuing system.