Please read the Kubernetes installation if needed

How kubernetes works ?

Kubernetes will first need a Namespace where we will push pods (containers).

Here, for example, we will create a Namespace called « webserver » where we will run nginx pods.

The workflow will consist of create a Namespace (If no Namespace is defined, it will use a « default » one), deploy a Pod image with an image (here nginx) and expose them in the kubernetes cluster.

All will be done on Master Node

Namespace configuration

To create the Namespace, just do :

kubectl create namespace webwerver

Most of the time, you can also use shorter commands in kubernetes, for example here

kubectl create ns webserver

Note that this Namespace’s name must be in lowercase

You can delete Namespace using

kubectl delete namespace webserver

To view your Namespace use :

kubectl get namespaces

You will see all the current namespace created and network namespace if you use for example Calico.

Deployement and services

You must create a deployment and services in order to push them in pods.

For that, you have many ways to do this.

Using only commands :

Create the deployment :

kubectl create deployment nginx --image=nginx -n webserver

Create the associate service:

kubectl create service nodeport nginx --tcp=80:80 -n webserver

Or better,

Use an YAML file

apiVersion: apps/v1
kind: Deployment
name: nginx
app: nginx
replicas: 2
app: nginx
- name: nginx
image: nginx:stable
- containerPort: 80

Here we have :

  • kind: type of file (here deployment)
  • replicas: number of desired pods (here 2)
  • image: Image’s name
  • containerPort: exposed port (here the host port will be set automatically)

You can then apply your YAML file in your Namespace using :

kubectl apply -f nginx.yaml -n webserver

Of course, change the YAML file’s name according to your file.

I prefer this method with YAML file because if you have to rebuild your application, you will just have to run the YAML file again, and you don’t have to remember the parameters you used. It also provides the deployment specification and services in one shot.

Once you ran your deployment, you can, as the Namespace, check or delete them :

kubectl get deployments -n webserver
kubectl delete deployments nginx -n webserver

Expose you deployment / service

You have now created your application with the configuration (port, number of pods, image)

You can now push and activate them (expose them)

kubectl expose deployment nginx --type=NodePort -n webserver

It will create the number of « replica » pods in your kubernetes cluster and expose the specified port.

You can check this service and get the host port:

kubectl get service nginx -n webserver

You should have an output like :

nginx NodePort 80:3272/TCP 26s

Here, we have the service nginx listening on port 80 internally and expose to the port 3272 in host.

You can also have more info using this command:

kubectl describe service nginx -n webserver

Patches, Scale and other commands


Patch can be used to change on the fly, for example, the host port if you want to specify a new one :

kubectl patch service nginx -n webserver --type='json' --patch='[{"op": "replace", "path": "/spec/ports/0/nodePort", "value":31001}]'

For example, here, we force the service nginx to get the host port 31001 instead of the previous 3272.


Scale is used to increase the desire replica (or pods) or to unexposed them:

kubectl scale deployment nginx -n webserver –replicas=3

kubectl scale deployment nginx -n webserver –replicas=0

Endpoints (describe)

Ok you create pods…but you may want to know where they are no ? You can for that use the previous describe command:

kubectl describe service nginx -n webserver | grep "Endpoints"

Point to take care

The common error is to forget to add the Namespace if you use one, just check if you specify it in all your commands :

<command> -n namespce

Don’t forget to check at each step what you did before doing the next step

kubectk get <pods,deploymenets,services....>


Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *