PHP CURL failed with Operation timeout in Kubernetes CronJob

4/22/2020

I have a cronJob that runs at some time interval to download images from remote servers. I had alpine-php:7.2-fpm docker image. It works fine with some of the URLs. but it is failing with some URLs.

Here is the code for CURL

$fp = fopen($fileNameWithPath, 'w');

$ch = curl_init();
curl_setopt_array($ch, array(
        CURLOPT_URL => $url,
        CURLOPT_FILE => $fp,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_FOLLOWLOCATION => true,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "GET",
        CURLOPT_CONNECTTIMEOUT => 90,
        CURLOPT_TIMEOUT => 180,
        CURLOPT_SSL_VERIFYHOST => 0,
        CURLOPT_SSL_VERIFYPEER => 0,
        CURLOPT_VERBOSE => 1
));        
$result = curl_exec($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
fclose($fp);

I had enabled verbose and the logs in Kubernetes pods gives the following output

* TCP_NODELAY set 
* Connected to images.asos-media.com (23.32.5.80) port 443 (#0) 
* ALPN, offering http/1.1 
* successfully set certificate verify locations: 
*   CAfile: /etc/ssl/certs/ca-certificates.crt 
  CApath: none 
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384 
* ALPN, server accepted to use http/1.1 
* Server certificate: 
*  subject: C=GB; L=London; O=ASOS.com Limited; CN=*.asos-media.com 
*  start date: Feb 26 00:00:00 2020 GMT 
*  expire date: May 27 12:00:00 2021 GMT 
*  issuer: C=US; O=DigiCert Inc; OU=www.digicert.com; CN=DigiCert Secure Site ECC CA-1 
*  SSL certificate verify ok. 
> GET /products/wonderbra-new-ultimate-strapless-bra-a-g-cup/5980845-1-beige?$XXL$ HTTP/1.1 
Host: images.asos-media.com 
Accept: */* 
Accept-Encoding: deflate, gzip 
* old SSL session ID is stale, removing 

* Operation timed out after 180000 milliseconds with 0 bytes received 
* Closing connection 0 

If I run this code from docker-image locally it works fine.


Kubernetes Deployment Files

CronJoB

apiVersion: batch/v1beta1
kind: CronJob
metadata:
  namespace: scheduleApp
  name: imagedownlload
  labels:
    app: scheduleApp
spec:
  schedule: "5 */4 * * *" # Specify schedule using linux cron syntax
  concurrencyPolicy: Allow
  successfulJobsHistoryLimit: 1
  failedJobsHistoryLimit: 2
  jobTemplate:
    spec:
      parallelism: 1 # Number of Pods start together with Job
      template:
        metadata:
          labels:
            tier: cronservice
        spec:
          volumes:
            - name: pv-restorage
              persistentVolumeClaim:
                claimName: pipeline-volumeclaim
          containers:
            - name: imagedownload
              image: gcr.io/{project_id}/{image_name}:v1.0.2 # Set the image tobe used in container with full repository URL
              envFrom:
                - configMapRef:
                    name: app-config
                - secretRef:
                    name: app-secret
              volumeMounts:
                - name: pv-restorage
                  mountPath: /var/www/html/restorage
          restartPolicy: Never

Service file

apiVersion: v1
kind: Service
metadata:
  name: cron-loadbalancer
  namespace: scheduleApp
spec:
  selector:
    tier: cronservice
  ports:
    - name: http
      protocol: TCP
      port: 80
      targetPort: 80
    - name: https
      protocol: TCP
      port: 443
      targetPort: 443
  sessionAffinity: None
  type: LoadBalancer

Dockerfile

FROM php:7.2-fpm-alpine

RUN apk update && apk add \
  libzip-dev \
  unzip \
  && docker-php-ext-configure zip --with-libzip \
  && docker-php-ext-install mysqli zip \
  && rm -rf /var/cache/apk/*

COPY --from=composer:latest /usr/bin/composer /usr/local/bin/composer

COPY composer.* /var/www/html/

RUN cd /usr/local/etc/php/conf.d/ \
  && echo 'memory_limit = -1' >> /usr/local/etc/php/conf.d/docker-php-memlimit.ini

WORKDIR /var/www/html

RUN composer install && composer clear-cache

COPY . /var/www/html/

ENTRYPOINT ["php","console"]

CMD ["-V"]
-- Kathak Dabhi
kubernetes-cronjob
php-curl

0 Answers