Kubernetes PV/PVC使用实践

转载于https://www.cnblogs.com/ericnie/p/7733281.html

 

pv,pvc的概念不解释了,之前在registry中已经使用过PV和PVC,现在想把WebLogic Server中的日志给放到外部的存储中来,过程如下:

首先在需要放日志的目标节点上建立一个文件夹,比如/k8s/weblogic

在/etc/exports中加入一个nfs的mount点

[root@k8s-node-1 weblogic]# cat /etc/exports
/k8s/test *(insecure,rw,async,no_root_squash)
/k8s/weblogic  *(insecure,rw,async,no_root_squash)

重新启动nfs

service nfs restart

nfs是否成功,可以通过下面命令来进行验证。

mount -t nfs -o rw 192.168.0.103:/k8s/weblogic  /mnt/nfs

建立一个pv

复制代码
[root@k8s-master pv]# cat pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
    name: pv0003
spec:
    capacity:
      storage: 5Gi
    accessModes:
      - ReadWriteOnce
    persistentVolumeReclaimPolicy: Recycle
    nfs:
      path: /k8s/weblogic
      server: 192.168.0.103
复制代码

再建立一个pvc

复制代码
[root@k8s-master pv]# cat pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: weblogiclogs
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
复制代码

通过get pv查看是否关联上

复制代码
[root@k8s-master pv]# kubectl get pv
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM                  REASON    AGE
pv0003    5Gi        RWO           Recycle         Bound     default/weblogiclogs             41m
pv01      20Gi       RWX           Recycle         Bound     default/myclaim2                 36d
复制代码

这里遇到一个问题,开始建立的pv死活claim为空,查看pv以及pvc的配置发现并没有任何名称上的关联,继续研究,发现纯粹是通过storage大小进行匹配的,之前因为照抄书本,一个是5G,一个是8G所以就无法匹配了,修改后成功。

最后是weblogic的rc的配置。

复制代码
[root@k8s-master pv]# cat rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: helloworld-service
spec:
  replicas: 1
  template:
    metadata:
      labels:
        weblogic-app: "helloworld"
        version: "0.1"
    spec:
      containers:
      - name: weblogichelloworld
        image: 1213-helloworld:v1
        volumeMounts:
        - mountPath: "/u01/oracle/user_projects/domains/base_domain/servers/AdminServer/logs"
          name: mypd
        ports:
        - containerPort: 7001
      volumes:
      - name: mypd
        persistentVolumeClaim:
          claimName: weblogiclogs
---
apiVersion: v1
kind: Service
metadata:
  name: helloworldsvc
  labels:
    weblogic-app: helloworld
spec:
  type: NodePort
  ports:
  - port: 7001
    protocol: TCP
    targetPort: 7001
    name: http
    nodePort: 30005
  selector:
    weblogic-app: helloworld
复制代码

这里又遇到一个问题,之前想法是把Servers/AdminServer下面所有的都放在pv中,后来发现pod死活不启动。修改成最后log的路径后,启动成功。

随后我们也在pv中看到满屏的日志了。

==========================================================================

值得探讨的是,这种PV/PVC模式并不是存放WebLogic日志的好的方式,因为如果RC扩展为多个WebLogic Pod,意味着多个AdminServer都需要去访问和读写同一个目录和同一个文件,因为他们都是AdminServer,所以必然造成文件的损坏,所以以上只是做一个验证,日志的收集还是通过官方推荐的EFK方式比较合适,一方面能够记录应用日志,另一方面也可以记录Pod name, 但PV/PVC最适合什么场景,还需要继续探讨。

pv,pvc的概念不解释了,之前在registry中已经使用过PV和PVC,现在想把WebLogic Server中的日志给放到外部的存储中来,过程如下:

首先在需要放日志的目标节点上建立一个文件夹,比如/k8s/weblogic

在/etc/exports中加入一个nfs的mount点

[root@k8s-node-1 weblogic]# cat /etc/exports
/k8s/test *(insecure,rw,async,no_root_squash)
/k8s/weblogic  *(insecure,rw,async,no_root_squash)

重新启动nfs

service nfs restart

nfs是否成功,可以通过下面命令来进行验证。

mount -t nfs -o rw 192.168.0.103:/k8s/weblogic  /mnt/nfs

建立一个pv

复制代码
[root@k8s-master pv]# cat pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
    name: pv0003
spec:
    capacity:
      storage: 5Gi
    accessModes:
      - ReadWriteOnce
    persistentVolumeReclaimPolicy: Recycle
    nfs:
      path: /k8s/weblogic
      server: 192.168.0.103
复制代码

再建立一个pvc

复制代码
[root@k8s-master pv]# cat pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: weblogiclogs
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 5Gi
复制代码

通过get pv查看是否关联上

复制代码
[root@k8s-master pv]# kubectl get pv
NAME      CAPACITY   ACCESSMODES   RECLAIMPOLICY   STATUS    CLAIM                  REASON    AGE
pv0003    5Gi        RWO           Recycle         Bound     default/weblogiclogs             41m
pv01      20Gi       RWX           Recycle         Bound     default/myclaim2                 36d
复制代码

这里遇到一个问题,开始建立的pv死活claim为空,查看pv以及pvc的配置发现并没有任何名称上的关联,继续研究,发现纯粹是通过storage大小进行匹配的,之前因为照抄书本,一个是5G,一个是8G所以就无法匹配了,修改后成功。

最后是weblogic的rc的配置。

复制代码
[root@k8s-master pv]# cat rc.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: helloworld-service
spec:
  replicas: 1
  template:
    metadata:
      labels:
        weblogic-app: "helloworld"
        version: "0.1"
    spec:
      containers:
      - name: weblogichelloworld
        image: 1213-helloworld:v1
        volumeMounts:
        - mountPath: "/u01/oracle/user_projects/domains/base_domain/servers/AdminServer/logs"
          name: mypd
        ports:
        - containerPort: 7001
      volumes:
      - name: mypd
        persistentVolumeClaim:
          claimName: weblogiclogs
---
apiVersion: v1
kind: Service
metadata:
  name: helloworldsvc
  labels:
    weblogic-app: helloworld
spec:
  type: NodePort
  ports:
  - port: 7001
    protocol: TCP
    targetPort: 7001
    name: http
    nodePort: 30005
  selector:
    weblogic-app: helloworld
复制代码

这里又遇到一个问题,之前想法是把Servers/AdminServer下面所有的都放在pv中,后来发现pod死活不启动。修改成最后log的路径后,启动成功。

随后我们也在pv中看到满屏的日志了。

==========================================================================

值得探讨的是,这种PV/PVC模式并不是存放WebLogic日志的好的方式,因为如果RC扩展为多个WebLogic Pod,意味着多个AdminServer都需要去访问和读写同一个目录和同一个文件,因为他们都是AdminServer,所以必然造成文件的损坏,所以以上只是做一个验证,日志的收集还是通过官方推荐的EFK方式比较合适,一方面能够记录应用日志,另一方面也可以记录Pod name, 但PV/PVC最适合什么场景,还需要继续探讨。

猜你喜欢

转载自www.cnblogs.com/cheyunhua/p/8987666.html