This project was was created to provide a test container and set of deployment scripts to allow hands on lab work deploying the application into a Kubernetes cluster. This project helps re-enforce classroom learning provided through the PowerPoint slides and instructor lectures. As part of this project, students will:
- Review the configuration (YAML) files that make up a Kubernetes deployment
- Understand the Dockerfile configuration and what it does
- Observe the build of a Docker image and deployment to Docker Hub
- Log into the C2 provisioned Google Cloud Kubernetes cluster to deploy the application
- Upgrade and scale the application via configuration
- Observe various kubectl commands and monitoring capabilities built into the platform
Examples below are shown using my howieavp76 Docker Hub account:
- ng build --prod //compiles the Angular app to static code in the /dist folder
- docker image build -t howieavp76/k8-training-c2 . //builds the Docker image
- docker image build -t howieavp76/k8-training-c2:v1.1 . //builds with a tag name
- docker push howieavp76/k8-training-c2 //push to docker hub image repository
- docker run -p 3000:80 howieavp76/k8-training-c2:v1.1 //runs the Docker container with the specified tag
NOTE: Login first to Docker Hub
https://atlasc2.blob.core.windows.net/kub8-c2labs/k8training-deployment-student12.yaml
The deployment will be done by executing kubectl commands inside the Google Cloud Shell. The steps are as follows:
Log into Kubernetes Engine on Google Cloud Platform:
- Navigate to the Google Cloud Platform URL
- You will need a Google account (or to create a new one for this Lab)
- Once registered, navigate in the menu to Compute -> Kubernetes Engine
- Find the entergy-k8-c2 cluster and click "Connect"
- Click "Run in Cloud Shell" to open the CLI prompt
- If prompted, click "Start Cloud Shell"
- It should load a command to start the shell, press Enter to execute the command
- You should now have interactive login to Google Cloud and the Kubernetes cluster
Deploy our demo app into the Kubernetes cluster in a highly available configuration with 3 nodes:
- Access the deployment configuration file at https://atlasc2.blob.core.windows.net/kub8-c2labs/k8training-deployment.yaml
- Each student will have their own file; for example; https://atlasc2.blob.core.windows.net/kub8-c2labs/k8training-deployment-student1.yaml
- We will number off in class and you will grab the appropriate file for your number (up to 12 available)
- Execute the following command in Google Cloud Shell: kubectl apply -f https://atlasc2.blob.core.windows.net/kub8-c2labs/k8training-deployment-student#.yaml
- The console should show the deployment created along with two services
- Deployment - will create the specified number of pods
- Service - allows internal traffic in the cluster
- Load Balancer Service - creates external IP and load balances requests evenly across the pods created
- Verify your pods are running: kubectl get pods
- Verify your services are running: kubectl get services
- On the services, find the external IP of the Load Balancer service in the console log
- Paste the IP into your browser to see the running app
- Do the #HappyDance if it worked
Upgrade the container image to use the 2.0 version:
kubectl set image deployment/training-deployment training=howieavp76/k8-training-c2:v2.0
Scale the deployment up to handle the increased anticipated load
kubectl scale deployment training-deployment --replicas=5
Scale the deployment down to handle steady state traffic now that the surge is over
kubectl scale deployment training-deployment --replicas=3
NOTE: Do not set the number of replicas below 3. This will violate the minimum availability requirement and you will see a warning in GCP.
You can run into some tricky issues that are not well explained online or in any tutorials. A few of these issues are shown below:
- Just deploying your container into pods won't work. You have to create the load balancer service to externally expose the IP so you can reach it from outside the Kubernetes cluster.
- Not just any URL can be used to point at the YAML file. In particular, you cannot use a GitHub link to provide the YAML file. If you do, it throws the following error:
error: error parsing https://github.com/howieavp76/k8-training-c2/blob/master/k8training-deployment.yaml: error converting YAML to JSON: yaml: line 616: mapping values are not allowed in this context
- This error appears to happen on any GitHub YAML related link. Copying the same file to a S3 bucket or Azure blob resolves the issue.
- YAML formatting errors can be really hard to see and debug. If your deploy fails due to a format/syntax issue, it can take forever to find it. Online linting tools really help. For example, you can go to YAMLLINT for an online validator of your YAML format.
- In the Google Cloud Shell, if you paste commands they sometimes execute immediately or do not work as intended. Sometimes just retyping them removes some weird special characters or spacing that gets inserted on a paste.