DEV Community

Cover image for Complete Guide to setup LAMP Architecture Docker Image into Azure Kubernetes Services,with azure persistence disk attached
Saif
Saif

Posted on

Complete Guide to setup LAMP Architecture Docker Image into Azure Kubernetes Services,with azure persistence disk attached

Hey Everyone,

This tutorial provides a walkthrough of, how to create a docker image of LAMP(Linux,Apache,MySql,PHP) and Deploy it into Kubernetes cluster with Azure Static Provision Persistence disk attached.

Creating Docker Image

Installing Docker on Ubuntu

  • Step 1: Update Software Repositories

sudo apt-get update

  • Step 2:(Optional)Uninstall Old Versions of Docker

sudo apt-get remove docker docker-engine docker.io

  • Step 3: Install Docker

sudo apt install docker.io

  • Step 4: Start and Automate Docker

sudo systemctl start docker

sudo systemctl enable docker

  • Step 5 (Optional): Check Docker Version

docker --version

  • Step 6: Docker Infomation

docker info

  • Step 7: Docker Images

docker images

Deploy Docker LAMP Architecture

sudo docker pull ubuntu:latest

sudo docker ps -a #view all containers

sudo docker images -a #View all available images

sudo docker stop container_id #Stop a specific container

sudo docker rm container_id #Delete a specific container

sudo docker rmi image_id #Delete a specific image

sudo docker exec –it <container-name/id> /bin/bash# docker image exec

  • Installing PHP on Ubuntu VM in Azure

sudo apt-get install php

sudo apt-get install python-software-properties

sudo apt-get update

php -v # to check the PHP version

apt-get remove –purge php* # to uninstall PHP

  • Installing MySQL in Ubuntu 16.04

sudo apt-get install mysql-server # to get latest remove numeric

sudo apt-get update

  • Copying Source Code from localhost to VM

Path to copy: /var/www/websitename.com/html

scp -r pathlocation root@ip(ip of where to be copied):/pathlocation 
Example :
scp -r source_code.zip root@10.10.10.10:/var/www/websitename.com/html

  • Start the apache2 in the container

service apache2 start

  • Run the image as container
docker run –itd –p 70:80 –name <container-name> <image-name>
Example :
docker run –itd –p 70:80 –name zaif/lamp 

exit # exit from the container

Note:
Access the localhost with the exposed port number in the browser to check whether the application is deployed properly or not localhost:70

Commit the changes and create a new docker image

  • docker commit

      docker commit <container-name/id> <image-name>
    
  • Push the committed/created image to the docker hub

     docker push <account-name/repository-name>:tag
     docker push username/imagename:latest
    

Create an AKS cluster

To create an AKS cluster, complete the following steps,
On the Azure portal menu or from the Home page, select Create a resource_
__Select Containers
-> Kubernetes Service
On the Basics page, configure the following options

Alt Text

Project details: Select an Azure Subscription, then select or create an Azure Resource group, such as myResourceGroup
Cluster details: Enter a Kubernetes cluster name, such as myAKSCluster. Select a Region, Kubernetes version, and DNS name prefix for the AKS cluster
Primary node pool: Select a VM Node size for the AKS nodes. The VM size can't be changed once an AKS cluster has been deployed. - Select the number of nodes to deploy into the cluster. For this quick start, set Node count to 1. Node count can be adjusted after the cluster has been deployed.

Select Next: Scale when complete,
On the Scale page, keep the default options. At the bottom of the screen,
click Next: Authentication.
On the Authentication page, configure the following options:
Create a new service principal by leaving the Service Principal field with (new) default service principal. Or you can choose Configure service principal to use an existing one. If you use an existing one, you will need to provide the SPN client ID and secret.
Enable the option for Kubernetes role-based access controls (RBAC). This will provide more fine-grained control over access to the Kubernetes resources deployed in your AKS cluster.
By default, Basic networking is used, and Azure Monitor for containers is enabled. Click Review + Create and then Create when validation
completes
.
Alt Text

It takes a few minutes to create the AKS cluster. When your deployment is complete, click Go to resource, or browse to the AKS cluster resource group, such as myResourceGroup, and select the AKS resource, such as myAKSCluster. The AKS cluster dashboard is shown, as in this example:

Creating Azure Persistence Volume Disk

Steps:
select the same Resource Group Name where the AKS Cluster is placed
give azure disk name such myaks-pv and select the same region as of AKS is, size can be customized click on review and create
Alt Text

To configure kubectl to connect to your Kubernetes Cluster,use the
az aks get-credentials command ,this will download the credentials and configures the Kubernetes CLI to use them.
The following example gets credentials for the cluster name myAKSCluster in the resource group named myResourceGroup:

Create a Pod

The next step is to create a Pod and link it to Persistence volume (Azure Disk).

Use Azure cloud shell
Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser

Alt Text

  • enter this command in azure bash shell

      az aks get-credentials --resource-group myResourceGroup --name myAKSCluster
      az aks browse --resource-group myResourceGroup --name myAKSCluster 
    

Note: az extension add --name aks-preview # optional if any error

Alt Text

  • Upload YAML file (Create From Text Input) Here is the configuration file for the Pod:

kind: Pod
apiVersion: v1
metadata:
  name: myaks_pod
spec:
  containers:
  - name: myaks_container
    image: zaif/lamp_docker
    volumeMounts:
    - mountPath: "/mnt/azure/"
      name: volume
  volumes:
    - name: volume
      azureDisk:
        kind: Managed
        diskName: myaks-pv 
        diskURI: /subscriptions/73xxxx-xxx-xxxx-xxxxxas5/resourceGroups/myaksGroup/providers/Microsoft.Compute/disks/myaks-pv   

Note:disk URI copy the Resource ID from myaks-pv(Azure Disk)->properties
image below for reference
Alt Text

Run the following command in Azure CLI


kubectl get pods       #List all pods in ps output format
kubectl get pv or pvc  #Verify that the container in the Pod is running

kubectl label pod pod_name label=app
Example:
kubectl label pod myaks_pod label=app 

kubectl expose pod pod_name --type=LoadBalancer --port=80,443 #to expose the pod in order to get public sharing ip 
Example:
kubectl expose pod myaks_pod --type-LoadBalancer --port=80,443

kubectl get svc 
NAME           TYPE           CLUSTER-IP   EXTERNAL-IP    PORT(S)                      AGE

kubernetes      ClusterIP      10.0.0.1     <none>         443/TCP                      24h 

myaks_pod       LoadBalancer   10.0.39.57   52.143.95.12 80:30761/TCP,443:31098/TCP   6h9m 

Now Login into the Pod using command
Go to the shell,(Azure CLI)

kubectl exec -it my_pod_name --container container_name -- /bin/bash
Example: 
kubectl exec -it myaks_pod  --container myaks_container -- /bin/bash

Note:
To test the attached Azure Disk is connected and can be used for data lost retrieval, delete the POD and create a same config POD and check whether the database files can been retrieved form mnt/azure/lost+found path location

kubectl delete pod pod_name
Example:
kubectl delete pod myaks_pod

kubectl get svc

Note:
Follow the above same steps from creating a POD through YAML and the next further procedures in order to check whether the data files can be retrieved through azure persistence disk attached with reference to the POD "mnt/azure/lost+found"

The persistence disk is attached to the pod (myaks_pod) to retrieve the data when pod get’s evicted works as above

Persistence Mount Path: "/mnt/azure"

Top comments (0)