Before running the Operator, Kubernetes needs to know about the new custom resource definition the Operator will be watching. Deploy the memcached Custom Resource Definition:
kubectl create -f config/crd/bases/cache.example.com_memcacheds.yaml
This command creates a new resource type, called
memcached. Your Operator responds to the creation and modification of resources of this type.
With the CRD created, you can run the Operator either as a Pod inside a Kubernetes cluster, or in a testing mode as a Go program on your local system. This exercise tests the Operator locally, outside of the Kubernetes cluster.
Copying Roles for local development
It is important that the Role path referenced in watches.yaml exists on your machine. To run the Operator locally, copy any Roles it uses to a configured Ansible Roles path for your local machine (e.g.
mkdir -p /opt/ansible/roles cp -r roles/dymurray.memcached_operator_role /opt/ansible/roles/
Running the Operator
Create a new namespace called
tutorial and set
kubectl to use it by default. The Operator will watch Custom Resources only within this namespace.
kubectl create namespace tutorial kubectl config set-context --current --namespace=tutorial
Start the Operator with make's
WATCH_NAMESPACE=tutorial make run
Because the running Operator will block I/O in its terminal, open a new terminal window, tab or session and change to the tutorial directory.
Now that the Operator is running, create a CR to have the Operator deploy an instance of memcached. The Operator SDK generated a sample CR in the project scaffolding. Inspect
config/samples/cache_v1alpha1_memcached.yaml, then update it to specify 3 replicas by replacing
foo: bar with
apiVersion: cache.example.com/v1alpha1 kind: Memcached metadata: name: memcached-sample spec: size: 3
Now, create the CR. The Operator should notice it as a watched resource and begin deploying the 3 replicas of memcached.
kubectl --namespace tutorial create -f config/samples/cache_v1alpha1_memcached.yaml
Ensure that the memcached-operator creates the deployment for the CR by running
kubectl get deployment
Check the pods to confirm 3 replicas were created by running
kubectl get pods.
Change the Memcached CR to deploy 4 replicas
spec.size field in
config/samples/cache_v1alpha1_memcached.yaml from 3 to 4.
apiVersion: cache.example.com/v1alpha1 kind: Memcached metadata: name: memcached-sample spec: size: 4
And apply the change to the desired number of replicas on the cluster:
kubectl --namespace tutorial apply -f config/samples/cache_v1alpha1_memcached.yaml
Confirm that the Operator changes the Deployment size with another invocation of
kubectl get deployment.
Inspect the YAML list of
memcached resources in the
tutorial namespace, noting that the
spec.size field is now set to 4.
kubectl get memcached -o yaml
Removing Memcached from the cluster
First, delete the
memcached CR, which will trigger removal of the 4 Memcached Pods and the Deployment to which they belong:
kubectl --namespace tutorial delete -f config/samples/cache_v1alpha1_memcached.yaml
Verify the memcached CR and deployment have been properly removed.
kubectl get memcached kubectl get deployment kubectl get pods