Answer or sh script for task: “Deploy Nginx and Phpfpm on Kubernetes”

Hi, seem to be failing on this task. Does anyone have a reference yaml file or the yaml files which worked?

I tried using the advise from Hao Le, but it does not seem to recognize helm even after installing it.

The was the original commands and yaml files used:

The Nautilus Application Development team is planning to deploy one of the php-based application on Kubernetes cluster. As per discussion with DevOps team they have decided to use nginx and phpfpm. Additionally, they shared some custom configuration requirements. Below you can find more details. Please complete the task as per requirements mentioned below:



1) Create a service to expose this app, the service type must be NodePort, nodePort should be 30012.

vi app-service.yaml

apiVersion: v1
kind: Service
metadata:
  name: app-php-nginx
spec:
  type: NodePort
  selector:
    app: app-php-nginx
  ports:
   - port: 8091
     targetPort: 8091
	 nodePort: 30012
	 
kubectl apply -f app-service.yaml

kubectl get configmap -A
kubectl get configmap
kubectl get pods

2.) Create a config map nginx-config for nginx.conf as we want to add some custom settings for nginx.conf.


a) Change default port 80 to 8091 in nginx.conf.

b) Change default document root /usr/share/nginx to /var/www/html in nginx.conf.

c) Update directory index to index index.html index.htm index.php in nginx.conf.


vi app-configmap.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: nginx-config
data:
  nginx.conf: |
    events {} 
    http {
      server {
        listen 8092;
        index index.html index.htm index.php;
        root  /var/www/html;
        location ~ \.php$ {
          include fastcgi_params;
          fastcgi_param REQUEST_METHOD $request_method;
          fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
          fastcgi_pass 127.0.0.1:9000;
        }
      }
    }

kubectl apply -f app-configmap.yaml
kubectl get all


3.) Create a pod named nginx-phpfpm .

b) Create a shared volume shared-files that will be used by both containers (nginx and phpfpm) also it should be a emptyDir volume.

c) Map the ConfigMap we declared above as a volume for nginx container. 
Name the volume as nginx-config-volume, mount path should be /etc/nginx/nginx.conf and subPath should be nginx.conf

d) Nginx container should be named as nginx-container and it should use nginx:latest image. 
PhpFPM container should be named as php-fpm-container and it should use php:7.0-fpm image.

e) The shared volume shared-files should be mounted at /var/www/html location in both containers. 
Copy /opt/index.php from jump host to the nginx document root inside nginx container, once done you can access the app using App button on the top bar.


vi app-deploy.yaml

---
apiVersion: v1
kind: Pod
metadata:
  label: 
    app: nginx-phpfpm
  name: nginx-phpfpm
spec:
  volumes:
    - name: shared-files
      emptyDir: {}
    - name: nginx-config-volume
      configMap:
        name: nginx-config
  containers:
    - name: nginx-container
      image: nginx:latest
      volumeMounts:
        - name: shared-files
          mountPath: /var/www/html
        - name: nginx-config-volume
          mountPath: /etc/nginx/nginx.conf
          subPath: nginx.conf
    - name: php-fpm-container
      image: php:7.0-fpm
      volumeMounts:
        - name: shared-files
          mountPath: /var/www/html

kubectl apply -f app-deploy.yaml

Before clicking on finish button always make sure to check if all pods are in running state.

You can use any labels as per your choice.

Note: The kubectl utility on jump_host has been configured to work with the kubernetes cluster.


kubectl cp /opt/index.php nginx-phpfpm:/var/www/html

kubectl get configmap
kubectl get pods
kubectl describe pods

kubectl exec -it nginx-phpfpm -- /bin/bash

once logged into root@nginx-phpfpm:
echo "<?phpinfo();?>" > /var/www/html/index.php
curl http://localhost:8091

In pod you have given different label and in service you have given different selector label. Give labels in both pod and service same. Then only connection will work.

apiVersion: v1
kind: Service
metadata:
name: app-php-nginx
spec:
type: NodePort
selector:
app: app-php-nginx