|
| 1 | +# IBM MQ Golang application tutorial for OpenShift |
| 2 | +This sample provides a working template that you can use to build your own Golang |
| 3 | +application container image that runs in Red Hat OpenShift under the most secure |
| 4 | +"Restricted SCC" and uses the |
| 5 | +[IBM MQ Golang](https://github.com/ibm-messaging/mq-golang) or |
| 6 | +[IBM MQ Golang JMS](https://github.com/ibm-messaging/mq-golang-jms20) libraries to connect |
| 7 | +to an IBM MQ queue manager. |
| 8 | + |
| 9 | +You can build and run your application using the following simple steps; |
| 10 | +1. Modify the application file to add your code |
| 11 | +2. Build the application into a Docker container image locally |
| 12 | +3. Push the container image to your OpenShift cluster |
| 13 | +4. Configure the variables and run the application! |
| 14 | + |
| 15 | + |
| 16 | +Let's get started! |
| 17 | + |
| 18 | + |
| 19 | +## Step 1: Modify the application file to add your code |
| 20 | +Add your application logic into the [main.go](./src/main.go) file using either the |
| 21 | +[IBM MQ Golang](https://github.com/ibm-messaging/mq-golang) or |
| 22 | +[IBM MQ Golang JMS](https://github.com/ibm-messaging/mq-golang-jms20) interfaces depending |
| 23 | +on your preference. |
| 24 | + |
| 25 | +The file contains all the basic details necessary to create a |
| 26 | +connection to a queue manager, so you can simply add your logic to send and/or receive |
| 27 | +messages at the bottom of the function. |
| 28 | + |
| 29 | + |
| 30 | +## Step 2: Build the application into a Docker container image locally |
| 31 | +Open a command shell to the same directory as this readme file and execute the following command; |
| 32 | +```bash |
| 33 | +# Run the dockerfile build to create the container image |
| 34 | +docker build -t golang-app -f Dockerfile . |
| 35 | +``` |
| 36 | + |
| 37 | + |
| 38 | +## Step 3: Push the container image to your OpenShift cluster |
| 39 | +This step will vary depending on what sort of OpenShift cluster you are going to deploy to - |
| 40 | +for example a registry that is part of the cluster or an external registry that can be |
| 41 | +accessed by the cluster. |
| 42 | + |
| 43 | +In this example we are using the IBM Cloud Container Registry (in London) that is hosted |
| 44 | +at `uk.icr.io`. |
| 45 | + |
| 46 | +```bash |
| 47 | +# Tag the new image against the target registry |
| 48 | +docker tag golang-app uk.icr.io/golang-sample/golang-app:1.0 |
| 49 | + |
| 50 | +# Push the updated image to your registry |
| 51 | +docker push uk.icr.io/golang-sample/golang-app:1.0 |
| 52 | +``` |
| 53 | + |
| 54 | +**Note:** If you are iterating over this step multiple times while testing your application then |
| 55 | +don't forget to increase the tag version each time as image caching on cluster may mean |
| 56 | +that your updates don't actually get used when you expect them to! |
| 57 | + |
| 58 | + |
| 59 | +## Step 4: Configure the variables and run the application! |
| 60 | +Next we will do a one-time set up of some basic objects on the cluster in order to |
| 61 | +supply configuration settings to the application. |
| 62 | + |
| 63 | +```bash |
| 64 | +# Create a service account that we can use to deploy using the Restricted SCC |
| 65 | +oc apply -f ./yaml/sa-pod-deployer.yaml |
| 66 | + |
| 67 | +# Create a config map containing the details of your queue manager. |
| 68 | +# |
| 69 | +# If your queue manager is in the same OpenShift cluster then the hostname will be the |
| 70 | +# name of the "service". |
| 71 | +oc create configmap qmgr-details \ |
| 72 | + --from-literal=HOSTNAME=mydynamichostname \ |
| 73 | + --from-literal=PORT=34567 \ |
| 74 | + --from-literal=QMNAME=QM100 \ |
| 75 | + --from-literal=CHANNELNAME=SYSTEM.DEF.SVRCONN |
| 76 | + |
| 77 | +# If necessary, create a secret to hold the username and password your application should |
| 78 | +# use to authenticate to the queue manager. |
| 79 | +oc create secret generic qmgr-credentials \ |
| 80 | + --from-literal=USERNAME=appuser100 \ |
| 81 | + --from-literal=PASSWORD='password100' |
| 82 | +``` |
| 83 | + |
| 84 | +Now run the application! |
| 85 | +```bash |
| 86 | +# Before you run this command, be sure to update the "image" attribute on |
| 87 | +# line 8 of pod-sample.yaml so that it matches your image tag version from step 3. |
| 88 | +oc apply -f ./yaml/pod-sample.yaml --as=my-service-account |
| 89 | +``` |
| 90 | + |
| 91 | +Congratulations - and happy messaging! |
0 commit comments