Saturday, 15 November 2025

Kubernet - minikube - create a deployment

Kubernetes (and thus Minikube) manages containers by wrapping them in Pods and managing those Pods through higher-level objects like Deployments and Services

[mahesh@devopsvm01 ~]$ minikube status

minikube

type: Control Plane

host: Running

kubelet: Running

apiserver: Running

kubeconfig: Configured

[mahesh@devopsvm01 ~]$ minikube kubectl -- get pods
No resources found in default namespace.
[mahesh@devopsvm01 ~]$
[mahesh@devopsvm01 ~]$ minikube kubectl -- create deployment nginx-web --image=nginx
deployment.apps/nginx-web created
[mahesh@devopsvm01 ~]$

The command minikube kubectl -- create deployment nginx-web --image=nginx creates a Deployment named nginx-web in your local minikube cluster, and this Deployment manages a set of identical Nginx application Pods.

A Deployment ensures that your pod always runs — if it crashes, Kubernetes recreates it.

Part of CommandPurpose
minikubeThis is the primary command-line tool for interacting with your local Kubernetes cluster running inside minikube.
kubectlThis is the command-line tool for controlling a Kubernetes cluster. When used after minikube, it tells minikube to execute the kubectl command inside the minikube environment.
--This is an optional but often useful separator. It signals to the minikube command that all subsequent arguments should be treated as arguments for the subcommand (kubectl) and not for minikube itself.
create deploymentThis is the kubectl instruction to create a new Kubernetes Deployment resource. A Deployment is a high-level controller that provides declarative updates for Pods and ReplicaSets.
nginx-webThis specifies the name of the Deployment resource being created.
--image=nginxThis crucial flag tells the Deployment what container image to use for the Pods it will manage. In this case, it instructs Kubernetes to pull and run the official Nginx web server image from Docker Hub.

📦 What happens internally

When you run this command, Kubernetes will:

  1. Create a Deployment named nginx-web

  2. Deployment will create a ReplicaSet

  3. ReplicaSet will create 1 Pod

  4. That Pod runs a container from the image nginx


[mahesh@devopsvm01 ~]$ minikube kubectl -- get deployments
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
nginx-web   1/1     1            1           74m
[mahesh@devopsvm01 ~]$


[mahesh@devopsvm01 ~]$ minikube kubectl -- get pods
NAME                        READY   STATUS              RESTARTS   AGE
nginx-web-b9c74675f-sclnc   0/1     ContainerCreating   0          6s
[mahesh@devopsvm01 ~]$
[mahesh@devopsvm01 ~]$ minikube kubectl -- get pods
NAME                        READY   STATUS    RESTARTS   AGE
nginx-web-b9c74675f-sclnc   1/1     Running   0          62s
[mahesh@devopsvm01 ~]$

We can see a container is created inside minikube  container.

[mahesh@devopsvm01 ~]$ minikube ip

192.168.49.2
[mahesh@devopsvm01 ~]$
[mahesh@devopsvm01 ~]$ docker exec -it minikube bash
root@minikube:/#

root@minikube:/# hostname -i
192.168.49.2
root@minikube:/#

root@minikube:/# docker ps
CONTAINER ID   IMAGE                          COMMAND                  CREATED         STATUS         PORTS     NAMES
997f9a56a387   nginx                          "/docker-entrypoint.…"   3 minutes ago   Up 3 minutes             k8s_nginx_nginx-web-b9c74675f-sclnc_default_76771                                  3f0-cc49-4661-808a-b51ae17d83eb_0

Accessing the Webpage

Now that Nginx is running inside a Pod, you need a Service to expose it to the outside world (your host VM).

Expose the Deployment with a NodePort Service

A Service acts as a stable IP address and load balancer for your Pods. A NodePort Service exposes the application on a specific port on the Kubernetes node (which is your minikube container).

minikube kubectl -- expose deployment nginx-web --type=NodePort --port=80
[mahesh@devopsvm01 ~]$ minikube kubectl -- expose deployment nginx-web --type=NodePort --port=80
service/nginx-web exposed
[mahesh@devopsvm01 ~]$
Part of CommandPurpose
expose deploymentThis is the kubectl instruction to create a new Service object for the specified resource (nginx-web).
nginx-webThis specifies the Deployment that the new Service will target. The Service will automatically find and route traffic to the Pods managed by this Deployment.
--type=NodePortThis is the crucial part that defines how the Service will be exposed. A NodePort Service opens a specific port on every Node (in your case, the single minikube VM/container) in the cluster, making the application accessible from outside the cluster network.
--port=80This specifies the internal port on the Service itself. Traffic hitting the Service will be forwarded to the target Pods on their exposed port (which is port 80 for the Nginx image).

🌐 Resulting Service and Access

When this command is executed, a new Service resource named nginx-web is created with the following properties:

  1. Internal Access: Any Pod inside the cluster can now reach the Nginx application using the Service's ClusterIP and port 80.

  2. External Access (The NodePort):

    • Kubernetes automatically picks a random, high-numbered port (typically between 30000 and 32767)—for example, $31404$ as seen in your previous output. This is the NodePort.

    • The Nginx application is now accessible using the IP address of your minikube Node and the assigned NodePort.

[mahesh@devopsvm01 ~]$ minikube kubectl -- get service nginx-web

NAME        TYPE       CLUSTER-IP      EXTERNAL-IP   PORT(S)        AGE

nginx-web   NodePort   10.104.42.136   <none>        80:31404/TCP   103m

[mahesh@devopsvm01 ~]$

Get the Access URL

Minikube provides a simple command to figure out the exact URL and port assigned to your service:

Bash
minikube service nginx-web --url
[mahesh@devopsvm01 ~]$ minikube service nginx-web --url

http://192.168.49.2:31404
[mahesh@devopsvm01 ~]$




















One of the key feature of kubernet is the ability to restart a container , lets demonstrate that 


[mahesh@devopsvm01 ~]$ minikube kubectl -- get pods
NAME                        READY   STATUS    RESTARTS   AGE
nginx-web-b9c74675f-sclnc   1/1     Running   0          76m
[mahesh@devopsvm01 ~]$

We will use kubectl exec to run a command inside the container that deliberately stops the main Nginx process. Because the main process exits, the container will terminate (crash).

NOTE: Nginx is often PID 1 in the container. Running kill 1 inside a container is a common way to simulate an abrupt shutdown.


[mahesh@devopsvm01 ~]$ minikube kubectl -- get pods
NAME                        READY   STATUS    RESTARTS   AGE
nginx-web-b9c74675f-sclnc   1/1     Running   0          76m
[mahesh@devopsvm01 ~]$
[mahesh@devopsvm01 ~]$ minikube kubectl -- exec nginx-web-b9c74675f-sclnc -- /bin/bash -c "kill 1"

[mahesh@devopsvm01 ~]$
[mahesh@devopsvm01 ~]$ minikube kubectl -- get pods
NAME                        READY   STATUS      RESTARTS   AGE
nginx-web-b9c74675f-sclnc   0/1     Completed   0          78m
[mahesh@devopsvm01 ~]$ minikube kubectl -- get pods
NAME                        READY   STATUS    RESTARTS      AGE
nginx-web-b9c74675f-sclnc   1/1     Running   1 (12s ago)   78m
[mahesh@devopsvm01 ~]$
[mahesh@devopsvm01 ~]$ minikube kubectl -- get pods
NAME                        READY   STATUS    RESTARTS      AGE
nginx-web-b9c74675f-sclnc   1/1     Running   1 (20s ago)   78m
[mahesh@devopsvm01 ~]$



No comments:

Post a Comment

Building a Safer PostgreSQL CI/CD Pipeline with GitHub Actions: Dev → PR Review → Test Promotion

In my previous post, we explored a simple push-to-main deployment strategy . While functional, that model is not considered an industry best...