当前位置: > 其它学习 > Kubernetes >

K8S持久化存储PV,PVC的简单实现

时间:2019-05-30 14:34来源:linux.it.net.cn 作者:IT
1、测试使用NFS文件
 
查询有无安装上这两个,没有就装上
 
rpm -qa nfs-utils rpcbind 
 
[root@V71 nfs_test]# cat /etc/exports
#/nfs_test 192.168.0.0/16(rw,no_root_squash,no_all_squash,sync)
/nfs_test 192.168.0.0/16(rw,sync,all_squash)
 
为了方便测试放开权限
 
chown -R nfsnobody /nfs_test/
 
chmod 777 -R /nfs_test/
 
 
 
确实服务正常并且测试可以被客户端挂载,NFS默认使用2049端口,跨网段确认端口转发无误
 
[root@V71 nfs_test]# service rpcbind status
Redirecting to /bin/systemctl status rpcbind.service
● rpcbind.service - RPC bind service
   Loaded: loaded (/usr/lib/systemd/system/rpcbind.service; disabled; vendor preset: enabled)
   Active: active (running) since 三 2019-05-22 16:21:46 CST; 1h 43min ago
  Process: 2978 ExecStart=/sbin/rpcbind -w $RPCBIND_ARGS (code=exited, status=0/SUCCESS)
 Main PID: 2983 (rpcbind)
   CGroup: /system.slice/rpcbind.service
           └─2983 /sbin/rpcbind -w
 
5月 22 16:21:46 V71 systemd[1]: Starting RPC bind service...
5月 22 16:21:46 V71 systemd[1]: Started RPC bind service.
[root@V71 nfs_test]# service nfs status
Redirecting to /bin/systemctl status nfs.service
● nfs-server.service - NFS server and services
   Loaded: loaded (/usr/lib/systemd/system/nfs-server.service; disabled; vendor preset: disabled)
  Drop-In: /run/systemd/generator/nfs-server.service.d
           └─order-with-mounts.conf
   Active: active (exited) since 三 2019-05-22 16:27:20 CST; 1h 38min ago
  Process: 3577 ExecStopPost=/usr/sbin/exportfs -f (code=exited, status=0/SUCCESS)
  Process: 3574 ExecStopPost=/usr/sbin/exportfs -au (code=exited, status=0/SUCCESS)
  Process: 3573 ExecStop=/usr/sbin/rpc.nfsd 0 (code=exited, status=0/SUCCESS)
  Process: 3591 ExecStart=/usr/sbin/rpc.nfsd $RPCNFSDARGS (code=exited, status=0/SUCCESS)
  Process: 3586 ExecStartPre=/bin/sh -c /bin/kill -HUP `cat /run/gssproxy.pid` (code=exited, status=0/SUCCESS)
  Process: 3585 ExecStartPre=/usr/sbin/exportfs -r (code=exited, status=0/SUCCESS)
 Main PID: 3591 (code=exited, status=0/SUCCESS)
   CGroup: /system.slice/nfs-server.service
 
5月 22 16:27:20 V71 systemd[1]: Starting NFS server and services...
5月 22 16:27:20 V71 systemd[1]: Started NFS server and services.
 
 
 
[root@k8s1 ~]# mount -t nfs 192.168.137.1:/nfs_test /nfsclient/
 
 
 
2、创建PV,PVC
 
[root@k8s1 pv]# cat pv1.yml 
apiVersion: v1
kind: PersistentVolume
metadata:
    name: pv1
spec:
    capacity:
      storage: 2Gi
    accessModes:
      - ReadWriteOnce
    persistentVolumeReclaimPolicy: Recycle
    nfs:
      path: /nfs_test
      server: 192.168.137.1
[root@k8s1 pv]# cat pvc1.yml 
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: pvc1
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
 
 
 
[root@k8s1 pv]# kubectl create -f pv1.yml 
persistentvolume "pv1" created
 
[root@k8s1 pv]# kubectl get pv
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS      CLAIM     STORAGECLASS   REASON    AGE
pv1       2Gi        RWO            Recycle          Available                                      30s
 
 
 
[root@k8s1 pv]# kubectl create -f pvc1.yml 
persistentvolumeclaim "pvc1" created
[root@k8s1 pv]# kubectl get pv             
NAME      CAPACITY   ACCESS MODES   RECLAIM POLICY   STATUS    CLAIM          STORAGECLASS   REASON    AGE
pv1       2Gi        RWO            Recycle          Bound     default/pvc1                            6m
 
 
 
[root@k8s1 pv]# cat war.yml 
apiVersion: apps/v1
kind: Deployment
metadata:
  name: tomcatwars
spec:
  selector:
    matchLabels:
      app: tomcatwar 
  replicas: 2
  template:
    metadata:
      labels:
        app: tomcatwar
    spec:
      imagePullSecrets:
      - name: registry-key
      containers:
      - name: tomcatwar
        image: re.bcdgptv.com.cn/war:v1
        volumeMounts:
        - mountPath: "/usr/local/tomcat/logs"
          name: mpvc1
        ports: 
        - containerPort: 8080
      volumes:
      - name: mpvc1
        persistentVolumeClaim:
          claimName: pvc1  
 
 
[root@k8s1 pv]# cat warservice.yml 
kind: Service
apiVersion: v1
metadata: 
  name: warservice
  labels:
    app: tomcatwar
spec:
  type: NodePort
  ports:
    - port: 8080
      protocol: TCP
      nodePort: 8899
  selector:
    app: tomcatwar
 
 
 
创建Deployment及Service
 
[root@k8s1 pv]# kubectl create -f war.yml 
deployment.apps "tomcatwars" created
 
[root@k8s1 pv]# kubectl create -f warservice.yml 
 
 
 
3、测试访问,war.yml文件将tomcat的日志文件挂载到了pvc,此时访问项目,日志文件被写入到了nfs共享的路径
 
 
 
 
访问日志及路径:
[root@V71 nfs_test]# pwd
/nfs_test
[root@V71 nfs_test]# ls
1.txt  catalina.2019-05-22.log  host-manager.2019-05-22.log  localhost.2019-05-22.log  localhost_access_log.2019-05-22.txt  manager.2019-05-22.log
[root@V71 nfs_test]# cat localhost_access_log.2019-05-22.txt 
172.30.26.0 - - [22/May/2019:09:56:38 +0000] "GET /war1.war/login.jsp HTTP/1.1" 200 12313
172.30.26.0 - - [22/May/2019:09:56:39 +0000] "GET /war1.war/dwr/interface/loginService.js HTTP/1.1" 200 540
172.30.26.0 - - [22/May/2019:09:56:39 +0000] "GET /war1.war/dwr/engine.js HTTP/1.1" 200 44180
172.30.26.0 - - [22/May/2019:09:56:39 +0000] "GET /war1.war/dwr/util.js HTTP/1.1" 200 46250
172.30.26.0 - - [22/May/2019:09:56:39 +0000] "GET /war1.war/img/loading.gif HTTP/1.1" 304 -
172.30.26.0 - - [22/May/2019:09:56:39 +0000] "GET /war1.war/img/luck.gif HTTP/1.1" 304 -
172.30.26.0 - - [22/May/2019:09:56:39 +0000] "GET /war1.war/img/login-wel.gif HTTP/1.1" 304 -
172.30.26.0 - - [22/May/2019:09:56:39 +0000] "GET /war1.war/img/login-top-bg.gif HTTP/1.1" 304 -
172.30.26.0 - - [22/May/2019:09:56:39 +0000] "GET /war1.war/img/login_bg.jpg HTTP/1.1" 304 -
172.30.26.0 - - [22/May/2019:09:56:39 +0000] "GET /war1.war/img/login-content-bg.gif HTTP/1.1" 304 -
172.30.26.0 - - [22/May/2019:09:57:45 +0000] "GET /war1.war/login.jsp HTTP/1.1" 200 12313
172.30.26.0 - - [22/May/2019:09:57:46 +0000] "GET /war1.war/dwr/interface/loginService.js HTTP/1.1" 200 540
172.30.26.0 - - [22/May/2019:09:57:46 +0000] "GET /war1.war/img/loading.gif HTTP/1.1" 200 2377
172.30.26.0 - - [22/May/2019:09:57:46 +0000] "GET /war1.war/img/luck.gif HTTP/1.1" 200 429
172.30.26.0 - - [22/May/2019:09:57:46 +0000] "GET /war1.war/img/login-wel.gif HTTP/1.1" 200 8844
172.30.26.0 - - [22/May/2019:09:57:46 +0000] "GET /war1.war/dwr/engine.js HTTP/1.1" 200 44180
172.30.26.0 - - [22/May/2019:09:57:46 +0000] "GET /war1.war/dwr/util.js HTTP/1.1" 200 46250
172.30.26.0 - - [22/May/2019:09:57:46 +0000] "GET /war1.war/img/login-top-bg.gif HTTP/1.1" 200 174
172.30.26.0 - - [22/May/2019:09:57:46 +0000] "GET /war1.war/img/login_bg.jpg HTTP/1.1" 200 20754
172.30.26.0 - - [22/May/2019:09:57:46 +0000] "GET /war1.war/img/login-content-bg.gif HTTP/1.1" 200 20202
172.30.26.0 - - [22/May/2019:09:57:46 +0000] "GET /favicon.ico HTTP/1.1" 200 21630
172.30.26.0 - - [22/May/2019:10:14:02 +0000] "GET /war1.war/dwr/engine.js HTTP/1.1" 200 44180
30.26.1 - - [22/May/2019:10:14:02 +0000] "GET /war1.war/dwr/interface/loginService.js HTTP/1.1" 200 540
172.30.26.1 - - [22/May/2019:10:14:02 +0000] "GET /war1.war/dwr/util.js HTTP/1.1" 200 46250
172.30.26.1 - - [22/May/2019:10:14:09 +0000] "GET /war1.war/login.jsp HTTP/1.1" 200 12313
172.30.26.1 - - [22/May/2019:10:14:09 +0000] "GET /war1.war/dwr/interface/loginService.js HTTP/1.1" 200 540
172.30.26.1 - - [22/May/2019:10:14:09 +0000] "GET /favicon.ico HTTP/1.1" 200 21630
172.30.26.1 - - [22/May/2019:10:14:28 +0000] "GET /war1.war/login.jsp HTTP/1.1" 200 12313
172.30.26.1 - - [22/May/2019:10:14:28 +0000] "GET /war1.war/dwr/interface/loginService.js HTTP/1.1" 200 540
172.30.26.1 - - [22/May/2019:10:14:28 +0000] "GET /favicon.ico HTTP/1.1" 200 21630
 
 
(责任编辑:IT)
------分隔线----------------------------