- Kubernetes resource name should be unique within a namespace, but not across namespaces. It means, we can use same Pod/Deployment name in different namespaces
- Namespaces cannot be nested
- Each Kubernetes resource can only be in one namespace. It means, we cannot attach same Pod/Deployment to multiple namespaces
- Try to avoid creating namespace with prefix "kube-" as it is reserved for K8 internal system
- We can restrict resource usage limit (i.e. CPU, Memory) for each namespace
- We can restrict users to access only selected namespaces and it’s Kubernetes objects
- By default, all the k8 objects created under "default" namespace
Namespace |
description |
default |
By default,
all the resources created under “default” namespace without CPU and Memory
restrictions |
kube-system |
This
namespace for objects created by the Kubernetes system |
kube-public |
It is reserved
for cluster usage and it is accessible for all users. Use this only when k8 objects
should be visible to publicly throughout the cluster |
kube-node-lease |
It is for lease
objects associated with each node to improve performance of the
node heartbeats |
Create Namespace
- from YAML file
apiVersion: v1
kind: Namespace
metadata:
name: dev-ns
// create namespace resoure from yaml file
$ kubectl apply -f dev-ns.yaml
namespace/dev-ns created
// display all namespaces
$ kubectl get ns
NAME STATUS AGE
default Active 30d
kube-node-lease Active 30d
kube-public Active 30d
kube-system Active 30d
dev-ns Active 17m
// view namespace details using describe command
$ kubectl describe ns dev-ns
// view namespace details as yaml file
$ kubectl get ns dev-ns -o yaml
2. imperative way
// syntax $kubectl create ns <namespace-name>
$ kubectl create ns dev2-ns
namespace/dev2-ns created
// display all namespaces$ kubectl get nsNAME STATUS AGEdefault Active 30dkube-node-lease Active 30dkube-public Active 30dkube-system Active 30ddev-ns Active 17mdev2-ns Active 1m
// display all namespaces
$ kubectl get ns
NAME STATUS AGE
default Active 30d
kube-node-lease Active 30d
kube-public Active 30d
kube-system Active 30d
dev-ns Active 17m
dev2-ns Active 1m
Create Pod in selected Namespace
- from YAML file
we can achieve this by specifying namespace attribute in the metadata section
apiVersion: v1
kind: Pod
metadata:
name: pod-nginx
namespace: dev-ns
spec:
containers:
- name: nginx
image: nginx:1.14.2
Save above yaml content as "pod-dev-ns.yaml" and run following kubectl command
// create pod under "dev-ns" namespace using yaml file
$ kubectl apply -f pod-dev-ns.yaml
pod/pod-nginx created
// display all pods from dev-ns namespace
// syntax
$ kubectl get po -n <namespace-name>
$ kubectl get po -n dev-ns
NAME READY STATUS RESTARTS AGE
pod-nginx 1/1 Running 0 4m4s
// delete namespace
// all pods gets deleted automatically when namespace is deleted
$ kubectl delete ns dev-ns
namespace "dev-ns" deleted
// display all pods under dev-ns namespace
$ kubectl get po -n dev-ns
No resources found in dev-ns namespace.
// delete all pods without deleting namespace
//syntax:
kubectl delete po --all -n <namespace-name>
$ kubectl delete po --all -n dev2-ns
pod "pod-nginx" deleted
// display all namespaces
$ kubectl get ns
NAME STATUS AGE
default Active 31d
dev2-ns Active 47m
kube-node-lease Active 31d
kube-public Active 31d
kube-system Active 31d
2. imperative way
// syntax
// kubectl run <pod-name> --image <image-name> -n <namespace-name>
$ kubectl run pod-nginx --image nginx -n dev2-ns
pod/pod-nginx created
// display all pods from dev2-ns namespace// syntax kubectl get po -n <namespace-name>
$ kubectl get po -n dev2-nsNAME READY STATUS RESTARTS AGEpod-nginx 1/1 Running 0 4m4s
// display all pods from dev2-ns namespace
// syntax
kubectl get po -n <namespace-name>
$ kubectl get po -n dev2-ns
NAME READY STATUS RESTARTS AGE
pod-nginx 1/1 Running 0 4m4s
<servicename>.<namespace>.svc.cluster.local
Kubernetes for Developers Journey.
- Kubernetes for Developers #25: PersistentVolume and PersistentVolumeClaim in-detail
- Kubernetes for Developers #24: Kubernetes Volume hostPath in-detail
- Kubernetes for Developers #23: Kubernetes Volume emptyDir in-detail
- Kubernetes for Developers #22: Access to Multiple Clusters or Namespaces using kubectl and kubeconfig
- Kubernetes for Developers #21: Kubernetes Namespace in-detail
- Kubernetes for Developers #20: Create Automated Tasks using Jobs and CronJobs
- Kubernetes for Developers #19: Manage app credentials using Kubernetes Secrets
- Kubernetes for Developers #18: Manage app settings using Kubernetes ConfigMap
- Kubernetes for Developers #17: Expose service using Kubernetes Ingress
- Kubernetes for Developers #16: Kubernetes Service Types - ClusterIP, NodePort, LoadBalancer and ExternalName
- Kubernetes for Developers #15: Kubernetes Service YAML manifest in-detail
- Kubernetes for Developers #14: Kubernetes Deployment YAML manifest in-detail
- Kubernetes for Developers #13: Effective way of using K8 Readiness Probe
- Kubernetes for Developers #12: Effective way of using K8 Liveness Probe
- Kubernetes for Developers #11: Pod Organization using Labels
- Kubernetes for Developers #10: Kubernetes Pod YAML manifest in-detail
- Kubernetes for Developers #9: Kubernetes Pod Lifecycle
- Kubernetes for Developers #8: Kubernetes Object Name, Labels, Selectors and Namespace
- Kubernetes for Developers #7: Imperative vs. Declarative Kubernetes Objects
- Kubernetes for Developers #6: Kubernetes Objects
- Kubernetes for Developers #5: Kubernetes Web UI Dashboard
- Kubernetes for Developers #4: Enable kubectl bash autocompletion
- Kubernetes for Developers #3: kubectl CLI
- Kubernetes for Developers #2: Kubernetes for Local Development
- Kubernetes for Developers #1: Kubernetes Architecture and Features
Hi Rama Subbareddy,
ReplyDeleteI really appreciate your efforts to get this articles.
I have gone through your kubernetes articles, its only talks about basic concepts. My request is to get real time scenarios like how to autoscaling the POD's and how to deploy pod in specific node. If you provide articles like above scenarios it would be really great..
this is just my suggestion Rama.
Thanks for the feedback prashanth. I will cover in upcoming articles.
Delete