k8s持续化存储-NFS

搭建NFS储存:
Volume类型:支持卷:emptyDir,hostpath,quobyte,fc,iscsi,nfs…….等等。
nfs共享储存:
生产环境需要多个POD或者多个APP共享数据,而这些应用又在不同的机器的不同的POD里面,网络文件系统可以解决这一问题。kubernetes中通过简单的配置可以挂载NFS到POD中,而NFS的数据是可以永久保存的,同时NFS支持并发读写操作。

1.nfs搭建:(repo中安装)
[root@repo1 ~]# yum -y install nfs-utils
[root@repo1 ~]# mkdir -m 777 /var/webroot
[root@repo1 ~]# mkdir -m 777 /var/webroot1
[root@repo1 ~]# mkdir -m 777 /var/webroot2
[root@repo1 ~]#  vim /etc/exports
/var/webroot      *(rw)
/var/webroot1      *(rw)
/var/webroot2      *(rw)
[root@repo1 ~]# systemctl start nfs
[root@repo1 ~]# systemctl enable  nfs

2.node节点部署(所有node操作)
[root@node-0001 ~]# yum -y install nfs-utils
[root@node-0001 ~]# showmount -e 192.168.1.50
Export list for 192.168.1.50:
/var/webroot2 *
/var/webroot1 *
/var/webroot  *

3.创建PV和PVS
[root@master ~]# vim pv.yaml          //创建pv.yaml

apiVersion: v1  #当前格式的版本
kind: PersistentVolume
metadata:                                  #当前资源的元数据
name: pv-nfs
labels:
app: web-nfs
spec:
capacity:
storage: 15Gi                       #共享大小
accessModes:
– ReadWriteMany                   #多节点读写模式
persistentVolumeReclaimPolicy: Retain
nfs:
path: /var/webroot               #共享目录
server: 192.168.1.50               #共享的ip
[root@master ~]# kubectl create -f pv.yaml   //创建PV
[root@master ~]# kubectl get pv                 //查看PV
NAME       STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-nfs    Bound     pv-nfs    15Gi       RWX                           3m

[root@master ~]# vim pvc.yaml    //创建pvc.yaml

apiVersion: v1  #当前格式的版本
kind: PersistentVolumeClaim
metadata:
name: pvc-nfs
spec:
accessModes:
– ReadWriteMany
resources:
requests:
storage: 5Gi
selector:
matchLabels:
app: web-nfs
[root@master ~]# kubectl create -f pvc.yaml   //创建pvc
[root@master ~]# kubectl get pvc                 //查看pvc
NAME       STATUS    VOLUME    CAPACITY   ACCESS MODES   STORAGECLASS   AGE
pvc-nfs    Bound     pv-nfs    15Gi       RWX                           16m
[root@master ~]# kubectl get pv                   //再次查看PV,发现已经挂载
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM              STORAGECLASS   REASON    AGE
pv-nfs    15Gi       RWX            Retain           Bound     default/pvc-nfs    21m

4.验证nfs储存
[root@master ~]# vim appache.yaml    //创建一个apache和service

apiVersion: extensions/v1beta1         #当前格式的版本
kind: Deployment                       #当前创建资源的类型, 当前类型是Deployment
metadata:                              #当前资源的元数据
name: apache                        #当前资源的名字 是元数据必须的项
spec:                                  #是当前Deployment的规格说明
replicas: 3                          #指当前创建的副本数量 默认不填 默认值就为‘1’
template:                            #定义pod的模板
metadata:                          #当前pod的元数据
labels:                          #至少顶一个labels标签,可任意创建一个 key:value
app: apache
spec:                              #当前pod的规格说明
containers:                      #容器
– name: apache                   #是容器的名字容器名字是必须填写的
image: repo1:5000/myos:httpd         #镜像 镜像的名字和版本
stdin: true
tty: true
volumeMounts:
– mountPath: /var/www/html        #mount的路径
name: site-data
volumes:
– name: site-data
persistentVolumeClaim:
claimName: pvc-nfs            #mount的资源

apiVersion: v1              #当前的格式
kind: Service                #创建资源类型
metadata:                    #当前资源的元数据
name: apache-service    #资源名称
namespace: default    #资源的区域
spec:                            #资源的说明
clusterIP: 10.254.254.110     #资源的IP
ports:                         #端口
– port: 80                   #服务的端口
targetPort: 80           #容器的端口
nodePort:                 #发布的节点端口
protocol: TCP           #协议
selector:                     #标签选择器
app: apache                #后端服务资源
type: ClusterIP            #资源类型

[root@master ~]# kubectl create -f appache.yaml
[root@master ~]# kubectl get  pod
[root@master ~]# kubectl get service      //查看httpd的service-IP
NAME             TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)    AGE
apache-service   ClusterIP   10.254.254.110   <none>        80/TCP     12m
[root@repo1]# cd /var/webroot      //来到nfs共享文件目录
[root@repo1 webroot]# cat index.html         //创建静态网页
<pre>
hello world
hello world
hello world
[root@repo1 webroot]# cat info.php       //创建动态网页
<pre>
<?PHP
print_R(“nginx_host: \t”.$_SERVER[“SERVER_ADDR”].”\n”);
?>

[root@master ~]# watch -n 0.5 curl -i  http://10.254.254.110/info.php   //回到验证
<pre>
nginx_host:     10.254.3.2         //可以看到此IP不断的变换

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇