Monday, February 1, 2021

Kubernetes - Storage - Day 10

 Kubernetes - Storage - 1/27/2021

Today's topics - PVC/PV/SC
- Persistent volume claim
- Persistent volume
- Storage classes


> kc get pvc
> kc get pv
> kc get sc

How to manage storage using kubernetes


# docker ps
# docker run --name os11

# docker run -it -d  --name os11 vimal13/apache-webserver-php

-d - detach or backgroupn
-it

# docker ps

Go inside OS
# docker attach os11
or
go inside os and provide bash shell
# docker exec -it os11 bash

# docker exec -it os11 bash
[root@03feeb49598d /]#

[root@03feeb49598d /]# ls
anaconda-post.log  boot  etc   lib    lost+found  mnt  proc  run   srv  tmp  var
bin                dev   home  lib64  media       opt  root  sbin  sys  usr
[root@03feeb49598d /]# df -h .
Filesystem      Size  Used Avail Use% Mounted on
overlay          27G   14G   14G  52% /
[root@03feeb49598d /]#

[root@03feeb49598d /]# cd /var/www/html
[root@03feeb49598d html]# ls
index.php
[root@03feeb49598d html]# ls -ltr
total 4
-rw-r--r--. 1 root root 117 Aug 12  2017 index.php
[root@03feeb49598d html]# more index.php
<body bgcolor='aqua'>
<pre>

<?php

print "welcome to vimal web server for testing";


print `ifconfig`;

?>

</pre>
[root@03feeb49598d html]#


[root@03feeb49598d html]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 22  bytes 1732 (1.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0



Curl from master server
[root@master ~]# curl 172.17.0.2
<body bgcolor='aqua'>
<pre>

welcome to vimal web server for testingeth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.2  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:02  txqueuelen 0  (Ethernet)
        RX packets 28  bytes 2194 (2.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 3  bytes 182 (182.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


</pre>
Modify the file
[root@master ~]# vi index.html
Welcome to the clob !!!

Delete the docker instance
[root@master ~]# docker rm -f os11
os11
[root@master ~]#

Re-launch again, since we don't have replication controller like feature by default on docker.
[root@master ~]# docker run -it -d  --name os11 vimal13/apache-webserver-php
1638e7bc4cda2990bdb557f31863809acb48d65def134e598e3c494dd1fe79aa

Login again,
[root@master ~]# docker exec -it os11 bash
[root@1638e7bc4cda /]#
[root@1638e7bc4cda /]# cat /var/www/html/index.php
<body bgcolor='aqua'>
<pre><?php
print "welcome to vimal web server for testing";
print `ifconfig`;
?></pre>
[root@1638e7bc4cda /]#

modified content is gone..

But the content you modified will be gone. When you delete OS, entire os get deleted as well
storage provided to the docker will also be deleted, removed.

it is called temporary/empheral in nature...

-------------------------------------------------

k8s -> docker[container]-> POD -> storage -> temp/

k8s comes with a plug (persistence volume) in which provides permanent storage
- storaging data (data inside database is importand)
    for eg, database

docker does not permanent storage if container is deleted by default.


Lets see what happens if /var/www/html is deleted because container is deleted?

Since it contains critical data, can we save this dir even container crashes?


------------------------------

On master node, lets create a dir to store critical data.
we will link /var/www/html dir to dir on base (master) os.
On master node,
# mkdir /data1
# docker run -it -d --name os12
  -v  /var/www/html vimal13/apache-webserver-php
[root@master ~]# docker run -it -d --name os13 -v /data1:/var/www/html vimal13/apache-webserver-php
c2488831139a81a140025b0434e9a37604c6f8bf4f4f62825c5d20604b12193b
[root@master ~]#

login to your container
[root@master ~]# docker exec -it os12 bash
[root@c2488831139a /]# cd /var/www/html/
[root@c2488831139a html]# cat >index.php
Life is beautiful
[root@c2488831139a html]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.4  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)
        RX packets 12  bytes 936 (936.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


Go to master node
[root@master ~]# curl 172.17.0.4
Life is beautiful

Lets remove the container
[root@master ~]# docker rm -f os13
os13
relaunch the instance
[root@master ~]# docker run -it -d --name os13 -v /data1:/var/www/html vimal13/apache-webserver-php
d2cd60faba92159e12e188686ff7fc20386f0602e4c5463e3bc2ebf200d698d3


[root@d2cd60faba92 /]# cat /var/www/html/index.php
Life is beautiful
[root@d2cd60faba92 /]#

[root@d2cd60faba92 /]# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.17.0.4  netmask 255.255.0.0  broadcast 172.17.255.255
        ether 02:42:ac:11:00:04  txqueuelen 0  (Ethernet)
        RX packets 9  bytes 726 (726.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0


Run curl on master
[root@master ~]# curl 172.17.0.4
Life is beautiful


Now, your storage is available...

Storage
- NAS
- SAN (RAW)

To provide storage to docker, you can provision through
- Local host storage (no scalable, not reliable)
- Storage Unit (SAN, NAS - more reliable - nfs, efs, gluster ...)
- There are program (plugins) which helps you to attach storage from aws, gcp, azure.
google for kubernetes storage plugins


We don't have
[root@master ~]# kc get pvc
No resources found in default namespace.
[root@master ~]# kc get pv
No resources found in default namespace.
[root@master ~]# kc get sc
No resources found in default namespace.
[root@master ~]#

Lets launch a pod (container)
[root@master ~]# kc run p1 --image=vimal13/apache-webserver-php
pod/p1 created

Login to pod
[root@master ~]# kc exec -it p1 -- bash
[root@p1 /]#
[root@p1 /]# cat >myfile.txt
Welcome to k8s
[root@p1 /]# cat myfile.txt
Welcome to k8s
[root@p1 /]# exit

Now, delete the POD
[root@master ~]# kc delete pod p1
pod "p1" deleted

Re-launch a new pod
[root@master ~]# kc run p1 --image=vimal13/apache-webserver-php
pod/p1 created
[root@master ~]# kc exec -it p1 -- bash
[root@p1 /]# cat myfile.txt
cat: myfile.txt: No such file or directory
[root@p1 /]#

The file is deleted.


[root@master ~]# kc describe pod p1
you will not see persistence volume.

What ever file you add here will be deleted oe affermal.


We will use kubernetes to provide persistence volume.

Now, we will launch a pod and verify data still exist even after pod deletion.

Provide permanent storage from local worker node(host) no affermal
Task
pod -> data -> pod remove -> rd-> launch POD -> data available (PV)

PV can be from
- Cloud (AWS, GCP, Azure)
- NAS
- Gluster
- Local

Some vocabullary you need to know
- We have cloud services (AWS, GCP, NFS)


- Dev developered app and created Image
- Developer launch a POD with that image
- Developer wants a data directory and want it persistance (permanent)
- Say 20GB is required for database
- Here this POD will be created with persistance volume.
This process is called pvc - which means request


Dev [POD]  ---------PVC----------> k8s
    [POD]  <--------Claim--------- k8s

devloper request claim of 20GB with k8s


verify if there is any claim
> kc get pvc

Now, k8s is using storage from AWS,
so, k8s admin see the request from developer
-> admin use program (plugin) and contact aws (EBS) and allocate 20GB of storage


Verify if pv is allocated
[root@master ~]# kc get pv
No resources found in default namespace.

PV will created 20GB of storage on EBS. (Bound -provide storage to POS)

Developer always claim (request)
and admin creates PV how much storage is requested
PV will go and claim the storage.

Per PVC request we have to create PV

100 PVC request 100 PV

this approach looks more static.

request comes and admin guy assign the storage

so, new version of k8s came with new idea.


PV
- Static way (older approach - by default)
- Dynamic approach (we have to use storage class for this approach)

Storage class (openshift - uses )  -> SC (self service)


Dev ---PVC -->> k8s

when there is a new request comes, pv program is automatically run ..

How PV is created?
There is an extra componenet need netween storage provider (cloud, nfs)

storage class -> sc - app
when anyone comes for app to go aws
any request for backup to to GCP for storage
any web content go to nfs

AWS storage
-----------
general purpose
SAS storage
iops

This concept is managed through provisioners

google for kuberbetes storage class provisioners

aws. gcp  .......
storage class is way to manager..

new version of k8s, you will always see storage class.
we have to claim for pvc


minikube comes with provisioner

--------------------------------------------

storage class
-------------












No comments:

Post a Comment

Git branch show detached HEAD

  Git branch show detached HEAD 1. List your branch $ git branch * (HEAD detached at f219e03)   00 2. Run re-set hard $ git reset --hard 3. ...