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.
- 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.
-
Create a node port type service lemp-service to expose the web application, nodePort must be 30008.
-
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
nginx/1.10.3