K8S 使用NFS 创建PV和PVC的例子 学习From https://blog.csdn.net/xts_huangxin/article/details/51494472

1. 获取资料 网址: https://blog.csdn.net/xts_huangxin/article/details/51494472  感谢原作者 这里面 按照自己的机器情况进行了学习模仿 并且mark一下.

2. 下载yaml 文件:

https://github.com/FlyingShit-XinHuang/my-doc/blob/master/k8s/pv%26pvc/nfs.zip

3. 解压缩文件,然后根据实际情况进行修改.

4. 创建NFS

mkdir -p /nfs/k8s
创建目录
chmod  -R 777 /nfs
修改权限
vim /etc/exports
增加内容
/nfs/k8s *(rw,insecure,sync,no_subtree_check,no_root_squash)
保存.
exportfs -r
让文件生效
systemctl enable nfs
systemctl enable rpcbind
systemctl restart nfs
systemctl restart rpcbind
启动服务

5. 创建PV 用到的yaml文件进行修改后的为:

spec 里面定义相关的内容. nfs 里面对应的 server 制定 以及 path 下面 写好了nfs的目录

创建语句为 
kubectl create -f nfs-pv.yaml

yaml文件为

apiVersion: v1
kind: PersistentVolume
metadata:
  name: nfs
spec:
  capacity:
    storage: 1Gi
  accessModes:
    - ReadWriteMany
  nfs:
    server: 10.24.103.1 
    path: "/nfs/k8s"

6. 创建PVC

据说 capacity 里面的存储大小应该保持一致 ,避免出现问题

kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: nfs
spec:
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 1Gi

7. 进行测试验证, github 里面是定义了 一个 busybox 来写入 文件 nginx 来读取 当前目录的index.html的值

首先 busybox 相关的内容为

apiVersion: v1
kind: ReplicationController
metadata:
  name: nfs-busybox
spec:
  replicas: 1
  selector:
    name: nfs-busybox
  template:
    metadata:
      labels:
        name: nfs-busybox
    spec:
      containers:
      - image:busybox
        command:
          - sh
          - -c
          - 'while true; do date > /mnt/index.html; hostname >> /mnt/index.html; sleep $(($RANDOM % 5 + 5)); done'
        imagePullPolicy: IfNotPresent
        name: busybox
        volumeMounts:
          # name must match the volume name below
          - name: nfs
            mountPath: "/mnt"
      volumes:
      - name: nfs
        persistentVolumeClaim:
          claimName: nfs

创建 nginx 前段网页

apiVersion: v1
kind: ReplicationController
metadata:
  name: nfs-web
spec:
  replicas: 1
  selector:
    role: web-frontend
  template:
    metadata:
      labels:
        role: web-frontend
    spec:
      containers:
      - name: web
        image: nginx 
        ports:
          - name: web
            containerPort: 80
        volumeMounts:
            # name must match the volume name below
            - name: nfs
              mountPath: "/usr/share/nginx/html"
      volumes:
      - name: nfs
        persistentVolumeClaim:
          claimName: nfs

创建services

kind: Service
apiVersion: v1
metadata:
  name: nfs-web
spec:
  ports:
    - port: 80
  selector:
    role: web-frontend

然后 

kubectl get service
获取信息

[root@k8smaster01 ~]# kubectl get service
NAME           TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)        AGE
frontend       NodePort    10.100.8.215     <none>        80:30001/TCP   35d
kubernetes     ClusterIP   10.96.0.1        <none>        443/TCP        37d
nfs-web        ClusterIP   10.103.125.236   <none>        80/TCP         1h
redis-master   ClusterIP   10.111.216.233   <none>        6379/TCP       35d
redis-slave    ClusterIP   10.107.12.137    <none>        6379/TCP       35d

然后测试验证

命令 :curl 10.103.125.236
显示结果:
[root@k8smaster01 ~]# curl 10.103.125.236
Mon Jun 25 09:15:25 UTC 2018
nfs-busybox-l596c
[root@k8smaster01 ~]# 

7. 学习用到的其他命令

kubectl create -f *.yaml
创建资源
kubectl delete pv pvname
kubectl delete pvc pvcname
删除pv和pvc 但是如果 有 pod 在引用他们的话无法删除需要删除rc 才可以
kubectl delete pod  podname
删除pod k8s 会瞬间再启动一个起来. 如果没有images 重新pull一下 用这种方式很快就能从containercreating 变成 running状态.

kubectl delete rc rcname 
能够删除rc定义 保证pv也被完整性的删除

kubectl get service
kubectl get pod
kubectl get nodes
kubectl get rc
kubectl get rc -n kube-system (选择显示namespace)

猜你喜欢

转载自www.cnblogs.com/jinanxiaolaohu/p/9225277.html