|
| 1 | +# Backend |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +In this lab, you will deploy the pre-built Helidon Java backend Docker image to OKE, then configure the API Gateway. |
| 6 | + |
| 7 | +Estimated time: ~25-minutes. |
| 8 | + |
| 9 | +### Objectives |
| 10 | + |
| 11 | +* Set values for environment variables |
| 12 | +* Build and deploy the Docker image of the application |
| 13 | +* Deploy the image on the Oracle Kubernetes Engine (OKE) |
| 14 | +* Describe the steps for Undeploying |
| 15 | +* Configure the API Gateway |
| 16 | +* Test the backend application |
| 17 | + |
| 18 | +### Prerequisites |
| 19 | + |
| 20 | +This lab requires the completion of lab 1 and the provision of the OCI components. |
| 21 | + |
| 22 | +### Understanding the Java/backend application |
| 23 | + |
| 24 | +As with most React applications (https://reactjs.org/), this application uses remote APIs to handle data persistence. The backend implements 5 REST APIs including: |
| 25 | +- Retrieving the current list of todo items |
| 26 | +- Adding a new todo item |
| 27 | +- Finding a todo item by its id |
| 28 | +- Updating an existing todo item |
| 29 | +- Deleting a todo item. |
| 30 | + |
| 31 | +The APIs are documented using Swagger @ http://130.61.67.158:8780/swagger-ui/#/ |
| 32 | + |
| 33 | +The backend is implemented using the following Java classes (under ./backend/src/...): |
| 34 | +- Main.java: starts and configure the main entry points. |
| 35 | +- ToDoItem.java: maps a Todo Item instance to/from JSON document |
| 36 | +- ToDoItemStorage.java: stores the Todo item in a persistent store i.e., the Oracle Autonomous database |
| 37 | +- ToDoListAppService.java: implements the Helidon service and exposes the REST APIs |
| 38 | + |
| 39 | +  |
| 40 | + |
| 41 | + |
| 42 | +## **STEP 1**: Set values for workshop environment variables |
| 43 | + |
| 44 | +1. Set the root directory of the workshop |
| 45 | + ``` |
| 46 | + <copy>export MTDRWORKSHOP_LOCATION=~/mtdrworkshop</copy> |
| 47 | + ``` |
| 48 | +2. Run source addAndSourcePropertiesInBashrc.sh |
| 49 | + |
| 50 | +The following command will set the values of environment variables in mtdrworkshop.properties and source ~/.bashrc |
| 51 | + |
| 52 | + ``` |
| 53 | + <copy>cd $MTDRWORKSHOP_LOCATION; source addAndSourcePropertiesInBashrc.sh |
| 54 | + </copy> |
| 55 | + ``` |
| 56 | + |
| 57 | +## **STEP 2**: Build and push the Docker images to the OCI Registry |
| 58 | + |
| 59 | +1. Ensure that the "DOCKER_REGISTRY" variable is set |
| 60 | + |
| 61 | + Example: `<region-key>.ocir.io/<object-storage-namespace>/<firstname.lastname>/<repo-name>` |
| 62 | + If the variable is not set or is an empty string, the push will fail (but the docker image will be built). |
| 63 | + |
| 64 | +2. Make sure to be in backend/target/classes/wallet directory then execute |
| 65 | + ``` |
| 66 | + <copy>unzip ~/mtdrworkshop/setup-dev-environment/wallet.zip</copy> |
| 67 | + ``` |
| 68 | + |
| 69 | +3. Pick mtdrb_tp service alias (see the list of aliases in |
| 70 | + ./backend/target/classes/wallet/tnsnames.ora) |
| 71 | + |
| 72 | +  |
| 73 | + |
| 74 | +4. Edit ./backend/target/classes/application.yaml to set the database service and user password |
| 75 | +  |
| 76 | + |
| 77 | +5. Copy the edited ./backend/target/classes/application.yaml to ./backend/src/main/resources/application.yaml |
| 78 | + |
| 79 | +6. Edit ./backend/src/main/java/com/oracle/todoapp/Main.java |
| 80 | + - Locate the following code fragment |
| 81 | +  |
| 82 | + - Replace `eu-frankfurt-1` in `"https://objectstorage.eu-frankfurt-1.oraclecloud.com"` by your region |
| 83 | + |
| 84 | + - Save the file |
| 85 | + |
| 86 | +7. Run `build.sh` script to build and push the |
| 87 | + microservices images into the repository |
| 88 | + |
| 89 | + ``` |
| 90 | + <copy>cd $MTDRWORKSHOP_LOCATION/backend; ./build.sh</copy> |
| 91 | + ``` |
| 92 | + In a couple of minutes, you should have successfully built and pushed the images into the OCIR repository. |
| 93 | +
|
| 94 | +8. Check your container registry from the root compartment |
| 95 | + - Go to the Console, click the hamburger menu in the top-left corner and open |
| 96 | + **Developer Services > Container Registry**. |
| 97 | +
|
| 98 | +  |
| 99 | +
|
| 100 | +9. Mark Access as Public (if Private) |
| 101 | + (**Actions** > **Change to Public**): |
| 102 | +
|
| 103 | +  |
| 104 | +
|
| 105 | +## **STEP 3**: Deploy on Kubernetes and Check the Status |
| 106 | +
|
| 107 | +1. Run the `deploy.sh` script |
| 108 | +
|
| 109 | + ``` |
| 110 | + <copy>cd $MTDRWORKSHOP_LOCATION/backend; ./deploy.sh</copy> |
| 111 | + ``` |
| 112 | +
|
| 113 | +--> service/todolistapp-helidon-se-service created |
| 114 | +--> deployment.apps/todolistapp-helidon-se-deployment created |
| 115 | +
|
| 116 | +2. Check the status using the following commands |
| 117 | +$ kubectl get services |
| 118 | +
|
| 119 | +The following command returns the Kubernetes service of MyToDo application with a load balancer exposed through an external API |
| 120 | + ``` |
| 121 | + <copy>kubectl get services</copy> |
| 122 | + ``` |
| 123 | +
|
| 124 | + |
| 125 | +
|
| 126 | +3. $ kubectl get pods |
| 127 | + ``` |
| 128 | + <copy>kubectl get pods</copy> |
| 129 | + ``` |
| 130 | +
|
| 131 | + |
| 132 | +
|
| 133 | +5. Continuously tailing the log of one of the pods |
| 134 | +
|
| 135 | + $ kubectl logs -f <pod name> |
| 136 | + Example kubectl lgs -f todolistapp-helidon-se-deployment-7fd6dcb778-c9dbv |
| 137 | +
|
| 138 | + Returns: |
| 139 | + http://130.61.66.27/todolist |
| 140 | +
|
| 141 | +## **STEP 4**: UnDeploy (optional) |
| 142 | +
|
| 143 | + If you make changes to the image, you need to delete the service and the pods by running undeploy.sh then redo Steps 2 & 3. |
| 144 | +
|
| 145 | + 1. Run the `undeploy.sh` script |
| 146 | + ``` |
| 147 | + <copy>cd $MTDRWORKSHOP_LOCATION/backend; ./undeploy.sh</copy> |
| 148 | + ``` |
| 149 | + 2. Rebuild the image + Deploy + (Re)Configure the API Gateway |
| 150 | +
|
| 151 | +
|
| 152 | +## **STEP 5**: Configure the API Gateway |
| 153 | +
|
| 154 | +The API Gateway protects any RESTful service running on Container Engine for Kubernetes, Compute, or other endpoints through policy enforcement, metrics and logging. |
| 155 | +Rather than exposing the Helidon service directly, we will use the API Gateway to define cross-origin resource sharing (CORS). |
| 156 | +
|
| 157 | +1. From the hamburger menu navigate **Developer Services** > **API Management > Create Gateway** |
| 158 | +  |
| 159 | +
|
| 160 | +2. Configure the basic info: name, compartment, VCN and Subnet |
| 161 | + - VCN: pick on of the vitual circuit network |
| 162 | + - Subnet pick the public subnet |
| 163 | + The click "Create" |
| 164 | +  |
| 165 | +
|
| 166 | +3. Click on Todolist gateway |
| 167 | +  |
| 168 | +
|
| 169 | +4. Click on Deployments |
| 170 | +  |
| 171 | +
|
| 172 | +5. Create a todolist deployment |
| 173 | +  |
| 174 | +
|
| 175 | +
|
| 176 | +6. Configure Cross-origin resource sharing (CORS) policies. |
| 177 | + - CORS is a security mechanism that will prevent running application loaded from origin A from using resources from another origin B. |
| 178 | + - Allowed Origins: is the list of all servers (origins) that are allowed to access the API deployment typically your Kubernetes cluster IP. |
| 179 | + - Allowed methods: GET, PUT, DELETE, POST, OPTIONS are all needed. |
| 180 | +  |
| 181 | +
|
| 182 | +7. Configure the Headers |
| 183 | +  |
| 184 | +
|
| 185 | +8. Configure the routes: we will define two routes: |
| 186 | + - /todolist for the first two APIs: GET, POST and OPTIONS |
| 187 | +  |
| 188 | +
|
| 189 | + - /todolist/{id} for the remaining three APIs: (GET, PUT and DELETE) |
| 190 | +  |
| 191 | +
|
| 192 | +
|
| 193 | +## **STEP 6**: Testing the backend application through the API Gateway |
| 194 | +
|
| 195 | +1. Navigate to the newly create Gateway Deployment Detail an copy the endpoint |
| 196 | +  |
| 197 | +
|
| 198 | +2. Testing through the API Gateway endpoint |
| 199 | + postfix the gateway endpoint with "/todolist" as shown in the image below |
| 200 | +  |
| 201 | +
|
| 202 | + It should display the Todo Item(s) in the TodoItem table. At least the row you have created in Part I. |
| 203 | +
|
| 204 | +Congratulations, you have completed lab 2; you may now [proceed to the next lab](#next). |
| 205 | +
|
| 206 | +## Acknowledgements |
| 207 | +
|
| 208 | +* **Author** - - Kuassi Mensah, Dir. Product Management, Java Database Access |
| 209 | +* **Contributors** - Jean de Lavarene, Sr. Director of Development, JDBC/UCP |
| 210 | +* **Last Updated By/Date** - Kuassi Mensah, Database Product Management, April 2021 |
0 commit comments