Skip to main content

Kubernetes: 4. Namespaces

  • All the default pods that are required for networking, DNS etc are kept in kube-system namespace
  • The resource that are to be made available to the public are kept in kube-public namespace
  • All the user created resources can be kept in the default namespace or a custom namespace

Custom Namespaces
  • Can have their own policies, defining what can be done in the namespace
  • Resource quotas [CPU, Memory] can be enforced in the namespace
  • Each resource within the namespace can be reached directly by its service name
  • A resource in a namespace can reach to the resource in another namespace by appending the namespace name and further appending ".svc.cluster.local"
  • So a db-service pod in a dev namespace can reached as db-service.dev.svc.cluster.local
  • Note that we are referring to service-name here and NOT the pod-name
  • This is because when a service is created a DNS entry is automatically created in the above format
    • cluster.local is the domain name
    • svc is the sub-domain for service
    • dev is the namespace
    • db-service is the service name
  • To set the quota limits in a namespace, create the resource definition file for the namespace and set the limits in it

ResourceQuota
  • If a resource quota is applied to a namespace then all pod containers have to declare the requests and limits for CPU and Memory
  • Sum of all the CPU/Memory requests/limits in the pod cannot be more than what is declared in ResourceQuota for namespace
  • Requests are what containers gets for sure
  • Limits are the threshold values beyond which a container cannot go
  • Limit is always  set greater than requests otherwise kubernetes throws an error

namespace-definition.yaml
apiVersion: v1
kind: Namespace
metadata:
    name: dev

compute-resource-quota-definition.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
    name: compute-quota
    namespace: dev

spec:
    hard:
        pods: "10"
        requests.cpu: "4"
        requests.memory: "5Gi"
        limits.cpu: "10"
        limits.memory: "10Gi"

pod-definition-with-namespace.yaml
apiVersion: v1
kind: Pod
metadata:
    name: myapp-pod
    namespace: dev
    labels:
        app: myapp
        location: IN

spec:
    containers:
    - name: nginx-container
    image: nginx
    - name: backed-db
    image: redis

pod-definition.yaml
apiVersion: v1
kind: Pod
metadata:
    name: myapp-pod
    labels:
        app: myapp
        location: IN

spec:
    containers:
    - name: nginx-container
    image: nginx
    - name: backed-db
    image: redis

kubectl get pods                         
-> Get pods from default namespace

kubectl get pods --namespace kube-system 
-> Get pods from kube-system namespace

kubectl create -f pod-definition.yaml    
-> Create pod from definition file in default namespace

kubectl create -f pod-definition.yaml --namespace=dev 
-> Create pod from definition file in dev namespace, alternatively provide the namespace in metadata section

kubectl create -f namespace-definition.yaml 
-> Create[kubernetes resource] a namespace from definition file

kubectl create namespace dev             
-> Create a namespace from CLI

kubectl config set-context $(kubectl config current-context) --namespace=dev 
-> Set the dev namespace as the default namespace in the current context

kubectl get pods --all-namespaces        
-> Get pods from all the namespaces

kubectl create -f compute-resource-quota-definition.yaml 
-> Create[kubernetes resource] namespace and define the quotas in the namespace

kubectl get ns                           
-> Get the namespaces

kubectl get namespace                    
-> Get the namespaces



Comments

Popular posts from this blog

Kubernetes: 15. Multiple Schedulers

Custom Scheduler Kubernetes allows to create custom schedulers There can be multiple schedulers running at a same time apart from the default scheduler or A custom scheduler can replace the default kube-scheduler to become the default one So a few pods that requires additional checks apart from taints and toleration, node affinity can go through the custom scheduler before getting scheduled on the node Whereas the rest of the pods can go through the default kube-scheduler Create Custom Scheduler We can either download the kube-scheduler and run it as a service or alternatively create it using a static pod Below here we are downloading the binaries to run it The property scheduler-name is used to define the name of the scheduler, if not set then it will be defaulted to default-scheduler For your custom schedulers, update this property name to set a custom name for your scheduler For Static pods, the name can be updated directly in the pod-definition file Use kubectl create -f <pod-de...

Kubernetes: 19. Configure Application

Configuring application consists of Configuring commands and arguments on applications Configuring environment variables Configuring secrets Docker Commands docker run ubuntu  -> Runs ubuntu container and exit, container CMD is set to [bash], so the container quitely exits docker run ubuntu echo "Hello World" -> Runs ubuntu container, prints "Hello World" exits quitely. To update the default settings, create your own image from the base image lets call this ubuntu-sleeper image FROM ubuntu CMD sleep 5 CMD can also be mentioned in the JSON format like CMD ["sleep", "5"] Note that with JSON format the first element should always be the command to execute,  for eg, it CANNOT be ["sleep 5"] Run build the new ubuntu-sleeper image and run the new image docker build -t ubuntu-sleeper .  -> Build the image docker run ubuntu-sleeper -> Run the new image So the new image will launch ubuntu container, sleep for 5 seconds and quitely ex...

Kubernetes: 20. ConfigMaps

  A Java map is a object that maps key to value. The key has to be unique. Environment Variables Environment variables can be directly added into Pod definition file under specs.env array But they will be limited to only the pod for which they are added For new Pods, the environment variables have to be added again ConfigMaps ConfigMaps are a way of storing the data in key: value pair This data is then injected into Pods via the definition file The data injected can be created as environment variables in the pod Or the data is just injected as a file that then can be used by the pod Create ConfigMaps There are two ways to create the ConfigMaps like any other Kubernetes objects Imperative  Declarative Note that in the declarative way there is no specs , we instead have data section config-map APP_COLOR: Blue APP_ENV: Prod config-map-creation-imperative kubectl create configmap -> Imperative way of creating configmap <config-name> --from-literal=<key>=<v...