Key Concepts Unveiled
Key Concepts Unveiled
Create Pod:
kubectl apply -f pod-example.yml
Container
A container is a lightweight, standalone executable package of software. In the above example,
nginx is a container running inside a pod.
Deployment
A Deployment manages multiple pod replicas and provides updates/rollbacks.
# deployment-example.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
Create Deployment:
kubectl apply -f deployment-example.yml
2. Docker vs Kubernetes
Docker
Docker is used to create, manage, and run containers.
Build Docker Image:
docker build -t myapp:latest .
Run Container:
docker run -d -p 8080:80 myapp:latest
Kubernetes
Kubernetes orchestrates containers across multiple nodes.
Deploy on Kubernetes:
# k8s-deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: k8s-app
spec:
replicas: 2
selector:
matchLabels:
app: myapp
template:
metadata:
labels:
app: myapp
spec:
containers:
- name: myapp
image: myapp:latest
ports:
- containerPort: 8080
Apply to Cluster:
kubectl apply -f k8s-deployment.yml
3. Main Components of Kubernetes Architecture
• API Server: Accepts commands (e.g., kubectl) and manages cluster state.
• Etcd: Stores configuration data.
• Controller Manager: Ensures cluster state matches the desired state.
• Scheduler: Assigns pods to nodes.
• Kubelet: Runs on nodes to manage pod lifecycle.
• Kube-Proxy: Handles network routing.
• Container Runtime: Runs containers (e.g., Docker, containerd).
Example: To view cluster components:
kubectl get componentstatuses
# Deploy Service
docker service create --name myweb --replicas 3 -p 8080:80 nginx
Kubernetes
# deployment.yml
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-deployment
spec:
replicas: 3
selector:
matchLabels:
app: web
template:
metadata:
labels:
app: web
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
kubectl apply -f deployment.yml
Kubernetes Pod:
# pod-example.yml
apiVersion: v1
kind: Pod
metadata:
name: nginx-pod
spec:
containers:
- name: nginx
image: nginx
ports:
- containerPort: 80
6. Namespace in Kubernetes
# Create Namespace
kubectl create namespace dev-environment
# Deploy in Namespace
kubectl apply -f deployment.yml -n dev-environment
# Check Namespaces
kubectl get namespaces
7. Role of Kube-Proxy
• Handles routing rules for traffic between pods and services.
• Ensures services are reachable within the cluster.
Example: Check kube-proxy
kubectl get pods -n kube-system | grep kube-proxy
8. Deployment vs ReplicaSet
Deployment:
apiVersion: apps/v1
kind: Deployment
metadata:
name: frontend
spec:
replicas: 2
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: nginx
image: nginx
ReplicaSet:
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: frontend-rs
spec:
replicas: 2
selector:
matchLabels:
app: frontend
template:
metadata:
labels:
app: frontend
spec:
containers:
- name: nginx
image: nginx
• NodePort:
apiVersion: v1
kind: Service
metadata:
name: nodeport-service
spec:
type: NodePort
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30007
• LoadBalancer:
apiVersion: v1
kind: Service
metadata:
name: lb-service
spec:
type: LoadBalancer
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80