Skip to content

Commit e07b782

Browse files
Update Sunlife_Notes
1 parent 88537d7 commit e07b782

File tree

1 file changed

+190
-0
lines changed

1 file changed

+190
-0
lines changed

Sunlife_Notes

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1688,3 +1688,193 @@ spec:
16881688
port: 80
16891689
targetPort: 80
16901690
------------------------------------------------
1691+
1692+
--------------Volumes-------------------------
1693+
Kubernetes Volumes (emptyDir,hostPath,Persistence Volume)
1694+
By Raman
1695+
When a Container crashes, kubelet will restart it, but the files will be lost - the Container starts with a clean state. Second, when running Containers together in a Pod it is often necessary to share files between those Containers. The Kubernetes Volume abstraction solves both of these problems.
1696+
1697+
emptyDir
1698+
An emptyDir volume is first created when a Pod is assigned to a Node, and exists as long as that Pod is running on that node. As the name says, it is initially empty. Containers in the Pod can all read and write the same files in the emptyDir volume, though that volume can be mounted at the same or different paths in each Container. When a Pod is removed from a node for any reason, the data in the emptyDir is deleted forever
1699+
1700+
Lab
1701+
1702+
# nginx-emptydir.yaml
1703+
apiVersion: v1
1704+
kind: Pod
1705+
metadata:
1706+
name: nginx-emptydir
1707+
spec:
1708+
containers:
1709+
- name: nginx-container
1710+
image: nginx
1711+
volumeMounts:
1712+
- name: test-vol
1713+
mountPath: /test-mnt
1714+
volumes:
1715+
- name: test-vol
1716+
emptyDir: {}
1717+
1718+
1719+
*******************************************************************
1720+
1721+
2. Create & Display Pod with emptyDir volume
1722+
1723+
1724+
1725+
kubectl create -f nginx-emptydir.yaml
1726+
1727+
kubectl get po -o wide
1728+
1729+
kubectl exec nginx-emptydir df /test-mnt
1730+
1731+
kubectl describe pod nginx-emptydir
1732+
1733+
1734+
1735+
*******************************************************************
1736+
1737+
3. Cleanup
1738+
1739+
1740+
1741+
kubectl delete po nginx-emptydir
1742+
1743+
1744+
1745+
hostPath
1746+
A hostPath volume mounts a file or directory from the host node's filesystem into your Pod. This is not something that most Pods will need, but it offers a powerful escape hatch for some applications.
1747+
1748+
LAB
1749+
1750+
# 1. HostPath YAML file
1751+
1752+
1753+
apiVersion: v1
1754+
kind: Pod
1755+
metadata:
1756+
name: nginx-hostpath
1757+
spec:
1758+
containers:
1759+
- name: nginx-container
1760+
image: nginx
1761+
volumeMounts:
1762+
- mountPath: /test-mnt
1763+
name: test-vol
1764+
volumes:
1765+
- name: test-vol
1766+
hostPath:
1767+
path: /test-vol
1768+
1769+
1770+
1771+
1772+
*******************************************************************
1773+
1774+
# 2. Create and Display HostPath
1775+
1776+
1777+
1778+
kubectl create -f nginx-hostpath.yaml
1779+
1780+
kubectl get po
1781+
1782+
kubectl exec nginx-hostpath df /test-mnt
1783+
1784+
1785+
1786+
*******************************************************************
1787+
1788+
3. Test: Creating "test" file underlying host dir & accessing from from pod
1789+
1790+
1791+
1792+
From HOST:
1793+
1794+
~~~~~~~~~~
1795+
1796+
cd /test-vol
1797+
1798+
echo "From Host" > from-host.txt
1799+
1800+
cat from-host.txt
1801+
1802+
1803+
1804+
From POD:
1805+
1806+
~~~~~~~~
1807+
1808+
kubectl exec nginx-hostpath cat /test-mnt/from-host.txt
1809+
1810+
4. Test: Creating "test" file inside the POD & accessing from underlying host dir
1811+
1812+
1813+
1814+
From POD:
1815+
1816+
~~~~~~~~~
1817+
1818+
kubectl exec nginx-hostpath -it -- /bin/sh
1819+
1820+
cd /test-mnt
1821+
1822+
echo "From Pod" > from-pod.txt
1823+
1824+
cat from-pod.txt
1825+
1826+
1827+
1828+
From Host:
1829+
1830+
~~~~~~~~~~
1831+
1832+
cd /test-vol
1833+
1834+
ls
1835+
1836+
cat from-pod.txt
1837+
1838+
1839+
1840+
1841+
1842+
*******************************************************************
1843+
1844+
5. Clean up
1845+
1846+
1847+
1848+
kubectl delete po nginx-hostpath
1849+
1850+
kubectl get po
1851+
1852+
ls /test-vol
1853+
1854+
1855+
1856+
PersistentVolume (PV)
1857+
A PersistentVolume (PV) is a piece of storage in the cluster that has been provisioned by an administrator or dynamically provisioned using Storage Classes. It is a resource in the cluster just like a node is a cluster resource. PVs are volume plugins like Volumes, but have a lifecycle independent of any individual Pod that uses the PV. This API object captures the details of the implementation of the storage, be that NFS, iSCSI, or a cloud-provider-specific storage system.
1858+
1859+
PersistentVolumeClaim (PVC)
1860+
A PersistentVolumeClaim (PVC) is a request for storage by a user. It is similar to a Pod. Pods consume node resources and PVCs consume PV resources. Pods can request specific levels of resources (CPU and Memory). Claims can request specific size and access modes (e.g., they can be mounted ReadWriteOnce, ReadOnlyMany or ReadWriteMany
1861+
1862+
#pv.yaml
1863+
apiVersion: v1
1864+
kind: PersistentVolume
1865+
metadata:
1866+
name: pv-hostpath
1867+
labels:
1868+
type: local
1869+
spec:
1870+
storageClassName: manual
1871+
capacity:
1872+
storage: 1Gi
1873+
accessModes:
1874+
- ReadWriteOnce
1875+
hostPath:
1876+
path: "/kube"
1877+
1878+
#create pv with below command
1879+
kubectl create -f pv.yaml
1880+
---------------------------------------------

0 commit comments

Comments
 (0)