Deployment

Deployment

A Deployment in Kubernetes is a higher-level abstraction that manages the creation, update, and scaling of a set of pods. Deployments provide a declarative way to define and manage application lifecycle, ensuring that the desired state of the application is maintained.

Basic setup

deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: crazy-backend
spec:
  replicas: 1
  selector:
    matchLabels:
      app: crazy-backend
  template:
    metadata:
      labels:
        app: crazy-backend
    spec:
      containers:
      - name: backend
        image: registry.gitlab.com/bettersolutions/tools/common-images/connection-checker:1.0.7361650064
        ports:
        - containerPort: 3000

Debug the deployment:

kubectl describe deployment crazy-backend

Debug pod:

kubectl describe pod crazy-backend-xxx

Image Pull Secrets

Image Pull Secrets in Kubernetes store credentials needed to authenticate with private container registries, allowing your cluster to securely pull container images. They automate the process of accessing private registries, ensuring that sensitive information is securely managed and not exposed.

kubectl create secret docker-registry gitlab-auth --docker-server=https://registry.gitlab.com --docker-username=gitlab-ci-token --docker-password=TOKEN --docker-email=super.crazy@mail.com
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: crazy-backend
spec:
 replicas: 1
 selector:
   matchLabels:
     app: crazy-backend
 template:
   metadata:
     labels:
       app: crazy-backend
   spec:
     containers:
     - name: backend
       image: registry.gitlab.com/bettersolutions/tools/common-images/connection-checker:1.0.7361650064
       ports:
       - containerPort: 3000
     imagePullSecrets:
     - name: gitlab-auth

Resources

Requests and Limits: Define the CPU and memory resources required by the containers. Requests specify the minimum resources required, while limits specify the maximum resources that can be used.

Units: CPU are measured in milliCPUs (m or millicores) or cores. For example, 500m equals 0.5 CPU, which is half of one CPU core.

deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: crazy-backend
spec:
 replicas: 1
 selector:
   matchLabels:
     app: crazy-backend
 template:
   metadata:
     labels:
       app: crazy-backend
   spec:
     containers:
     - name: backend
       image: registry.gitlab.com/bettersolutions/tools/common-images/connection-checker:1.0.7361650064
       ports:
       - containerPort: 3000
       resources:
         requests:
           memory: "64Mi"
           cpu: "250m"
         limits:
           memory: "128Mi"
           cpu: "500m"
     imagePullSecrets:
     - name: gitlab-auth

Health Checks

Liveness Probes: Ensure that containers are running. If a container fails the liveness probe, it is restarted.

Readiness Probes: Ensure that containers are ready to handle traffic.

deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: crazy-backend
spec:
 replicas: 1
 selector:
   matchLabels:
     app: crazy-backend
 template:
   metadata:
     labels:
       app: crazy-backend
   spec:
     containers:
     - name: backend
       image: registry.gitlab.com/bettersolutions/tools/common-images/connection-checker:1.0.7361650064
       ports:
       - containerPort: 3000
       resources:
         requests:
           memory: "64Mi"
           cpu: "250m"
         limits:
           memory: "128Mi"
           cpu: "500m"
       livenessProbe:
         httpGet:
           path: /health
           port: 3000
         initialDelaySeconds: 3
         periodSeconds: 10
       readinessProbe:
         httpGet:
           path: /health
           port: 3000
         initialDelaySeconds: 3
         periodSeconds: 10
     imagePullSecrets:
     - name: gitlab-auth

Envs

deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: crazy-backend
spec:
 replicas: 1
 selector:
   matchLabels:
     app: crazy-backend
 template:
   metadata:
     labels:
       app: crazy-backend
   spec:
     containers:
     - name: backend
       image: registry.gitlab.com/bettersolutions/tools/common-images/connection-checker:1.0.7361650064
       env:
       - name: POPE
         value: "2137"
       - name: FOO
         value: "BAR"
       ports:
       - containerPort: 3000
       resources:
         requests:
           memory: "64Mi"
           cpu: "250m"
         limits:
           memory: "128Mi"
           cpu: "500m"
       livenessProbe:
         httpGet:
           path: /health
           port: 3000
         initialDelaySeconds: 3
         periodSeconds: 10
       readinessProbe:
         httpGet:
           path: /health
           port: 3000
         initialDelaySeconds: 3
         periodSeconds: 10
     imagePullSecrets:
     - name: gitlab-auth

Configmap

ConfigMaps in Kubernetes are used to store non-confidential data in key-value pairs, allowing you to decouple configuration artifacts from container images to keep containerized applications portable. They enable you to manage configuration data centrally and provide it to your applications via environment variables, command-line arguments, or configuration files.

configmap.yaml
apiVersion: v1
kind: ConfigMap
metadata:
 name: backend-config
data:
 lorem-ipsum.txt: |
   Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
deployment.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
 name: crazy-backend
spec:
 replicas: 1
 selector:
   matchLabels:
     app: crazy-backend
 template:
   metadata:
     labels:
       app: crazy-backend
   spec:
     containers:
     - name: backend
       image: registry.gitlab.com/bettersolutions/tools/common-images/connection-checker:meme-generator
       env:
       - name: POPE
         value: "2137"
       - name: FOO
         value: "BAR"
       ports:
       - containerPort: 8000
       resources:
         requests:
           memory: "64Mi"
           cpu: "250m"
         limits:
           memory: "128Mi"
           cpu: "500m"
       livenessProbe:
         httpGet:
           path: /health
           port: 8000
         initialDelaySeconds: 3
         periodSeconds: 10
       readinessProbe:
         httpGet:
           path: /health
           port: 8000
         initialDelaySeconds: 4
         periodSeconds: 15
       volumeMounts:
       - name: config-volume
         mountPath: /etc/config
         subPath: lorem-ipsum.txt
     imagePullSecrets:
     - name: gitlab-auth
     volumes:
     - name: config-volume
       configMap:
         name: backend-config
         items:
         - key: lorem-ipsum.txt
           path: lorem-ipsum.txt