<p>Sometimes (not always) my app goes down when Kubernetes starts terminating old pods. It returns the HTTP 500 status as PHP cannot connect to SQL Proxy's sidecar container.</p>
<p>It all happens less often now, but I notice it still does. My last attempts have been to add livenessProbe and readinessProbe to the containers and include a preStop hook to make Apache quit gracefully. Plus adding a big terminationGracePeriodSeconds and adding the <code>-term_timeout=2000s</code> parameter to Cloud SQL Proxy trying to keep it alive  some more minutes.</p>
<p>Shouldn't all this make the old pod go away in peace? What else is missing? My k8s manifest is the following:</p>
<pre><code>apiVersion: apps/v1
kind: Deployment
metadata:
  name: myapp
spec:
  replicas: 3
  minReadySeconds: 60
  selector:
    matchLabels:
      run: myapp
  template:
    metadata:
      labels:
        run: myapp
    spec:
      readinessGates:
        - conditionType: &quot;cloud.google.com/load-balancer-neg-ready&quot;
      terminationGracePeriodSeconds: 2000
      containers:
      - name: myapp
        image: [my image URL goes here]
        lifecycle:
          preStop:
            exec:
              command: [&quot;sh&quot;, &quot;-c&quot;, &quot;sleep 11 &amp;&amp; /usr/sbin/apachectl graceful-stop&quot; ]
        resources:
          requests:
            memory: 1Gi
            cpu: 500m
          limits:
            memory: 1Gi
            cpu: 500m
        ports:
        - containerPort: 80
        startupProbe:
          httpGet:
            path: /admin/
            port: 80
        readinessProbe:
          httpGet:
            path: /admin/
            port: 80
        livenessProbe:
          httpGet:
              path: /admin/
              port: 80
      - name: cloud-sql-proxy
        image: gcr.io/cloudsql-docker/gce-proxy:1.27.1-alpine
        command:
          - &quot;/cloud_sql_proxy&quot;
          - &quot;-instances=myproject-development:southamerica-east1:mysql8-testing=tcp:3306&quot;
          - &quot;-term_timeout=2000s&quot;
        securityContext:
          runAsNonRoot: true
        startupProbe:
            exec:
              command: [&quot;nc&quot;, &quot;-z&quot;, &quot;127.0.0.1&quot;, &quot;3306&quot;]
        readinessProbe:
            exec:
              command: [&quot;nc&quot;, &quot;-z&quot;, &quot;127.0.0.1&quot;, &quot;3306&quot;]
        livenessProbe:
            exec:
              command: [&quot;nc&quot;, &quot;-z&quot;, &quot;127.0.0.1&quot;, &quot;3306&quot;]
        resources:
          requests:
            memory: &quot;2Gi&quot;
            cpu:    &quot;1&quot;
---
apiVersion: &quot;autoscaling/v2beta1&quot;
kind: &quot;HorizontalPodAutoscaler&quot;
metadata:
  name: &quot;myapp-hpa&quot;
  namespace: &quot;default&quot;
  labels:
    run: &quot;myapp&quot;
spec:
  scaleTargetRef:
    kind: &quot;Deployment&quot;
    name: &quot;myapp&quot;
    apiVersion: &quot;apps/v1&quot;
  minReplicas: 1
  maxReplicas: 7
  metrics:
  - type: &quot;Resource&quot;
    resource:
      name: &quot;memory&quot;
      targetAverageUtilization: 80
---
apiVersion: v1
kind: Service
metadata:
  name: myapp-service
  annotations:
    cloud.google.com/neg: '{&quot;exposed_ports&quot;: {&quot;80&quot;:{&quot;name&quot;: &quot;myapp-neg&quot;}}}'
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
  selector:
    run: myapp
</code></pre>

deniable_encryption
<p>You should add the <code>periodSeconds</code>: How often (in seconds) to perform the probe. Default to 10 seconds. Minimum value is 1.</p>
<p>So if you set the <code>periodSeconds</code> very less like <code>1</code> K8s will check the status of POD every 1 second, very frequently and there could be chances you wont get the 500 response.</p>
<p>As soon as the application stops giving <strong>200</strong> at the endpoint K8s will mark the <strong>POD</strong> unready and no more traffic will get routed to it.</p>
