Category : Kubernetes | Sub Category : Kubernetes With Java | By Prasad Bonam Last updated: 2024-01-30 09:39:34 Viewed : 577
To configure a MySQL database in a Kubernetes cluster, you can use Kubernetes manifests to define the necessary resources such as deployments, services, and persistent volumes. Here is a basic example to get you started:
Create a file named mysql-deployment.yaml
with the following content:
yamlapiVersion: apps/v1
kind: Deployment
metadata:
name: mysql-deployment
spec:
replicas: 1
selector:
matchLabels:
app: mysql
template:
metadata:
labels:
app: mysql
spec:
containers:
- name: mysql
image: mysql:latest
env:
- name: MYSQL_ROOT_PASSWORD
value: your-root-password
- name: MYSQL_DATABASE
value: your-database-name
- name: MYSQL_USER
value: your-username
- name: MYSQL_PASSWORD
value: your-password
ports:
- containerPort: 3306
volumeMounts:
- name: mysql-persistent-storage
mountPath: /var/lib/mysql
volumes:
- name: mysql-persistent-storage
persistentVolumeClaim:
claimName: mysql-pv-claim
Replace the placeholders (your-root-password
, your-database-name
, your-username
, your-password
) with your desired values.
Create a file named mysql-pv-claim.yaml
with the following content:
yamlapiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: mysql-pv-claim
spec:
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 1Gi
This creates a PersistentVolumeClaim (PVC) for MySQL to ensure data persistence.
Create a file named mysql-service.yaml
with the following content:
yamlapiVersion: v1
kind: Service
metadata:
name: mysql-service
spec:
selector:
app: mysql
ports:
- protocol: TCP
port: 3306
targetPort: 3306
type: ClusterIP
This creates a service to expose the MySQL deployment within the Kubernetes cluster.
Apply these configurations to your Kubernetes cluster using the following commands:
bashkubectl apply -f mysql-pv-claim.yaml kubectl apply -f mysql-deployment.yaml kubectl apply -f mysql-service.yaml
This assumes that you have kubectl
configured to connect to your Kubernetes cluster.
Ensure that the MySQL image, version, and configuration parameters suit your requirements. Additionally, consider using secrets or more secure ways to manage sensitive information in production deployments.