Notice: StatefulSet
is more preferable resource to manage database applications, but just for simplicity Deployment
is used for MySQL here.
-
Create
database
namespace.kubectl create namespace database
-
Check your namespace exists
kubectl get ns database NAME STATUS AGE database Active 9s
-
-
Create Deployment for MySQL.
kubectl apply -f mysql-deployment.yaml -n database
how to create the yaml easily
Create base yaml file:
kubectl create deploy mysql --image=mysql:8 --dry-run=client -o yaml > mysql-deployment.yaml
Add required
env
:env: - name: MYSQL_ROOT_PASSWORD value: password - name: MYSQL_DATABASE value: test_db - name: MYSQL_USER value: sample_app - name: MYSQL_PASSWORD value: password
-
Check
Deployment
in databaseNamespace
kubectl get deploy -n database
-
Check
ReplicaSet
in databaseNamespace
kubectl describe replicaset -n database
result
... Controlled By: Deployment/mysql ...
-
Check
Pod
in databaseNamespace
kubectl get pods -n database
-
Check if you can connect to MySQL with
kubectl exec
kubectl exec -n database -it $(kubectl get po -n database | grep mysql | head -1 | awk '{print $1}') -- mysql -uroot -ppassword
result
mysql: [Warning] Using a password on the command line interface can be insecure. Welcome to the MySQL monitor. Commands end with ; or \g. Your MySQL connection id is 8 Server version: 8.0.31 MySQL Community Server - GPL Copyright (c) 2000, 2022, Oracle and/or its affiliates. Oracle is a registered trademark of Oracle Corporation and/or its affiliates. Other names may be trademarks of their respective owners. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement. mysql>
-
-
Create Service for MySQL.
-
Create
Service
.kubectl create -f mysql-service.yaml -n database
how to create the yaml easily
kubectl create service clusterip mysql --tcp=3306 --dry-run=client --output yaml > mysql-service.yaml
-
Check the created
Service
kubectl get service -n database
-
-
Sample application: https://github.com/nakamasato/fastapi-sample
-
Set your name to
namespace
variable.namespace=<yourname>
-
Create your namespace.
kubectl create namespace $namespace
-
Check your namespace exists
kubectl get ns $namespace NAME STATUS AGE naka Active 71m
-
-
Create
Deployment
yaml.- Create Deployemnt yaml with container image to
ghcr.io/nakamasato/fastapi-sample:v1.0
.kubectl create deploy sample-app --image=ghcr.io/nakamasato/fastapi-sample:v1.0 --dry-run=client -o yaml > sample-app-deployment.yaml
- Set environment variables from
ConfigMap
.envFrom: - configMapRef: name: sample-app
- Set environment variable from
Secret
.envFrom: .. - secretRef: name: sample-app
- Create Deployemnt yaml with container image to
-
Create
ConfigMap
yaml.-
Prepare
env.txt
.MYSQL_HOST=mysql.database.svc.cluster.local MYSQL_DATABASE=test_db MYSQL_USER=sample_app
-
Create yaml from
env.txt
.kubectl create cm sample-app --from-env-file=env.txt --dry-run=client -o yaml > sample-app-configmap.yaml
-
-
Create
Secret
yaml.kubectl create secret generic sample-app --from-literal=MYSQL_PASSWORD=password --dry-run=client -o yaml > sample-app-secret.yaml
-
Apply
Deployment
,ConfigMap
, andSecret
.kubectl apply -f sample-app-deployment.yaml,sample-app-configmap.yaml,sample-app-secret.yaml -n $namespace
-
Check
Deployment
in your namespacekubectl get deploy -n $namespace
-
Check
Pod
in your namespacekubectl get pods -n $namespace
-
Check
ConfigMap
in your namespacekubectl get cm -n $namespace
-
Check
Secret
in your namespacekubectl get secret -n $namespace
-
-
Apply
Service
.kubectl apply -f sample-app-service.yaml -n $namespace
How to create the yaml easily
kubectl create service clusterip sample-app --tcp=80 --dry-run=client -o yaml > sample-app-service.yaml
-
Check the created
Service
kubectl get service -n $namespace
-
Check accessibility with
kubectl port-forward
kubectl port-forward svc/sample-app 8080:80 -n $namespace
curl http://localhost:8080/ {"message":"Hello World"}
-
kubectl delete -f sample-app-deployment.yaml,sample-app-configmap.yaml,sample-app-secret.yaml,sample-app-service.yaml -n $namespace
kubectl delete -f mysql-deployment.yaml,mysql-service.yaml -n database
kubectl delete ns $namespace
kubectl delete ns database