Introduction

In this new article we will discuss about Namespaces and Imperative commands ! 🙂

What is a Namespace and how to create them ?

Kubernetes supports multiple virtual clusters backed by the same physical cluster. These virtual clusters are called namespaces. Namespaces are intended for use in environments with many users spread across multiple teams, or projects. They are a way to divide cluster resources between multiple users. It is not necessary to use multiple namespaces just to separate slightly different resources, such as different versions of the same software: use labels to distinguish resources within the same namespace !

How to create a namespace ?

Pretty easy, use the following command :

master $ kubectl create namespace mynamespace
namespace/mynamespace created

How to delete a namespace ?

Pretty easy, use the following command :

master $ kubectl delete namespace mynamespace
namespace "mynamespace" deleted

How to use a namespace in a YAML configuration file ?

Just add the namespace in the metadata section like so :

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
  namespace: my-namespace
spec:
  containers:
  - name: my-pod
    image: nginx

Cheat sheet on Imperative commands

We have seen in previous articles how to create pods, services or deployments. Here are some imperative commands that will help you to save time. It can be interesting, specially if you have planned to pass the Kubernetes Certification !

POD

  • Here is an example on how to create POD with imperative commands :
kubectl run --generator=run-pod/v1 nginx-podname --image=nginx





  • Generate POD Manifest YAML file (-o yaml) but don’t create it (–dry-run) :
kubectl run --generator=run-pod/v1 nginx --image=nginx --dry-run -o yaml

Deployment

  • Create a deployment :
kubectl create deployment nginx-deploy --image=nginx

  • Generate Deployment YAML file (-o yaml) but don’t create it(–dry-run)
kubectl create deployment nginx-deploy --image=nginx --dry-run -o yaml

  • Scale a deployment to 4 replicas :
kubectl scale deployment nginx-deploy --replicas=4

Service

  • Create a Service named redis-service of type ClusterIP to expose pod redis on port 6379
kubectl expose pod redis --port=6379 --name redis-service --dry-run -o yaml

  • Create a Service named nginx of type NodePort to expose pod nginx’s port 80 on port 30080 on the nodes:
kubectl create service nodeport nginx --tcp=80:80 --node-port=30080 --dry-run -o yaml

Sources

kubernetes.io

Leave a Comment

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