Hướng dẫn chi tiết bài LAB Kubernetes (K8s): Volume, PersistentVolume và PersistentVolumeClaim

Hướng dẫn chi tiết bài LAB Kubernetes (K8s): Volume, PersistentVolume và PersistentVolumeClaim


1. Giới thiệu về Volume trong Kubernetes

Kubernetes hỗ trợ nhiều loại Volume, được sử dụng để lưu trữ và chia sẻ dữ liệu giữa các containers:

  • Volume bên trong Pod: Lưu trữ tạm thời (ví dụ: emptyDir), chỉ tồn tại trong vòng đời của Pod.
  • Volume trong Cluster: Lưu trữ dữ liệu dùng chung giữa các Node trong Cluster (sử dụng configMap, secret, hoặc hostPath).
  • Volume ngoài Cluster: Lưu trữ dữ liệu trên NFS server, iSCSI SAN, hoặc các dịch vụ lưu trữ đám mây (AWS EBS, Google Cloud Storage).

Chi tiết thêm về các loại volume: Kubernetes Documentation


2. Tạo Volume trong Pod

Bước 1: Tạo thư mục và file YAML
mkdir volumetest && cd volumetest
nano podvolume.yaml
Nội dung file podvolume.yaml:
apiVersion: v1
kind: Pod
metadata:
  name: fortune
spec:
  containers:
  - name: html-generator
    image: luksa/fortune
    volumeMounts:
    - name: html
      mountPath: /var/htdocs
  - name: web-server
    image: nginx:alpine
    ports:
    - containerPort: 80
      protocol: TCP
    volumeMounts:
    - name: html
      mountPath: /usr/share/nginx/html
      readOnly: true
  volumes:
  - name: html
    emptyDir: {}
Bước 2: Deploy Pod
kubectl apply -f podvolume.yaml
kubectl get pod -o wide
Bước 3: Kiểm tra hoạt động của Volume
  1. Truy cập container html-generator:kubectl exec -it fortune -c html-generator -- /bin/sh
    • Kiểm tra dữ liệu: ls /var/htdocs touch /var/htdocs/text1.txt
    • Thoát container: exit
  2. Truy cập container web-server:kubectl exec -it fortune -c web-server -- /bin/sh
    • Kiểm tra file: ls /usr/share/nginx/html

3. PersistentVolume (PV)

PersistentVolume là không gian lưu trữ được cung cấp bởi quản trị viên, nằm ngoài Kubernetes Cluster.

Bước 1: Thiết lập NFS Server

Trên máy chủ NFS (hoặc EC2):

sudo apt update
sudo apt install nfs-kernel-server
sudo mkdir -p /data/mydata
sudo chown -R nobody:nogroup /data/mydata
sudo chmod -R 777 /data/mydata

Cập nhật file /etc/exports:

nano /etc/exports

Thêm dòng:

/data/mydata *(rw,sync,no_subtree_check,insecure)

Khởi động lại dịch vụ:

sudo systemctl restart nfs-kernel-server
Bước 2: Tạo PersistentVolume

Tạo file pv-create-nfs.yaml:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv1
spec:
  storageClassName: nfsstorageclass
  capacity:
    storage: 3Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: "/data/mydata/"
    server: "IP_CUA_NFS_SERVER"

Áp dụng file:

kubectl apply -f pv-create-nfs.yaml
kubectl get pv
kubectl describe pv pv1

4. PersistentVolumeClaim (PVC)

PersistentVolumeClaim là yêu cầu từ Pod để sử dụng PV.

Bước 1: Tạo file PVC

Tạo file pvc-nfs.yaml:

apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: pvc1
spec:
  storageClassName: nfsstorageclass
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 3Gi

Áp dụng file:

kubectl apply -f pvc-nfs.yaml
kubectl get pvc

5. Sử dụng PV và PVC trong Pod

Bước 1: Tạo file Deployment

Tạo file apache-https.yaml:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: httpd
spec:
  replicas: 1
  selector:
    matchLabels:
      app: httpd
  template:
    metadata:
      labels:
        app: httpd
    spec:
      containers:
      - name: app
        image: httpd
        resources:
          limits:
            memory: "100Mi"
            cpu: "100m"
        ports:
        - containerPort: 80
        volumeMounts:
        - mountPath: /usr/local/apache2/htdocs/
          name: htdocs
      volumes:
      - name: htdocs
        persistentVolumeClaim:
          claimName: pvc1
Bước 2: Deploy Deployment
kubectl apply -f apache-https.yaml
kubectl get pod
Bước 3: Kiểm tra

Truy cập Pod để kiểm tra dữ liệu:

curl http://<POD_IP>

6. Tạo Service để truy cập

Để truy cập Pod từ bên ngoài, bạn có thể tạo một NodePort Service (file YAML).

Nếu bạn cần thêm thông tin chi tiết, hãy cho tôi biết!


debugging:

Lỗi FailedMount với thông báo MountVolume.SetUp failed for volume "pv1" : mount failed: exit status 32 thường xảy ra khi Kubernetes không thể gắn kết (mount) PersistentVolume (PV) vào Pod. Dưới đây là các bước kiểm tra và khắc phục vấn đề này:


1. Kiểm tra nguyên nhân lỗi

a. Xác minh cấu hình PersistentVolume (PV)

Kiểm tra PV pv1:

kubectl describe pv pv1
  • Thông tin cần kiểm tra:
    • StorageClassName: Phải khớp với PVC.
    • AccessModes: Xem có phù hợp với yêu cầu của Pod hay không (ví dụ: ReadWriteMany).
    • NFS PathNFS Server: Đảm bảo đúng đường dẫn và IP của NFS server.

b. Xác minh cấu hình PersistentVolumeClaim (PVC)

Kiểm tra PVC:

kubectl describe pvc pvc1
  • Thông tin cần kiểm tra:
    • PVC phải ở trạng thái Bound.
    • PVC phải sử dụng đúng StorageClass và PV.

2. Kiểm tra kết nối với NFS Server

Nếu bạn sử dụng NFS, Kubernetes cần kết nối với NFS Server để mount volume.

a. Kiểm tra trạng thái NFS Server

Trên NFS Server, kiểm tra danh sách các thư mục được chia sẻ:

showmount -e
  • Đảm bảo thư mục /data/mydata đã được chia sẻ.

b. Kiểm tra từ Node Kubernetes

Trên Node Kubernetes nơi Pod được lên lịch (ví dụ: worker2), kiểm tra khả năng mount thủ công:

sudo apt install nfs-common   # Nếu chưa cài đặt
sudo mount -t nfs <NFS_SERVER_IP>:/data/mydata /mnt
  • Nếu lệnh này thành công, NFS Server hoạt động bình thường.
  • Nếu thất bại, kiểm tra:
    • Đúng địa chỉ IP của NFS Server (<NFS_SERVER_IP>).
    • Mở firewall trên NFS Server: sudo ufw allow from 0.0.0.0/0 to any port nfs

3. Kiểm tra quyền trên thư mục NFS

Trên NFS Server, kiểm tra quyền truy cập của thư mục /data/mydata:

ls -ld /data/mydata
  • Quyền cần thiết:
    • nobody:nogroup hoặc quyền 777 (mở rộng cho tất cả): sudo chmod 777 /data/mydata

4. Kiểm tra logs của kubelet

Xem logs của kubelet trên Node Kubernetes:

journalctl -u kubelet -f
  • Tìm kiếm các lỗi liên quan đến gắn kết (mount) NFS.

5. Kiểm tra và cập nhật cấu hình PV

Nếu cấu hình PV có vấn đề, cập nhật file YAML và áp dụng lại:

apiVersion: v1
kind: PersistentVolume
metadata:
  name: pv1
spec:
  storageClassName: nfsstorageclass
  capacity:
    storage: 3Gi
  accessModes:
    - ReadWriteMany
  nfs:
    path: "/data/mydata/"
    server: "<NFS_SERVER_IP>"  # Địa chỉ IP của NFS Server

Áp dụng lại file:

kubectl delete pv pv1
kubectl apply -f pv-create-nfs.yaml

6. Xóa Pod và tạo lại

Sau khi kiểm tra và khắc phục, xóa Pod httpd và thử tạo lại:

kubectl delete pod httpd-56d5c59999-55lsq
kubectl apply -f apache-https.yaml

7. Kiểm tra trạng thái lại

Kiểm tra trạng thái của Pod, PV và PVC:

kubectl get pods -o wide
kubectl get pv
kubectl get pvc

Nếu vẫn không giải quyết được, hãy cho tôi biết kết quả của các lệnh kiểm tra trên, đặc biệt là:

  • Log từ kubectl describe pod.
  • Log của kubectl describe pvkubectl describe pvc.
  • Kết quả kiểm tra kết nối với NFS Server.

The error indicates that Kubernetes is unable to mount the NFS volume on worker2 due to missing or misconfigured NFS utilities on the node. This is a common issue and can be resolved with the following steps:


1. Install NFS Utilities on worker2

The NFS client tools might not be installed on the worker node. SSH into the worker2 node and install the required packages:

ssh worker2
sudo apt update
sudo apt install -y nfs-common

For CentOS/RHEL systems:

sudo yum install -y nfs-utils

2. Test Manual Mounting on worker2

After installing the NFS utilities, manually test mounting the NFS share:

sudo mkdir -p /mnt/test
sudo mount -t nfs 10.7.18.209:/data/mydata /mnt/test
ls /mnt/test
  • If this works, unmount the directory: sudo umount /mnt/test
  • If it fails, check the NFS server configuration or firewall (see step 5).

3. Restart Kubelet on worker2

After ensuring the NFS utilities are installed, restart the kubelet service:

sudo systemctl restart kubelet

4. Check and Retry Pod Creation

Delete the stuck Pod and allow Kubernetes to recreate it:

kubectl delete pod httpd-56d5c59999-szg59 -n projectnodejs

Monitor the pod status as it gets recreated:

kubectl get pods -o wide -n projectnodejs

5. Verify NFS Server Configuration

On the NFS server (10.7.18.209):

  1. Ensure the directory /data/mydata is exported correctly: showmount -e
  2. Verify the Kubernetes node (worker2) has permission to mount the share:
    • Check /etc/exports: cat /etc/exports The entry should look like this: /data/mydata *(rw,sync,no_subtree_check,insecure)
    • If changes are made, restart the NFS service: sudo exportfs -r sudo systemctl restart nfs-kernel-server
  3. Check firewall settings on the NFS server:
    • Allow NFS traffic: sudo ufw allow from <worker2-IP> to any port nfs

6. Check Node Logs

If the issue persists, check the logs of the worker2 node for detailed errors:

journalctl -u kubelet -f

7. Retry and Monitor

Once the above steps are completed, retry the Pod creation and verify:

kubectl apply -f apache-https.yaml
kubectl get pods -o wide -n projectnodejs

Let me know if you encounter any issues after these steps or need further assistance!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top