Lately I have been exploring the beautiful and breathtaking world of Containers and Scalable Microservices. That’s when I found out about Kubernetes, Docker and other top dogs. So as a “newbie”, I searched for articles and tutorials on how to manage a Dockerized Magento 2 application using Kubernetes.
I did the job of piecing together (Magento, Kubernetes and Docker) and I want to share it. The goal of this article is to turn simple docker images (alexcheng/magento2 & mysql:5.6) into an applications running on Kubernetes. Basically the MySQL and core Magento 2 web application are packaged into individual Docker containers and these Docker containers will run on kubernetes via Minikube.
This will only run locally not on the cloud. My next technical article will be about running Magento 2 on Google Cloud — Kubernetes Engine (GKE), so if you interested in DevOps, Cloud Computing, Site Reliable Engineering (SRE), full-stack development, back-end development, Solution architecture, Systems administration and Computer Hacking, please click that follow button.
First of all, who are these guys, Magento, Kubenetes, Minikube, Docker?
Let us define them:
Magento empowers thousands of retailers and brands with the best eCommerce platform and flexible cloud solutions to rapidly innovate and grow.
Kubernetes is an open-source system for automating deployment, scaling, and management of containerized applications.
Minikube is a tool that makes it easy to run Kubernetes locally. Minikube runs a single-node Kubernetes cluster inside a VM on your laptop for users looking to try out Kubernetes or develop with it day-to-day.
Docker is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud.
I will try to explain the process for you step by step. It will take us few minutes.
Before we can start let’s make sure that we have these Prerequisites:
Note: The platform I am using is Docker for Mac on OS X
2. For OS X, we need Homebrew to install the xhyve driver.
3. Clone my Repository.
We will be using Minikube to create a local cluster. We need to download it first:
Use curl to download and install the latest Minikube release. Let us cut and paste this to the terminal
curl -Lo minikube https://storage.googleapis.com/minikube/releases/latest/minikube-darwin-amd64 && \ chmod +x minikube && \ sudo mv minikube /usr/local/bin/
Use Homebrew to install the xhyve driver and set its permissions:
brew install docker-machine-driver-xhyvesudo chown root:wheel $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyvesudo chmod u+s $(brew --prefix)/opt/docker-machine-driver-xhyve/bin/docker-machine-driver-xhyve
The next challenge is to download the kubectl command-line tool, which I will be using to interact with Kubernetes clusters:
$ brew install kubectl
Now let us get started! The first thing is to start minikube cluster. We type this command in the terminal:
$ minikube start — vm-driver=xhyve
Our output will be something like this:
Now we need to set the Minikube context. The context is what determines which cluster kubectl is interacting with.
$ kubectl config use-context minikubeOutput: Switched to context "minikube".
We make sure that we are using the Minikube Docker daemon:
$ eval $(minikube docker-env)
A Kubernetes Pod is a group of one or more Containers, tied together for the purposes of administration and networking. A Kubernetes Deployment checks on the health of our Pod and restarts the Pod’s Container if it terminates. Deployments are the recommended way to manage the creation and scaling of Pods.
To access the Kubernetes Dashboard, we run this command after starting minikube to get the address:
$ minikube dashboard
Kubernetes UI (dashboard) will automatically open up!
Nothing Deployed yet
This is were things get more interesting and I promise you will find kubernetes very attractive. The following gist describes a single-instance of Magento 2 Deployment and Service. It uses feature like a Secret for the password. It uses a different setting: **_type: NodePort_**
. This setting exposes Magento 2 to traffic from outside of our minikube cluster.
On our terminal we type the following command to deploy Magento 2:
$ kubectl create -f magento2-deployment.yaml
To view the Deployments we simple type the following command:
$ kubectl get deployments
Output:
We have one deployment
Let us view the Pods:
$ kubectl get pods
Output:
We have only one service
If we also want to view cluster events you just type this on the terminal:
$ kubectl get events
You just saw the code in the file above (magento2-deployment.yaml). The following code taken from above file is for creating a Magento 2 service.
apiVersion: v1kind: Servicemetadata:name: magento2labels:app: magento2spec:ports:- port: 80selector:app: magento2tier: frontendtype: NodePort
Note: To simplify things you can put this code in a separate file (name it :magento2-service.yaml) and write this command in the terminal :
$ kubectl create -f magento2-service.yaml
It will expose magento2 service for you.
By default, the Pod is only accessible by its internal IP address within the Kubernetes cluster. So to make the Magento 2 Container accessible from outside the Kubernetes virtual network, we have to expose the Pod as a Kubernetes Service.
So to see the service we just created. We write the following command in the command line:
$ kubectl get services
Our output:
Note: magento2 TYPE is NodePort
It is the same process as deploying Magento 2 Deployment.
mysql-deployment.yaml file look like this:
Note: The MySQL container mounts the PersistentVolume at /var/lib/mysql. The **MYSQL_ROOT_PASSWORD**
environment variable sets the database password from the Secret. To create Secrets object that stores a piece of sensitive data, password we type this in the terminal: kubectl create secret generic mysql-pass --from-literal=password=MY_PASSWORD
$ kubectl create -f mysql-deployment.yaml
Now let us view the all Deployments:
$ kubectl get deployments
Our output:
Note: now we have two deployments
Let us see all the Pods:
$ kubectl get pods
Output:
Note: now we have two pods
We already exposed MySQL with the code above in mysql-deployment.yaml file**.** So to view the Service we have just created:
$ kubectl get services
Our output:
Finally our kubernetes UI (Dashboard) will be like this:
This is cool!
Netwoking and Exposing
The minikube VM is exposed to the host system via a host-only IP address, that can be obtained with the minikube ip command. Any services of type NodePort can be accessed over that IP address, on the NodePort. To determine the NodePort for your service, you can use a kubectl command like this:
$ kubectl get service $SERVICE output=’jsonpath=”{.spec.ports[0].nodePort}”’
The following will automatically open up a browser window using a local IP address that serves our app and show the Magento 2 setup page.
$ minikube service magento2
Amazing! Just like that! Now we have our Magento 2 App running!
Our next step is to press**: Press Agree and Setup>Star Readiness Check>Next**
Now we are about to close this chapter, look at the following pages, do everything I did and you shall get there!
Ladies and gentleman, now we have arrived to the end of our journey.
You can also read about Persistent Volumes. Thanks to the creator of “alexcheng/magento2” image. If you have any question, please feel free to post in the comments section or visit the minikube community.
I will publish another blog post whereby I will be using Google Cloud — Kubernetes Engine, not minikube. Please clap, follow me here on medium & twitter **@**sphemalgas ).
I Thank you…..I’m out!