从本章开始,我们在k8s下进行一系列持久卷(PersistentVolume)相关的实战,熟悉了解如何使用k8s环境中的存储资源;
原文地址:http://blog.csdn.net/boling_cavalry/article/details/79516039
整个《Kubernetes持久卷实战》由以下两篇文章组成:
本章目标是用最少的步骤和时间体验PV,所以先不展开每个配置和开发的细节,主要完成以下操作:
为了能顺利完成实战,您需要做好以下的准备工作:
您可以在GitHub下载本章用到的4个文件,地址和链接信息如下表所示:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | [email protected]:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
这个文件夹下面有四个文件,功能如下所示,请下载下来,稍后会用到:
准备工作已经完成,接下来可以实战体验了;
找一台电脑,部署NFS Server,具体操作可以参照《Ubuntu16环境安装和使用NFS》,假设该电脑的IP地址是192.168.119.128,共享目录是/usr/local/work/nfs;
将下载好的pv1.yaml和pvc1.yaml文件放在一个目录下,然后执行命令kubectl create -f pv1.yaml,pvc1.yaml即可创建PV和PVC,再执行命令kubectl get pv查看是否创建成功,如下:
root@maven:/usr/local/work/k8spv# kubectl create -f pv1.yaml,pvc1.yaml
persistentvolume "pv1" created
persistentvolumeclaim "pvc1" created
root@maven:/usr/local/work/k8spv# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pv1 1Gi RWO Recycle Bound default/pvc1 1m
可见pv1已经绑定到pvc1,接下来创建Pod挂载pvc1;
将下载好的k8spvdemo.yaml和k8spvdemo-svc.yaml文件放在一个目录下,然后执行命令kubectl create -f k8spvdemo.yaml,k8spvdemo-svc.yaml即可创建Pod和Service,打开dashboard页面,可以看到已经部署好的容器情况,如下图:
服务部署完毕了,接下来试试上传文件到服务端,看是PV是否有效;
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<groupId>com.bolingcavalrygroupId>
<artifactId>uploadfileclientartifactId>
<version>1.0-SNAPSHOTversion>
<properties>
<project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
properties>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>4.3.5version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpmimeartifactId>
<version>4.3.5version>
dependency>
dependencies>
project>
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.File;
import java.io.IOException;
/**
@Description : 上传文件的类,将本地文件POST到server
@Author : [email protected]
@Date : 2018-02-24 18:12
*/
public class UploadFileClient {
/**
/**
public static void main(String[] args) throws Exception{
System.out.println(“start upload”);
CloseableHttpClient httpclient = HttpClients.createDefault();
try {
HttpPost httppost = new HttpPost(POST_URL);
//基本的配置信息
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(200000).setSocketTimeout(200000).build();
httppost.setConfig(requestConfig);
//要上传的文件
FileBody bin = new FileBody(new File(UPLOAD_FILE_FULLPATH));
//在POST中添加一个字符串请求参数
StringBody comment = new StringBody("This is comment", ContentType.TEXT_PLAIN);
HttpEntity reqEntity = MultipartEntityBuilder.create().addPart("file", bin).addPart("comment", comment).build();
httppost.setEntity(reqEntity);
System.out.println("executing request " + httppost.getRequestLine());
//发起POST
CloseableHttpResponse response = httpclient.execute(httppost);
try {
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseEntityStr = EntityUtils.toString(response.getEntity());
System.out.println("response status : " + response.getStatusLine());
System.out.println("response content length: " + resEntity.getContentLength());
System.out.println("response entity str : " + responseEntityStr);
}
EntityUtils.consume(resEntity);
} finally {
response.close();
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
httpclient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("end upload");
}
}
以上源码中,有两处要注意:
第一,POST_URL = "http://192.168.119.153:30010//upload"中的192.168.119.153是Pod所在的节点机器的IP地址,请替换为您的k8s环境中的节点IP地址;
第二,UPLOAD_FILE_FULLPATH = "D:\\temp\\201802\\21\\abc.zip",这里配置的是要上传的本地文件的路径,请替换为您要上传的文件路径;
3. 直接运行UploadFileClient.java,看到控制台输出如下信息表示上传成功:
```shell
start upload
executing request POST http://192.168.119.153:30010//upload HTTP/1.1
response status : HTTP/1.1 200
response content length: 40
response entity str : SpringBoot环境下,上传文件成功
end upload
Process finished with exit code 0
上传成功了,在dashboard上查看容器的日志,能看到上传有关的日志如下图红框所示:
现在理论上现在文件已经保存在NFS Server的/usr/local/work/nfs目录下了,去检查一下;
登录NFS Server,进入/usr/local/work/nfs,查看文件信息如下:
root@nfs:/usr/local/work/nfs# ll
total 160
drwxrwxrwx 2 root root 4096 Mar 11 06:02 ./
drwxr-xr-x 5 root root 4096 Mar 8 20:31 ../
-rw-r--r-- 1 nobody nogroup 153035 Mar 11 03:24 abc.zip
可见k8s上的tomcat应用可以通过PVC的方式将客户端上传的文件保存在NFS服务器上;
至此,对k8s持久卷服务的体验就完成了,后续章节我们将深入以上内容的细节,对PV做进一步了解;