Namespaces¶
- Namespaces - Imperative using kubectl
- Namespaces - Declarative using YAML & LimitRange
- Namespaces - Declarative using YAML & ResourceQuota
Step-01: Kubernetes Namespaces - Imperative using kubectl¶
- Namespaces allow to split-up resources into different groups.
- Resource names should be unique in a namespace
- We can use namespaces to create multiple environments like dev, staging and production etc
- Kubernetes will always list the resources from
default namespaceunless we provide exclusively from which namespace we need information from.
Kubernetes Manifests¶
#04-mysql-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:5.6
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-db-password
key: db-password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
- name: usermanagement-dbcreation-script
mountPath: /docker-entrypoint-initdb.d #https://hub.docker.com/_/mysql Refer Initializing a fresh instance
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: ebs-mysql-pv-claim
- name: usermanagement-dbcreation-script
configMap:
name: usermanagement-dbcreation-script
#06-UserManagementMicroservice-Deployment-Service.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: usermgmt-microservice
labels:
app: usermgmt-restapp
spec:
replicas: 1
selector:
matchLabels:
app: usermgmt-restapp
template:
metadata:
labels:
app: usermgmt-restapp
spec:
initContainers:
- name: init-db
image: busybox:1.31
command: ['sh', '-c', 'echo -e "Checking for the availability of MySQL Server deployment"; while ! nc -z mysql 3306; do sleep 1; printf "-"; done; echo -e " >> MySQL DB Server has started";']
containers:
- name: usermgmt-restapp
image: stacksimplify/kube-usermanagement-microservice:1.0.0
ports:
- containerPort: 8095
env:
- name: DB_HOSTNAME
value: "mysql"
- name: DB_PORT
value: "3306"
- name: DB_NAME
value: "usermgmt"
- name: DB_USERNAME
value: "root"
- name: DB_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-db-password
key: db-password
livenessProbe:
exec:
command:
- /bin/sh
- -c
- nc -z localhost 8095
initialDelaySeconds: 60
periodSeconds: 10
readinessProbe:
httpGet:
path: /usermgmt/health-status
port: 8095
initialDelaySeconds: 60
periodSeconds: 10
Step-02: Namespaces Generic - Deploy in Dev1 and Dev2¶
Create Namespace¶
# List Namespaces
kubectl get ns
# Craete Namespace
kubectl create namespace <namespace-name>
kubectl create namespace dev1
kubectl create namespace dev2
# List Namespaces
kubectl get ns
Comment NodePort in UserMgmt NodePort Service¶
- File: 07-UserManagement-Service.yml
- Why?:
- Whenever we create with same manifests multiple environments like dev1, dev2 with namespaces, we cannot have same worker node port for multiple services.
- We will have port conflict.
- Its good for k8s system to provide dynamic nodeport for us in such situations.
- Error if not commented
Deploy All k8s Objects¶
# Deploy All k8s Objects
kubectl apply -f kube-manifests/ -n dev1
kubectl apply -f kube-manifests/ -n dev2
# List all objects from dev1 & dev2 Namespaces
kubectl get all -n dev1
kubectl get all -n dev2
Step-03: Verify SC,PVC and PV¶
- Shorter Note
- PVC is a namespace specific resource
- PV and SC are generic
- Observation-1:
Persistent Volume Claim (PVC)gets created in respective namespaces - Observation-2:
Storage Class (SC) and Persistent Volume (PV)gets created generic. No specifc namespace for them
Step-04: Access Application¶
Dev1 Namespace¶
# Get Public IP
kubectl get nodes -o wide
# Get NodePort for dev1 usermgmt service
kubectl get svc -n dev1
# Access Application
http://<Worker-Node-Public-Ip>:<Dev1-NodePort>/usermgmt/health-stauts
Dev2 Namespace¶
# Get Public IP
kubectl get nodes -o wide
# Get NodePort for dev2 usermgmt service
kubectl get svc -n dev2
# Access Application
http://<Worker-Node-Public-Ip>:<Dev2-NodePort>/usermgmt/health-stauts
AWS EKS - Elastic Kubernetes Service - Masterclass¶
Step-05: Clean-Up¶
# Delete namespaces dev1 & dev2
kubectl delete ns dev1
kubectl delete ns dev2
# List all objects from dev1 & dev2 Namespaces
kubectl get all -n dev1
kubectl get all -n dev2
# List Namespaces
kubectl get ns
# List sc,pv
kubectl get sc,pv
# Delete Storage Class
kubectl delete sc ebs-sc
# Get all from All Namespaces
kubectl get all -all-namespaces
References:¶
- https://kubernetes.io/docs/tasks/administer-cluster/namespaces-walkthrough/
🎉 New Course
Ultimate DevOps Real-World Project Implementation on AWS
$15.99
$84.99
81% OFF
APRIL2026
Enroll Now on Udemy
🎉 Offer
