In applications, there are two main scenarios for handling data storage:
- Database Storage: Data is saved to a database. The application connects to the database service via code and uses SQL statements to read and write data. This method does not require storage provided by the K8s cluster.
- File Storage: Since the file system within a Pod’s container is recreated with each restart, any files stored within the container are not retained after a restart. However, applications may need to persist certain files (such as logs, user-uploaded files, etc.) so that they can be accessed again after a container restart. This is the problem of persistent file storage, which requires the K8s cluster to provide a storage solution.
K8S Volume
K8s uses an abstract concept called a Volume to solve the problem of persistent file storage. In a Pod, we first define a Volume representing a certain storage space. Then, within the Pod’s container, by mounting this Volume to a specific directory in the container, we create a mapping relationship.
Any operations performed in this directory (such as creating directories, reading/writing files, etc.) are actually performed in the designated storage space. As a result, these operations are naturally retained in the storage space, achieving…