Objective
Today I am going to show how to create snapshots in Kubernetes. This is really helpful in case you want to manage or mitigate the risk of losing your PODs.
From UI:
From the command line:
Creating periodic snapshots
When you create a StorageClass, you can specify a snapshot schedule on the volume as specified below. This allows snapshotting the persistent data of the running pod using the volume.
Above spec will take snapshots of the portworx-repl-1-snap-internal PVC every 190 minutes.
Creating a snapshot on demand
You can also trigger a new snapshot on a running POD by creating a PersistentVolumeClaim as specified in the following spec:
Note the format of the “name” field. The format is name.<new_snap_name>-source.<old_volume_name>. Above example references the parent (source) persistent volume claim pvc013 and creates a snapshot by the name snap013.
Listing snapshots
To list snapshots taken by Portworx, use the /opt/pwx/bin/pxctl volume snapshot list command.
For example:
# /opt/pwx/bin/pxctl volume snapshot list
ID NAME SIZE HA SHARED IO_PRIORITY SCALE STATUS
1056733319296008813 snap013 1 GiB 2 no LOW 1 up - detached
You can use the ID or NAME of the snapshots when using them to restore a volume.
Restoring a pod from a snapshot
To restore a pod to use the created snapshot, use the pvc name.snap013-source.pvc013 in the pod spec.
Managing snapshots through pxctl
You can provide the SAN capabilities, using portworx and then create a snapshot of a volume, let's say using an MYSQL DB.
First, create a database and a demo table in your MYSQL container.
# mysql --user=tleoncio --password=password
MySQL [(none)]> create database pxThiagoLeoncio;
Query OK, 1 row affected (0.00 sec)
MySQL [(none)]> use pxThiagoLeoncio;
Database changed
MySQL [pxdemo]> create table K8SThiagoLeoncioSample (counter int unsigned);
Query OK, 0 rows affected (0.04 sec)
MySQL [pxdemo]> quit;
Bye
Now create a snapshot of this database using pxctl.
First, use pxctl volume list to see what volume you want to snapshot
# /opt/pwx/bin/pxctl v l
ID NAME SIZE HA SHARED ENCRYPTED IO_PRIORITY SCALE STATUS
381983511213673988 pvc-e7e66f98-0915-11e7-94ca-7cd30ac1a138 20 GiB 2 no no LOW 0 up - attached on 147.75.105.196
Then use pxctl to snapshot your volume
opt/pwx/bin/pxctl snap create 381983511213673988 --name snap-013
Volume successfully snapped: 835956864616765999
Also, you can use pxctl to see your snapshot created
# /opt/pwx/bin/pxctl snap list
ID NAME SIZE HA SHARED ENCRYPTED IO_PRIORITY SCALE STATUS
835956864616765999 snap-01 20 GiB 2 no no LOW 0 up - detached
Then we create a mysql Pod to mount the snapshot discussed in this article:
kubectl create -f portworx-mysql-snap-pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: test-portworx-snapped-volume-pod
spec:
containers:
- image: mysql:5.6
name: mysql-snap13
env:
# Use secret in real usage
- name: MYSQL_ROOT_PASSWORD
value: password
ports:
- containerPort: 3306
name: mysql
volumeMounts:
- name: snap-013
mountPath: /var/lib/mysql
volumes:
- name: snap-013
# This Portworx volume must already exist.
portworxVolume:
volumeID: "vol1"
Inspect that the database shows the cloned tables in the new MYSQL instance.
# mysql --user=tleoncio --password=password
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| mysql |
| performance_schema |
| pxThiagoLeoncio |
+--------------------+
3 rows in set (0.00 sec)
mysql> use pxThiagoLeoncio;
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> show tables;
+-----------------------+
| Tables_in_pxdemo |
+-----------------------+
| K8SThiagoLeoncioSample|
+-----------------------+
1 row in set (0.00 sec)
I hope this helps and happy coding,
Thiago Leoncio.
No comments:
Post a Comment