1. 程式人生 > >Docker Kubernetes Volume 網路資料卷

Docker Kubernetes Volume 網路資料卷

Docker Kubernetes Volume 網路資料卷

由於支援網路資料卷眾多 今天只拿nfs作為案例。

支援網路資料卷

  • nfs
  • iscsi
  • glusterfs
  • awsElasticBlockStore
  • cephfs
  • azureFileVolume
  • azureDiskVolume
  • vsphereVolume
  • .....

環境:

  • 系統:Centos 7.4 x64
  • Docker版本:18.09.0
  • Kubernetes版本:v1.8
  • 管理節點:192.168.1.79
  • 工作節點:192.168.1.78
  • 工作節點:
    192.168.1.77

一、搭建NFS服務與客戶端

1、管理節點:安裝nfs服務端、配置nfs主配置檔案、新增許可權、啟動

yum install nfs-utils -y
vim /etc/exports
# 新增目錄給相應網段訪問並新增讀寫許可權 /data 192.168.1.0/24(insecure,rw,async,no_root_squash)
# 建立共享目錄,新增許可權
mkdir -p /data
chmod 777 /data
# 開啟rpc服務
systemctl start rpcbind
# 啟動服務並設定開機自啟 systemctl start nfs

2、工作節點:安裝nfs客戶端、啟動服務

yum install nfs-utils -y
# 開啟rpc服務
systemctl start rpcbind
# 啟動服務並設定開機自啟
systemctl start nfs

二、共享NFS網路資料卷

1、管理節點:建立yaml檔案

vim nginx-nfs.yaml

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: nginx-deployment-nfs
spec:
  replicas: 3
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      
- name: nginx image: nginx:1.10 volumeMounts: - name: wwwroot mountPath: /var/www/html ports: - containerPort: 80 volumes: - name: nfs: server: 192.168.1.79 path: /data
# 指定api版本
apiVersion: extensions/v1beta1
# 指定需要建立的資源物件
kind: Deployment
# 源資料、可以寫name,名稱空間,物件標籤
metadata:
# 指定物件名稱
  name: nginx-deployment2-nfs
# 描述資源相關資訊
spec:
# 指定pod 副本數,預設1
  replicas: 3
# 描述資源具體資訊
  template:
# 匹配標籤欄位
    metadata:
# 指定pod標籤value:key
      labels:
# 標籤名
        app: nginx
# 管理容器
    spec:
# 指定容器資訊
      containers:
# 指定容器名稱
      - name: nginx
# 指定映象名稱
        image: nginx:1.10
# 網路資料卷管理
        volumeMounts:
# 資料卷名稱
        - name: wwwroot
# 容器資料卷掛載路徑
          mountPath: /var/www/html
# 埠管理
        ports:
# 暴露埠
        - containerPort: 80
# 網路共享資料卷管理
      volumes:
# 資料卷名稱兩邊需要相同
      - name: wwwroot
# 資料卷型別為nfs
        nfs:
# NFS伺服器地址
          server: 192.168.1.79
# 服務端共享路徑
          path: /data
檔案註解

2、管理節點:建立Deployment

kubectl create -f nginx-nfs.yaml
命令:kubectl get pods -o wide

NAME                                    READY     STATUS    RESTARTS   AGE       IP            NODE
nginx-deployment-nfs-5fbcddddb6-7btt4   1/1       Running   0          55s       172.17.2.11   192.168.1.78
nginx-deployment-nfs-5fbcddddb6-sf6bz   1/1       Running   0          55s       172.17.2.10   192.168.1.78
nginx-deployment-nfs-5fbcddddb6-ws8wk   1/1       Running   0          55s       172.17.1.9    192.168.1.77
檢視建立情況
命令:kubectl describe nginx-deployment-nfs-5fbcddddb6-sf6bz

    Mounts:
      /var/www/html from wwwroot (rw)
Conditions:
  Type           Status
  Initialized    True 
  Ready          True 
  PodScheduled   True 
Volumes:
  wwwroot:
    Type:        NFS (an NFS mount that lasts the lifetime of a pod)
    Server:      192.168.1.79
    Path:        /data
    ReadOnly:    false
QoS Class:       BestEffort
Node-Selectors:  <none>
Tolerations:     <none>
reated container
檢視詳細資訊

3、測試

# 1、宿主端nfs共享檔案內建立檔案
命令:touch /data/123

# 2、進入容器內檢視檔案是否共享
命令:kubectl exec nginx-deployment-nfs-5fbcddddb6-sf6bz -it bash

[email protected]-deployment-nfs-5fbcddddb6-sf6bz:/# ls /var/www/html/
123