您好,登錄后才能下訂單哦!
上一篇文章中kubernetes系列教程(八)Pod健康檢查機制介紹了kubernetes中Pod健康檢查機制,通過實戰介紹了kubernetes中兩種健康檢查探針:livenessProbe存活檢查,readinessProbe就緒檢查,存活檢查用于檢查應用的可用性,就緒檢查用于檢查容器是否準備接受流量,健康檢查包含三種探測的方法:exec命令行探測,tcpSocket端口檢測,httpGet請求檢測,分別適用于不同場景下的健康檢查。接下來介紹kubernetes系列教程pod的存儲管理。
kubernetes存儲管理按照發展的歷程,涉及到有Volume,PV(Persistent Volume)和PVC(PersistentVolumeClaims),和StorageClass,Volume是最早提出的存儲卷,主要解決容器和數據存儲的依賴關系,抽象底層驅動以支持不同的存儲類型;使用Volume需要了解底層存儲細節,因此提出了PV,Persistent Volume是由k8s管理員定義的存儲單元,應用端使用PersistentVolumeClaims聲明去調用PV存儲,進一步抽象了底層存儲;隨著PV數量的增加,管理員需要不停的定義PV的數量,衍生了通過StorageClass動態生成PV,StorageClass通過PVC中聲明存儲的容量,會調用底層的提供商生成PV。本文介紹Volume的使用,下篇文章介紹PV,PVC和StorageClass。
kubernetes容器中的數據是臨時的,即當重啟重啟或crash后容器的數據將會丟失,此外容器之間有共享存儲的需求,所以kubernetes中提供了volume存儲的抽象,volume后端能夠支持多種不同的plugin驅動,通過.spec.volumes中定義一個存儲,然后在容器中.spec.containers.volumeMounts調用,最終在容器內部以目錄的形式呈現。
kubernetes內置能支持多種不同的驅動類型,大體上可以分為四種類型:1. 公/私有云驅動接口,如awsElasticBlockStore實現與aws EBS集成,2. 開源存儲驅動接口,如ceph rbd,實現與ceph rb塊存儲對接,3. 本地臨時存儲,如hostPath,4. kubernetes對象API驅動接口,實現其他對象調用,如configmap,每種存儲支持不同的驅動,如下介紹:
emptyDir是一種臨時存儲,pod創建的時候會在node節點上為容器申請一個臨時的目錄,跟隨容器的生命周期,如容器刪除,emptyDir定義的臨時存儲空間也會隨之刪除,容器發生意外crash則不受影響,同時如果容器發生了遷移,其上的數據也會丟失,emptyDir一般用于測試,或者緩存場景。
[root@node-1 happylau]# cat emptydir-redis.yaml
apiVersion: v1
kind: Pod
metadata:
name: emptydir-redis
labels:
volume: emptydir
annotations:
kubernetes.io/storage: emptyDir
spec:
containers:
- name: emptydir-redis
image: redis:latest
imagePullPolicy: IfNotPresent
ports:
- name: redis-6379-port
protocol: TCP
containerPort: 6379
volumeMounts: #將定義的驅動emptydir-redis掛載到容器的/data目錄,通過名字方式關聯
- name: emptydir-redis
mountPath: /data
volumes: #定義一個存儲,驅動類型為emptyDir,大小1G
- name: emptydir-redis
emptyDir:
sizeLimit: 1Gi
[root@node-1 happylau]# kubectl apply -f emptydir-redis.yaml
pod/emptydir-redis created
執行kubectl describe pods emptydir-redis查看容器的存儲掛載信息
Containers:
emptydir-redis:
Container ID: docker://dddd9f3d0e395d784c08b712631d2b0c259bfdb30b0c655a0fc8021492f1ecf9
Image: redis:latest
Image ID: docker-pullable://redis@sha256:cb379e1a076fcd3d3f09e10d7b47ca631fb98fb33149ab559fa02c1b11436345
Port: 6379/TCP
Host Port: 0/TCP
State: Running
Started: Tue, 01 Oct 2019 11:04:30 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts: #掛載信息,將emptydir-redis掛載到/data目錄,且是rw讀寫狀態
/data from emptydir-redis (rw)
/var/run/secrets/kubernetes.io/serviceaccount from default-token-5qwmc (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes: #定義了一個EmptyDir類型的存儲,大小為1Gi
emptydir-redis:
Type: EmptyDir (a temporary directory that shares a pod's lifetime)
Medium:
SizeLimit: 1Gi
default-token-5qwmc:
Type: Secret (a volume populated by a Secret)
SecretName: default-token-5qwmc
Optional: false
獲取pod的ip地址
[root@node-1 happylau]# kubectl get pods emptydir-redis -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
emptydir-redis 1/1 Running 1 17m 10.244.1.27 node-2 <none> <none>
安裝客戶端redis-cli
[root@node-1 ~]# yum install redis
向redis中寫入兩個key
10.244.1.27:6379> set volume emptydir
OK
10.244.1.27:6379> set username happylauliu
OK
10.244.1.27:6379> get volume
"emptydir"
10.244.1.27:6379> get username
"happylauliu"
登陸容器
[root@node-1 ~]# kubectl exec -it emptydir-redis /bin/bash
安裝軟件
root@emptydir-redis:/data# apt-get update ; apt-get install procps
可以通過top查看進程,進程號一般為1
root@emptydir-redis:/data# kill 1
[root@node-1 ~]# redis-cli -h 10.244.1.27
10.244.1.27:6379> get volume
"emptydir"
10.244.1.27:6379> get username
"happylauliu"
[root@node-2 ~]# docker container list |grep redis
e0e9a6b0ed77 01a52b3b5cd1 "docker-entrypoint.s…" 20 minutes ago Up 20 minutes k8s_emptydir-redis_emptydir-redis_default_4baadb25-1e62-4cf5-9724-821d04dcdd44_2
dfef32905fe5 k8s.gcr.io/pause:3.1 "/pause" 45 minutes ago Up 45 minutes k8s_POD_emptydir-redis_default_4baadb25-1e62-4cf5-9724-821d04dcdd44_0
docker container inspect e0e9a6b0ed77查看存儲內容如下圖:
查看目錄的信息:
[root@node-2 ~]# ls -l /var/lib/kubelet/pods/4baadb25-1e62-4cf5-9724-821d04dcdd44/volumes/kubernetes.io~empty-dir/emptydir-redis
總用量 4
-rw-r--r-- 1 polkitd input 156 10月 8 14:55 dump.rdb
[root@node-1 ~]# kubectl delete pods emptydir-redis
pod "emptydir-redis" deleted
[root@node-1 ~]# ssh node-2
Last login: Tue Oct 8 15:15:41 2019 from 10.254.100.101
[root@node-2 ~]# ls -l /var/lib/kubelet/pods/4baadb25-1e62-4cf5-9724-821d04dcdd44/volumes/kubernetes.io~empty-dir/emptydir-redis
ls: 無法訪問/var/lib/kubelet/pods/4baadb25-1e62-4cf5-9724-821d04dcdd44/volumes/kubernetes.io~empty-dir/emptydir-redis: 沒有那個文件或目錄
小結:emptyDir是host上定義的一塊臨時存儲,通過bind mount的形式掛載到容器中使用,容器重啟數據會保留,容器刪除則volume會隨之刪除。
與emptyDir類似,hostpath支持將node節點的目錄或文件掛載到容器中使用,用于單機測試場景,此外適用于一些容器業務需要訪問宿主機目錄,如監控系統訪問/proc和/sys目錄,日志系統訪問/var/lib/docker目錄的一些場景。支持設置不同的type類型
[root@node-1 happylau]# cat hostpath-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: hostpath-demo
labels:
storage: hostpath
annotations:
kubernetes.io/storage: hostpath
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- name: nginx-http-port
protocol: TCP
containerPort: 80
volumeMounts: #掛載到nginx的web站點目錄下
- name: hostpath-demo
mountPath: /usr/share/nginx/html
volumes: #定一個hostPath本地的存儲
- name: hostpath-demo
hostPath:
type: DirectoryOrCreate
path: /mnt/data
[root@node-1 happylau]# kubectl apply -f hostpath-demo.yaml
pod/hostpath-demo created
獲取pod所在的node節點
[root@node-1 happylau]# kubectl get pods hostpath-demo -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hostpath-demo 1/1 Running 0 31s 10.244.2.24 node-3 <none> <none>
生成web站點的數據
[root@node-1 happylau]# ssh node-3
Last login: Tue Oct 8 22:49:14 2019 from 10.254.100.101
[root@node-3 ~]# echo "hostPath test page" >/mnt/data/index.html
[root@node-3 ~]# curl http://10.244.2.24
hostPath test page
#docker層面kill掉進程
[root@node-3 ~]# docker container list |grep hostpath
39a7e21afebb f949e7d76d63 "nginx -g 'daemon of…" 11 minutes ago Up 11 minutes k8s_nginx_hostpath-demo_default_6da41e3d-8585-4997-bf90-255ca0948030_0
490f50108e41 k8s.gcr.io/pause:3.1 "/pause" 11 minutes ago Up 11 minutes k8s_POD_hostpath-demo_default_6da41e3d-8585-4997-bf90-255ca0948030_0
[root@node-3 ~]# docker container kill 39a7e21afebb
39a7e21afebb
[root@node-3 ~]# exit
登出
#獲取pod的地址,通過RESTART可知,容器重啟過一次,測試數據依舊保留
[root@node-1 happylau]# kubectl get pods -o wide hostpath-demo
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
hostpath-demo 1/1 Running 1 12m 10.244.2.24 node-3 <none> <none>
[root@node-1 happylau]# curl http://10.244.2.24
hostPath test page
小結:hostPath與emptyDir類似提供臨時的存儲,hostPath適用于一些容器需要訪問宿主機目錄或文件的場景,對于數據持久化而言都不是很好的實現方案。
NFS是實現Network File System網絡文件共享的NAS存儲,kubernetes與NFS對接實現存儲的共享,當容器刪除不影響存儲且可以實現跨機存儲共享,本文以搭建一個NFS存儲實現kubernetes對接。
安裝nfs服務
[root@node-1 ~]# yum install nfs-utils -y
配置nfs共享,提前創建好目錄
[root@node-1 ~]# cat /etc/exports
/mnt/data 10.254.100.0/24(rw)
重啟并驗證
[root@node-1 ~]# systemctl restart nfs
[root@node-1 ~]# showmount -e node-1
Export list for node-1:
/mnt/data 10.254.100.0/24
[root@node-1 happylau]# cat nfs-demo.yaml
apiVersion: v1
kind: Pod
metadata:
name: nfs-demo
labels:
storage: nfs
annotations:
kubernetes.io/storage: nfs
spec:
containers:
- name: nginx
image: nginx:latest
imagePullPolicy: IfNotPresent
ports:
- name: nginx-http-port
protocol: TCP
containerPort: 80
volumeMounts: #掛載到nfs的目錄下
- name: nfs-demo
mountPath: /usr/share/nginx/html
volumes: #定義一個nfs驅動的存儲
- name: nfs-demo
nfs:
server: 10.254.100.101
path: /mnt/data
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 40s default-scheduler Successfully assigned default/nfs-demo to node-2
Warning FailedMount 39s kubelet, node-2 MountVolume.SetUp failed for volume "nfs-demo" : mount failed: exit status 32
Mounting command: systemd-run
Mounting arguments: --description=Kubernetes transient mount for /var/lib/kubelet/pods/78bf6a81-082d-4d6c-a163-75241bf21cde/volumes/kubernetes.io~nfs/nfs-demo --scope -- mount -t nfs 10.254.100.101:/mnt/data /var/lib/kubelet/pods/78bf6a81-082d-4d6c-a163-75241bf21cde/volumes/kubernetes.io~nfs/nfs-demo
Output: Running scope as unit run-29843.scope.
mount: wrong fs type, bad option, bad superblock on 10.254.100.101:/mnt/data,
missing codepage or helper program, or other error
(for several filesystems (e.g. nfs, cifs) you might
need a /sbin/mount.<type> helper program)
[root@node-1 happylau]# ssh node-2
Last login: Tue Oct 8 15:22:04 2019 from 10.254.100.101
[root@node-2 ~]# yum install nfs-utils -y
[root@node-1 happylau]# kubectl get pods nfs-demo -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nfs-demo 1/1 Running 0 4m41s 10.244.1.28 node-2 <none> <none>
[root@node-1 happylau]# echo "nfs test age" >/mnt/data/index.html
[root@node-1 happylau]# curl http://10.244.1.28
nfs test age
[root@node-1 happylau]# kubectl delete pods nfs-demo
pod "nfs-demo" deleted
[root@node-1 happylau]# mount.nfs node-1:/mnt/data/ /media/
[root@node-1 happylau]# ls -l /media/
總用量 4
-rw-r--r-- 1 root root 13 10月 8 23:26 index.html
TKE支持在創建Workload時如Deployments,DaemonSets,StatefulSets等指定存儲卷,支持臨時目錄emptyDir,主機路徑hostPath,nfs盤,pvc,云硬盤,configmap,secrets,此處以騰訊云CFS為例(提前在CFS中創建好存儲,確保CFS和容器宿主機在同一個VPC網絡內)。
apiVersion: apps/v1beta2
kind: Deployment
metadata:
annotations:
deployment.kubernetes.io/revision: "1"
description: demo
creationTimestamp: "2019-10-08T15:45:18Z"
generation: 1
labels:
k8s-app: the-volume-demo
qcloud-app: the-volume-demo
name: the-volume-demo
namespace: default
resourceVersion: "618380753"
selfLink: /apis/apps/v1beta2/namespaces/default/deployments/the-volume-demo
uid: a0fc4600-e9e2-11e9-b3f4-decf0ef369cf
spec:
minReadySeconds: 10
progressDeadlineSeconds: 600
replicas: 1
revisionHistoryLimit: 10
selector:
matchLabels:
k8s-app: the-volume-demo
qcloud-app: the-volume-demo
strategy:
rollingUpdate:
maxSurge: 1
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
k8s-app: the-volume-demo
qcloud-app: the-volume-demo
spec:
containers:
- image: nginx:latest
imagePullPolicy: Always
name: nginx-demo
resources:
limits:
cpu: 500m
memory: 1Gi
requests:
cpu: 250m
memory: 256Mi
securityContext:
privileged: false
terminationMessagePath: /dev/termination-log
terminationMessagePolicy: File
volumeMounts: #掛載到pod中
- mountPath: /usr/share/nginx/html
name: volume-nfs-demo
dnsPolicy: ClusterFirst
imagePullSecrets:
- name: qcloudregistrykey
- name: tencenthubkey
restartPolicy: Always
schedulerName: default-scheduler
securityContext: {}
terminationGracePeriodSeconds: 30
volumes: #CFS存儲
- name: volume-nfs-demo
nfs:
path: /
server: 10.66.200.7
本文介紹了kubernetes存儲中最基本volume的使用,介紹了volume支持多種不同驅動,以實際案例介紹emptyDir,hostPath,nfs驅動的對接,并介紹了TKE下volume功能的使用。由于volume需要知道底層存儲的細節,不便于廣泛使用,后來衍生為PV,管理員定義PV實現和底層存儲對接,用戶通過PVC使用PV,下節我們將介紹PV/PVC和StorageClass的使用。
volume管理:https://kubernetes.io/docs/concepts/storage/volumes/
pod中使用volume:https://kubernetes.io/docs/tasks/configure-pod-container/configure-volume-storage/
當你的才華撐不起你的野心時,你就應該靜下心來學習
返回kubernetes系列教程目錄
**如果覺得文章對您有幫助,請訂閱專欄,分享給有需要的朋友吧
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。