Kubernetes Simplified: Embarking on the Zero to Hero Journey - Part 2
Tags: kubernetes, tutorial, beginners
Published: 2024-03-13 00:01:46

In the previous post, we discussed the basics of Kubernetes and containerization. In this post, we will continue our journey and explore some best practices for deploying and managing containerized applications using Kubernetes.

## Namespaces

Namespaces are a way to divide cluster resources between multiple users or teams. They provide a scope for names, which helps avoid naming conflicts. By using namespaces, you can create a logical separation between different parts of your application.

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

## Readiness and Liveness Probes

Readiness and liveness probes are essential for ensuring that your application is running correctly. Readiness probes check if your application is ready to receive traffic, while liveness probes check if your application is still running. By using these probes, you can ensure that your application is always available.

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    readinessProbe:
      httpGet:
        path: /healthz
        port: 8080
      initialDelaySeconds: 5
      periodSeconds: 10
    livenessProbe:
      httpGet:
        path: /healthz
        port: 8080
      initialDelaySeconds: 15
      periodSeconds: 20
```

## Autoscaling

Autoscaling allows you to automatically adjust the number of replicas of your application based on demand. This ensures that your application can handle traffic spikes without manual intervention.

```yaml
apiVersion: autoscaling/v1
kind: HorizontalPodAutoscaler
metadata:
  name: my-hpa
spec:
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: my-deployment
  minReplicas: 1
  maxReplicas: 10
  targetCPUUtilizationPercentage: 50
```

## Resource Requests and Limits

Resource requests and limits allow you to specify the amount of CPU and memory that your application needs. By using these, you can ensure that your application has enough resources to run correctly.

```yaml
apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
  - name: my-container
    image: my-image
    resources:
      requests:
        cpu: 100m
        memory: 128Mi
      limits:
        cpu: 200m
        memory: 256Mi
```

## Deploying Your Pods

Deploying your Pods as part of a Deployment, DaemonSet, ReplicaSet or StatefulSet across nodes ensures that your application is highly available and fault-tolerant.

```yaml
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: my-container
        image: my-image
```

## Multiple Nodes

Using multiple nodes ensures that your application can handle traffic spikes and is highly available.

```yaml
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - name: http
    port: 80
    targetPort: 8080
  type: LoadBalancer
```

## Role-based Access Control (RBAC)

RBAC allows you to control access to your Kubernetes resources. By using RBAC, you can ensure that only authorized users have access to your resources.

```yaml
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
  name: my-role
rules:
- apiGroups: [""]
  resources: ["pods"]
  verbs: ["get", "watch", "list"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
  name: my-role-binding
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: Role
  name: my-role
subjects:
- kind: User
  name: my-user
```

## Hosting Your Kubernetes Cluster Externally

Hosting your Kubernetes cluster externally using a cloud provider ensures that your application is highly available and fault-tolerant.

```yaml
apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - name: http
    port: 80
    targetPort: 8080
  type: Load
```

## Conclusion

In this post, we explored some best practices for deploying and managing containerized applications using Kubernetes. By following these best practices, you can ensure that your application is highly available, fault-tolerant, and always running smoothly.

Photo by <a href="https://unsplash.com/@growtika?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Growtika</a> on <a href="https://unsplash.com/photos/background-pattern-GSiEeoHcNTQ?utm_content=creditCopyText&utm_medium=referral&utm_source=unsplash">Unsplash</a>