This lab will show how to use Kubernetes Init Containers to run scripts and load data prior to the initialization of the primary container.
I'm not going to lie, things are going to get a little weird.
We are going to pull data from a remote source(Refer to initContainers section in init-web-api-sample.yaml):, and then populate the database(mongodb) from that source file. All before the primary container(api container) is initialized.
I told you it was going to be weird.
For reference, take a look at the Kubernetes.io docs on Init Containers by clicking here
Take note of the two files in this directory, init-db-sample.yaml and init-web-api-sample.yaml. We'll use these as our reference points for deployment.
Perform below steps in the Jumpbox
-
Clear anything out of your cluster by deleting your deployments
kubectl delete -f heroes-db.yaml kubectl delete -f heroes-web-api.yaml kubectl delete -f heroes-web-api-ingress.yaml
-
View the init-db-sample.yaml in the
helper-files
directory. We have the definitions to deploy the publically available the mongo db image -
Deploy the db to your cluster with the db yaml
cd ~/container-bootcamp/labs/helper-files kubectl create -f init-db-sample.yaml
-
Modify the init-web-api-sample.yaml to add your imagePullSecrets as well as reference your container registry and image,
note that you only need to add your login info to the top level container(s) - the init containers pull from docker hubs public repo
change
name: heroes-api spec: containers: - image: sonofjorel/rating-api:v1
to
name: heroes-api spec: imagePullSecrets: - name: acr-secret containers: - image: <login server>/azureworkshop/rating-api:v1
and change
name: heroes-web spec: containers: - image: sonofjorel/rating-web:v1
to
name: heroes-web spec: imagePullSecrets: - name: acr-secret containers: - image: <login server>/azureworkshop/rating-web:v1
-
Look at the initContainers section in the init-web-api-sample.yaml file. In here we can see commands to download the data(heroes.json, sites.json, ratings.json) from the github repo to the data-dir folder.
In the same section we can see shell commands to import data (heroes.json, sites.json, ratings.json) into the webratings database of mongodb. Unlike previous lab exercises where we ran import.sh inside the mongo db pod, here the data is imported by a init container. This ensures data is present in database well before the init container is initialized.
-
Deploy the web and api app to your cluster with the modified yaml
kubectl create -f init-web-api-sample.yaml
-
See the init containers getting creating one after the other by executing the below command
kubectl get pods --watch
NAME READY STATUS RESTARTS AGE heroes-api-deploy-cd5858bbc-k2qkh 0/1 Init:4/6 3 18s heroes-db-deploy-c8b656bf9-p7jb4 1/1 Running 0 2m heroes-web-deploy-d684ff8f6-ptrhd 1/1 Running 0 18s heroes-api-deploy-cd5858bbc-k2qkh 0/1 Init:5/6 3 18s heroes-api-deploy-cd5858bbc-k2qkh 0/1 Init:5/6 3 19s heroes-api-deploy-cd5858bbc-k2qkh 0/1 PodInitializing 0 20s heroes-api-deploy-cd5858bbc-k2qkh 1/1 Running 0 22s
Use Ctrl+C to exit
-
Lookup the available service "web"
kubectl get services | grep web
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE web LoadBalancer 10.0.125.16 13.68.178.31 8080:31707/TCP 25m
-
Access the web app using it's EXTERNAL-IP on Port 8080 and see if it works as expected