创建configmap
1.创建 configmap
student@ubuntu:~/volumes$echo 1 > one
student@ubuntu:~/volumes$echo 2 > two
student@ubuntu:~/volumes$kubectl create configmap cm-config --from-literal=text=number --from-file=./one --from-file=./two
2.查看configmap
student@ubuntu:~/volumes$kubectl get configmaps
NAME DATA AGE
cm-config 3 65s
student@ubuntu:~/volumes$kubectl get configmaps cm-config -o yaml
apiVersion: v1
data:
one: |
1
text: number
two: |
2
kind: ConfigMap
metadata:
creationTimestamp: 2018-12-05T02:11:50Z
name: cm-config
namespace: default
resourceVersion: "759323"
selfLink: /api/v1/namespaces/default/configmaps/cm-config
uid: 2087d2b4-f833-11e8-9072-52540066b534
3.env使用configmap
student@ubuntu:~/volumes$cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: nginx
image: nginx
env:
- name: TEXT
valueFrom:
configMapKeyRef:
name: cm-config
key: text
student@ubuntu:~/volumes$kubectl exec test env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=test
TEXT=number
4.envFrom 使用configmap
student@ubuntu:~/volumes$cat pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: nginx
image: nginx
envFrom:
- configMapRef:
name: cm-config
student@ubuntu:~/volumes$kubectl exec test env
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
HOSTNAME=test
one=1
text=number
two=2
5.volume使用configmap
apiVersion: v1
kind: Pod
metadata:
name: test
spec:
containers:
- name: nginx
image: nginx
volumeMounts:
- name: cm-config
mountPath: /etc/cm/
volumes:
- name: cm-config
configMap:
name: cm-config
student@ubuntu:~/volumes$kubectl exec test ls /etc/cm/
one
text
two
使用PV(NFS)
1.安装nfs
服务端
sudo apt-get install -y nfs-kernel-server
sudo mkdir /opt/sfw
sudo chmod 777 /opt/sfw
student@ubuntu:~/volumes$sudo cat /etc/exports
/opt/sfw/ *(rw,sync,no_root_squash,subtree_check)
sudo exportfs -av
客户节点
sudo apt-get install -y nfs-commom
- 创建pv
apiVersion: v1
kind: PersistentVolume
metadata:
name: pvvol-1
spec:
capacity:
storage: 1Gi
accessModes:
- ReadWriteMany
persistentVolumeReclaimPolicy: ReTain
nfs:
path: /opt/sfw
server: 172.30.81.194
readOnly: false
student@ubuntu:~/volumes$kubectl create -f pv.yaml
persistentvolume/pvvol-1 created
student@ubuntu:~/volumes$kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvvol-1 1Gi RWX Retain Available 3s
3.创建pvc
student@ubuntu:~/volumes$cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-one
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 100Mi
student@ubuntu:~/volumes$kubectl create -f pvc.yaml
persistentvolumeclaim/pvc-one created
student@ubuntu:~/volumes$kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pvc-one Bound pvvol-1 1Gi RWX 21s
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvvol-1 1Gi RWX Retain Bound default/pvc-one 104s
4.使用pvc
student@ubuntu:~/volumes$cat nginx-one.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-one
labels:
system: secondary
spec:
replicas: 2
template:
metadata:
labels:
app: nginx
spec:
containers:
- image: nginx
imagePullPolicy: Always
name: nginx
ports:
- containerPort: 80
protocol: TCP
volumeMounts:
- name: pvc
mountPath: /volumes/
volumes:
- name: pvc
persistentVolumeClaim:
claimName: pvc-one
student@ubuntu:~/volumes$kubectl exec -it nginx-one-7bc6c4dc6f-n5jgh bash
root@nginx-one-7bc6c4dc6f-n5jgh:/# df -ah|grep volumes
172.30.81.194:/opt/sfw 38G 8.2G 28G 23% /volumes
使用resourcequota限制pvc
1.创建storagequota
student@ubuntu:~/volumes$cat storage-quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: storage-quota
namespace: default
spec:
hard:
persistentvolumeclaims: "2"
requests.storage: "100Mi"
2.查看resourcequota
student@ubuntu:~/volumes$kubectl describe ns default
Name: default
Labels: <none>
Annotations: <none>
Status: Active
Resource Quotas
Name: storage-quota
Resource Used Hard
-------- --- ---
persistentvolumeclaims 0 2
requests.storage 0 100Mi
student@ubuntu:~/volumes$kubectl get resourcequotas
NAME CREATED AT
storage-quota 2018-12-04T07:16:13Z
3.创建pv,pvc查看resourcequota的变化
student@ubuntu:~/volumes$kubectl create -f pv.yaml
persistentvolume/pvvol-1 created
student@ubuntu:~/volumes$kubectl create -f pvc.yaml
persistentvolumeclaim/pvc-one created
student@ubuntu:~/volumes$kubectl describe ns default
Name: default
Labels: <none>
Annotations: <none>
Status: Active
Resource Quotas
Name: storage-quota
Resource Used Hard
-------- --- ---
persistentvolumeclaims 1 2
requests.storage 100Mi 100Mi
4.deployment里面使用,查看resourcequota的变化
student@ubuntu:~/volumes$kubectl exec -it nginx-one-7bc6c4dc6f-gxglq bash
root@nginx-one-7bc6c4dc6f-gxglq:/# cd /volumes/
root@nginx-one-7bc6c4dc6f-gxglq:/volumes# dd if=/dev/zero of=test bs=1M count=150
150+0 records in
150+0 records out
157286400 bytes (157 MB, 150 MiB) copied, 5.2506 s, 30.0 MB/s
root@nginx-one-7bc6c4dc6f-gxglq:/volumes#
root@nginx-one-7bc6c4dc6f-gxglq:/volumes# exit
exit
student@ubuntu:~/volumes$kubectl describe ns default
Name: default
Labels: <none>
Annotations: <none>
Status: Active
Resource Quotas
Name: storage-quota
Resource Used Hard
-------- --- ---
persistentvolumeclaims 1 2
requests.storage 100Mi 100Mi
5.创建pvc大于resourcequota限制的容量
student@ubuntu:~/volumes$cat storage-quota.yaml
apiVersion: v1
kind: ResourceQuota
metadata:
name: storage-quota
namespace: default
spec:
hard:
persistentvolumeclaims: "2"
requests.storage: "100Mi"
student@ubuntu:~/volumes$cat pvc.yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
name: pvc-one
spec:
accessModes:
- ReadWriteMany
resources:
requests:
storage: 150Mi
student@ubuntu:~/volumes$kubectl create -f pvc.yaml
Error from server (Forbidden): error when creating "pvc.yaml": persistentvolumeclaims "pvc-one" is forbidden: exceeded quota: storage-quota, requested: requests.storage=150Mi, used: requests.storage=0, limited: requests.storage=100Mi
persistentVolumeReclaimPolicy
1.Retain 数据保留
student@ubuntu:~/volumes$kubectl create -f pv.yaml
persistentvolume/pvvol-1 created
student@ubuntu:~/volumes$kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvvol-1 1Gi RWX Retain Available
student@ubuntu:~/volumes$kubectl create -f pvc.yaml
persistentvolumeclaim/pvc-one created
student@ubuntu:~/volumes$kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvvol-1 1Gi RWX Retain Bound default/pvc-one 31s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pvc-one Bound pvvol-1 1Gi RWX 4s
student@ubuntu:~/volumes$kubectl delete -f pvc.yaml
persistentvolumeclaim "pvc-one" deleted
student@ubuntu:~/volumes$kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvvol-1 1Gi RWX Retain Released default/pvc-one 68s
student@ubuntu:~/volumes$kubectl create -f pvc.yaml
persistentvolumeclaim/pvc-one created
student@ubuntu:~/volumes$kubectl get pvc,pv
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pvc-one Pending 5s
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvvol-1 1Gi RWX Retain Released default/pvc-one 88s
2.Delete (volume插件获取失败,无法删除数据)
student@ubuntu:~/volumes$kubectl create -f pv.yaml
persistentvolume/pvvol-1 created
student@ubuntu:~/volumes$kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvvol-1 1Gi RWX Delete Available 5s
student@ubuntu:~/volumes$kubectl create -f pvc.yaml
persistentvolumeclaim/pvc-one created
student@ubuntu:~/volumes$kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvvol-1 1Gi RWX Delete Bound default/pvc-one 22s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pvc-one Bound pvvol-1 1Gi RWX
student@ubuntu:~/volumes$kubectl delete -f pvc.yaml
persistentvolumeclaim "pvc-one" deleted
student@ubuntu:~/volumes$kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvvol-1 1Gi RWX Delete Failed default/pvc-one 54s
student@ubuntu:~/volumes$kubectl create -f pvc.yaml
persistentvolumeclaim/pvc-one created
student@ubuntu:~/volumes$kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvvol-1 1Gi RWX Delete Failed default/pvc-one 83s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pvc-one Pending 5s
3.Recycle 数据会被删除
student@ubuntu:~/volumes$kubectl create -f pv.yaml
persistentvolume/pvvol-1 created
student@ubuntu:~/volumes$kubectl get pv
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
pvvol-1 1Gi RWX Recycle Available 3s
student@ubuntu:~/volumes$
student@ubuntu:~/volumes$kubectl create -f pvc.yaml
persistentvolumeclaim/pvc-one created
student@ubuntu:~/volumes$kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvvol-1 1Gi RWX Recycle Bound default/pvc-one 26s
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
persistentvolumeclaim/pvc-one Bound pvvol-1 1Gi RWX 4s
student@ubuntu:~/volumes$kubectl delete -f pvc.yaml
student@ubuntu:~/volumes$kubectl get pv,pvc
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
persistentvolume/pvvol-1 1Gi RWX Recycle Available 68s
网友评论