How I built my Kubernetes Homelab – Part 5

Ok, here we go. In the last 4 parts we prepared this great moment. We now deploy our first app. Often demo environments have a webshop or a blog to demonstrate the service.

For our demo application we will install just two helm charts for mySQL and phpMyAdmin and fill the database server with content. But before we can do this we want to configure a LoadBalancer to allow external access to phpMyAdmin via own IP adress.

Every application should have it’s own namespace. namespace are e.g. later used from Kasten K10 to select the application and all services, pods, secrets, etc.

MetalLB

As Loadbalancer we want to use MetalLB. With this commands we create a new namespace, a secret, a configmap and everything else which is needed to build this Loadbalancer.

marco@lab-kube-m1:~$ kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.5/manifests/namespace.yaml

# Generate Secret
marco@lab-kube-m1:~$ kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 128)"

marco@lab-kube-m1:~$ sudo tee metallb-config.yaml >/dev/null <<EOF
apiVersion: v1
kind: ConfigMap
metadata:
  namespace: metallb-system
  name: config
data:
  config: |
    address-pools:
    - name: default
      protocol: layer2
      addresses:
      - 192.168.30.100-192.168.30.119
EOF

marco@lab-kube-m1:~$ kubectl apply -f metallb-config.yaml

marco@lab-kube-m1:~$ kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.9.5/manifests/metallb.yaml

MySQL & phpMyAdmin

We create a new namespace with a manifest file and apply these file with kubectl to create the namespace

#Content of mysqldemo-namespace.yaml
kind: Namespace
apiVersion: v1
metadata:
  name: mysqldemo
  labels:
    name: mysqldemo
marco@lab-kube-m1:~$ kubectl apply -f mysqldemo-namespace.yaml
namespace/mysqldemo created

Many open source projects are already packaged in helm charts via Bitnami. We just need add the Bitnami helm chart and install the mysql package from this helm chart.

marco@lab-kube-m1:~$ helm repo add bitnami https://charts.bitnami.com/bitnami
marco@lab-kube-m1:~$ helm install mysql bitnami/mysql --namespace=mysqldemo --set global.storageClass=nas-sc

After installing mysql, I want to check if everything is up and running. For this is just used this command to show me all ressources from my namespace “mysqldemo”.

marco@lab-kube-m1:~$ kubectl get all --namespace=mysqldemo
NAME                              READY   STATUS    RESTARTS   AGE
pod/mysql-0                       1/1     Running   2          22m
pod/mysql-client                  1/1     Running   0          21m

NAME                     TYPE           CLUSTER-IP      EXTERNAL-IP      PORT(S)                      AGE
service/mysql            ClusterIP      10.101.72.123   <none>           3306/TCP                     22m
service/mysql-headless   ClusterIP      None            <none>           3306/TCP                     22m

NAME                     READY   AGE
statefulset.apps/mysql   1/1     22m

Fine our database server is up and running, but how we can connect to the system? By default this mysql package will autogenerate passwords. This passwords we (or the shell) needs to know to deploy the phpMyAdmin. With the export commands we receive and decode the passwords and save them to temporary environment variables of this shell.

marco@lab-kube-m1:~$ export MYSQL_ROOT_PASSWORD=$(kubectl get secret --namespace mysqldemo mysql -o jsonpath="{.data.mysql-root-password}" | base64 --decode)
marco@lab-kube-m1:~$ export MYSQL_PASSWORD=$(kubectl get secret --namespace mysqldemo mysql -o jsonpath="{.data.mysql-password}" | base64 --decode)

This variables will now be used to install the phpMyAdmin package. With –set you can set various configuration options like passwords, loadbalancer and mysql host

marco@lab-kube-m1:~$ helm install phpmyadmin bitnami/phpmyadmin --namespace=mysqldemo --set mysql.auth.rootPassword=$MYSQL_ROOT_PASSWORD,mysql.auth.password=$MYSQL_PASSWORD,service.type=LoadBalancer,db.host=mysql.mysqldemo.svc.cluster.local
[...]

Everything is deployed now, but how can I access phpMyAdmin? It’s quite “easy”. Kubernetes stores the data inside of secrets and we can access these secret via kubectl.

marco@lab-kube-m1:~$ export SERVICE_IP=$(kubectl get svc --namespace mysqldemo phpmyadmin --template "{{ range (index .status.loadBalancer.ingress 0) }}{{.}}{{ end }}")

marco@lab-kube-m1:~$ echo "phpMyAdmin URL: http://$SERVICE_IP/"
phpMyAdmin URL: http://192.168.30.102/


marco@lab-kube-m1:~/homelab-k8s/mysqldemo$ echo $MYSQL_ROOT_PASSWORD
J2Aaa2i6Rw

Now just open the URL in your Webbrowser and log in with the root credentials

For demonstration purposes we want to create a database with sample data. I used the world database from MySQL Documentation Website. This database can be just imported by the import function of phpMyAdmin. Just select the world.sql.zip and press “Go” at the bottom of the page. Creation of the database can take some seconds depending on your lab.

The import created a new database called “world” with three tables and each table sample data.

In preparation of this blog post I also though about to use a complete prebuild sample application with pods, secrets, etc. But I decided against it, because I could not show how to build something like this from scratch. I wanted to understand how the interaction between pods works and I think this was quite a good exercise for understanding how applications can be build without being an developer.

In the next part of my blog series we will install Kasten K10 and create a backup policy for our mysqldemo application.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *