DEV Community

Cover image for 7.Kubernetes LEMP Setup
Thu Kha Kyawe
Thu Kha Kyawe

Posted on

7.Kubernetes LEMP Setup

Lab Information

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:
Enter fullscreen mode Exit fullscreen mode

name: password
value: R00t

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

name: username
value: kodekloud_cap

name: password
value: Rc5C9EyvbU

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

name: database
value: kodekloud_db8

Create a secret named mysql-host with key/value pairs as below:
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

5) 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.
Enter fullscreen mode Exit fullscreen mode

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

7) 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.
Enter fullscreen mode Exit fullscreen mode

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


Lab Solutions

Step 1: Create Secrets using YAML files

Create yaml file by using vi commands

  1. mysql-root-pass-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysql-root-pass
type: Opaque
data:
  password: UjAwdA==  # R00t base64 encoded
Enter fullscreen mode Exit fullscreen mode
  1. mysql-user-pass-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysql-user-pass
type: Opaque
data:
  username: a29kZWtsb3VkX2NhcA==  # kodekloud_cap base64 encoded
  password: UmM1QzlFeXZiVQ==       # Rc5C9EyvbU base64 encoded
Enter fullscreen mode Exit fullscreen mode
  1. mysql-db-url-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysql-db-url
type: Opaque
data:
  database: a29kZWtsb3VkX2RiOA==  # kodekloud_db8 base64 encoded
Enter fullscreen mode Exit fullscreen mode
  1. mysql-host-secret.yaml
apiVersion: v1
kind: Secret
metadata:
  name: mysql-host
type: Opaque
data:
  host: bXlzcWwtc2VydmljZQ==  # mysql-service base64 encoded
Enter fullscreen mode Exit fullscreen mode

Apply the secrets:

kubectl apply -f mysql-root-pass-secret.yaml
kubectl apply -f mysql-user-pass-secret.yaml
kubectl apply -f mysql-db-url-secret.yaml
kubectl apply -f mysql-host-secret.yaml
Enter fullscreen mode Exit fullscreen mode

Step 2: Create ConfigMap using YAML file

php-config-configmap.yaml

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

Apply the configmap:

kubectl apply -f php-config-configmap.yaml
Enter fullscreen mode Exit fullscreen mode

Step 3: Create the Deployment

lemp-wp-deployment.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: lemp-wp
spec:
  selector:
    matchLabels:
      app: lemp-wp
  template:
    metadata:
      labels:
        app: lemp-wp
    spec:
      containers:
      # Nginx-PHP container
      - name: nginx-php-container
        image: webdevops/php-nginx:alpine-3-php7
        volumeMounts:
        - name: php-config
          mountPath: /opt/docker/etc/php/php.ini
          subPath: php.ini
        - name: app-volume
          mountPath: /app
        env:
        - 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

      # MySQL container
      - name: mysql-container
        image: mysql:5.6
        env:
        - 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

      volumes:
      - name: php-config
        configMap:
          name: php-config
      - name: app-volume
        emptyDir: {}
Enter fullscreen mode Exit fullscreen mode

Apply the deployment:

kubectl apply -f lemp-wp-deployment.yaml
Enter fullscreen mode Exit fullscreen mode

Step 4: Create Services

services.yaml

apiVersion: v1
kind: Service
metadata:
  name: lemp-service
spec:
  type: NodePort
  selector:
    app: lemp-wp
  ports:
    - protocol: TCP
      port: 80
      targetPort: 80
      nodePort: 30008
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: lemp-wp
  ports:
    - protocol: TCP
      port: 3306
      targetPort: 3306
Enter fullscreen mode Exit fullscreen mode

Apply the services:

kubectl apply -f services.yaml
Enter fullscreen mode Exit fullscreen mode

Step 5: Copy and Modify index.php

cat /tmp/index.php 
Enter fullscreen mode Exit fullscreen mode


sudo chmod 777 /tmp/index.php   **Password:** `mjolnir123
vi index.php
Enter fullscreen mode Exit fullscreen mode


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

$connect = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if (!$connect) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
?>
Enter fullscreen mode Exit fullscreen mode

Copy the file to the container:

# Get the pod name
POD_NAME=$(kubectl get pods -l app=lemp-wp -o jsonpath='{.items[0].metadata.name}')

# Copy the modified file
kubectl cp /tmp/index.php $POD_NAME:/app -c nginx-php-container
Enter fullscreen mode Exit fullscreen mode

Step 6: Verify Resources

kubectl get secret mysql-root-pass mysql-user-pass mysql-db-url mysql-host
kubectl get configmap php-config
kubectl get deploy lemp-wp
kubectl get pod -l app=lemp-wp
kubectl get svc lemp-service mysql-service
Enter fullscreen mode Exit fullscreen mode

Step 7: Verify Application Accessibility

Access the Application:

Click the Website button in the lab interface.


Resources & Next Steps
📦 Full Code Repository: KodeKloud Learning Labs
💬 Join Discussion: DEV Community - Share your thoughts and questions
💼 Let's Connect: LinkedIn - I'd love to connect with you

Credits
• All labs are from: KodeKloud
• I sincerely appreciate your provision of these valuable resources.

Top comments (0)