K8s:部署MySQL:修订间差异

来自WHY42
Riguz留言 | 贡献
Riguz留言 | 贡献
 
(未显示同一用户的2个中间版本)
第44行: 第44行:
</syntaxhighlight>
</syntaxhighlight>


== 部署 ==
<syntaxhighlight lang="bash">
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
</syntaxhighlight>
== 绑定IP ==
<syntaxhighlight lang="bash">
---
apiVersion: v1
kind: Service
metadata:
  name: mysql-service
spec:
  selector:
    app: mysql
  ports:
  - protocol: TCP
    port: 3306
    targetPort: 3306
</syntaxhighlight>


若需要从外部访问,可以使用端口转发:
<syntaxhighlight lang="bash">
# 如果需要从所有网卡地址访问,加参数: --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
</syntaxhighlight>
[[Category:Linux/Unix]]
[[Category:Linux/Unix]]

2021年9月17日 (五) 01:50的最新版本

部署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