Skip to content

Commit

Permalink
Merge pull request #172 from BCDevOps/pod-life-cycle-updates
Browse files Browse the repository at this point in the history
Improve pod lifecycle instructions
  • Loading branch information
mtspn authored Oct 28, 2022
2 parents 52d760e + 66ced73 commit e8f31b5
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 21 deletions.
90 changes: 69 additions & 21 deletions 101-lab/content/15_pod_lifecycle.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,68 @@ A Pod can be extended beyond the normal operation of the container by allowing d
- modify the default `entrypoint` of a container

## Init Containers
Init containers are specialized containers that run before app containers in a pod. Init containers can contain utilities or setup scripts not present in an app image.

Init containers are best used to prepare the pod for normal operation. In this lab, you will add a simple init container that posts a message to rocketchat with your pod hostname.

__Objective__: Create an init container
- From the Web Console, navigate to `Topology` and select your `rocketchat-[username]` deployment.
- Open your Rocketchat web application from its web route, ensuring to log in as the admin user that your previously created.

- Click on the three-dot icon to see more menu options, and choose 'Administration'

![](./images/15_pod_lifecycle_03.png)

- Using the left navigation menu, select 'Integrations' and choose the 'New Integration'

![](./images/15_pod_lifecycle_04.png)

- Next, select the option to create an 'Incoming WebHook'

![](./images/15_pod_lifecycle_05.png)

- Configure the WebHook. Be sure to set 'enabled' to 'true', choose the account name you set earlier, choose the default #general channel. Optionally, you can link to an avatar image or set and emoji as the avatar. Be sure to save the settings at the bottom right of the page, and confirm that no errors appear at the top right of the page.

![](./images/15_pod_lifecycle_06.png)

- Once these settings are saved, navigate to the 'Integrations' page again and take note of the webhook URL that is generated.

![](./images/15_pod_lifecycle_07.png)

- Next the OpenShift Web Console, navigate to `Topology` and select your `rocketchat-[username]` deployment.
- Navigate to the __YAML__ tab.
> If you wish to perform this from the cli with the `oc` tool, type `oc edit deployment/rocketchat-[username]`

![](./images/12_pod_lifecycle_01.png)

- Add the following section of code under `spec: -> template: -> spec:`

```
initContainers:
- name: init
image: giantswarm/tiny-tools
command: ["/bin/sh", "-c", "c=$(curl -X POST -H 'Content-Type: application/json' --data '{\"text\":\"Say Hello\"}' https://chat.pathfinder.gov.bc.ca/hooks/xxx/xxx)"]
- After replacing the URL below with the webhook URL from the previous step, add the following section of code under `spec: -> template: -> spec:`. As always with YAML, pay close attention to the formatting and indenting.

```YAML
initContainers:
- name: init
image: giantswarm/tiny-tools
command:
- /bin/sh
- '-c'
- >-
c=$(curl -X POST -H 'Content-Type: application/json' --data
'{"text":"Say Hello"}'
http://[YOUR_WEBHOOK_URL])
resources: {}
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
imagePullPolicy: Always
```
- Select Save
- Ask the instructor to ensure the rocketchat instance is displayed to the class
- Save your changes to the YAML. It should look similar to this:
![](./images/15_pod_lifecycle_08.png)
- Now that you've added this init container, every time one of your Rocketchat pods initializes, the 'Say Hello' message will be posted to your #general channel on Rocketchat web application.
![](./images/15_pod_lifecycle_09.png)
- Explore the `Pod Details` to notice the difference with the Init Container

![](./images/12_pod_lifecycle_02.png)
Expand All @@ -37,34 +78,41 @@ oc -n [-dev] logs rocketchat-[username]-[pod-id] -c init
```

## Lifecycle Hooks
Lifecycle hooks can be configured to start and stop a container properly. The lifecycle hook is tied directly to each container. Add a similar pre and post hook as the `initContainer` to demonstrate when it executes in your rocketchat deployment.
Lifecycle hooks can be configured to start and stop a container properly. The lifecycle hook is tied directly to each container. Next we will add a similar pre and post hook as the `initContainer` to demonstrate when it executes in your rocketchat deployment.

- From the Web Console, navigate to the `rocketchat-[username]` deployment and click on `YAML` tab
- If you wish to perform this from the cli with the `oc` tool, type `oc edit deployment/rocketchat-[username]`
- Add the following section of code under `spec: -> template: -> spec: -> containers`
```
lifecycle:
- After replacing both URLs below with the webhook URL from the earlier step, as well as replacing $HOSTNAME with the Add the following section of code under `spec: -> template: -> spec: -> containers`. Again, pay careful attention to the YAML indentation.
```YAML
lifecycle:
postStart:
exec:
command: ["/bin/sh", "-c", "c=$(curl -X POST -H 'Content-Type: application/json' --data '{\"text\": \"'\"$HOSTNAME\"' is at the postStart phase, huzzah! \"}' https://chat.pathfinder.gov.bc.ca/hooks/xxx/xxx)"]
command: ["/bin/sh", "-c", "c=$(curl -X POST -H 'Content-Type: application/json' --data '{\"text\": \"'\"$HOSTNAME\"' is at the postStart phase, hooray! \"}' http://YOUR_WEBHOOK_URL)"]
preStop:
exec:
command: ["/bin/sh", "-c", "c=$(curl -X POST -H 'Content-Type: application/json' --data '{\"text\": \"'\"$HOSTNAME\"' is just about to STOPPPPPP! \"}' https://chat.pathfinder.gov.bc.ca/hooks/xxx/xxx)"]
command: ["/bin/sh", "-c", "c=$(curl -X POST -H 'Content-Type: application/json' --data '{\"text\": \"'\"$HOSTNAME\"' is just about to STOPPPPPP! \"}' http://YOUR_WEBHOOK_URL)"]
```
- Select Save
- Ask the instructor to ensure the rocketchat instance is displayed to the class
- Save your changes to the YAML. It should now look similar to this:

![](./images/15_pod_lifecycle_10.png)

## Overriding the Entrypoint
It may be necessary, from time to time, to override the initial command/entrypoint of a container image. Generally this is used for troubleshooting purposes, or to override a vendor provided image.

- From the Web Console, navigate to the `rocketchat-[username]` deployment and click on `YAML` tab
- If you wish to perform this from the cli with the `oc` tool, type `oc edit deployment/rocketchat-[username]`
- Add the following section of code under `spec: -> template: -> spec: -> containers`
- After replacing $HOSTNAME and your WebHook URL, add the following section of code under `spec: -> template: -> spec: -> containers: -> resources`

```YAML
command: ["/bin/sh", "-c", "c=$(curl -X POST -H 'Content-Type: application/json' --data '{\"text\": \"'\"$HOSTNAME\"' is AN OVERRIDING COMMAND! \"}' https://chat.pathfinder.gov.bc.ca/hooks/xxx/xxx)"]
```
command: ["/bin/sh", "-c", "c=$(curl -X POST -H 'Content-Type: application/json' --data '{\"text\": \"'\"$HOSTNAME\"' is AN OVERRIDING COMMAND! \"}' https://chat.pathfinder.gov.bc.ca/hooks/xxx/xxx)"]
```

Your rocketchat deployment YAML should look similar to this (some sections have been collapsed for easier viewing):

![](./images/15_pod_lifecycle_11.png)


Be sure to save the changes to your YAML. Then:
- Take note of the pattern that will happen in the rocketchat notification screen
- Remove the previous command to enable the rocketchat instance to start properly again

Expand Down
Binary file added 101-lab/content/images/15_pod_lifecycle_03.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 101-lab/content/images/15_pod_lifecycle_04.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 101-lab/content/images/15_pod_lifecycle_05.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 101-lab/content/images/15_pod_lifecycle_06.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 101-lab/content/images/15_pod_lifecycle_07.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 101-lab/content/images/15_pod_lifecycle_08.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 101-lab/content/images/15_pod_lifecycle_09.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 101-lab/content/images/15_pod_lifecycle_10.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 101-lab/content/images/15_pod_lifecycle_11.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit e8f31b5

Please sign in to comment.