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: 6. Imperative Commands

Imperative commands are useful in quickly creating the resources on Kubernetes kubectl --dry-run -> When created with this option, Kubernetes just validates the definition and does not actually create the resource -o=yaml -> Gets the output in YAML format kubectl run nginx-pod --image=nginx:alpine  -> Create a pod with name nginx-pod and image nginx:alpine kubectl run httpd --image=httpd:alpine  -> By default, run implies run-a-pod kubectl run redis --image=redis:alpine --labels=tier=db  -> Create a pod with name redis and image redis:alpine and labels set to tier=db kubectl run custom-nginx --image=nginx --port=8080  -> Create a pod with name custom-nginx and image nginx to run on port 8080 kubectl expose pod redis --port=6379 --name=redis-service  -> Create a service with name redis-service to expose pod named redis on service-port 6379 kubectl create deployment webapp --image=kodekloud/webapp-color --replicas=3  -> Create a depl...

Kubernetes: 21. Secrets

Passwords In the webapps we store the properties file for storing and retrieving the data required by application But we never store the application passwords, truststore, keystore passwords etc here We might store them in an encrypted format, but storing them as plain text is not the correct way In Kubernetes we store these sensitive information in Secrets https://medium.com/avmconsulting-blog/secrets-management-in-kubernetes-378cbf8171d0 Secrets Secrets are used to store the sensitive information They are similar to ConfigMaps, except that they are stored in hashed or encoded format Note that they are only encoded (using base64) but are not encrypted So secrets are a safe option to store sensitive information but infact they are not the safest option As such secret objects should be not checked into source code tools, its best to store them encrypted at REST in ETCD Again as in ConfigMaps, we have to create the secrets object first and then inject them into the pods There are 2 ways ...

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...