Deploy Nginx Web Server on Kubernetes Cluster

Ticker

6/recent/ticker-posts

Deploy Nginx Web Server on Kubernetes Cluster

Question Some of the Nautilus team developers are developing a static website and they want to deploy it on Kubernetes cluster. They want it to be highly available and scalable. Therefore, based on the requirements, the DevOps team has decided to create deployment for it with multiple replicas. Below you can find more details about it:

Create a deployment using nginx image with latest tag only and remember to mention tag i.e nginx:latest and name it as nginx-deployment. App labels should be app: nginx-app and type: front-end. The container should be named as nginx-container; also make sure replica counts are 3.

Also create a service named nginx-service and type NodePort. The targetPort should be 80 and nodePort should be 30011.

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.


Please Note :-  Perform the below commands based on your question  server,  user name & other details might differ . So please read task carefully before executing. All the Best 👍

Solution:  

1. At first  check existing deployment and  pods running status   

    kubectl get namespace

    kubectl get pods


2.  Create YAML  file with all the parameters, you can copy form GitLab

    https://gitlab.com/nb-tech-support/devops.git

    Refer Below Video for more clarity )

thor@jump_host /$ vi /tmp/nginx.yml

thor@jump_host /$ cat /tmp/nginx.yml

apiVersion: v1

kind: Service

metadata:

  name: nginx-service

spec:

  type: NodePort

  selector:

    app: nginx-app

    type: front-end

  ports:

    - port: 80

      targetPort: 80

      nodePort: 30011

---

apiVersion: apps/v1

kind: Deployment

metadata:

  name: nginx-deployment

  labels:

    app: nginx-app

    type: front-end

spec:

  replicas: 3

  selector:

    matchLabels:

      app: nginx-app

      type: front-end

  template:

    metadata:

      labels:

        app: nginx-app

        type: front-end

    spec:

      containers:

        - name: nginx-container

          image: nginx:latest

thor@jump_host /$

 3. Run below command to create pod 

 thor@jump_host /$ kubectl create -f /tmp/nginx.yml

service/nginx-service created

deployment/nginx-deployment created

thor@jump_host /$ 

 4.  Wait for pods to get running status 


thor@jump_host /$ kubectl get pods

NAME        READY   STATUS    RESTARTS   AGE

nginx-deployment-56cdb5d774-49rqs      1/1     Running   0          34s

nginx-deployment-56cdb5d774-6q8b2     1/1     Running   0          34s

nginx-deployment-56cdb5d774-nfwfp      1/1     Running   0          34s

thor@jump_host /$

5.  Validate the task by running below command 

 kubectl exec nginx-deployment-56cdb5d774-49rqs  -- curl http://localhost

 6.  Click on Finish & Confirm to complete the task successfully

Happy Learning!!!!

 Apart from this if you need more clarity,  I have made a  tutorial video on this , 

please go through and share your comments. Like and share the knowledge


Post a Comment

4 Comments

  1. Hi. Great Code! Although I have a doubt please: would it occur a syntax error if I put the same name in the Container as in the apps? For an instante, in your code, would it occur a syntax error if I put the name "nginx-app" in spite of "nginx-container"? Thanks a lot in advance for your comments on that. Best regards!

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Thanks for your kind words. Please feel free to reach for any queries / doubt you have. Regarding your doubt, you can change any name, please make sure you change it wherever is applicable. Still have concern the ping me for online support chat. All the Best . Keep Learning

      Delete
  2. Hi!
    Thanks for the response!
    I understood that I can change any name.
    But I have a more specific question please.

    I would like to know if it is possible to have the same names in the apps and in the Container.

    For an instance, in your code, if you put the Container name as "nginx-app" (the same name of the apps declared together in your code) would it function correctly? Or may it occur an error?

    I have this doubt because for some API types, such as ReplicaSets, the 2 instance label selectors must not overlap in ONE namespace, or the controller may see these as conflicting instructions and not be able to determine how many replicas should be present.

    So it is why I am asking you that.

    Thanks a lot in advance for your help!

    Kind regards,
    Sergio Guimarães Villaça

    ReplyDelete

Latest Posts

KodeKloud Kubernetes Security CKS  Lab Challenge 4 |  Audit-policy | Install & configure falco utility | Inspect the API server audit logs and identify the user