DEV Community

Cover image for Deploy Angular application on Kubernetes
Saiprasad Tirumala
Saiprasad Tirumala

Posted on

Deploy Angular application on Kubernetes

Prerequisites:
Nodejs Installed.
Docker Installed.
Kubernetes Installed.

Step 1: Create an angular application
Install angular CLI using below command

npm install -g @angular/cli
Enter fullscreen mode Exit fullscreen mode

Below command helps you to create an Angular application.

ng new hello-world
Enter fullscreen mode Exit fullscreen mode

Alternatively you can clone from my repository link

Now go inside the hello-world directory and run the local server using below command.

ng serve
Enter fullscreen mode Exit fullscreen mode

By default application runs on port number 4200.You can test in your browser using this link

Step 2: Build the application in production environment
Run the below command which creates the dist folder in your root directory.

ng build --prod
Enter fullscreen mode Exit fullscreen mode

Image description

Step 3: Create a DockerFile
We need to copy the files from dist folder to nginx path.

FROM nginx:alpine
COPY ./dist/hello-world ./usr/share/nginx/html
Enter fullscreen mode Exit fullscreen mode

Step 4: Build the DockerFile
Navigate to the directory where you created the Dockerfile and enter the below command.

docker build -t angular/hello-world:v1 .
Enter fullscreen mode Exit fullscreen mode

Image description
Once the build process is completed, You can check the docker images with below command.

docker ps -a
Enter fullscreen mode Exit fullscreen mode

Step 5: Create a kubernetes Deployment Pod
You can get deployment file from my github repository link

kubectl apply -f deployment.yaml
Enter fullscreen mode Exit fullscreen mode

deployment.yaml

Explanation for above YAML in short:

  • Sample template for Deployment file can be found in official site
  • apiVersion defines the version of kubernetes.
  • kind indicates the type if it is a Deployment, Service, Job etc.
  • metadata is the deployment name.
  • Under spec.template.spec.containers, we need to mention the name of the docker image. In this example, we haven't pushed Docker image to its registry. So we need to mention imagePullPolicy: Never. If it is not mentioned, kubernetes tries to pull the image from the docker registry.
  • containerPort tells in which port our deployment needs to run.

Step 6: Create a Service
You can get service file from my github repository link
Enter the below command to create a service pod.

kubectl apply -f service.yaml
Enter fullscreen mode Exit fullscreen mode

service.yaml

Service pod helps in connecting to the application based on the deployment name mentioned under spec.selector.app

Verify the deployments

kubectl get all
Enter fullscreen mode Exit fullscreen mode

Image description

Step 7: Final Step - Port Forwarding
This helps in forwarding our requests to our application

kubectl port-forward <service-name> <expose port no>:<service port num>
Enter fullscreen mode Exit fullscreen mode

Image description
Now go to your browser and check by hitting (http://localhost:80). You should able to access your web application.

Top comments (2)

Collapse
 
vnsanilkumar profile image
vnsanilkumar

Don't we need to make changes for nginx.conf file?

Collapse
 
saiprasad2595 profile image
Saiprasad Tirumala

Not necessary, unless you want to specify any configuration.