? mysql use PV and PVC practice operation record

Source: Internet
Author: User
Tags create database docker ps k8s

Create an NFS storage directory

[Email protected] ~]# MKDIR/DATA/K8S/MYSQLPV1


Create PV

[email protected] ~]# cat Mysql-pv.yml

Apiversion:v1

Kind:persistentvolume

Metadata

Name:mysqlpv1

Spec

Capacity:

Storage:1gi

Accessmodes:

-Readwriteonce

Persistentvolumereclaimpolicy:retain

Storageclassname:nfs1

Nfs:

Path:/DATA/K8S/MYSQLPV1

server:192.168.211.135

[Email protected] ~]# Kubectl apply-f mysql-pv.yml

Persistentvolume "Mysqlpv1" created

[[email protected] ~]# Kubectl get PV

NAME capacity ACCESS MODES RECLAIM POLICY STATUS CLAIM storageclass REASON Age

Mypv1 1Gi RWO Retain Bound default/mypvc1 NFS 47m

Mysqlpv1 1Gi RWO Retain Available nfs1 3s


Create PVC

[email protected] ~]# cat Mysql-pvc1.yml

Apiversion:v1

Kind:persistentvolumeclaim

Metadata

Name:mysqlpvc1

Spec

Accessmodes:

-Readwriteonce

Resources

Requests:

Storage:1gi

Storageclassname:nfs1

[Email protected] ~]# Kubectl apply-f mysql-pvc1.yml

Persistentvolumeclaim "MYSQLPVC1" created

[Email protected] ~]#

[[email protected] ~]# Kubectl get pvc-o Wide

NAME STATUS VOLUME capacity ACCESS MODES Storageclass age

MYPVC1 Bound mypv1 1Gi RWO NFS 48m

MYSQLPVC1 Bound mysqlpv1 1Gi RWO nfs1 36s

[Email protected] ~]#


Deploying MySQL


Yml file references are as follows:


Some notes:

---this symbol is a delimiter


[email protected] ~]# cat Mysql-svc.yml

Apiversion:v1

Kind:service

Metadata

Name:mysql

Spec

Ports

-port:3306

Selector

App:mysql


---

Apiversion:apps/v1beta1

Kind:deployment

Metadata

Name:mysql

Spec

Selector

Matchlabels:

App:mysql

Template

Metadata

Labels

App:mysql

Spec

Containers

-image:mysql:5.6

Name:mysql

Env:

-Name:mysql_root_password

Value:password

Ports

-containerport:3306

Name:mysql

Volumemounts:

-Name:mysql-data

Mountpath:/var/lib/mysql

Volumes

-Name:mysql-data

Persistentvolumeclaim:

Claimname:mysqlpvc1

[Email protected] ~]#


[Email protected] ~]# Kubectl apply-f mysql-svc.yml

Service "MySQL" unchanged

Deployment.apps "MySQL" Created


[[email protected] ~]# Kubectl get pod-o Wide

NAME ready STATUS Restarts IP NODE

MYSQL-7BD85DBB6F-5FJLX 1/1 Running 0 3m 10.244.1.106 Kubernetes2


You can see that the pod is generated on the Kubernetes2

Execute the command in this node


[[email protected] ~]# Docker PS

CONTAINER id        image                  &NBS P                          ,         &NB Sp                              COMMAND  &nbs P               created             STATUS    ;           ports               names

13b98755ae54 docker.io/[email protected]:0267b9b43034ed630e94f846ca825140994166c6c7d39d43d4dbe8d1404e1129 "Dock   Er-entrypoint ... " 5 minutes ago up 5 minutes k8s_mysql_mysql-7bd85dbb6f-5fjlx_default_0fae8658-7b73-11e8-bd C5-000c292f3b91_0



Inspect under


[email protected] ~]# Docker inspect 13b98755ae54



You can see that MYSQLPV1 is used.


Generate data, test the effect:


In the container operations database:


Create a database T1


[[email protected] ~]# Docker exec 13b98755ae54 mysql-uroot-ppassword-e "CREATE Database T1"

Warning:using a password on the command line interface can is insecure.

[[email protected] ~]# Docker exec 13b98755ae54 mysql-uroot-ppassword-e "show Databases"

Warning:using a password on the command line interface can is insecure.

Database

Information_schema

Mysql

Performance_schema

T1

[Email protected] ~]#


Create a data table in T1 T1


[[email protected] ~]# Docker exec 13b98755ae54 mysql-uroot-ppassword-e "Use t1;create table t1 (i int);"

Warning:using a password on the command line interface can is insecure.


data table T1 inserting data and retrieving


[[email protected] ~]# Docker exec 13b98755ae54 mysql-uroot-ppassword-e "use T1;insert into T1 values (1), (3), (3);"

Warning:using a password on the command line interface can is insecure.

[[email protected] ~]# Docker exec 13b98755ae54 mysql-uroot-ppassword-e "use t1;select * from T1;"

Warning:using a password on the command line interface can is insecure.

I

1

3

3

[Email protected] ~]#


The data has been generated and we look at the NFS storage directory and the container storage directory situation


NFS Directory


[Email protected] ~]# ls/data/k8s/mysqlpv1/

AUTO.CNF ibdata1 ib_logfile0 ib_logfile1 mysql performance_schema t1

[Email protected] ~]#

[Email protected] ~]# LS/DATA/K8S/MYSQLPV1/T1

Db.opt t1.frm T1.IBD

[Email protected] ~]#


Container


[[email protected] ~]# Docker exec 13b98755ae54 Ls/var/lib/mysql

Auto.cnf

Ib_logfile0

Ib_logfile1

Ibdata1

Mysql

Performance_schema

T1

[[email protected] ~]# Docker exec 13b98755ae54 ls/var/lib/mysql/t1

Db.opt

T1.frm

T1.ibd

[Email protected] ~]#


Data comparison shows that PVC is used successfully

Now simulate the malfunction, the Kubernetes2 machine dies.


Kubernetes2 shut down the machine

[[Email protected] ~]# init 0

Connection closing ... Socket Close.


Connection closed by foreign host.


Disconnected from remote host (Kubernetes2) at 16:34:27.




[[email protected] ~]# Kubectl get pod-o Wide

NAME ready STATUS Restarts IP NODE

MYSQL-7BD85DBB6F-5FJLX 1/1 Unknown 0 30m 10.244.1.106 kubernetes2

MYSQL-7BD85DBB6F-JWWFV 1/1 Running 0 38s 10.244.2.114 Kubernetes3


Pod automatically switches to Kubernetes3.


Working with the container database on the KUBERNETES3


[[email protected] ~]# Docker PS

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES

5bb97bd16595        84581e99d807                  &NBS P                       nginx-g       "Daemon ..." &n Bsp  3 minutes ago       up 3 minutes                  & nbsp         K8s_nginx-deployment_nginx-deployment-6b5c99b6fd-tcwfc_default_ 48C5442E-7B77-11E8-BDC5-000C292F3B91_0

eec205b40845 97fdbdd65c6a "Docker-entrypoint ..." 3 minutes ago up 3 minutes k8s_mysql_mysql-7bd85dbb6f-jwwfv_default_48b8e8ad-7b77-11e8-bd C5-000c292f3b91_0



Retrieving data

[[email protected] ~]# Docker exec eec205b40845 mysql-uroot-ppassword-e "use t1;select * from T1;"

Warning:using a password on the command line interface can is insecure.

I

1

3

3

[Email protected] ~]#


Data intact


MySQL uses PV and PVC practice operations Records

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.