Introduction to Minio, stand-alone and cluster deployment, JAVA upload and download practice

Posted by stueee on Fri, 25 Feb 2022 16:31:59 +0100

Official website address

https://min.io/

Official website document

https://docs.min.io/docs/minio-quickstart-guide.html

Download address

https://dl.min.io/

Basic concepts


Object: the basic object stored in Minio, such as file and byte stream
Bucket: the logical space used to store objects. The data of each bucket is isolated from each other. For the client, it is equivalent to a top-level folder for storing files.
Drive: the disk that stores data. It is passed in as a parameter when Minio is started. All object data in Minio will be stored in the drive.
Set: that is, a set of drives. Distributed deployment automatically divides one or more sets according to the cluster size, and the drives in each set are distributed in different locations. An object is stored on a set. (For example: {1...64} is divided into 4 sets each of size 16.)
An object is stored on a Set
A cluster is divided into multiple sets
The number of drives contained in a Set is fixed, which is automatically calculated by the system according to the cluster size by default
The drives in a SET are distributed on different nodes as much as possible

Deployment

Stand alone deployment

1. After downloading the minio file, you need to execute

chmod +x minio

2. Write startup script

touch miniostart

chmod +x miniostart

vi miniostart

3. Script content

###############1 One node and one disk, stand-alone mode. If the disk is broken, data will be lost##############
#####minoi account
export MINIO_ROOT_USER=admin
#####minio password
export MINIO_ROOT_PASSWORD=12345678

#####Start in the background. If there is no miniodada1 folder in the root directory, Minio will automatically create the folder. After startup, a folder will be generated under the miniodada1 folder minio. The hidden folder of sys is used to store the relevant configuration information of Minio
nohup /minio/minio server --address ":9000" --console-address ":50001" /miniodata1 > /minio/minio-9000.log 2>&1 &

4. Save exit execution script view startup log

./miniostart
tail -f minio-9000.log

If no error is reported normally, it can be changed to the ip of minio service through 127.0.0.1
http://127.0.0.1:9000
perhaps
http://127.0.0.1:50001
Visit, 9000 and 50001 are the port numbers specified in the script. Enter the account password to enter the management home page

After entering, you can create a bucket. For example, I added a hello bucket
At this time, a hello folder will appear in the miniodata1 directory
Click the Browse button on the home page to enter hello. You can upload a file, such as a HelloWorld txt
At this time, a HelloWorld will appear under / miniodata1/hello Txt file
Note: for the deployment of single machine and single disk, the uploaded files will be stored as their own files


Single machine deployment requires high machine performance and disk, which is suitable for scenarios with a small number of files,
It is recommended to deploy another single machine to form a primary and standby to prevent single point of failure. The primary and standby can be realized by using minio MC mirror, or the contents of folders can be synchronized through operation and maintenance, depending on the scenario and the tolerance of failure
Reference documents
https://docs.min.io/minio/baremetal/reference/minio-mc/mc-mirror.html#command-mc-mirror

Single machine multi disk deployment

Note that if it was a single disk before, it cannot be directly upgraded to a single disk with multiple disks. minio will start erasure code storage only when there are at least 4 disks. Erasure code mode can recover data when less than half of the disks are lost,
Due to different storage methods, it is impossible to directly upgrade from a single disk to the erasure mode at startup, so it is recommended to deploy directly according to the erasure mode,
Then import the previous files into the new file system. The import method can use rclone tool, mc tool or other means to import the final files into the new system.

The script is as follows

###############1 Four disks in one node, stand-alone mode. If four disks are broken at the same time, more than two disks will lose data, and only two disks will be broken minio Data can be recovered automatically##############
#####minoi account
export MINIO_ROOT_USER=admin
#####minio password
export MINIO_ROOT_PASSWORD=12345678

#####Start in the background. If there are no 4 folders of miniodata1, miniodata2, miniodata3 and miniodata4 in the root directory, minio will be created automatically. When the local IP is 10.0.110.120, the configuration is as follows. Note 120 120 and 1 There are three points in the middle
nohup /minio/minio server --address ":9000" --console-address ":50001" http://10.0.110.{120...120}:9000/miniodata{1...4} > /minio/minio-9000.log 2>&1 &

Single machine capacity expansion scheme

Single machine capacity expansion is to attach multiple disks. minio will automatically coordinate the group of disks to which files should be uploaded according to the disk storage scheme specified in the capacity expansion script. Recovery is also based on this group of policies

The script is as follows

###############1 Four disks in one node, stand-alone mode. If four disks are broken at the same time, more than two disks will lose data, and only two disks will be broken minio Data can be recovered automatically##############
#####minoi account
export MINIO_ROOT_USER=admin
#####minio password
export MINIO_ROOT_PASSWORD=12345678

#####Background start, you can find that is to add a group http://10.0.110.{120...120}:9000/miniodata{1...4}
nohup /minio/minio server --address ":9000" --console-address ":50001" http://10.0.110.{120...120}:9000/miniodata{1...4} http://10.0.110.{120...120}:9000/miniodata{5...8} http://10.0.110.{120...120}:9000/miniodata{9...12} > /minio/minio-9000.log 2>&1 &

Cluster scheme, multi node

Single machine performance is limited. minio usually deploys multiple nodes. The scripts are roughly the same, but all attached disks need to be specified at startup. Specifically http://10.0.110.{120...123}:9000/miniodata{1...4} modify the IP. minio will automatically coordinate resources in the cluster for response file operation
Note that in actual use, it is found that the failure of any node will lead to the unavailability of the service, so it is recommended to make a backup for each node

Java operation minio realizes upload and download

Reference documents
https://docs.min.io/docs/java-client-quickstart-guide.html

Introducing dependencies into maven pom files

<dependency>
    <groupId>io.minio</groupId>
    <artifactId>minio</artifactId>
    <version>8.3.0</version>
</dependency>

Upload and download code

import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;

import io.minio.BucketExistsArgs;
import io.minio.DownloadObjectArgs;
import io.minio.GetObjectArgs;
import io.minio.MakeBucketArgs;
import io.minio.MinioClient;
import io.minio.ObjectWriteResponse;
import io.minio.UploadObjectArgs;
import io.minio.errors.MinioException;

public class MinioTest {

	/** minio address */
	private static final String MINIO_HOST = "http://10.0.110.120:9000";

	/** minio account number */
	private static final String MINIO_ROOT_USER = "admin";

	/** minio password */
	private static final String MINIO_ROOT_PASSWORD = "12345678";

	/** minio bucket name */
	private static final String MINIO_BUCKET = "hello";

	public static void main(String[] args) throws InvalidKeyException, NoSuchAlgorithmException, IllegalArgumentException, IOException {
		try {
			// Create a minio client
			MinioClient minioClient = MinioClient.builder().endpoint(MINIO_HOST).credentials(MINIO_ROOT_USER, MINIO_ROOT_PASSWORD).build();

			// Check whether the bucket exists
			boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(MINIO_BUCKET).build());
			if (!found) {
				// Create a bucket
				minioClient.makeBucket(MakeBucketArgs.builder().bucket(MINIO_BUCKET).build());
			} else {
				System.out.println("bucket Already exists");
			}

			// Upload the file, and the final file is stored in the abc folder HelloWorld In HTML, if there is no abc folder, it will be created automatically. The folder can be set to multiple levels, such as abc/def/xyz
			String fileName = "abc/helloworld.html";
			// Upload file, local file path
			String filePath = "/home/helloworld.html";

			ObjectWriteResponse uploadObject = minioClient.uploadObject(UploadObjectArgs.builder().bucket(MINIO_BUCKET).object(fileName).filename(filePath).build());
			System.out.println("file helloworld.html Uploaded to hello this bucket in" + String.format("[etag:%s,Storage location:%s]", uploadObject.etag(), uploadObject.object()));

			// Suppose there is a file ABC / HelloWorld. In minio html
			// Download to the local folder / home/download. The folder must exist to save successfully
			String localFilePath = "/home/download/helloworld.html";

			// This method has no return value and only stores the file locally
			minioClient.downloadObject(DownloadObjectArgs.builder().bucket(MINIO_BUCKET).object(fileName).filename(localFilePath).build());

			// This method returns an input stream, which needs to be saved locally or output to the browser
			InputStream in = minioClient.getObject(GetObjectArgs.builder().bucket(MINIO_BUCKET).object(fileName).build());
			System.out.println("Download succeeded! file size:" + in.available());
		} catch (MinioException e) {
			System.out.println("Error occurred: " + e);
			System.out.println("HTTP trace: " + e.httpTrace());
		}
	}
}

Topics: Java Back-end architecture MinIO