K8s:部署MySQL:修订间差异

来自WHY42
Riguz留言 | 贡献
建立內容為「=部署mysql5.7= ==创建密码== <syntaxhighlight lang="bash"> echo -n "1125482715" | base64 </syntaxhighlight> <syntaxhighlight lang="yaml"> --- apiVersion: v1…」的新頁面
 
Riguz留言 | 贡献
 
(未显示同一用户的5个中间版本)
第5行: 第5行:
</syntaxhighlight>
</syntaxhighlight>


mysql-secret.yml:
<syntaxhighlight lang="yaml">
<syntaxhighlight lang="yaml">
---
---
第18行: 第19行:
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
kubectl apply -f mysql-secret.yml
kubectl apply -f mysql-secret.yml
kubectl get secret
kubectl describe secret mysql-secrets
</syntaxhighlight>
== 创建持久化卷==
<syntaxhighlight lang="yaml">
---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-data-disk
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 100m
</syntaxhighlight>
<syntaxhighlight lang="bash">
kubectl apply -f mysql-volume.yml
kubectl get persistentvolumeclaim
kubectl describe persistentvolumeclaim mysql-data-disk
</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>
</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