This guide provides step-by-step instructions to set up MicroK8s, deploy MySQL and phpMyAdmin, and configure port forwarding using MacOS.
Ensure the following are installed on your system before proceeding:
kubectl is used to manage Kubernetes clusters.
- Optional: MicroK8s includes
kubectlby default. - If
kubectlis already installed separately, assign MicroK8s to use the installed version. Follow the steps after installed MicroK8s.
MicroK8s runs inside a Multipass VM, which must be installed.
Install Multipass:
brew install --cask multipassCheck if it's running properly::
multipass infoInstall MicroK8s:
brew install ubuntu/microk8s/microk8sOnce installed, run:
microk8s installNote : Above command is to run microk8s, although it wrote as 'install'
Check if MicroK8s is Running
microk8s status --wait-readyIf it's not running, start it:
microk8s startIf you already have kubectl, you can set it to use MicroK8s by running
microk8s kubectl config view --raw > ~/.kube/configNow, check if MicroK8s is working:
kubectl get nodesIf you prefer to use MicroK8s' built-in kubectl, you can run:
microk8s kubectl get nodesEnable necessary add-ons like the Kubernetes Dashboard, DNS, and storage:
microk8s enable dashboard dns storageTo access the Kubernetes Dashboard, port-forward it and generate a login token:
microk8s kubectl port-forward -n kube-system service/kubernetes-dashboard 10443:443View config:
microk8s kubectl config view --rawGet secret generated to verify:
microk8s kubectl get secrets -n kube-systemGenerate the token from secret with related namespace:
microk8s kubectl get secret microk8s-dashboard-token -n kube-system -o jsonpath="{.data.token}" | base64 --decodeAccess the Dashboard at:
https://127.0.0.1:10443
Create a mysql-deployment.yaml file for MySQL deployment:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
---
apiVersion: v1
kind: ConfigMap
metadata:
name: mysql-config
data:
my.cnf: |
[mysqld]
bind-address = 0.0.0.0
---
apiVersion: v1
kind: Secret
metadata:
name: mysql-secret
type: Opaque
data:
root-password: c3VwZXJzZWNyZXQ= # Base64 encoded 'supersecret'
mysql-user: bXl1c2Vy # Base64 encoded 'myuser'
mysql-password: bXlwYXNzd29yZA== # Base64 encoded 'mypassword'
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:8.0
env:
- name: MYSQL_ROOT_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: root-password
- name: MYSQL_USER
valueFrom:
secretKeyRef:
name: mysql-secret
key: mysql-user
- name: MYSQL_PASSWORD
valueFrom:
secretKeyRef:
name: mysql-secret
key: mysql-password
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-storage
mountPath: /var/lib/mysql
- name: mysql-config
mountPath: /etc/mysql/my.cnf
subPath: my.cnf
volumes:
- name: mysql-storage
persistentVolumeClaim:
claimName: mysql-pvc
- name: mysql-config
configMap:
name: mysql-config
---
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
selector:
app: mysql
type: NodePort # Expose MySQL outside the cluster
ports:
- protocol: TCP
port: 3306 # MySQL internal port
targetPort: 3306 # MySQL container port
nodePort: 30009 # Host accessible port (must be 30000-32767)
Create a phpmyadmin.yaml file:
apiVersion: apps/v1
kind: Deployment
metadata:
name: phpmyadmin
spec:
replicas: 1
selector:
matchLabels:
app: phpmyadmin
template:
metadata:
labels:
app: phpmyadmin
spec:
containers:
- name: phpmyadmin
image: phpmyadmin
env:
- name: PMA_HOST
value: "mysql" # Connects to MySQL inside Kubernetes
- name: PMA_PORT
value: "3306"
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: phpmyadmin
spec:
selector:
app: phpmyadmin
type: NodePort # Exposes phpMyAdmin on a high port
ports:
- protocol: TCP
port: 80
targetPort: 80
nodePort: 30008 # Change this if needed (must be between 30000-32767)Run the following commands to apply the configurations:
microk8s kubectl apply -f mysql-deployment.yaml
microk8s kubectl apply -f phpmyadmin.yamlExpose MySQL on port 3309 and phpMyAdmin on 3310:
microk8s kubectl port-forward svc/mysql 3309:3306 &
microk8s kubectl port-forward svc/phpmyadmin 3310:80 &Check if the services are running correctly:
microk8s kubectl get pods
microk8s kubectl get svc -AOpen your browser and go to:
http://127.0.0.1:3310
Login using:
- Username: root
- Password: supersecret
You have successfully set up MicroK8s with MySQL and phpMyAdmin, configured port-forwarding, and accessed the services locally. 🚀