Below are the steps that we'll be using to deploy a complete web app. Note that this demo will deploy a broken application and we'll be fixing the Kubernetes yaml documents to make the deployment successful.
First we'll be:
- Deploying a new Namespace for our application
- Deploying a redis server into the new namespace
- Reviewing the objects that we've deployed with Kubernetes (Deployments, Pods)
- Getting Logs from the deployed resources via
kubectl
- Create namespace in Kubernetes
kubectl apply -f namespace.ymlUseskubectltoapplythe file (-f) with theNamespacedefinition. Note that the namespace we're deploying into is defined in the yaml documents in01_redis/.
- Deploy Redis Master and Replica
kubectl apply -f 01_redis/Useskubectltoapplyallyamldefinitions in the folder01_redis/.kubectl get deploymentsGets thedeploymentresources from Kubernetes in thedefaultnamespace. You will not see the redis deployments since we have deployed them to a different namespace.kubectl get podsGets the pods in thedefaultnamespace. You won't see the redis pods since they've been created in another namespace.kubectl get --namespace k8s-workshop deployments,podsGets thedeploymentsandpodswithkubectlin thek8s-workshopnamespace. You should now see that adding--namespace k8s-workshopwill scope yourgetrequest to thek8s-workshopnamespace.kubectl describe --namespace k8s-workshop pod <pod name>Describes the current state of the<pod name>pod in Kubernetes. You can copy paste the name of the pod where you see<pod name>.kubectl get --namespace k8s-workshop servicesThis lists all theservicedefinitions in thek8s-workshopnamespace.kubectl logs --namespace k8s-workshop <pod name>This prints the pods (containers)logsto your terminal. You can paste any pod name in thek8s-workshopnamespace to replace<pod name>.
We should now see a healthy Redis Master and Replica in the k8s-workshop namespace in Kubernetes.
Next we'll be:
- Deploying the web app into the
defaultnamespace (A different namespace from the one we created above) - Watching the Cloud Load Balancer create external access to the web app
- CURLing the newly deployed web app to test manually
- Looking at the logs of the web app
- Fixing the broken web app deployment be deploying to the correct namespace
- CURL the web app again to test
- Deploy the basic webapp
kubectl apply -f 02_webapp/This deploys all the yaml definitions in the02_webapp/folder. Note that we are not defining the namespace in themetadataof the yaml, so it will default to thedefaultnamespace configured withkubectl.kubectl get servicesGets all theservicedefinitions in thedefaultnamespace. We are going to wait untilExternal IPhas an IP Address.
- Test the web app
curl [external_ip]This should show a hello world type response.curl [external_ip]/asdf/1234When youcurlthis website, you should experience an error connecting to Redis.kubectl logs <webapp pod name>Let's look at the errors in the logs from the pod... Cannot connect to redis because it's default namespace DNS resolution won't work from a different namespace.
- Fix the broken deployment
kubectl delete -f 02_webapp/This will delete all the objects we created so that we can deploy correctly into thek8s-workshopnamespace.kubectl apply -f 02_webapp/ --namespace k8s-workshopThis overrides the unsetdefaultnamespace and deploys all the yaml files into thek8s-workshopnamespace.kubectl get services --namespace k8s-workshopWe need to see the new service we've created, so the Load Balancer IP will have changed.
- Test the new deployment
curl [external_ip]/asdf/1234Now we see that the web app correctly saves the key and value to the database.curl [external_ip]/asdfWe can also query the value from the database.
This section allows us to load test the web app and see the cluster responding by scaling the pod count up.
- Apply some load
./load.sh - Open a new terminal or tab in your shell
- Look at the hpa occasionally to see the cpu usage go up
kubectl get hpa -n k8s-workshop - The number of pods should scale up to meet the new demand
kubectl get deployment, po -n k8s-workshop
The 03_ingress sub-directory contains an optional Ingress definition. The kubernetes.io/ingress.class annotation may need to be changed to match that of your Ingress Controller. There is a second v1.18 yaml file for Kubernetes versions v1.18.0 and earlier, which do not support the networking.k8s.io/v1 APIVersion of Ingress.
Note that an Ingress Controller must be installed for Ingress resources to function. See the Ingress Nginx installation instructions for more information.