Kubernetes Deployments and Replica Sets
Control Plane Login
- Log into the control plane (master node) to get started.
- Verify nodes are ready using
kubectl get nodes.
Cluster IP and Node Ports
- Cluster IP: Allows pods to communicate within the cluster.
- NodePort: Exposes pods to the internet.
Replica Sets
Definition: Replica sets ensure a specified number of pod replicas are running.
YAML File Creation: Create a
replica-sets.yamlfile.Label Matching: Ensure the app name in the label matches the selector.
Example:
apiVersion: apps/v1 kind: ReplicaSet metadata: name: my-replica-set spec: replicas: 2 selector: matchLabels: app: my-app template: metadata: labels: app: my-app spec: containers: - name: my-container image: <image-name> ports: - containerPort: 8080Port Configuration: Define the container port (e.g., 8080).
Match Labels: The
matchLabelsin the replica set selector must match the labels in the pod template metadata.Replication: The number of replicas determines how many pods are created.
Example: If replicas = 2, two pods will be created.
Replica Set Template: The template defines the pod configuration.
NodePort Service
- Service Definition: Exposes the replica set to the internet.
- Label Matching: The service selector must match the pod labels.
*Example:yaml apiVersion: v1 kind: Service metadata: name: my-app-service spec: selector: app: my-app ports: - port: 80 targetPort: 8080 nodePort: 30200 type: NodePort - Port Configuration: Define the service port, target port, and NodePort.
- Service Type: Set the service type to NodePort.
- NodePort Range: Custom NodePorts must be within the range of 30,000 - 32,767. Kubernetes can assign a random port if not specified.
- Accessing the Service: Access the application via the node's public IP and NodePort.
Replica Sets in Action
- Creating Replica Sets: Use
kubectl apply -f replica-sets.yaml. - Verifying Replica Sets: Use
kubectl get replica sets. - Verifying Pods: Use
kubectl get pods. - Describing the Service: Use
kubectl describe service <service-name>to inspect the service and its endpoints. - Testing the Application: Access the application via the node's public IP and NodePort, followed by the application's endpoint (e.g.,
/hello). - Automatic Pod Replacement: If a pod is deleted, the replica set automatically recreates it.
Deployments
- Definition: Deployments provide declarative updates for pods and replica sets.
- Benefits: Enable scaling, rolling updates, and rollbacks.
- YAML File Creation: Create a
deploy.yamlfile. - Kind: Set the kind to
Deployment. - Replicas: Specify the desired number of pod replicas.
- Selector: Define the selector to match the pod labels.
- Template: Define the pod template, including labels, container image, and ports.
*Example:yaml apiVersion: apps/v1 kind: Deployment metadata: name: my-web-deployment labels: app: my-web spec: replicas: 2 selector: matchLabels: app: my-web template: metadata: labels: app: my-web spec: containers: - name: my-web-container image: <image-name> ports: - containerPort: 8080
Connecting Service to Deployment
- Service Definition: Create a service to expose the deployment.
- Selector: The service selector must match the pod labels in the deployment.
- Port Configuration: Define the service port, target port, and NodePort.
Scaling Deployments
- Manual Scaling: Use
kubectl scale --replicas=<number> deployment/<deployment-name>to scale the deployment.
Example:shell kubectl scale --replicas=5 deployment/my-web-deployment
Rolling Updates and Rollbacks
- Deployment Strategies: Define how updates are applied to the pods.
- RollingUpdate: Updates pods gradually to avoid downtime. ( Default strategy)
- maxUnavailable: Specifies the maximum number of pods that can be unavailable during the update.
- maxSurge: Specifies the maximum number of pods that can be created above the desired number during the update.
- Recreate: Kills all pods before creating new ones.
- RollingUpdate: Updates pods gradually to avoid downtime. ( Default strategy)
- Annotations: Metadata used for deployment tracking and modification (e.g., versioning).
- Updating Deployments: Modify the deployment YAML file and apply the changes using
kubectl apply -f deploy.yaml.
Deployment History
- Checking History: Use
kubectl rollout history deployment/<deployment-name>to view the deployment history. - Rolling Back: Use
kubectl rollout undo deployment/<deployment-name> --to-revision=<revision-number>to roll back to a specific revision.
Importance of Replica Sets and Deployments
- Maintaining Replicas: Replica sets ensure the desired number of pods are always running.
- Zero Downtime Deployments: Rolling updates in deployments allow for updates without downtime.
- Scaling: Deployments enable easy scaling of applications.
- Rollbacks: Deployments allow for easy rollbacks to previous versions.