Lab 3: Deploy the Lab Operator

Deploy the Custom Resource

  1. Deploy the ansible-operator-frontend Custom Resource

    kubectl create -f  ~/ansible-operator/ansible-operator-frontend/deploy/crds/ansiblelab.ibm.com_v1beta1_myansiblelabdemo_cr.yaml 
    
    > MyAnsibleLabDemo.ansiblelab.ibm.com/example-MyAnsibleLabDemo created
    

    ``

    From the resource that we defined earlier:

    apiVersion: ansiblelab.ibm.com/v1beta1
    kind: MyAnsibleLabDemo
    metadata:
      name: example-MyAnsibleLabDemo
    spec:
      # Add fields here
      size: 3
      demo:
        image: niklaushirt/k8sdemo:1.0.0
    

    ``

  2. Check that the CustomResource is running

    kubectl get pods
       
    > NAME                                         READY   STATUS    RESTARTS   AGE
    > ansible-operator-frontend-fd78bcf5-zxgws     2/2     Running   0          3m11s
    > k8sdemo-7fc8554dff-2krkz                     1/1     Running   0          45s
       
    

    ``

  3. Check the version of the deployed Image

kubectl describe deployment k8sdemo | grep Image

> Image:      niklaushirt/k8sdemo:1.0.0
  1. Once the status reads Running, we need to expose that deployment as a service so we can access it through the IP of the worker nodes. The k8sdemo application listens on port 3000.

    Run:

 kubectl expose deployment k8sdemo --name k8sdemoansible-service -n default --type="NodePort" --port=3000
 
 > service "k8sdemoansible-service" exposed
  1. Open the application in your Browser

    Training VM

    If you are using the training VM, you can open the webpage directly by typing:

    minikube service k8sdemoansible-service
    

    where k8sdemo-service is the name of the exposed kubernetes service.


    Cloud/Standalone

    If you are NOT using the training VM, you can open the webpage by forwarding a local port to the service in your Cluster.

    Execute this in a separate Terminal Window/Tab in order to be able to access the web page:

    kubectl port-forward --namespace default $(kubectl get po -n default -l app=k8sdemo | grep k8sdemo| \awk '{print $1;}') 3000:3000 
    

    And then navigating to http://localhost:3000/ in your browser.

  2. Get the name of the Operator Pod (if you have not done that in the previous task)

 kubectl get pods
 
  > NAME                                       READY   STATUS    RESTARTS   AGE
  > ansible-operator-frontend-fd78bcf5-zxgws   2/2     Running   0          87s
  > k8sdemo-7c5f4f5895-g7ft7                   1/1     Running   0          2s

  1. Get the log of the Operator Pod
 kubectl logs -c operator ansible-operator-frontend-fd78bcf5-zxgws

We can see that the Operator has picked up on the CustomResource of type MyAnsibleLabDemo and handed it to the CustomResource Controller.

 > {"level":"info","ts":1593618338.3867893,"logger":"ansible-controller","msg":"Watching resource","Options.Group":"ansiblelab.ibm.com","Options.Version":"v1beta1","Options.Kind":"MyAnsibleLabDemo"}
 > {"level":"info","ts":1593618338.387424,"logger":"kubebuilder.controller","msg":"Starting EventSource","controller":"myansiblelabdemo-controller","source":"kind source: ansiblelab.ibm.com/v1beta1, Kind=MyAnsibleLabDemo"}
 > {"level":"info","ts":1593618338.487868,"logger":"kubebuilder.controller","msg":"Starting Controller","controller":"myansiblelabdemo-controller"}
 > {"level":"info","ts":1593618338.5881243,"logger":"kubebuilder.controller","msg":"Starting workers","controller":"myansiblelabdemo-controller","worker count":1}
 > {"level":"info","ts":1593618669.4742577,"logger":"logging_event_handler","msg":"[playbook task]","name":"example-myansiblelabdemo","namespace":"default","gvk":"ansiblelab.ibm.com/v1beta1, Kind=MyAnsibleLabDemo","event_type":"playbook_on_task_start","job":"6354677430737639809","EventData.Name":"Gathering Facts"}
 ...

And that it has executed the Ansible Playbook to create it

> {"level":"info","ts":1593618737.0413835,"logger":"logging_event_handler","msg":"[playbook task]","name":"example-myansiblelabdemo","namespace":"default","gvk":"ansiblelab.ibm.com/v1beta1, Kind=MyAnsibleLabDemo","event_type":"playbook_on_task_start","job":"520010200565329963","EventData.Name":"myansiblelabdemo : Create the k8sdemo deployment"}
> {"level":"info","ts":1593618737.89282,"logger":"logging_event_handler","msg":"[playbook task]","name":"example-myansiblelabdemo","namespace":"default","gvk":"ansiblelab.ibm.com/v1beta1, Kind=MyAnsibleLabDemo","event_type":"playbook_on_task_start","job":"520010200565329963","EventData.Name":"myansiblelabdemo : Create the k8sdemo service"}
> {"level":"info","ts":1593618738.7778618,"logger":"runner","msg":"Ansible-runner exited successfully","job":"520010200565329963","name":"example-myansiblelabdemo","namespace":"default"}

Update the Custom Resource

We now proceed to modifying the Custom Resource that we have created in order to demonstrate how the Operator is able to update an existing deployment based on modifications done to the Custom Resource.

  1. Modify the Custom Resource
  #Linux 
 sed -i 's|image: niklaushirt/k8sdemo:1.0.0|image: niklaushirt/k8sdemo:1.0.1|g' ~/ansible-operator/ansible-operator-frontend/deploy/crds/ansiblelab.ibm.com_v1beta1_myansiblelabdemo_cr.yaml
 
 
 #Mac 
 sed -i '' 's|image: niklaushirt/k8sdemo:1.0.0|image: niklaushirt/k8sdemo:1.0.1|g' ~/ansible-operator/ansible-operator-frontend/deploy/crds/ansiblelab.ibm.com_v1beta1_myansiblelabdemo_cr.yaml
  1. Check that the Image tag (version) has been changed to 1.0.1

    more ~/ansible-operator/ansible-operator-frontend/deploy/crds/ansiblelab_v1beta1_myansiblelabdemo_cr.yaml 
    

    `

    The yaml manifest should look like this

    apiVersion: ansiblelab.ibm.com/v1beta1
    kind: MyAnsibleLabDemo
    metadata:
      name: example-myansiblelabdemo
    spec:
      # Add fields here
      size: 3
      demo:
        image: niklaushirt/k8sdemo:1.0.1
    

    ``

  2. Update the Custom Resource

    kubectl apply -f ~/ansible-operator/ansible-operator-frontend/deploy/crds/ansiblelab.ibm.com_v1beta1_myansiblelabdemo_cr.yaml 
    

    `

  3. Check that the Operator has picked up the modification in the CustomResource and that the version of the deployed Image has been changed by the Operator to 1.0.1

kubectl describe deployment k8sdemo | grep Image

> Image:      niklaushirt/k8sdemo:1.0.1
  1. And if you reload the browser you should see some nice orange peppers….