Launching your Kubernetes Cluster with Deployment

Launching your Kubernetes Cluster with Deployment

Day 32 & 33 for 90DayOfDevOps

ยท

6 min read

What is Deployment in k8s?

In Kubernetes, a Deployment is an abstraction that manages the deployment and scaling of a set of identical Pods. It provides a declarative way to define the desired state of your application and ensures that the current state matches the desired state.

Key features and concepts of Deployments include:

  1. Declarative Configuration: You define the desired state of your application in a Deployment manifest, typically written in YAML. This includes specifying the container image, number of replicas, and other configurations.

  2. Pod Template: Deployments use a Pod template to create and update the Pods it manages. The template includes information such as the container image, volumes, and other settings. The Pods created by the Deployment are often referred to as "replica Pods."

  3. Scaling: Deployments allow you to easily scale the number of replicas (Pod instances) up or down based on demand. Scaling can be done manually or automatically through features like Horizontal Pod Autoscaling (HPA).

  4. Rolling Updates and Rollbacks: Deployments support rolling updates, allowing you to update the application without downtime. When you update the Deployment's configuration, it gradually replaces the old Pods with new ones. If issues arise, you can perform rollbacks to a previous stable version.

  5. Versioning and Rollout History: Deployments keep a history of all changes, making it easy to roll back to a previous version or examine the history of updates. This is valuable for troubleshooting and auditing changes.

  6. Self-Healing: If a Pod within a Deployment fails or is terminated, the Deployment controller automatically replaces it to maintain the desired number of replicas.

Basic example of a Deployment manifest in YAML:

yamlCopy codeapiVersion: apps/v1
kind: Deployment
metadata:
  name: my-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: my-app
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - name: my-app-container
        image: my-app-image:latest

In this example:

  • replicas: 3 specifies that the Deployment should maintain three replicas (Pods) of the application.

  • The selector specifies the labels used to match the Pods controlled by the Deployment.

  • The template defines the Pod template, including the container image and labels.

You can apply this manifest using kubectl apply -f filename.yaml, and Kubernetes will ensure that the specified number of replicas is running, updating them as needed.

Task 1:

Create one Deployment file to deploy a sample todo-app on K8s using "Auto-healing" and "Auto-Scaling" feature

apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-app-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: todo-app
  template:
    metadata:
      labels:
        app: todo-app
    spec:
      containers:
      - name: todo-app-container
        image: todo-app-image:latest
        ports:
        - containerPort: 80
  autoscaler:
    enabled: true
    minReplicas: 2
    maxReplicas: 5
    targetCPUUtilizationPercentage: 50

In this example:

  • replicas: 3 specifies that the Deployment should maintain three replicas (Pods) of the todo-app.

  • The selector specifies the labels used to match the Pods controlled by the Deployment.

  • The template defines the Pod template, including the container image and labels.

  • autoscaler section is added to enable Horizontal Pod Autoscaling (HPA). It sets the minimum and maximum number of replicas and the target CPU utilization percentage.

To apply this deployment to your Minikube cluster, save the above content in a file named deployment.yaml and run the following command:

kubectl apply -f deployment.yaml

What are Namespaces and Services in k8s:

Namespaces:

A Namespace in Kubernetes is a way to divide cluster resources between multiple users (or projects) within the same physical cluster. It provides a scope for names, allowing the same resource names to be used in different namespaces without conflict. Namespaces can be particularly useful in large and multi-team environments.

Key points about Namespaces:

  1. Isolation: Namespaces provide a level of isolation for resources. Resources created in one namespace are typically invisible to other namespaces.

  2. Scoping: Namespaces provide a way to organize and scope resources. For example, you can have different environments (like development, testing, and production) or different projects each in its own namespace.

  3. Default Namespace: When you create resources without specifying a namespace, they are placed in the default namespace. You can switch between namespaces using the kubectl command.

Example of creating a Namespace:

apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace

Apply this YAML file using kubectl apply -f my-namespace.yaml.

Services:

In Kubernetes, a Service is an abstraction that defines a logical set of Pods and a policy by which to access them. It provides a stable endpoint (IP address and port) that other applications can use to connect to the Pods.

Key points about Services:

  1. Stable Endpoint: A Service provides a stable IP address and port, which can be used by other services to communicate with the Pods behind it.

  2. Load Balancing: Services can distribute incoming network traffic across multiple Pods. This load balancing helps distribute the workload and ensures high availability.

  3. Service Types: Kubernetes supports different types of services, including ClusterIP (internal service), NodePort (exposes the service on a static port on each node's IP), LoadBalancer (provisions an external load balancer), and ExternalName (maps the service to a DNS name).

Example of creating a Service:

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
    - protocol: TCP
      port: 80
      targetPort: 8080

In this example:

  • The selector field is used to specify which Pods the Service should target.

  • The ports field defines the port on which the Service will be available and the target port on the Pods.

Apply this YAML file using kubectl apply -f my-service.yaml.

Task 2

  • Create a Namespace for your Deployment

  • Use the command kubectl create namespace <namespace-name> to create a Namespace

  • Update the deployment.yml file to include the Namespace

  • Apply the updated deployment using the command: kubectl apply -f deployment.yml -n <namespace-name>

  • Verify that the Namespace has been created by checking the status of the Namespaces in your cluster.

Step 1: Create a Namespace

Use the following command to create a Namespace. Replace <namespace-name> with your desired namespace name.

For example:

bashCopy codekubectl create namespace my-app-namespace

Step 2: Update Deployment YAML

Here's the updated deployment.yaml file with the namespace field added:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: todo-app-deployment
  namespace: my-app-namespace  # Add this line to specify the Namespace
spec:
  replicas: 3
  selector:
    matchLabels:
      app: todo-app
  template:
    metadata:
      labels:
        app: todo-app
    spec:
      containers:
      - name: todo-app-container
        image: your-todo-app-image:latest
        ports:
        - containerPort: 80
  autoscaler:
    enabled: true
    minReplicas: 2
    maxReplicas: 5
    targetCPUUtilizationPercentage: 50

Step 3: Apply the Updated Deployment

Apply the updated deployment.yaml file using the following command. Make sure to replace <namespace-name> with the actual namespace name you used.

For example:

kubectl apply -f deployment.yaml -n my-app-namespace

Step 4: Verify the Namespace

You can verify that the Namespace has been created by checking the status of the Namespaces in your cluster:

kubectl get namespaces

You should see your newly created Namespace in the list.

plaintextCopy codeNAME                STATUS   AGE
default             Active   10d
kube-node-lease     Active   10d
kube-public         Active   10d
kube-system         Active   10d
my-app-namespace    Active   2m

Now, your Deployment is running within the specified Namespace. If you have other resources associated with this deployment (e.g., services), you may need to update them to reference the correct Namespace as well.

๐Ÿ™Œ Thank you for reading my blog! ๐Ÿš€ If you found this information helpful, drop a comment and spread the knowledge! ๐Ÿ” For more Kubernetes insights and updates, feel free to follow me on:

LinkedIn: Sumit Katkar ๐ŸŒ

HashNode: Sumit Katkar's Blog ๐Ÿ“

Happy Learning! ๐ŸŽ‰

ย