1.创建秘钥



#创建秘钥
kubectl create secret generic mysql-root-password --from-literal=password=123456




2.创建pvc

#查看sc
[root@k8s-master01 mysql]# kubectl get sc
NAME              PROVISIONER                     RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
nfs-boge          nfs-provisioner-01              Retain          Immediate           false                  91d


#编辑pvc
vi  mysql-pvc.yaml
i
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: mysql-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi
  # 存储类,具有相同存储类名称的pv和pvc才能进行绑定
  storageClassName: nfs-boge


#创建pvc
kubectl apply -f mysql-pvc.yaml

#检查
kubectl get pv -A
kubectl get pvc -A

3.创建mysql

vi  mysql-svc.yaml
i
apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: mysql
  name: mysql
spec:
  selector:
    matchLabels:
      app: mysql
  template:
    metadata:
      labels:
        app: mysql
    spec:
      containers:
      - image: mysql:5.7
        name: mysql
        env:
        - name: MYSQL_ROOT_PASSWORD
          valueFrom:
            secretKeyRef:
              name: mysql-root-password
              key: password
          # 如果你不想使用secret对象保存mysql登录密码,可以直接使用下面的方式指定,简单粗暴未尝不可
          #value: "123456"
        ports:
        - containerPort: 3306
        volumeMounts:
        - name: mysqlvolume
          mountPath: /var/lib/mysql
      volumes:
      - name: mysqlvolume
        # 使用pvc
        persistentVolumeClaim:
          claimName: mysql-pvc
---
#定义mysql的Service
apiVersion: v1
kind: Service
metadata:
  labels:
    app: svc-mysql
  name: svc-mysql
spec:
  selector:
    app: mysql
  type: NodePort
  ports:
  - port: 3306
    protocol: TCP
    targetPort: 3306
    nodePort: 31234



kubectl apply -f  mysql-svc.yaml

4.修改用户登入权限

kubectl -n dev get all


#连接数据库
kubectl -n dev  exec -it mysql-856fd4594c-gxkmx -- mysql -u root -p


#切换数据库
mysql> use mysql;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

#修改用户登入授权
mysql> GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;
Query OK, 0 rows affected, 1 warning (0.00 sec)

#刷新权限
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

#退出
mysql> exit


5.mysql工具连接

image.png

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐