k8s pod使用iscsi块存储
一、iscsi设备建立块存储
本例子iscsi设备为tgtd+lvm
lvcreate -V 1G -T volgrp/lvpool -n node4 #添加块设备
[root@target ~]# lvs
node4 volgrp Vwi-aotz-- 1.00g lvpool 4.79
配置/etc/tgt/targets.conf添加以下配置
<target iqn.2001-04.com.example:storage.kube.sys1.xyz>
backing-store /dev/volgrp/node4
initiator-address 192.168.3.71
initiator-address 192.168.3.74
initiator-address 192.168.3.75
</target>
重启服务
systemctl restart tgtd.service
二、k8s pod挂载
编辑yaml文件iscsipod.yaml
---
apiVersion: v1
kind: Pod
metadata:
name: iscsipd
spec:
containers:
- name: iscsipd-rw
image: httpd
volumeMounts:
- mountPath: "/mnt"
name: iscsipd-rw
volumes:
- name: iscsipd-rw
iscsi:
targetPortal: 192.168.3.4:3260
portals: ['192.168.3.4:3260']
iqn: iqn.2001-04.com.example:storage.kube.sys1.xyz
lun: 1
fsType: ext4
readOnly: true
执行:
kubectl apply -f iscsipod.yaml
查看pod的信息:
kubectl describe pod iscsipd
Name: iscsipd
Namespace: default
Priority: 0
Node: k8s-worker02/192.168.3.75
Start Time: Tue, 23 Feb 2021 16:53:19 +0800
Labels: <none>
Annotations: cni.projectcalico.org/podIP: 10.255.2.9/32
Status: Running
IP: 10.255.2.9
IPs:
IP: 10.255.2.9
Containers:
iscsipd-rw:
Container ID: docker://bc10d08edcca5a04ba423dfe9b3932dad73da2174641d78d95a8b2abe778490e
Image: httpd
Image ID: docker-pullable://httpd@sha256:2fab99fb3b1c7ddfa99d7dc55de8dad0a62dbe3e7c605d78ecbdf2c6c49fd636
Port: <none>
Host Port: <none>
State: Running
Started: Tue, 23 Feb 2021 16:53:50 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/mnt from iscsipd-rw (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-88ss9 (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
iscsipd-rw:
Type: ISCSI (an ISCSI Disk resource that is attached to a kubelet's host machine and then exposed to the pod)
TargetPortal: 192.168.3.4:3260
IQN: iqn.2001-04.com.example:storage.kube.sys1.xyz
Lun: 1
ISCSIInterface default
FSType: ext4
ReadOnly: true
Portals: [192.168.3.4:3260]
DiscoveryCHAPAuth: false
SessionCHAPAuth: false
SecretRef: nil
InitiatorName: <none>
default-token-88ss9:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-88ss9
Optional: false
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s
node.kubernetes.io/unreachable:NoExecute for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 8m5s default-scheduler Successfully assigned default/iscsipd to k8s-worker02
Normal SuccessfulAttachVolume 8m5s attachdetach-controller AttachVolume.Attach succeeded for volume "iscsipd-rw"
Normal Pulling 7m36s kubelet, k8s-worker02 Pulling image "httpd"
Normal Pulled 7m35s kubelet, k8s-worker02 Successfully pulled image "httpd"
Normal Created 7m34s kubelet, k8s-worker02 Created container iscsipd-rw
Normal Started 7m34s kubelet, k8s-worker02 Started container iscsipd-rw
kubectl get pod -o wide
iscsipd 1/1 Running 0 9m31s 10.255.2.9 k8s-worker02 <none> <none>
在k8s-worker02查看挂载情况
[root@k8s-worker02 ~]# lsscsi
[2:0:0:0] disk ATA QEMU HARDDISK 3 /dev/sda
[8:0:0:0] storage IET Controller 0001 -
[8:0:0:1] disk IET VIRTUAL-DISK 0001 /dev/sdb
# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
├─sda1 8:1 0 1G 0 part /boot
└─sda2 8:2 0 153.9G 0 part
├─centos-root 253:0 0 150G 0 lvm /
└─centos-swap 253:1 0 3.9G 0 lvm
sdb 8:16 0 1G 0 disk /var/lib/kubelet/pods/c1fee497-392f-4a9e-b9ab-cc49d3cb2538/volumes/kubernetes.io~iscsi/iscsipd-rw
df -hT | grep iscsi
/dev/sdb ext4 976M 2.6M 958M 1% /var/lib/kubelet/pods/c1fee497-392f-4a9e-b9ab-cc49d3cb2538/volumes/kubernetes.io~iscsi/iscsipd-rw
进入pod
# kubectl exec -it iscsipd bash
kubectl exec [POD] [COMMAND] is DEPRECATED and will be removed in a future version. Use kubectl kubectl exec [POD] -- [COMMAND] instead.
root@iscsipd:/usr/local/apache2#
root@iscsipd:/usr/local/apache2#
root@iscsipd:/usr/local/apache2# ls
bin build cgi-bin conf error htdocs icons include logs modules
root@iscsipd:/usr/local/apache2# ls /
bin boot dev etc home lib lib64 media mnt opt proc root run sbin srv sys tmp usr var
root@iscsipd:/usr/local/apache2# lsblk
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
sda 8:0 0 200G 0 disk
|-sda1 8:1 0 1G 0 part
`-sda2 8:2 0 153.9G 0 part
|-centos-root 253:0 0 150G 0 lvm /etc/hosts
`-centos-swap 253:1 0 3.9G 0 lvm
sdb 8:16 0 1G 0 disk /mnt
root@iscsipd:/usr/local/apache2# cd /mnt
root@iscsipd:/mnt# echo "wangjinxiong" >> wangjinxiong.txt
root@iscsipd:/mnt# cat wangjinxiong.txt
wangjinxiong