Kubernetes pod/deploy/sts/ds example
March 4, 2023

Deployment/Pod/Statefulset c PV emptyDir

Для создания таких ресурсов можно использовать следующие манифесты.

Deployment.

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cam-nginx
  namespace: default
  labels:
    env: prod
  annotations:
    author: cameda
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate 
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: cam-nginx
          image: nginx:latest
          imagePullPolicy: IfNotPresent
          resources:
            requests:
              cpu: 250m
              memory: 300Mi
            limits:
              memory: 300Mi
          ports:
            - containerPort: 80
            - containerPort: 443
          livenessProbe:
            failureThreshold: 3
            successThreshold: 1
            httpGet:
              path: /
              port: 80
            periodSeconds: 10
            timeoutSeconds: 5
            initialDelaySeconds: 20
          readinessProbe:
            failureThreshold: 3
            successThreshold: 1
            exec:
              command:
              - curl
              - http://127.0.0.1:80
            periodSeconds: 10
            timeoutSeconds: 5
            initialDelaySeconds: 20
          volumeMounts:
          - name: cameda-volume
            mountPath: /cameda
      restartPolicy: Always
      volumes:
      - name: cameda-volume
        emptyDir: {}
EOF

Проверяем, что все загрузилось:

kubectl get deploy
NAME        READY   UP-TO-DATE   AVAILABLE   AGE
cam-nginx   3/3     3            3           48m

Все 3 реплики пода создались и находятся в Ready.

kubectl get po

Сделаем describe pod:

kubectl describe po pod_name.

Видим там, что все успешно примонтировалось:
Mounts:
/cameda from cameda-volume (rw)
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-ptbcq (ro)
Volumes:
cameda-volume:
Type: EmptyDir (a temporary directory that shares a pod's lifetime)

Зайдем в под и убедимся, что в примонтированную директорию можно записать информацию:

kubectl exec --stdin --tty cam-nginx-pod -- /bin/bash
cd /cameda && touch file && ls

Запись проводится. Все в порядке.

Pod.

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Pod
metadata:
  name: cameda-nginx
  namespace: default
  labels:
    app: nginx
  annotations:
    author: cameda
spec:
  containers:
  - name: cameda-nginx
    image: nginx:latest
    imagePullPolicy: IfNotPresent
    resources:
      requests:
        cpu: 300m
        memory: 500Mi
      limits:
        memory: 800Mi
    ports:
      - containerPort: 80
      - containerPort: 443
    livenessProbe:
      failureThreshold: 3
      successThreshold: 1
      httpGet:
        path: /
        port: 80
      periodSeconds: 10 
      timeoutSeconds: 5
      initialDelaySeconds: 20
    readinessProbe:
      failureThreshold: 3
      successThreshold: 1
      exec:
        command:
        - curl
        - http://127.0.0.1:80
      periodSeconds: 10
      timeoutSeconds: 5
      initialDelaySeconds: 20
    volumeMounts:
    - name: dir-volume
      mountPath: /dir
  restartPolicy: OnFailure
  volumes:
  - name: dir-volume
    emptyDir: {}
EOF

StetefulSet.

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: StatefulSet
metadata:
  name: cam-redis
  namespace: default
  labels:
    app: redis
  annotations:
    author: cameda
spec:
  serviceName: cam-redis
  replicas: 1
  selector:
    matchLabels:
      app: cam-redis
  template:
    metadata:
      labels:
        app: cam-redis
    spec:
      containers:
      - name: redis
        image: redis:latest
        imagePullPolicy: IfNotPresent
        resources:
          requests:
            cpu: 1
            memory: 1Gi
          limits:
            memory: 2Gi
        ports:
         - containerPort: 6379
         - containerPort: 26379
         - containerPort: 80
        volumeMounts:
          - name: cameda-volume
            mountPath: /cameda
      restartPolicy: Always                
      volumes:
      - name: cameda-volume
        emptyDir: {}
EOF