Here is an example YAML file for deploying a MySQL database in a Kubernetes cluster:

apiVersion: v1
kind: Service
metadata:
  name: mysql
spec:
  ports:
  - protocol: TCP
    port: 3306
    targetPort: 3306
  selector:
    app: mysql
  type: ClusterIP
---
apiVersion: apps/v1beta2
kind: Deployment
metadata:
  name: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - name: mysql
        image: mysql:5.7
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secrets
              key: root-password
        - name: MYSQL_DATABASE
          valueFrom:
            configMapKeyRef:
              name: mysql-config
              key: database
        - name: MYSQL_USER
          valueFrom:
            configMapKeyRef:
              name: mysql-config
              key: user
        - name: MYSQL_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-secrets
              key: password
        ports:
        - containerPort: 3306
          name: mysql
        volumeMounts:
        - name: mysql-persistent-storage
          mountPath: /var/lib/mysql
      volumes:
      - name: mysql-persistent-storage
        persistentVolumeClaim:
          claimName: mysql-pvc
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: mysql-config
data:
  database: mydb
  user: myuser
---
apiVersion: v1
kind: Secret
metadata:
  name: mysql-secrets
type: Opaque
data:
  root-password: <base64-encoded-root-password>
  password: <base64-encoded-password>

This YAML file creates a Service and Deployment for a MySQL database. The Deployment specifies one replica, which runs a container based on the official MySQL 5.7 image. The container is configured with environment variables for the MySQL root password, database name, user, and password, which are sourced from a Secret and a ConfigMap. The container also mounts a PersistentVolumeClaim for persistent storage. The Service exposes the MySQL port (3306) for other pods to access.

MySQL K8s Deployment with YAML Example

原文地址: https://www.cveoy.top/t/topic/ome5 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录