This is the 11th day of my participation in the Challenge. For details, see:More article challenges
This article describes how to deploy a MySQL database based on Kubernetes.
Create Service
Create a Service that is fixed to the IP address of the MySQL database to be deployed while providing load balancing. Here is the contents of the mysql-service.yaml file:
apiVersion: v1
kind: Service
metadata:
name: mysql
spec:
selector:
app: mysql
ports:
- port: 3306
Copy the code
The above configuration creates a Service object named mysql that will proxy requests to a Pod using TCP port 3306 with the tag app=mysql.
Create resource:
kubectl create -f mysql-service.yaml
Copy the code
Example Create persistent volume PV
Create a MySQL persistent volume mysql-pv.yaml (Kubernetes also destroys temporary volumes when Pod no longer exists; However, Kubernetes does not destroy persistent volumes. :
apiVersion: v1
kind: PersistentVolume
metadata:
name: mysql-pv
labels:
type: local
spec:
storageClassName: manual
capacity:
storage: 20Gi
accessModes:
- ReadWriteOnce A volume can be mounted by a node in read/write mode
hostPath:
path: "/mnt/data"
Copy the code
Create resource:
kubectl create -f mysql-pv.yaml
Copy the code
Create persistent volume declaration PVC
Persistent volumes are resources in the cluster, and persistent volume claims are requests for these resources, and are also used to perform claim checks on resources. We will create a persistent volume named mysql-PVC and declare mysql-pvC.yaml:
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pvc
spec:
storageClassName: manual
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 20Gi
Copy the code
Create resource:
kubectl create -f mysql-pvc.yaml
Copy the code
Deploy MySQL
Create Pod using MySQL 5.7 image on port 3306, mysql-Deployment.yaml:
apiVersion: apps/v1
kind: Deployment
metadata:
name: mysql
spec:
selector:
matchLabels:
app: mysql
strategy:
type: Recreate
template:
metadata:
labels:
app: mysql
spec:
containers:
- image: Mysql: 5.7
name: mysql
env:
- name: MYSQL_ROOT_PASSWORD Use secret in production environments
value: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: mysql-data
mountPath: /var/lib/mysql
volumes:
- name: mysql-data
persistentVolumeClaim:
claimName: mysql-pvc
Copy the code
Create resource:
kubectl create -f mysql-deployment.yaml
Copy the code
Connect the MySQL
Kubectl run-it --rm --image=mysql:5.6 --restart=Never mysql-client -- mysql-hmysql-ppasswordCopy the code