Mon Nov 19 2018
Copied to clipboard! Copy reply
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
This readme will give a brief insight on how to dockerize an app
and deploy it our kubernetes structure.
Each section contains a link to a more detailed guide, which you should
probably read, and use this readme for reference when you forget how to do something.  

----------  

## —— Docker —— ##
  

### Dockerizing Apps

#####A summary of https://jira.trstone.com/wiki/pages/viewpage.action?spaceKey=DO&title=How+to+Dockerize+an+app

1. You will need docker installed in your local
2. You will need `ruby` and `ruby-bundler`
3. You will need to install https://github.com/asdf-vm/asdf
3. A Dockerfile contains instructions to build your Docker Image
4. Your docker image contains your project code (code, programs like Node, npm, etc)
5. We have some base images you can use: https://stash.trstone.com/projects/DOCKER
6. You may need to add this to your path: `export PATH=$PATH:/home/<username>/.gem/ruby/2.5.0/`
7. copy config dir with rs_docker_builder yaml file to your project  

#### Useful Docker commands

`$ docker build -t <username>/<name>`
`$ docker run <image>` - take an image and create a container from it and start it
`$ docker stop <name|id>` - stops a container
`$ docker start <name|id>` - starts a container
`$ docker ps [-a include stopped containers]` - shows containers
`$ docker rm <name|id>` rm a container 

  
  

----------

  

## &mdash;&mdash; Kubernetes &mdash;&mdash; ##
### Install & Setup

##### A summary of https://jira.trstone.com/wiki/display/DO/Kubernetes

1. Install `kubectl` (https://kubernetes.io/docs/tasks/tools/install-kubectl/)
2. install `jq` on your system. (json parsing tool)
3. Install handy `kubectl` shortcuts: https://jira.trstone.com/wiki/display/DO/Kubernetes
4. `mkdir -p ~/projects/dep/ && cd $_ && git clone ssh://git@stash.trstone.com/dep/local-dev.git`
5. `git clone ssh://git@stash.trstone.com/dep/local-dev.git`
6. If you're logged in to your machine as something different than your AD login, add `export ACTIVE_DIRECTORY_USERNAME=<your ad username>` to your .zshrc.local file. Otherwise go on to the next bullet.
7. `kauth all` to authenticate
8. https://jira.trstone.com/wiki/pages/viewpage.action?pageId=140210228 for other auth methods.

  

#### handy kubectl commands (rs shortcuts)

- `kc 1` - not sure yet
- `kgal marketing-api` - will show pods
- `kd <pod name>` - describe a pod (show info)
- `ki services/marketing-api` - inspect a pod
- `k port-forward service/marketing-api 5001:80`  access your jenkins-deployed app locally via localhost
	- replace 5001 with the port your app listens on (ie, index.js)

- `kips` shows IPs where you app might be residing
- `$ k get pods --all-namespaces | grep -e NAME -e <your_app>` 
  
  
  
### Deleting pods and resources
Running `k get pods --all-namespaces | grep -e NAME -e appname`  
 will return something like   

    NAMESPACE   NAME                          READY     STATUS     RESTARTS   AGE
    qa1         appname-qa1-5c5795877-6q64d    1/1      Running    0          22m

----------

`k delete deploy appname-qa1 -n qa1` Will delete the pod...

However, it there are still other resources running. If you don't
clean the up, the pod will eventually restart.

Run `k get svc,ingress,pods,hpa -n qa1 | grep appname`  
and you will see some output of the other resources.

0. delete pod as mentioned above, with `k delete deploy appname-qa1 -n qa1`
1. `k delete svc appname -n qa1`
2. `k delete ingress appname-qa1 -n qa1`
3. `k delete hpa appname-qa1 -n qa1`

To check if everything was cleaned up, run `$ k get all -n qa1 | grep appname`.  
Should return empty


### Quick steps for dockerizing & kubernetizing an app

##### you are going to use gaia-progress-tracker as a starting point.

1. Clone https://stash.trstone.com/projects/PLAT/repos/gaia-progress-tracker/browse
2. Copy these files from gaia-progress-tacker folder into your app:
3. `bin/ci` - tells jenkins how to build your app in CI. No changes.
4. `bin/update_schema` - change `project_name` and `project_abbr` - only if your project has a db. that line just runs a db migration to the given sql database. comment out sequelize line otherwise
5. `config/rs_docker_builder.yaml` Main app config. Tells docker how to build img. 
   - Change `internal_port: 5000` to the port your node app listens on
6. `docker/docker-compose.yaml` - change `gaia-progress-tracker` to your app's name
7. `jenkinsfile` - change appName from `gaia` to your app's name
8. `.npmrc` - rs npm registry
9. In your project's `package.json`, you'll need these deps:
	- "@rosetta/crypto-cli": "1.0.8"
	- "@rosetta/node-helpers": "2.0.3"
	- "@rosetta/sequelize-migrator": "^1.2.0" (if you run a db)
10. Your node app needs to handle two endpoints (return 200 or 400):
	- `/api/status/health_check` k8n checks if the pod is ready and should be put in service
	- `/api/status/` k8n checks to see if the pod is alive and kubernetes will restart the pod if it fails

  

##### kubectl config set-context "${USER}-CLUSTER-kubernetes" --namespace="RS_ENVIRONMENT"

##### kubectl config use-context "${USER}-CLUSTER-kubernetes"

  
  

##### kubectl config set-context "bfranchi-CLUSTER-kubernetes" --namespace="RS_ENVIRONMENT"

##### kubectl config use-context "bfranchi-CLUSTER-kubernetes"

  
  

##### if app writes logs to /app/log, it goes to https://splunk.trstone.com/en-US/account/login?return_to=%2Fen-US%2Fapp%2FDockerSearch%2Fsearch%3Fq%3Dsearch%2520index%253Ddockerdev%2520source%253D%252Aqa1%252Amarketing-api%252A%26sid%3D1531168531.2246182_0FB66084-2C65-4756-8CF0-37C050E6A08E%26display.page.search.mode%3Dsmart%26dispatch.sample_ratio%3D1%26earliest%3D%2540d%26latest%3Dnow