K8s:部署MySQL

来自WHY42

部署mysql5.7

创建密码

echo -n "1125482715" | base64

mysql-secret.yml:

---
apiVersion: v1
kind: Secret
metadata:
  name: mysql-secrets
type: Opaque
data:
  ROOT_PASSWORD: MTEyNTQ4MjcxNQ==
kubectl apply -f mysql-secret.yml
kubectl get secret
kubectl describe secret mysql-secrets

创建持久化卷

---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-data-disk
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100m
kubectl apply -f mysql-volume.yml
kubectl get persistentvolumeclaim
kubectl describe persistentvolumeclaim mysql-data-disk

部署

apiVersion: apps/v1
kind: Deployment
metadata:
  name: mysql-deployment
  labels:
    app: mysql
spec:
  replicas: 1
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
        - name: mysql
          image: mysql:5.7
          ports:
            - containerPort: 3306
          volumeMounts:
            - mountPath: "/var/lib/mysql"
              subPath: "mysql"
              name: mysql-data
          env:
            - name: MYSQL_ROOT_PASSWORD
              valueFrom:
                secretKeyRef:
                  name: mysql-secrets
                  key: ROOT_PASSWORD
      volumes:
        - name: mysql-data
          persistentVolumeClaim:
            claimName: mysql-data-disk

绑定IP

---
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
  - protocol: TCP
    port: 3306
    targetPort: 3306

若需要从外部访问,可以使用端口转发:

# 如果需要从所有网卡地址访问,加参数: --address 0.0.0.0
kubectl port-forward service/mysql-service 3306:3306
Forwarding from 127.0.0.1:3306 -> 3306
Forwarding from [::1]:3306 -> 3306
Handling connection for 3306