User Tools

Site Tools


raspberry_pi_cluster:nginx

# Kubernetes NGINX Deployment using CLI and YAML

Overview:

This will be a two-part write-up where we will first use the CLI to create a deployment that runs the NGINX image. We will display the details of the deployment, check logs, and then delete the deployment.

In the section part, we will be creating the same deployment using a yaml file but with 4 nginx containers and then verifying via the command line.

We will first create the deployment using the following command.

  jmbargallo@eva00:~ $ kubectl create deployment nginx-project --image=nginx
  deployment.apps/nginx-project created
  jmbargallo@eva00:~ $

To describe the deployment we can use the following command.

  jmbargallo@eva00:~ $ kubectl describe deployment nginx-project
  Name:                   nginx-project
  Namespace:              default
  CreationTimestamp:      Fri, 16 Feb 2024 14:37:25 +0100
  Labels:                 app=nginx-project
  Annotations:            deployment.kubernetes.io/revision: 1
  Selector:               app=nginx-project
  Replicas:               1 desired | 1 updated | 1 total | 1 available | 0 unavailable
  StrategyType:           RollingUpdate
  MinReadySeconds:        0
  RollingUpdateStrategy:  25% max unavailable, 25% max surge
  Pod Template:
    Labels:  app=nginx-project
    Containers:
     nginx:
      Image:        nginx
      Port:         <none>
      Host Port:    <none>
      Environment:  <none>
      Mounts:       <none>
    Volumes:        <none>
  Conditions:
    Type           Status  Reason
    ----           ------  ------
    Available      True    MinimumReplicasAvailable
    Progressing    True    NewReplicaSetAvailable
  OldReplicaSets:  <none>
  NewReplicaSet:   nginx-project-6858bdb67c (1/1 replicas created)
  Events:
    Type    Reason             Age    From                   Message
    ----    ------             ----   ----                   -------
    Normal  ScalingReplicaSet  5m12s  deployment-controller  Scaled up replica set nginx-project-6858bdb67c to 1
  jmbargallo@eva00:~ $

To get the logs, we can run the following command.

  jmbargallo@eva00:~ $ kubectl logs deployment/nginx-project
  /docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perfo                                                                  rm configuration
  /docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
  /docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-defau                                                                  lt.sh
  10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d                                                                  /default.conf
  10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf                                                                  .d/default.conf
  /docker-entrypoint.sh: Sourcing /docker-entrypoint.d/15-local-resolvers.envsh
  /docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.s                                                                  h
  /docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.s                                                                  h
  /docker-entrypoint.sh: Configuration complete; ready for start up
  2024/02/16 13:37:28 [notice] 1#1: using the "epoll" event method
  2024/02/16 13:37:28 [notice] 1#1: nginx/1.25.4
  2024/02/16 13:37:28 [notice] 1#1: built by gcc 12.2.0 (Debian 12.2.0-14)
  2024/02/16 13:37:28 [notice] 1#1: OS: Linux 6.1.21-v8+
  2024/02/16 13:37:28 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
  2024/02/16 13:37:28 [notice] 1#1: start worker processes
  2024/02/16 13:37:28 [notice] 1#1: start worker process 29
  2024/02/16 13:37:28 [notice] 1#1: start worker process 30
  2024/02/16 13:37:28 [notice] 1#1: start worker process 31
  2024/02/16 13:37:28 [notice] 1#1: start worker process 32
  jmbargallo@eva00:~ $

Now we need to delete our deployment.

  jmbargallo@eva00:~ $ kubectl delete deployment nginx-project
  deployment.apps "nginx-project" deleted

We have deleted our deployment and continue to the second part.

To create what we had previously but with 4 containers, I’ll be using the following yaml file.

  apiVersion: v1
  kind: Service
  metadata:
    name: nginx-project
  spec:
    type: LoadBalancer
    ports:
      - port: 80
    selector:
      app: nginx-project
  ---
  apiVersion: apps/v1
  kind: Deployment
  metadata:
    name: nginx-project
  spec:
    replicas: 4
    selector:
      matchLabels:
        app: nginx-project
    template:
      metadata:
        labels:
          app: nginx-project
      spec:
        containers:
          - name: nginx
            image: nginx:1.17.3
            ports:
              - containerPort: 80

Save to a file and run the following command:

  jmbargallo@eva00:~ $ kubectl apply -f nginx.yaml
  deployment.apps/nginx-project created

Now let’s do another describe.

  jmbargallo@eva00:~ $ kubectl describe deployment nginx-project
  Name:                   nginx-project
  Namespace:              default
  CreationTimestamp:      Fri, 16 Feb 2024 14:56:14 +0100
  Labels:                 <none>
  Annotations:            deployment.kubernetes.io/revision: 1
  Selector:               app=nginx-project
  Replicas:               4 desired | 4 updated | 4 total | 4 available | 0 unavailable
  StrategyType:           RollingUpdate
  MinReadySeconds:        0
  RollingUpdateStrategy:  25% max unavailable, 25% max surge
  Pod Template:
    Labels:  app=nginx-project
    Containers:
     nginx:
      Image:        nginx:1.17.3
      Port:         80/TCP
      Host Port:    0/TCP
      Environment:  <none>
      Mounts:       <none>
    Volumes:        <none>
  Conditions:
    Type           Status  Reason
    ----           ------  ------
    Available      True    MinimumReplicasAvailable
    Progressing    True    NewReplicaSetAvailable
  OldReplicaSets:  <none>
  NewReplicaSet:   nginx-project-78b94b9cc8 (4/4 replicas created)
  Events:
    Type    Reason             Age    From                   Message
    ----    ------             ----   ----                   -------
    Normal  ScalingReplicaSet  5m49s  deployment-controller  Scaled up replica set nginx-project-78b94b9cc8 to 4
  jmbargallo@eva00:~ $

We can see that it has been scaled to 4.

We can also get specific information relating to the pods themselves, the deployments, and the services by running the following commands respectively.

  jmbargallo@eva00:~ $ kubectl get pods
  NAME                             READY   STATUS    RESTARTS   AGE
  nginx-project-78b94b9cc8-t829l   1/1     Running   0          8m17s
  nginx-project-78b94b9cc8-6j7cj   1/1     Running   0          8m17s
  nginx-project-78b94b9cc8-phsll   1/1     Running   0          8m17s
  nginx-project-78b94b9cc8-2ff27   1/1     Running   0          8m17s
  jmbargallo@eva00:~ $ kubectl get deployments
  NAME            READY   UP-TO-DATE   AVAILABLE   AGE
  nginx-project   4/4     4            4           8m30s
  jmbargallo@eva00:~ $ kubectl get services
  NAME            TYPE           CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE
  kubernetes      ClusterIP      10.43.0.1       <none>        443/TCP        25h
  nginx-project   LoadBalancer   10.43.171.236   <pending>     80:30557/TCP   41m
  jmbargallo@eva00:~ $

Now let’s delete our deployment.

  kubectl delete deployment nginx-project

We have successfully created containers with Kubernetes using the command line and using yaml.

raspberry_pi_cluster/nginx.txt · Last modified: 2025/02/07 11:42 by 85.219.17.206