Project setup
This contains the local machine project setup instructions
Project structure
The project has a master repo called
adidas-k8sEach microservice is in its own repo and connected to the master repo through git submodule
Folder infra contains k8s configuration files
Share code is stored inside adidas-common-lib
File structure architecture
Every microservice follows these guidelines
Localhost development setup
Install essential dependencies
Minikube
Docker
npm
pip3
Install minikube
Install kubectl
sudo snap install kubectl --classicRun
minikube startRun
minikube ipthen add the ip address to /etc/hosts with a custom url. Runsudo vim /etc/hosts. Then addx.x.x.x www.testadidas.ioCreate the postgres credentials on your local machine
# create postgres user
sudo -u postgres createuser adidasuser
# create dbs
sudo -u postgres createdb adidasemail
sudo -u postgres createdb adidasauth
sudo -u postgres createdb adidassub
# change user password
sudo -u postgres psql
alter user adidasuser with encrypted password 'superP4WORD$';
GRANT ALL PRIVILEGES ON DATABASE adidasauth TO adidasuser;
GRANT ALL PRIVILEGES ON DATABASE adidassub TO adidasuser;
GRANT ALL PRIVILEGES ON DATABASE adidasemail TO adidasuser;
Add environment variables
kubectl create secret generic postgres-user --from-literal=POSTGRES_USER='adidasuser'
kubectl create secret generic postgres-password --from-literal=POSTGRES_PASSWORD='superP4WORD$'
kubectl create secret generic postgres-host --from-literal=POSTGRES_HOST='127.0.0.1'
kubectl create secret generic database-type --from-literal=DATABASE_TYPE='test'
kubectl create secret generic postgres-sub-db --from-literal=POSTGRES_DB='mydatabase'
kubectl create secret generic postgres-email-db --from-literal=POSTGRES_DB='mydatabase'
kubectl create secret generic postgres-auth-db --from-literal=POSTGRES_DB='mydatabase'
kubectl create secret generic api-version --from-literal=API_VERSION='v1'
kubectl create secret generic secret-key --from-literal=SECRET_KEY='mn871rqc=2v$omiosampfodasmfdbyp62c)4794#y@s4123214'
kubectl create secret generic debug --from-literal=DEBUG='true'
kubectl create secret generic node-env --from-literal=NODE_ENV='test'
Or, apply the secret object
kubectl apply -f foobarfolder/secrets.yamlif you happen to have a secrets.yaml fileClone main repo
git clone git@github.com:codephillip/adidas-k8s.gitInitialize submodules
git submodule update --init --recursiveWe shall use sqlite in memory db for testing, otherwise, create Persistent volumes (PV)and Claims (PVC) yaml files for a more consistent database. Add these environment variables to or .dev.env in express services. NOTE: Do this for all express services
TYPEORM_CONNECTION=sqlite
TYPEORM_DATABASE=mydatabase
TYPEORM_ENTITIES=dist/**/*.model.js
TYPEORM_MIGRATIONS=dist/migrations/*.js
TYPEORM_MIGRATIONS_DIR=src/migrations
TYPEORM_MIGRATIONS_RUN=true
TYPEORM_SYNCHRONIZE=true
TYPEORM_LOGGING=true
Also add database credentials to the .env file. This is required by Typeform. NOTE: Do this for all express services
DB_NAME=mydatabase.sqlite
DB_STORAGE=mydatabase.sqlite
Enable ingress.
minikube addons enable ingressReplace
us.gcr.io/sixth-loader-344609withcodephillipin the microservices deployment files ie inside infra/k8s/adidas-express-email-depl.yamlRun
skaffold dev -f skaffold_dev.yamlto start local minikube(k8s) tools
More setup instructions(optional)
Update submodules incase of updates
git pull --recurse-submodules && git submodule update --recursiveandgit submodule foreach git pull origin masterIncase postgres fails to start with error
Is the server running on host "auth-postgres-service" (x.x.x.x) and accepting TCP/IP connections on port 5432?run this command to check active portssudo lsof -i -P -n | grep LISTEN.Then Deactivate postgres
systemctl stop postgresqland disable autostartsystemctl disable postgresqlRun
skaffold dev -f skaffold_dev.yamlIncase databases(PV and PVC) are getting deleted on local development, run
skaffold dev -f skaffold_dev.yaml --cleanup=falseIncase of limited internet, use
skaffold dev -f skaffold_dev.yaml --cache-artifacts=trueOptional: Set your IDE autosave threshold to 15 seconds to prevent skaffold from auto building
Incase you get errors in skaffold while installing npm packages
unable to stream build output: Get https://registry-1.docker.io/v2/: dial tcp: lookup registry-1.docker.io. Run this commandminikube stop && minikube startorminikube delete && minikube startIncase you run out of space. Run list images
minikube ssh -- docker images -f dangling=truethen delete usingminikube ssh -- docker image pruneInstall npm packages to stop the IDE false errors
npm install npm install @adidastest-phillip/common
Note
Sometimes the skaffold dev tools may malfunction and stop accepting requests to and from the pods. Such an error may appear Error: getaddrinfo *EAI_AGAIN* xyz. This may occur during npm package installation or when the pod has fully deployed. Quick solution is to run minikube stop then minikube start. If all else fails run docker system prune and minikube ssh -- docker system prune
Local dev machine setup to push directly to production with skaffold
Make code changes and push to gitlab
Ask for permission to the GCP project from the lead developer
Install
gcloudon your local machineLogin to gcloud using
gcloud auth application-default loginAdd docker/k8s context by clicking connect button and copying the command
gcloud container clusters get-credentials adidasttestcluster --zone europe-west2-c --project sixth-loader-344609Set zone if necessary
gcloud config set compute/zone europe-west2-cAdd environment variables if not done so already
kubectl create secret generic postgres-user --from-literal=POSTGRES_USER='adidas-testdb-instance'
kubectl create secret generic postgres-password --from-literal=POSTGRES_PASSWORD='s2e7gvCdG3eGxvCJ'
kubectl create secret generic postgres-host --from-literal=POSTGRES_HOST='35.189.219.141'
kubectl create secret generic database-type --from-literal=DATABASE_TYPE='production'
kubectl create secret generic postgres-sub-db --from-literal=POSTGRES_DB='adidas-test-sub'
kubectl create secret generic postgres-email-db --from-literal=POSTGRES_DB='adidas-test-email'
kubectl create secret generic postgres-auth-db --from-literal=POSTGRES_DB='adidas-test-auth'
kubectl create secret generic api-version --from-literal=API_VERSION='v1'
kubectl create secret generic secret-key --from-literal=SECRET_KEY='mn871rqc=2v$e-z9$rvl1m3njf+0byp62c)4794#y@s4y8d3@^*y'
kubectl create secret generic debug --from-literal=DEBUG='false'
kubectl create secret generic node-env --from-literal=NODE_ENV='test'
Add these environment variables to .prod.env in express services. NOTE: Do this for all services
TYPEORM_CONNECTION=postgres
TYPEORM_DATABASE=adidas-test-email
TYPEORM_USERNAME=adidas-testdb-instance
TYPEORM_PASSWORD=s2e7gvCdG3eGxvCJ
TYPEORM_HOST=35.189.219.141
TYPEORM_ENTITIES=dist/**/*.model.js
TYPEORM_MIGRATIONS=dist/migrations/*.js
TYPEORM_MIGRATIONS_DIR=src/migrations
TYPEORM_MIGRATIONS_RUN=true
TYPEORM_SYNCHRONIZE=true
TYPEORM_LOGGING=true
Also add database credentials to the .env file. This is required by Typeform. NOTE: Do this for all services
# adidas test use one of the POSTGRES_DB shown above
POSTGRES_DB=adidas-test-foobar
POSTGRES_USER=adidas-testdb-instance
POSTGRES_PASSWORD=s2e7gvCdG3eGxvCJ
POSTGRES_HOST=35.189.219.141
Run
skaffold devif you want to monitor directly in your terminal. Otherwiseskaffold runworks best
Code Update(finally)
These are structions on how I run, test and update the code after it is properly setup on the local machine
Run
skaffold dev -f skaffold_dev.yamlif not done so alreadyEdit source files in repo
Since we are using skaffold, autoreload is enabled therefore no need to restart minikube(k8s)
Run tests
npm run testMonitor k8s resources with
minikube dashboardCheck for common library updates
npm update @adidastest-philip/commonSave changes by running
git add .,git commit -m "my message"and thengit push