DEV Community

Cover image for Kubernetes Simplified: Embarking on the Zero to Hero Journey - Part 2
Scott Gordon
Scott Gordon

Posted on

Kubernetes Simplified: Embarking on the Zero to Hero Journey - Part 2

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.

apiVersion: v1
kind: Namespace
metadata:
  name: my-namespace
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

Multiple Nodes

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

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - name: http
    port: 80
    targetPort: 8080
  type: LoadBalancer
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

Hosting Your Kubernetes Cluster Externally

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

apiVersion: v1
kind: Service
metadata:
  name: my-service
spec:
  selector:
    app: my-app
  ports:
  - name: http
    port: 80
    targetPort: 8080
  type: Load
Enter fullscreen mode Exit fullscreen mode

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 Growtika on Unsplash

Top comments (0)