網站首頁 編程語言 正文
nfs 文件系統
使用 nfs 文件系統 實現kubernetes存儲動態掛載
1. 安裝服務端和客戶端
root@hello:~# apt install nfs-kernel-server nfs-common
其中 nfs-kernel-server 為服務端, nfs-common 為客戶端。
2. 配置 nfs 共享目錄
root@hello:~# mkdir /nfs
root@hello:~# sudo vim /etc/exports
/nfs *(rw,sync,no_root_squash,no_subtree_check)
各字段解析如下:
/nfs: 要共享的目錄
:指定可以訪問共享目錄的用戶 ip, * 代表所有用戶。192.168.3. 指定網段。192.168.3.29 指定 ip。
rw:可讀可寫。如果想要只讀的話,可以指定 ro。
sync:文件同步寫入到內存與硬盤中。
async:文件會先暫存于內存中,而非直接寫入硬盤。
no_root_squash:登入 nfs 主機使用分享目錄的使用者,如果是 root 的話,那么對于這個分享的目錄來說,他就具有 root 的權限!這個項目『極不安全』,不建議使用!但如果你需要在客戶端對 nfs 目錄進行寫入操作。你就得配置 no_root_squash。方便與安全不可兼得。
root_squash:在登入 nfs 主機使用分享之目錄的使用者如果是 root 時,那么這個使用者的權限將被壓縮成為匿名使用者,通常他的 UID 與 GID 都會變成 nobody 那個系統賬號的身份。
subtree_check:強制 nfs 檢查父目錄的權限(默認)
no_subtree_check:不檢查父目錄權限?
配置完成后,執行以下命令導出共享目錄,并重啟 nfs 服務:
root@hello:~# exportfs -a
root@hello:~# systemctl restart nfs-kernel-server
root@hello:~#
root@hello:~# systemctl enable nfs-kernel-server
客戶端掛載
root@hello:~# apt install nfs-common
root@hello:~# mkdir -p /nfs/
root@hello:~# mount -t nfs 192.168.1.66:/nfs/ /nfs/
root@hello:~# df -hT
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 7.8G 0 7.8G 0% /dev
tmpfs tmpfs 1.6G 2.9M 1.6G 1% /run
/dev/mapper/ubuntu--vg-ubuntu--lv ext4 97G 9.9G 83G 11% /
tmpfs tmpfs 7.9G 0 7.9G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 7.9G 0 7.9G 0% /sys/fs/cgroup
/dev/loop0 squashfs 56M 56M 0 100% /snap/core18/2128
/dev/loop1 squashfs 56M 56M 0 100% /snap/core18/2246
/dev/loop3 squashfs 33M 33M 0 100% /snap/snapd/12704
/dev/loop2 squashfs 62M 62M 0 100% /snap/core20/1169
/dev/loop4 squashfs 33M 33M 0 100% /snap/snapd/13640
/dev/loop6 squashfs 68M 68M 0 100% /snap/lxd/21835
/dev/loop5 squashfs 71M 71M 0 100% /snap/lxd/21029
/dev/sda2 ext4 976M 107M 803M 12% /boot
tmpfs tmpfs 1.6G 0 1.6G 0% /run/user/0
192.168.1.66:/nfs nfs4 97G 6.4G 86G 7% /nfs
創建配置默認存儲
[root@k8s-master-node1 ~/yaml]# vim nfs-storage.yaml
[root@k8s-master-node1 ~/yaml]#
[root@k8s-master-node1 ~/yaml]# cat nfs-storage.yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
name: nfs-storage
annotations:
storageclass.kubernetes.io/is-default-class: "true"
provisioner: k8s-sigs.io/nfs-subdir-external-provisioner
parameters:
archiveOnDelete: "true" ## 刪除pv的時候,pv的內容是否要備份
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nfs-client-provisioner
labels:
app: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
spec:
replicas: 1
strategy:
type: Recreate
selector:
matchLabels:
app: nfs-client-provisioner
template:
metadata:
labels:
app: nfs-client-provisioner
spec:
serviceAccountName: nfs-client-provisioner
containers:
- name: nfs-client-provisioner
image: registry.cn-hangzhou.aliyuncs.com/chenby/nfs-subdir-external-provisioner:v4.0.2
# resources:
# limits:
# cpu: 10m
# requests:
# cpu: 10m
volumeMounts:
- name: nfs-client-root
mountPath: /persistentvolumes
env:
- name: PROVISIONER_NAME
value: k8s-sigs.io/nfs-subdir-external-provisioner
- name: NFS_SERVER
value: 192.168.1.66 ## 指定自己nfs服務器地址
- name: NFS_PATH
value: /nfs/ ## nfs服務器共享的目錄
volumes:
- name: nfs-client-root
nfs:
server: 192.168.1.66
path: /nfs/
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: nfs-client-provisioner-runner
rules:
- apiGroups: [""]
resources: ["nodes"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["persistentvolumes"]
verbs: ["get", "list", "watch", "create", "delete"]
- apiGroups: [""]
resources: ["persistentvolumeclaims"]
verbs: ["get", "list", "watch", "update"]
- apiGroups: ["storage.k8s.io"]
resources: ["storageclasses"]
verbs: ["get", "list", "watch"]
- apiGroups: [""]
resources: ["events"]
verbs: ["create", "update", "patch"]
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: run-nfs-client-provisioner
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: ClusterRole
name: nfs-client-provisioner-runner
apiGroup: rbac.authorization.k8s.io
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
rules:
- apiGroups: [""]
resources: ["endpoints"]
verbs: ["get", "list", "watch", "create", "update", "patch"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: leader-locking-nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
subjects:
- kind: ServiceAccount
name: nfs-client-provisioner
# replace with namespace where provisioner is deployed
namespace: default
roleRef:
kind: Role
name: leader-locking-nfs-client-provisioner
apiGroup: rbac.authorization.k8s.io
創建
[root@k8s-master-node1 ~/yaml]# kubectl apply -f nfs-storage.yaml
storageclass.storage.k8s.io/nfs-storage created
deployment.apps/nfs-client-provisioner created
serviceaccount/nfs-client-provisioner created
clusterrole.rbac.authorization.k8s.io/nfs-client-provisioner-runner created
clusterrolebinding.rbac.authorization.k8s.io/run-nfs-client-provisioner created
role.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created
rolebinding.rbac.authorization.k8s.io/leader-locking-nfs-client-provisioner created
[root@k8s-master-node1 ~/yaml]#
查看是否創建默認存儲
[root@k8s-master-node1 ~/yaml]# kubectl get storageclasses.storage.k8s.io
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
nfs-storage (default) k8s-sigs.io/nfs-subdir-external-provisioner Delete Immediate false 100s
[root@k8s-master-node1 ~/yaml]#
創建pvc進行測試
[root@k8s-master-node1 ~/yaml]# vim pvc.yaml
[root@k8s-master-node1 ~/yaml]# cat pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: nginx-pvc
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 200Mi
[root@k8s-master-node1 ~/yaml]#
[root@k8s-master-node1 ~/yaml]# kubectl apply -f pvc.yaml
persistentvolumeclaim/nginx-pvc created
[root@k8s-master-node1 ~/yaml]#
查看pvc
[root@k8s-master-node1 ~/yaml]#
[root@k8s-master-node1 ~/yaml]# kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
nginx-pvc Bound pvc-8a4b6065-904a-4bae-bef9-1f3b5612986c 200Mi RWX nfs-storage 4s
[root@k8s-master-node1 ~/yaml]#
查看pv
[root@k8s-master-node1 ~/yaml]# kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvc-8a4b6065-904a-4bae-bef9-1f3b5612986c 200Mi RWX Delete Bound default/nginx-pvc nfs-storage 103s
[root@k8s-master-node1 ~/yaml]#
原文鏈接:https://www.oiox.cn/index.php/archives/32/
相關推薦
- 2023-03-22 Nginx轉發丟失cookie表現形式及解決方案_nginx
- 2022-08-25 Python進階Matplotlib庫圖繪制_python
- 2022-10-10 Axios和Jquery實現文件上傳功能_jquery
- 2022-12-24 C++中類的三種訪問權限解析:private、public與protect_C 語言
- 2023-12-17 SpringSecurity的默認登錄頁的使用
- 2022-07-07 Python?pluggy模塊的用法示例演示_python
- 2023-05-29 tf.nn.conv2d與tf.layers.conv2d的區別及說明_python
- 2022-09-30 Python實現圖像增強_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支