Kubernetes
Kubernetes (K8s)
1. Kubernetes Basics
Display cluster informationkubectl cluster-info`
List all nodes in the clusterkubectl get nodes`
List all pods in the current namespacekubectl get pods`
List all serviceskubectl get services`
List all deploymentskubectl get deployments`
2. Managing Pods
Create a pod with Nginxkubectl run my-pod` --image=nginx
Delete a podkubectl delete pod my-pod`
View pod logskubectl logs my-pod`
Access a pod's shellkubectl exec -it my-pod -- /bin/sh`
3. Managing Deployments
Create a deploymentkubectl create deployment my-deploy --image=nginx`
Scale deployment to 3 replicaskubectl scale deployment my-deploy --replicas=3`
Check rollout statuskubectl rollout status deployment my-deploy`
Rollback to the previous versionkubectl rollout undo deployment my-deploy`
4. Managing Services
Expose deployment as a servicekubectl expose deployment my-deploy --type=NodePort --port=80`
List serviceskubectl get svc`
Get service detailskubectl describe svc my-service`
5. Namespaces
List all namespaceskubectl get ns`
Create a new namespacekubectl create namespace dev`
Delete a namespacekubectl delete namespace dev`
6. ConfigMaps & Secrets
Create a ConfigMapkubectl create configmap my-config --from-literal=key=value`
List ConfigMapskubectl get configmap`
Create a secretkubectl create secret generic my-secret --from-literal=password=12345`
List secretskubectl get secrets`
7. Troubleshooting
View cluster eventskubectl get events
Get detailed pod informationkubectl describe pod my-pod
View logs of a specific podkubectl logs my-pod
Show resource usage of podskubectl top pod
8. Helm (Package Manager for Kubernetes)
Add a Helm repohelm repo add stable https://charts.helm.sh/stable
Install a Helm charthelm install my-release stable/nginx
List installed releaseshelm list
Uninstall a releasehelm delete my-release
9. Persistent Volumes & Storage
List persistent volume claimskubectl get pvc
List persistent volumeskubectl get pv
Describe a persistent volume claimkubectl describe pvc <pvc>
Delete a persistent volume claimkubectl delete pvc <pvc>
10. Autoscaling
Enable autoscalingkubectl autoscale deployment <deployment> --cpu-percent=50 --min=1 --max=10
View horizontal pod autoscalerkubectl get hpa
11. Kubernetes Debugging
Show eventskubectl get events --sort-by=.metadata.creationTimestamp
Show pod detailskubectl describe pod <pod>
Check logskubectl logs <pod>
Access pod shellkubectl exec -it <pod> -- /bin/sh
Kubernetes YAML Configurations
1. Pod
apiVersion: v1
kind: Pod
metadata:
name: my-pod
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
2. Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
3. ReplicaSet
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-replicaset
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: nginx
image: nginx:latest
4. Service (ClusterIP, NodePort, LoadBalancer)
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
type: NodePort
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30080
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
type: LoadBalancer
ports:
- protocol: TCP
port: 80
targetPort: 80
5. ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
key1: value1
key2: value2
6. Secret
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
password: cGFzc3dvcmQ= # Base64 encoded value
7. Persistent Volume (PV)
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /mnt/data
8. Persistent Volume Claim (PVC)
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 500Mi
9. Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: my-ingress
spec:
rules:
- host: example.com
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: my-service
port:
number: 80
10. Horizontal Pod Autoscaler (HPA)
apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
name: my-hpa
spec:
scaleTargetRef:
apiVersion: apps/v1
kind: Deployment
name: my-deployment
minReplicas: 2
maxReplicas: 5
metrics:
- type: Resource
resource:
name: cpu
target:
type: Utilization
averageUtilization: 50
11. CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: my-cronjob
spec:
schedule: "*/5 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: my-cron
image: busybox
command: ["echo", "Hello from CronJob"]
restartPolicy: OnFailure