Kubernetes 之 Nginx 动静态 PV 持久存储 下篇
发布日期:2019-12-13 06:52   浏览次数:
68人阅读 2019-12-05 17:40:01

接上篇 Kubernetes 中部署 NFS Provisioner 为 NFS 提供静态分配卷(上篇)请添加链接描绘

一、nginx 运用 nfs 静态 PV

1、静态 nfs-static-nginx-rc.yaml

##清算资源
kubectl delete -f nfs-static-nginx-rc.yaml -n test

cat >nfs-static-nginx-rc.yaml<<\EOF
##创立namespace
---
apiVersion: v1
kind: Namespace
metadata:
   name: test
   labels:
     name: test
##创立nfs-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
  labels:
    pv: nfs-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs  # 留意这里运用nfs的storageClassName,假如没改k8s的默许storageClassName的话,这里可以省略
  nfs:
    path: /data/nfs/nginx/
    server: 10.198.1.155
##创立nfs-pvc
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-pvc
  namespace: test
  labels:
    pvc: nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: nfs-pv
##部署使用nginx
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: nginx-test
  namespace: test
  labels:
    name: nginx-test
spec:
  replicas: 2
  selector:
    name: nginx-test
  template:
    metadata:
      labels:
       name: nginx-test
    spec:
      containers:
      - name: nginx-test
        image: docker.io/nginx
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-data
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nfs-pvc
##创立service
---
apiVersion: v1
kind: Service
metadata:
  namespace: test
  name: nginx-test
  labels:
    name: nginx-test
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: http
    nodePort: 30080
  selector:
    name: nginx-test
EOF

##创立资源
kubectl apply -f nfs-static-nginx-rc.yaml -n test

##检查pv资源
kubectl get pv -n test --show-labels

##检查pvc资源
kubectl get pvc -n test --show-labels

##检查pod
$ kubectl get pods -n test
NAME               READY   STATUS    RESTARTS   AGE
nginx-test-r4n2j   1/1     Running   0          54s
nginx-test-zstf5   1/1     Running   0          54s

#可以看到,nginx使用曾经部署成功。
#nginx使用的数据目录是运用的nfs共享存储,我们在nfs共享的目录里参加index.html文件,然后再拜访nginx-service暴露的端口
#切换到到nfs-server效劳器上

echo "Test NFS Share discovery with nfs-static-nginx-rc" > /data/nfs/nginx/index.html

#在阅读器上拜访kubernetes主节点的 http://master:30080,就能拜访到这个页面内容了

2、静态 nfs-static-nginx-deployment.yaml

##清算资源
kubectl delete -f nfs-static-nginx-deployment.yaml -n test

cat >nfs-static-nginx-deployment.yaml<<\EOF
##创立namespace
---
apiVersion: v1
kind: Namespace
metadata:
   name: test
   labels:
     name: test
##创立nfs-pv
---
apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs-pv
  labels:
    pv: nfs-pv
spec:
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteMany
  persistentVolumeReclaimPolicy: Retain
  storageClassName: nfs  # 留意这里运用nfs的storageClassName,假如没改k8s的默许storageClassName的话,这里可以省略
  nfs:
    path: /data/nfs/nginx/
    server: 10.198.1.155
##创立nfs-pvc
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs-pvc
  namespace: test
  labels:
    pvc: nfs-pvc
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 10Gi
  storageClassName: nfs
  selector:
    matchLabels:
      pv: nfs-pv
##部署使用nginx
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  namespace: test
  labels:
    name: nginx-test
spec:
  replicas: 2
  selector:
    matchLabels:
      name: nginx-test
  template:
    metadata:
      labels:
       name: nginx-test
    spec:
      containers:
      - name: nginx-test
        image: docker.io/nginx
        volumeMounts:
        - mountPath: /usr/share/nginx/html
          name: nginx-data
        ports:
        - containerPort: 80
      volumes:
      - name: nginx-data
        persistentVolumeClaim:
          claimName: nfs-pvc
##创立service
---
apiVersion: v1
kind: Service
metadata:
  namespace: test
  name: nginx-test
  labels:
    name: nginx-test
spec:
  type: NodePort
  ports:
  - port: 80
    protocol: TCP
    targetPort: 80
    name: http
    nodePort: 30080
  selector:
    name: nginx-test
EOF

##创立资源
kubectl apply -f nfs-static-nginx-deployment.yaml -n test

##检查pv资源
kubectl get pv -n test --show-labels

##检查pvc资源
kubectl get pvc -n test --show-labels

##检查pod
$ kubectl get pods -n test
NAME                                READY   STATUS    RESTARTS   AGE
nginx-deployment-64d6f78cdf-8bw8t   1/1     Running   0          55s
nginx-deployment-64d6f78cdf-n5n4q   1/1     Running   0          55s

#可以看到,nginx使用曾经部署成功。
#nginx使用的数据目录是运用的nfs共享存储,我们在nfs共享的目录里参加index.html文件,然后再拜访nginx-service暴露的端口
#切换到到nfs-server效劳器上

echo "Test NFS Share discovery with nfs-static-nginx-deployment" > /data/nfs/nginx/index.html

#在阅读器上拜访kubernetes主节点的 http://master:30080,就能拜访到这个页面内容了
                 
            
				 
» 下一篇:没有了
  • 友情链接:
Copyright(C) 2007-2010 EntLib.com(上海闵行区浦江镇863软件孵化基地) - 企业级的电子商务系统 基于Microsoft .Net Framework / ASP.Net / C# / AJAX 平台构建的标准电子商务系统 All Rights Reserved (R)    沪ICP备08014628号