Deploy a Sample Application

In this exercise, we will deploy a sample application in Kubernetes and access it.

  1. Create webapp.yaml file with contents below. This file defines a Kubernetes deployment for your Sample Application.
apiVersion: apps/v1
kind: Deployment
metadata:
  name: webapp
spec:
  replicas: 2
  selector:
    matchLabels:
      app: webapp
  template:
    metadata:
      labels:
        app: webapp
    spec:
      containers:
      - image: nginx
        name: nginx
  1. Deploy the application:
kubectl apply -f webapp.yaml
  1. Verify the deployment:
kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
webapp 2/2 2 2 89s
  1. Set up port forwarding to access the application. In this step, we are establishing a port forwarding rule that redirects network traffic from a specific port on your local machine to the corresponding port on the Kubernetes pod hosting the server. By doing so, you enable direct access to the server via http://localhost:8080 from your local computer. This action bridges the network gap between your local environment and the isolated Kubernetes pod, allowing you to test and interact with the deployed application as if it were running locally. It’s important to run this in a new terminal tab to keep the port forwarding active throughout your testing session.
kubectl port-forward deploy/webapp 8080:80
Forwarding from 127.0.0.1:8080 -> 80
Forwarding from [::1]:8080 -> 80
  1. Test the sample application by sending an HTTP request.
curl http://localhost:8080/
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
html { color-scheme: light dark; }
<Output Truncated>
  1. Cleanup by deleting the deployment:
kubectl delete deploy webapp