Kubernetes LEMP Setup Error

The Nautilus DevOps team want to deploy a static website on Kubernetes cluster. They are going to use Nginx, phpfpm and MySQL for the database. The team had already gathered the requirements and now they want to make this website live. Below you can find more details:

Create some secrets for MySQL.

Create a secret named mysql-root-pass wih key/value pairs as below:

name: password
value: R00t

Create a secret named mysql-user-pass with key/value pairs as below:

name: username
value: kodekloud_top

name: password
value: BruCStnMT5

Create a secret named mysql-db-url with key/value pairs as below:

name: database
value: kodekloud_db4

Create a secret named mysql-host with key/value pairs as below:

name: host
value: mysql-service

Create a config map php-config for php.ini with variables_order = “EGPCS” data.

Create a deployment named lemp-wp.

Create two containers under it. First container must be nginx-php-container using image webdevops/php-nginx:alpine-3-php7 and second container must be mysql-container from image mysql:5.6. Mount php-config configmap in nginx container at /opt/docker/etc/php/php.ini location.

  1. Add some environment variables for both containers:

MYSQL_ROOT_PASSWORD, MYSQL_DATABASE, MYSQL_USER, MYSQL_PASSWORD and MYSQL_HOST. Take their values from the secrets you created. Please make sure to use env field (do not use envFrom) to define the name-value pair of environment variables.

  1. Create a node port type service lemp-service to expose the web application, nodePort must be 30008.

  2. Create a service for mysql named mysql-service and its port must be 3306.

We already have a /tmp/index.php file on jump_host server.

Copy this file into the nginx container under document root i.e /app and replace the dummy values for mysql related variables with the environment variables you have set for mysql related parameters. Please make sure you do not hard code the mysql related details in this file, you must use the environment variables to fetch those values.

Once done, you must be able to access this website using Website button on the top bar, please note that you should see Connected successfully message while accessing this page.

answer:

kubectl create secret generic mysql-root-pass --from-literal=password=R00t
kubectl create secret generic mysql-user-pass --from-literal=username=kodekloud_top --from-literal password=BruCStnMT5
kubectl create secret generic mysql-db-url --from-literal=database=kodekloud_db4
kubectl create secret generic mysql-host --from-literal=host=mysql-service

vi php-config.yaml

apiVersion: v1
kind: ConfigMap
metadata:
name: php-config
data:
php.ini : |
variables_order = “EGPCS”

k create -f php-config.yaml

vi lemp-wp.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: lemp-wp
name: lemp-wp
spec:
replicas: 1
selector:
matchLabels:
app: lemp-wp
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: lemp-wp
spec:
containers:
- image: webdevops/php-nginx:alpine-3-php7
name: nginx-php-container
volumeMounts:
- name: ini
mountPath: /opt/docker/etc/php/php.ini
subPath: php.ini
env: &enviroment
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-root-pass
key: password
- name: MYSQL_DATABASE
valueFrom:
secretKeyRef:
name: mysql-db-url
key: database
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: mysql-user-pass
key: username
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-user-pass
key: password
- name: MYSQL_HOST
valueFrom:
secretKeyRef:
name: mysql-host
key: host
- image: mysql:5.6
name: mysql-container
env: *enviroment
resources: {}
volumes:
- name: ini
configMap:
name: php-config
items:
- key: php.ini
path: php.ini

k create -f lemp-wp.yaml

k expose deployment lemp-wp --port 80 --type NodePort --name lemp-service
k expose deployment lemp-wp --port 3306 --name mysql-service

k cp -c nginx-php-container index.php lemp-wp-89cf9c585-hpdfx:/app

facing this issue

k exec -it lemp-wp-6cbdd8dbf7-k4tpm -c nginx-php-container – /bin/bash
bash-4.3# curl localhost:80

403 Forbidden

403 Forbidden


nginx/1.10.3

@Alistair_KodeKloud plz help with this

cp /tmp/index.php .
vi index.php

<?php $dbname = getenv('MYSQL_DATABASE'); $dbuser = getenv('MYSQL_USER'); $dbpass = getenv('MYSQL_PASSWORD'); $dbhost = getenv('MYSQL_HOST'); $connect = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'"); $test_query = "SHOW TABLES FROM $dbname"; $result = mysqli_query($test_query); if ($result->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?> i have done this as well

Here in UK it is time to get kids ready for school, then do day job. I cannot look at this till UK evening.

Please, when pasting YAML, put it in a

code block

so that it does not lose all the indentation. I cannot make sense of what is above.

php-config.yaml

apiVersion: v1
kind: ConfigMap
metadata:
  name: php-config
data:
  php.ini : |
    variables_order = "EGPCS"

lemp-wp.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: lemp-wp
  name: lemp-wp
spec:
  replicas: 1
  selector:
    matchLabels:
      app: lemp-wp
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: lemp-wp
    spec:
      containers:
      - image: webdevops/php-nginx:alpine-3-php7 
        name: nginx-php-container
        volumeMounts:
        - name: ini
          mountPath: /opt/docker/etc/php/php.ini
          subPath: php.ini
        env: &enviroment
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-root-pass
              key: password
        - name: MYSQL_DATABASE
          valueFrom:
            secretKeyRef:
              name: mysql-db-url
              key: database
        - name: MYSQL_USER
          valueFrom:
            secretKeyRef:
              name: mysql-user-pass
              key: username
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-user-pass
              key: password
        - name: MYSQL_HOST
          valueFrom:
            secretKeyRef:
              name: mysql-host
              key: host      
      - image: mysql:5.6
        name: mysql-container
        env: *enviroment
        resources: {}
      volumes:
      - name: ini
        configMap:
          name: php-config
          items:
          - key: php.ini
            path: php.ini

index.php

<?php
$dbname = getenv('MYSQL_DATABASE');
$dbuser = getenv('MYSQL_USER');
$dbpass = getenv('MYSQL_PASSWORD');
$dbhost = getenv('MYSQL_HOST');

$connect = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");

$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($test_query);

if ($result->connect_error) {
   die("Connection failed: " . $conn->connect_error);
}
  echo "Connected successfully";
?>

okay thank you for your reply

I remembered I did a solution for this here.

yeah i already refer that but this is another problem not same and im using the same steps which you have mention here

Which level is the question in?

It is this question, right?

If so, then the solution is as I have given in the other post. I have re-tested the solution and it still works.
And the resources are lamp not lemp. If you name them incorrectly, you will not pass the question. Note that LAMP is an acronym for Linux, Apache, MySQL, PHP

If it is not this question, please tell me which one it is, and in which level, e.g.

image

image

Thank you for your support i have solve that problem successfully.

Excellent.

You will have seen that with a few minor modifications, it is almost exactly the same as the LAMP question.