Deploy your first application
Learn how to deploy an application to a Kubernetes cluster.
Once your client is configured, you are ready to deploy your first application, k8sdemo
.
💡 INFO: The frontend example application
In this part of the lab we will deploy an application called k8sdemo
that has already been built and uploaded to DockerHub under the name niklaushirt/k8sdemo
.
We will use the following yaml:
kind: Deploymentmetadata:name: k8sdemonamespace: defaultspec:replicas: 1template:metadata:labels:
It defines the Container image to deploy (with 1 instances/replicas) and an environment variable BACKEND_URL that points to the backend servcie we will be deploying in the next section.
🚀 TASK: Deploy the frontend application
Start by running
k8sdemo
kubectl create -f ./deployment/demoapp.yamlThis action will take a bit of time. To check the status of the running application, you can use
kubectl get pods -n defaultYou should see output similar to the following:
> NAME READY STATUS RESTARTS AGE> k8sdemo-7d46f69d68-bd2cw 0/1 Running 0 17s
Eventually, the status should show up as
1/1 Running
.kubectl get pods -n default> NAME READY STATUS RESTARTS AGE> k8sdemo-7d46f69d68-bd2cw 1/1 Running 0 5mThe end result of the run command is to create a Deployment resource that manages the lifecycle of those pods.
Once the status reads
Running
, we need to expose that deployment as a service so we can access it through the IP of the worker nodes. Thek8sdemo
application listens on port 3000.Run:
kubectl expose deployment k8sdemo --name k8sdemo-service -n default --type="NodePort" --port=3000> service "k8sdemo-service" exposedTo find the port used on that worker node, examine your new service:
kubectl get service -n default k8sdemo-service> NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE> k8sdemo-service NodePort 10.109.30.227 <none> 3000:30931/TCP 11mWe can see that our in this example
<nodeport>
is31208
. We can see in the output the port mapping from 3000 inside the pod exposed to the cluster on port 30931. This port in the 31000 range is automatically chosen, and will probably be different for you.k8sdemo
is now running on your cluster, and exposed to the internet.
You can open the webpage by forwarding a local port to the service in your Cluster:
kubectl port-forward --namespace default $(kubectl get po -n default -l app=k8sdemo | grep k8sdemo | \awk '{print $1;}') 3000:3000And then navigating to http://localhost:3000/ in your browser.
Your
k8sdemo
should now be open in the your default browser. However it will show an error, because we have not yet deployed the backend.Testing DEMO_API STATUS: ERROR Trying to reach backend ….
Hit
CTRL-C
several times to cancel the port forward
🚀 TASK: Deploy the application backend
In this part of the lab we will deploy the application backend called k8sdemo-backend
that has already been built and uploaded to DockerHub under the name niklaushirt/k8sdemo-backend
.
Start by running
k8sdemo-backend
kubectl create -f ./deployment/demoapp-backend.yamlThis action will take a bit of time. To check the status of the running application, you can use
kubectl get pods
.You should see output similar to the following:
kubectl get pods -n default> NAME READY STATUS RESTARTS AGE> k8sdemo-7d46f69d68-xcgcw 0/1 Running 0 13m> k8sdemo-backend-9c777544b-cp59q 0/1 Running 0 1m> k8sdemo-backend-9c777544b-jqjz9 0/1 Running 0 1mEventually, the status should show up as
1/1 Running
.kubectl get pods -n default> NAME READY STATUS RESTARTS AGE> k8sdemo-7d46f69d68-xcgcw 1/1 Running 0 13m> k8sdemo-backend-9c777544b-cp59q 1/1 Running 0 6m35s> k8sdemo-backend-9c777544b-jqjz9 1/1 Running 0 6m35sThe end result of the run command is to create a Deployment resource that manages the lifecycle of those pods.
Once the status reads
Running
, we need to expose that deployment as a service so we can access it through the IP of the worker nodes. Thek8sdemo
application listens on port 3000. Run:kubectl expose deployment k8sdemo-backend --name k8sdemo-backend-service -n default --type="NodePort" --port=3000> service "k8sdemo-service" exposedForward the local port again to the frontend service in your Cluster:
kubectl port-forward --namespace default $(kubectl get po -n default -l app=k8sdemo | grep k8sdemo | \awk '{print $1;}') 3000:3000And then navigating to http://localhost:3000/ in your browser.
Now reload the webpage and verify, that it shows
Testing DEMO_API STATUS: OK ….
If you reload the webpage several times, you should see, tht the IP Address of the backend API Pod is changing between the two Pods that have been spun up.
Hit
CTRL-C
several times to cancel the port forward