Deploy Nginx and Phpfpm on Kubernetes Failed

@Inderpreet @devops503 @Tej-Singh-Rana
Hello, I has failed task without explanation, where has i wrong?



Pods has runing, nginx answered on the required port

My manifests:

kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-config
data:
nginx.conf: |
events {
}
http {
server {
listen 8092 default_server;
listen [::]:8092 default_server;

# Set nginx to serve files from the shared volume!
root /var/www/html;
index index.html index.htm index.php;
server_name _;

location / {
  try_files $uri $uri/ =404;
}

location ~ \.php$ {
  include fastcgi_params;
  fastcgi_index index.php;
  fastcgi_pass 127.0.0.1:9000;
}

}
}

kind: Pod
apiVersion: v1
metadata:
name: nginx-phpfpm
spec:
volumes:
# Create the shared files volume to be used in both pods
- name: shared-files
emptyDir: {}

# Add the ConfigMap we declared above as a volume for the pod
- name: nginx-config-volume
  configMap:
    name: nginx-config

containers:
# Our PHP-FPM application
- image: php:7.0-fpm
name: php-fpm-container
volumeMounts:
- name: shared-files
mountPath: /var/www/html

# Our nginx container, which uses the configuration declared above,
# along with the files shared with the PHP-FPM app.
- image: nginx:latest
  name: nginx-container
  volumeMounts:
    - name: shared-files
      mountPath: /var/www/html
    - name: nginx-config-volume
      mountPath: /etc/nginx/nginx.conf
      subPath: nginx.conf

For the same task, " kubectl get pods" shows always ‘ContainerCreating’ as shown below.

thor@jump_host /$ kubectl apply -f pod.yml
pod/nginx-phpfpm created
thor@jump_host /$ kubectl get pods
NAME           READY   STATUS              RESTARTS   AGE
nginx-phpfpm   0/2     ContainerCreating   0          8s
thor@jump_host /$ 

Any idea on this please?

@dushasokol Did you used same command (kubectl apply) to deploy the application?

balu.networks7
Make kubectl describe pods.Most likely some image is still “pulling”.

I had appied by:
kubectl apply -f nginx_config.yml
kubectl apply -f pod.yml

See my second screenshoot, on it nginx answered after the pods running

1 Like

@Inderpreet @kodecloud @rahul456 @kodekloud-support3

As shown in below screenshot, PODs are running successfully. But Task got failed without mentioning any reasons. Can you please check what is the problem here. Thanks for your help.

I found why it fails. Verifying script try to connect to nginx and take “/” location, no “/index.php”. New config solve this. And i suppose authors need to fix task description.

cat << EOF > nginx_config.yml
kind: ConfigMap
apiVersion: v1
metadata:
name: nginx-config
data:
nginx.conf: |
events {

}

http {
  server {
    listen 8093 default_server;
    listen [::]:8093 default_server;
    
    # Set nginx to serve files from the shared volume!
    root /var/www/html;
    index index.html index.htm index.php;
    server_name _;

    location / {
      rewrite .* /index.php;
    }

    location ~ \.php$ {
      include fastcgi_params;
      fastcgi_index index.php;
      fastcgi_pass 127.0.0.1:9000;
      fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
      fastcgi_param REQUEST_METHOD \$request_method;
    }
  }
}

EOF

how do you test this? after checking the pod is running, I use curl to test, but got forbidden error? is this ok for this task?
then i go in the pod, can’t find the /etc/nginx.conf file

curl http://10.44.0.1:8093

403 Forbidden

403 Forbidden


nginx/1.19.2

403 means its no exist index.php, but kodekloud’s verifying script will create it by itself, when you finish the task
For selfcheck:

kubectl exec -it podname – /sh/bash

you will be taken to the directory /var/www/html

echo “<?phpinfo();?>” > index.php

and exit.
After it curl from jumphost will be work

curl 10.44.0.1:8093

For checking nginx.conf you need also specify “-c nginx-container-name” in exec command

3 Likes

@Inderpreet
the validation script might be a bug, I check the configuration, nginx and pod all in running state. can you mark this success for me - jenna
thank you.

1 Like

@Inderpreet @mmumshad

lately, the lab environment is loading slow, including the kodekloud engineer and even in the kubernetes CKA course. Is there a way to speed the loading process? could be this cause the validation failed?

image

@balu.networks7, sorry for inconvenience this is marked pending for you, please give it an another try

1 Like

hi @Jenna, sorry for inconvenience this is marked pending for you, please give it an another try

Ok this was a hard task for me,

This was key for troubleshooting

@dushasokol One thing that I don’t understand is why you used this:

I mean, I know this is the address this link uses, but I have used before this parameter with the unix socket like this fastcgi_pass unix:/var/run/php/php<YourPHPVersionHere>-fpm.sock; but that doesn’t work for me this time.

Can you explain me why the address works and the socket doesn’t work?

Thank you in advance!

It depends of php “listen” value.
You need to check “listen” parametr in /etc/php/fpm-<YourPHPVersionHere>'.X/php-fpm.conf

1 Like

@dushasokol I am seeing “File not found” for curl cmd, as shown below. But index.php exists in /var/www/html

thor@jump_host /$ kubectl exec --stdin --tty nginx-phpfpm -- /bin/bash
Defaulting container name to php-fpm-container.
Use 'kubectl describe pod/nginx-phpfpm -n default' to see all of the containers in this pod.
root@nginx-phpfpm:/var/www/html# ls
root@nginx-phpfpm:/var/www/html# echo "<?phpinfo();?>" > index.php
root@nginx-phpfpm:/var/www/html# ls
index.php
root@nginx-phpfpm:/var/www/html# cat index.php
<?phpinfo();?>
root@nginx-phpfpm:/var/www/html# exit
exit
thor@jump_host /$ kubectl describe pod nginx-phpfpm | grep Ip
thor@jump_host /$ kubectl describe pod nginx-phpfpm | grep IP
IP:           10.44.0.1
IPs:
  IP:  10.44.0.1
thor@jump_host /$ curl 10.44.0.1:8097
File not found.
thor@jump_host /$

Can you please guide me to fix this.

I tried following things:

  1. Made sure proper permission for /var/www/html folder
  2. Added index.php manually and gave full permission

I really have no idea. But I would check the “rootdir” in nginx.conf.
And also i would create “test.htm” and will make curl 10.44.0.1:8097/index.htm.
If it does not help me, i would redeploy pod without configmap and try to change config manually step-by-step from default to needed for me options

1 Like

@balu.networks7 be careful, you are not specifying in which container to execute commands, so it is choosing the php-fpm-container, but you need to test in the web server container (the other)

Check docs on how to exec in a pod with multiple containers:

2 Likes

If anyone still facing any problems , you can refer to the below link for your reference. Hope it helps!

https://matthewpalmer.net/kubernetes-app-developer/articles/php-fpm-nginx-kubernetes.html

Thank you,
Dinesh

1 Like