上一篇文章中kubernetes系列教程(八)Pod健康檢查機(jī)制介紹了kubernetes中Pod健康檢查機(jī)制,通過實(shí)戰(zhàn)介紹了kubernetes中兩種健康檢查探針:livenessProbe存活檢查,readinessProbe就緒檢查,存活檢查用于檢查應(yīng)用的可用性,就緒檢查用于檢查容器是否準(zhǔn)備接受流量,健康檢查包含三種探測的方法:exec命令行探測,tcpSocket端口檢測,httpGet請求檢測,分別適用于不同場景下的健康檢查。接下來介紹kubernetes系列教程pod的存儲管理。
江城網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)公司!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、成都響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)公司成立于2013年到現(xiàn)在10年的時間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)公司。
kubernetes存儲管理按照發(fā)展的歷程,涉及到有Volume,PV(Persistent Volume)和PVC(PersistentVolumeClaims),和StorageClass,Volume是最早提出的存儲卷,主要解決容器和數(shù)據(jù)存儲的依賴關(guān)系,抽象底層驅(qū)動以支持不同的存儲類型;使用Volume需要了解底層存儲細(xì)節(jié),因此提出了PV,Persistent Volume是由k8s管理員定義的存儲單元,應(yīng)用端使用PersistentVolumeClaims聲明去調(diào)用PV存儲,進(jìn)一步抽象了底層存儲;隨著PV數(shù)量的增加,管理員需要不停的定義PV的數(shù)量,衍生了通過StorageClass動態(tài)生成PV,StorageClass通過PVC中聲明存儲的容量,會調(diào)用底層的提供商生成PV。本文介紹Volume的使用,下篇文章介紹PV,PVC和StorageClass。
kubernetes容器中的數(shù)據(jù)是臨時的,即當(dāng)重啟重啟或crash后容器的數(shù)據(jù)將會丟失,此外容器之間有共享存儲的需求,所以kubernetes中提供了volume存儲的抽象,volume后端能夠支持多種不同的plugin驅(qū)動,通過.spec.volumes中定義一個存儲,然后在容器中.spec.containers.volumeMounts調(diào)用,最終在容器內(nèi)部以目錄的形式呈現(xiàn)。
kubernetes內(nèi)置能支持多種不同的驅(qū)動類型,大體上可以分為四種類型:1. 公/私有云驅(qū)動接口,如awsElasticBlockStore實(shí)現(xiàn)與aws EBS集成,2. 開源存儲驅(qū)動接口,如ceph rbd,實(shí)現(xiàn)與ceph rb塊存儲對接,3. 本地臨時存儲,如hostPath,4. kubernetes對象API驅(qū)動接口,實(shí)現(xiàn)其他對象調(diào)用,如configmap,每種存儲支持不同的驅(qū)動,如下介紹:
emptyDir是一種臨時存儲,pod創(chuàng)建的時候會在node節(jié)點(diǎn)上為容器申請一個臨時的目錄,跟隨容器的生命周期,如容器刪除,emptyDir定義的臨時存儲空間也會隨之刪除,容器發(fā)生意外crash則不受影響,同時如果容器發(fā)生了遷移,其上的數(shù)據(jù)也會丟失,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: #將定義的驅(qū)動emptydir-redis掛載到容器的/data目錄,通過名字方式關(guān)聯(lián)
- name: emptydir-redis
mountPath: /data
volumes: #定義一個存儲,驅(qū)動類型為emptyDir,大小1G
- name: emptydir-redis
emptyDir:
sizeLimit: 1Gi
[root@node-1 happylau]# kubectl apply -f emptydir-redis.yaml
pod/emptydir-redis created
執(zhí)行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:
Mounts: #掛載信息,將emptydir-redis掛載到/data目錄,且是rw讀寫狀態(tài)
/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
安裝客戶端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查看進(jìn)程,進(jìn)程號一般為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查看存儲內(nèi)容如下圖:
查看目錄的信息:
[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: 沒有那個文件或目錄
小結(jié):emptyDir是host上定義的一塊臨時存儲,通過bind mount的形式掛載到容器中使用,容器重啟數(shù)據(jù)會保留,容器刪除則volume會隨之刪除。
與emptyDir類似,hostpath支持將node節(jié)點(diǎn)的目錄或文件掛載到容器中使用,用于單機(jī)測試場景,此外適用于一些容器業(yè)務(wù)需要訪問宿主機(jī)目錄,如監(jiān)控系統(tǒng)訪問/proc和/sys目錄,日志系統(tǒng)訪問/var/lib/docker目錄的一些場景。支持設(shè)置不同的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站點(diǎn)目錄下
- 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節(jié)點(diǎn)
[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
生成web站點(diǎn)的數(shù)據(jù)
[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掉進(jìn)程
[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可知,容器重啟過一次,測試數(shù)據(jù)依舊保留
[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
[root@node-1 happylau]# curl http://10.244.2.24
hostPath test page
小結(jié):hostPath與emptyDir類似提供臨時的存儲,hostPath適用于一些容器需要訪問宿主機(jī)目錄或文件的場景,對于數(shù)據(jù)持久化而言都不是很好的實(shí)現(xiàn)方案。
NFS是實(shí)現(xiàn)Network File System網(wǎng)絡(luò)文件共享的NAS存儲,kubernetes與NFS對接實(shí)現(xiàn)存儲的共享,當(dāng)容器刪除不影響存儲且可以實(shí)現(xiàn)跨機(jī)存儲共享,本文以搭建一個NFS存儲實(shí)現(xiàn)kubernetes對接。
安裝nfs服務(wù)
[root@node-1 ~]# yum install nfs-utils -y
配置nfs共享,提前創(chuàng)建好目錄
[root@node-1 ~]# cat /etc/exports
/mnt/data 10.254.100.0/24(rw)
重啟并驗(yàn)證
[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驅(qū)動的存儲
- 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. 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
[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支持在創(chuàng)建Workload時如Deployments,DaemonSets,StatefulSets等指定存儲卷,支持臨時目錄emptyDir,主機(jī)路徑hostPath,nfs盤,pvc,云硬盤,configmap,secrets,此處以騰訊云CFS為例(提前在CFS中創(chuàng)建好存儲,確保CFS和容器宿主機(jī)在同一個VPC網(wǎng)絡(luò)內(nèi))。
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支持多種不同驅(qū)動,以實(shí)際案例介紹emptyDir,hostPath,nfs驅(qū)動的對接,并介紹了TKE下volume功能的使用。由于volume需要知道底層存儲的細(xì)節(jié),不便于廣泛使用,后來衍生為PV,管理員定義PV實(shí)現(xiàn)和底層存儲對接,用戶通過PVC使用PV,下節(jié)我們將介紹PV/PVC和StorageClass的使用。
volume管理:https://kubernetes.io/docs/concepts/storage/volumes/
pod中使用volume:https://kubernetes.io/docs/tasks/configure-pod-container/configure-volume-storage/
當(dāng)你的才華撐不起你的野心時,你就應(yīng)該靜下心來學(xué)習(xí)
返回kubernetes系列教程目錄
**如果覺得文章對您有幫助,請訂閱專欄,分享給有需要的朋友吧