# instruction guide of dockerization and k8s of barnpa on minikube and azure
---
## Step 1 : Update the application
tags : __\#optional__ __\#not a part of dockerization__
### why update the application before dockerization?
the original barnpa use codeigniter2 framework and php5. The current version of codeigniter is version 4, which has a lot of difference. The most important thing is that the official codeigniter image on docker hub starts from codeigniter 3, and it is really difficult to download codeigniter 2 from the internet nowadays. Therefore, upgrading the application is a must before dockerization. And we upgrade this to version 4 if we need to upgrade it anyway. Read the website for more information : [codeigniter4 website](https://codeigniter4.github.io/userguide/)
### development environment
A normal development environment will be download a codeigniter 4 framework in your localhost and test the application using localhost. In this way, you need to set up a database in localhost as well.
A better way will be using docker as development environment. You can use volume mount to sync the files on localhost and inside the container. There is a well-made docker set up for this purpose.
https://github.com/atsanna/codeigniter4-docker
Following the instruction will create a docker environment with a new codeigniter 4 framework embedded.
### what to do / what to pay attention when upgrade the application
Disable the use of phpCAS in the project because phpCAS is not usable in this project.
In addition to the changes to function prototypes, the file structure is changed too. So pay attention to the url and file links in the project.
Remember to change the `.env` file in the project root to set the environment variable. The `.env` file can be created from duplicate the `env` file and add a dot in the name. Uncomment some lines in the file if specific environment parameter needs to be set.
Moreover, remember to change the `ci_sessions` table in the `.sql` file because codeigniter 4 require new table structure in the sql file in order to use session data. For more information, check : [codeigniter4 session](https://codeigniter4.github.io/userguide/libraries/sessions.html#session-drivers)
## Step 2 : create a local image of codeigniter 4
create a file structure :
<application folder name>/
- app/
- public/
- mysql/
- barnpa.sql
- Dockerfile
- .env
- yaml/
- kustomization.yaml
- codeigniter4.yaml
- codeigniter4-mysql.yaml
the `app` and `public` folder are the `app` and `public` from the codeigniter4 project. The `.env` file is the environment file for the project which will be used in the docker environment (if you use docker as the development environment which I introduce above, then the `env` file will be very similar in here). The `Dockerfile` is for creating a new image base on the new barnpa in codeigniter4 and the files in yaml folder is for deploying the application in kubernetes.
### How to write the docker file
```Dockerfile
FROM atsanna/codeigniter4:latest
RUN apt-get update
RUN apt-get install
RUN apt-get install -y vim
RUN ["apt-get", "install", "-y", "libzip-dev"]
RUN ["docker-php-ext-install", "mysqli", "pdo", "pdo_mysql", "zip"]
RUN touch /usr/local/etc/php/conf.d/uploads.ini \
&& echo "upload_max_filesize=64M;\r\n post_max_size=128M;\r\nmemory_limit = 512M" >> /usr/local/etc/php/conf.d/uploads.ini
RUN rm -r /codeigniter4/app /codeigniter4/public
COPY ./.env /codeigniter4/
COPY ./public /codeigniter4/public
COPY ./app /codeigniter4/app
```
This is the content of the `Dockerfile`. The base image will be a pre-made codeigniter4 image which is public on docker hub : [atsanna/codeigniter4
](https://hub.docker.com/r/atsanna/codeigniter4). However, we add a few utilities on top of the base image for some reasons. First, we add vim to the image such that `vi` command can be used inside the container. We also install `mysqli` and `pdo_mysql` extensions for connection to database and allowing php 7 to use caching_sha2_password in mysql 8 respectively.
We also increase the `upload_max_filesize` of the apache web server to 64MB as the default `upload_max_filesize` is 2MB which is too small and will cause error when uploading soundfiles.
Finally, we remove the original files in the codeigniter4 framework and add our own files into the container.
### build the docker image locally
try to create a local image and test if the web app work perfectly. We can do this by using a `docker-compose.yaml` file to create a container of web server and a container of mysql all at once.
```yaml
version: "3.1"
services :
codeigniter4:
container_name: "codeigniter4"
build:
context: ./
image: codeigniter4
restart: always
ports:
- 8081:80
links :
- codeigniter4-mysql
codeigniter4-mysql :
container_name: 'codeigniter4-mysql'
command: mysqld --default-authentication-plugin=mysql_native_password
image : mysql:8.0.20
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: barnpa
MYSQL_USER: barnpauser
MYSQL_PASSWORD: barnpapassword
ports:
- 3306:3306
```
This is the content of `docker-compose.yaml`. Run this file using
docker-compose up -d --build
the `-d` flag mean run the containers in detached mode and `--build` flag mean rebuild the docker images.
__Note that we set the mysql environment and hostname in the `docker-compose` file. Therefore, we need to change the `.env` to match the `docker-compose` file.__
Then you should be able to access the barnpa by `localhost:8081`.
Run `docker-compose down` to tear the containers after test.
## Step 3 : deploy the application of azure kubernetes
__If you are not familiar with the azure CLI and kubernetes CLI, please read the official documentation or the instruction guide we provided for the dockerization of iLearn.__
First login azure using command
az login
Then create a resource group using command
az group create --name <resoruces group name> --location <location e.g. eastus>
Then create a azure container registry (ACR) to hold the image using command
az acr create --resource-group <name of resoruce group> --name <name of acr> --sku Basic
__Actually, the ACR is just a cloud repos to store your image. It is because when we deploy our application in kubernetes, it needs a place to pull the image. The cloud repository does not have to be on azure.__
Then login the ACR using command
az acr login --name <acr name>
Then tag the local image and push it to ACR
image_name=<image name>
acr_login_server=$(az acr show --name barnpaAcr --query loginServer --output tsv)
docker tag $image_name $acr_login_server/$image_name
docker push $acr_login_server/$image_name
check whether the image is push using the command
az acr repository list --name <acr name>
create a kubernetes cluster with specific number of nodes on azure kubernetes service (AKS)
az aks create --resource-group <resource group> --name <cluster name> --node-count <number of node> --enable-addons monitoring --generate-ssh-keys
connect the kubectl CLI with the cluster
az aks get-credentials --resource-group <resource group> --name <cluster name>
if you have more than one cluster, you can use
kubectl config view
to view the context and cluster that kubectl is connected to. To switch between kubernetes cluster, use
kubectl config use-context <context name>
to ensure that you create node successfully
kubectl get nodes
## Step 4 - prepare .yaml file for kubernetes
We create two `.yaml` files, one for the set up of web server and another one for the set up of database in the cluster. These two files can be grouped and run together by a `kustomization.yaml` file.
The files are too long and will not be included here. However, the files contains comments which explain some details.
### integrate ACR with kubernetes cluster
This official contains comprehensive guide of different methods to connect ACR with kubernetes cluster : https://docs.microsoft.com/en-us/azure/container-registry/container-registry-authentication
In the `codeigniter4.yaml`, we specify the image as
```yaml
containers:
- image: barnpaacr.azurecr.io/codeigniter4:latest
imagePullPolicy: IfNotPresent
name: codeigniter4
ports:
...
```
By default, kubernetes cannot access the ACR. Therefore you need to authenticate ACR with the AKS. An easier way will be attaching the ACR with AKS such that AKS will know the image in the ACR. This can be done using this command
az aks update -n <cluster name> -g <resource group name> --attach-acr <acrName>
### deploy the application
Run this command
kubectl apply -k ./
the `-k` flag means that kubectl will use the `kustomization.yaml` to do the set up.
Check if the resources are created using command
kubectl get all