K8s objects with practicals
Let’s dive deep into Kubernetes Objects with detailed explanations, YAML examples, and practical steps. Kubernetes objects are the building blocks of your cluster, and understanding them is crucial for managing applications effectively.
2. Kubernetes Objects
Kubernetes objects are persistent entities that represent the state of your cluster. They describe:
What containerized applications are running (and on which nodes).
The resources available to those applications.
Policies around how those applications behave.
Below, we’ll cover the most important Kubernetes objects with YAML examples and practical steps.
1. Pods
A Pod is the smallest deployable unit in Kubernetes. It can contain one or more containers that share storage, network, and specifications.
YAML Example: Pod
apiVersion: v1
kind: Pod
metadata:
name: my-pod
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
Practical Steps
Save the above YAML as
pod.yaml
.Create the Pod:
kubectl apply -f pod.yaml
Verify the Pod:
kubectl get pods
Describe the Pod for details:
kubectl describe pod my-pod
Delete the Pod:
kubectl delete -f pod.yaml
2. ReplicaSets
A ReplicaSet ensures that a specified number of Pod replicas are running at any given time.
YAML Example: ReplicaSet
apiVersion: apps/v1
kind: ReplicaSet
metadata:
name: my-replicaset
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
Practical Steps
Save the YAML as
replicaset.yaml
.Create the ReplicaSet:
kubectl apply -f replicaset.yaml
Verify the ReplicaSet and Pods:
kubectl get replicaset kubectl get pods
Scale the ReplicaSet:
kubectl scale replicaset my-replicaset --replicas=5
Delete the ReplicaSet:
kubectl delete -f replicaset.yaml
3. Deployments
A Deployment provides declarative updates for Pods and ReplicaSets. It’s the most commonly used object for managing applications.
YAML Example: Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-deployment
labels:
app: my-app
spec:
replicas: 3
selector:
matchLabels:
app: my-app
template:
metadata:
labels:
app: my-app
spec:
containers:
- name: my-container
image: nginx
ports:
- containerPort: 80
Practical Steps
Save the YAML as
deployment.yaml
.Create the Deployment:
kubectl apply -f deployment.yaml
Verify the Deployment and Pods:
kubectl get deployments kubectl get pods
Update the Deployment (e.g., change the image):
kubectl set image deployment/my-deployment my-container=nginx:1.19
Rollback the Deployment:
kubectl rollout undo deployment/my-deployment
Delete the Deployment:
kubectl delete -f deployment.yaml
4. Services
A Service provides stable networking for Pods. It abstracts the Pods behind a single IP address.
YAML Example: Service (ClusterIP)
apiVersion: v1
kind: Service
metadata:
name: my-service
spec:
selector:
app: my-app
ports:
- protocol: TCP
port: 80
targetPort: 80
type: ClusterIP
Practical Steps
Save the YAML as
service.yaml
.Create the Service:
kubectl apply -f service.yaml
Verify the Service:
kubectl get services
Access the Service:
kubectl port-forward service/my-service 8080:80
Then open
http://localhost:8080
in your browser.Delete the Service:
kubectl delete -f service.yaml
5. ConfigMaps and Secrets
ConfigMap: Stores configuration data as key-value pairs.
Secret: Stores sensitive data like passwords or tokens.
YAML Example: ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
name: my-config
data:
app.properties: |
color=blue
size=large
YAML Example: Secret
apiVersion: v1
kind: Secret
metadata:
name: my-secret
type: Opaque
data:
username: YWRtaW4= # base64 encoded
password: MWYyZDFlMmU2N2Rm # base64 encoded
Practical Steps
Create the ConfigMap and Secret:
kubectl apply -f configmap.yaml kubectl apply -f secret.yaml
Verify:
kubectl get configmaps kubectl get secrets
Use them in a Pod:
env: - name: COLOR valueFrom: configMapKeyRef: name: my-config key: color - name: PASSWORD valueFrom: secretKeyRef: name: my-secret key: password
6. PersistentVolumes and PersistentVolumeClaims
PersistentVolume (PV): Represents physical storage in the cluster.
PersistentVolumeClaim (PVC): Requests storage from a PV.
YAML Example: PV and PVC
# PersistentVolume
apiVersion: v1
kind: PersistentVolume
metadata:
name: my-pv
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteOnce
hostPath:
path: /mnt/data
# PersistentVolumeClaim
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: my-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
Practical Steps
Create the PV and PVC:
kubectl apply -f pv.yaml kubectl apply -f pvc.yaml
Verify:
kubectl get pv kubectl get pvc
Use the PVC in a Pod:
volumes: - name: my-storage persistentVolumeClaim: claimName: my-pvc
7. Namespaces
Namespaces provide logical partitioning of resources in a cluster.
YAML Example: Namespace
apiVersion: v1
kind: Namespace
metadata:
name: my-namespace
Practical Steps
Create the Namespace:
kubectl apply -f namespace.yaml
Verify:
kubectl get namespaces
Create resources in the Namespace:
kubectl apply -f pod.yaml -n my-namespace
8. Jobs and CronJobs
Job: Runs a task to completion.
CronJob: Runs Jobs on a schedule.
YAML Example: Job
apiVersion: batch/v1
kind: Job
metadata:
name: my-job
spec:
template:
spec:
containers:
- name: my-container
image: busybox
command: ["echo", "Hello, Kubernetes!"]
restartPolicy: Never
YAML Example: CronJob
apiVersion: batch/v1
kind: CronJob
metadata:
name: my-cronjob
spec:
schedule: "*/1 * * * *"
jobTemplate:
spec:
template:
spec:
containers:
- name: my-container
image: busybox
command: ["echo", "Hello, Kubernetes!"]
restartPolicy: OnFailure
Practical Steps
Create the Job or CronJob:
kubectl apply -f job.yaml kubectl apply -f cronjob.yaml
Verify:
kubectl get jobs kubectl get cronjobs
By practicing these examples, you’ll gain a solid understanding of Kubernetes objects and how to manage them effectively. Let me know if you need further clarification or additional examples!