Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Deploying Apps on Kubernetes

1. Introduction

Kubernetes is an open-source platform designed to automate deploying, scaling, and operating application containers. This lesson will guide you through the process of deploying applications on Kubernetes, covering key concepts, architecture, and practical steps.

2. Prerequisites

  • Basic understanding of Linux commands.
  • Familiarity with containerization (Docker).
  • Kubernetes installed on your machine or access to a Kubernetes cluster.

3. Kubernetes Architecture

Kubernetes architecture consists of a master node and multiple worker nodes. The master node contains the control plane, while the worker nodes run the applications. Major components include:

  • Control Plane: Manages the Kubernetes cluster.
  • Node: A worker machine in Kubernetes.
  • Pod: The smallest deployable unit in Kubernetes.
  • Service: Exposes a set of pods as a network service.

4. Deploying Applications

4.1 Creating a Deployment

To deploy an application, you need to create a Deployment resource. Here’s a basic example of deploying an Nginx application:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Save the above YAML to a file named nginx-deployment.yaml and deploy it using:

kubectl apply -f nginx-deployment.yaml

4.2 Exposing the Application

After deploying, expose your application using a Service:

apiVersion: v1
kind: Service
metadata:
  name: nginx-service
spec:
  type: LoadBalancer
  ports:
    - port: 80
  selector:
    app: nginx

Apply the Service configuration with:

kubectl apply -f nginx-service.yaml

5. Best Practices

  • Use versioned images for deployments.
  • Define resource requests and limits for containers.
  • Utilize health checks (liveness and readiness probes).
  • Implement logging and monitoring solutions.
  • Regularly update and patch your Kubernetes cluster.

6. FAQ

What is a Pod?

A Pod is the smallest deployable unit in Kubernetes, which can contain one or more containers that share the same network namespace.

How can I scale my application?

You can scale your application by modifying the replicas field in your Deployment configuration or using the command:

kubectl scale deployment nginx-deployment --replicas=5
What is the difference between a Deployment and a StatefulSet?

A Deployment manages stateless applications, while a StatefulSet is used for stateful applications requiring stable identities and storage.