Kubernetes
July 18, 2023

Создаём deployment с pvc и скейлим его. Изучение поведения.

Как поведут себя pv/pvc при скейле деплоймента? Для изучения будет использоваться тип диска yc-network-hdd.

Поехали!

cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc-nginx11
  namespace: default
  labels:
    pvc: nginx
  annotations:
    author: cameda
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 6Gi
EOF
cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cam-nginx
  namespace: default
  labels:
    app: cam-nginx
  annotations:
    author: cameda
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate  
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
        - name: nginx
          image: nginx:latest
          imagePullPolicy: IfNotPresent
          resources:
            requests:
              cpu: 250m
              memory: 300Mi
            limits:
              memory: 600Mi 
          ports:
            - containerPort: 80
            - containerPort: 443
          volumeMounts:
          - name: cameda-volume
            mountPath: /cameda             
      volumes: 
      - name: cameda-volume
        persistentVolumeClaim:
          claimName: pvc-nginx11
EOF
kubectl get deploy | grep "cam-nginx"
cam-nginx                          1/1     1            1           67s

Теперь поскейлим deployment и посмотрим что будет с pvc в данном случае.

kubectl scale deploy cam-nginx --replicas=2 
kubectl get deploy | grep "cam-nginx"
cam-nginx                          1/2     2            1           4m26s

kubectl get po | grep "cam-nginx"
cam-nginx-7dfb4cc9d-2c4mx                           0/1     ContainerCreating   0             2m2s
cam-nginx-7dfb4cc9d-wp7tw                           1/1     Running             0             4m37s

В эвентах пода, в котором не создаётся контейнер.

Events:
  Type     Reason              Age    From                     Message
  ----     ------              ----   ----                     -------
  Normal   Scheduled           2m21s  default-scheduler        Successfully assigned default/cam-nginx-7dfb4cc9d-2c4mx to cl1k0hr81fu4s6fuqlue-ejuj
  Warning  FailedAttachVolume  2m22s  attachdetach-controller  Multi-Attach error for volume "pvc-23713137-ddbc-4792-83da-da06cfa96d7d" Volume is already used by pod(s) cam-nginx-7dfb4cc9d-wp7tw
  Warning  FailedMount         19s    kubelet                  Unable to attach or mount volumes: unmounted volumes=[cameda-volume], unattached volumes=[kube-api-access-r58lz cameda-volume]: timed out waiting for the condition

Итак, поскейлить деплоймент в котором используется pvc с типом ReadWriteOnce не получится.

Удалим deployment

kubectl delete deploy cam-nginx

И создадим снова с использованием Affinity.

cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
  name: cam-nginx
  namespace: default
  labels:
    app: cam-nginx
  annotations:
    author: cameda
spec:
  replicas: 1
  selector:
    matchLabels:
      app: nginx
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate  
  template:
    metadata:
      labels:
        app: nginx
    spec:
      affinity:
        podAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
          - labelSelector:
              matchExpressions:
              - key: app
                operator: In
                values: 
                - nginx 
            topologyKey: "kubernetes.io/hostname"
      containers:
        - name: nginx
          image: nginx:latest
          imagePullPolicy: IfNotPresent
          resources:
            requests:
              cpu: 250m
              memory: 300Mi
            limits:
              memory: 600Mi 
          ports:
            - containerPort: 80
            - containerPort: 443
          volumeMounts:
          - name: cameda-volume
            mountPath: /cameda             
      volumes: 
      - name: cameda-volume
        persistentVolumeClaim:
          claimName: pvc-nginx11
EOF
kubectl scale deploy cam-nginx --replicas=2 
kubectl get deploy | grep "cam-nginx"
cam-nginx                          2/2     2            2           19m

На этот раз всё удачно поскейлилось.