kubernetes 存儲卷
數據卷用於實現容器持久化數據,Kubernetes對於數據卷從新定義,提供了豐富強大的功能。
在Kubernetes系統中,當Pod重建的時候,數據卷會丟失,Kubernetes也是經過數據捲來提供Pod數據的持久化的。Kubernetes數據卷是對Docker數據庫的擴展,Kubernetes數據卷是Pod級別,能夠用來實現Pod中容器的文件共享。
經過命令查看支持的存儲類型:kubectl explain pods.spec.volumes
本地數據卷有2種:html
網絡存儲:node
一個pod裏面2個容器,掛載同一個目錄。
注意:emptyDir的生命週期同pod週期,簡單來講,pod刪除了,emptyDir也隨之刪除。nginx
apiVersion: v1 kind: Pod metadata: name: pod-demo namespace: default labels: app: myapp tier: frontend spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 volumeMounts: #容器掛載 - name: html mountPath: /data/web/html/ - name: busybox image: busybox:latest command: ["/bin/sh" ,"-c","sleep 3600"] volumeMounts: #容器掛載 - name: html mountPath: /data/ volumes: #定義存儲 - name: html emptyDir: {}
進入busybox測試web
kubectl exec -it pod-demo -c busybox -- /bin/sh / # echo $(date) >>/data/index.html / # cat /data/index.html Sun Mar 17 08:20:03 UTC 2019 進入myapp查看數據共享 kubectl exec -it pod-demo -c myapp -- /bin/sh / # cat /data/web/html/index.html Sun Mar 17 08:20:03 UTC 2019
這種會把宿主機上的指定卷加載到容器之中,固然,若是 Pod 發生跨主機的重建,其內容就難保證了。數據庫
這種卷通常和DaemonSet搭配使用,用來操做主機文件,例如進行日誌採集的 FLK 中的 FluentD 就採用這種方式,加載主機的容器日誌目錄,達到收集本主機全部日誌的目的。vim
apiVersion: v1 kind: Pod metadata: name: nginx-volume namespace: default spec: containers: - name: nginx image: nginx ports: - containerPort: 80 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ volumes: - name: html hostPath: path: /data/nginx/v1/ #目錄會在節點上自動建立 type: DirectoryOrCreate
咱們在節點的/data/nginx/html/建立一個文件centos
[root@node01 html]# echo "hello nginx!!" >>/data/nginx/html/index.html [root@master volumes]# curl 172.17.8.7 hello nginxll
NFS(Network File System)即網絡文件系統,是FreeBSD支持的文件系統中的一種,它容許網絡中的計算機之間經過TCP/IP網絡共享資源。在NFS的應用中,本地NFS的客戶端應用能夠透明地讀寫位於遠端NFS服務器上的文件,就像訪問本地文件同樣。api
首先要準備好nfs,我在192.168.247.144的機器上安裝nfs,處處的目錄爲/nfs/data
bash
yum install nfs-utils -y vim /etc/exports /data/volumes 192.168.247.0/16(rw,no_root_squash) systemctl start nfs
nfs準備好了,注意,在pod的節點機器要必定肯定能夠掛載nfs類型的,不然建立pod會出錯。執行yum install nfs-utils -y 就能夠掛載了
nfs pod 的yaml文件:服務器
apiVersion: v1 kind: Pod metadata: name: nginx-volume-nfs namespace: default spec: containers: - name: nginx-nfs image: nginx ports: - containerPort: 80 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ volumes: - name: html nfs: path: /data/volumes server: 192.168.247.144
nfs deployment的yaml文件,即多個副本同個存儲。
apiVersion: extensions/v1beta1 kind: Deployment metadata: name: nginx-deployment spec: replicas: 2 template: metadata: labels: app: web_server spec: containers: - name: nginx image: nginx ports: - containerPort: 80 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ volumes: - name: html nfs: path: /fs01/nfs/html server: 172.31.182.154
在nfs上添加測試文件
[root@node volumes]# echo "hello nfs" >>/data/volumes/index.html [root@master volumes]# curl 172.17.8.7 hello nfs
PersistentVolume 和 PersistentVolumeClaim 提供了對存儲支持的抽象,也提供了基礎設施和應用之間的分界,管理員建立一系列的 PV 提供存儲,而後爲應用提供 PVC,應用程序僅須要加載一個 PVC,就能夠進行訪問。
對於pv和pvc,首先要準備存儲的,我用的是nfs,在192.168.247.144的節點上劃分了5個路徑做爲存儲的設備。
mkdir /data/volumes/{v1,v2,v3,v4,v5} -pv vim /etc/exports 添加以下的內容: /data/volumes/v1 192.168.0.0/16(rw,no_root_squash) /data/volumes/v2 192.168.0.0/16(rw,no_root_squash) /data/volumes/v3 192.168.0.0/16(rw,no_root_squash) /data/volumes/v4 192.168.0.0/16(rw,no_root_squash) /data/volumes/v5 192.168.0.0/16(rw,no_root_squash) 保存,導出下。 exportfs -avr 查看下是否正常。
nfs的存儲的好了。下一步要先建立pv,yaml文件以下
apiVersion: v1 kind: PersistentVolume metadata: name: pv01 labels: name: pv01 spec: nfs: path: /data/v1/ server: 192.168.247.144 accessModes: ["ReadWriteOnce","ReadWriteMany"] capacity: storage: 5Gi --- apiVersion: v1 kind: PersistentVolume metadata: name: pv02 labels: name: pv02 spec: nfs: path: /data/v2/ server: 192.168.247.144 accessModes: ["ReadWriteOnce","ReadWriteMany"] capacity: storage: 3Gi --- apiVersion: v1 kind: PersistentVolume metadata: name: pv03 labels: name: pv03 spec: nfs: path: /data/v3/ server: 192.168.247.144 accessModes: ["ReadWriteOnce","ReadWriteMany"] capacity: storage: 5Gi --- apiVersion: v1 kind: PersistentVolume metadata: name: pv04 labels: name: pv04 spec: nfs: path: /data/v4/ server: 192.168.247.144 accessModes: ["ReadWriteOnce","ReadWriteMany"] capacity: storage: 10Gi --- apiVersion: v1 kind: PersistentVolume metadata: name: pv05 labels: name: pv05 spec: nfs: path: /data/v5/ server: 192.168.247.144 accessModes: ["ReadWriteOnce","ReadWriteMany"] capacity: storage: 10Gi
#建立pv
kubectl apply -f pv-nfs.yaml
顯示,pv建立完成
接下來建立pvc和pod,yaml文件以下
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: mypvc namespace: default spec: accessModes: ["ReadWriteMany"] resources: requests: storage: 5Gi #建立pvc的指定使用存儲大小 --- apiVersion: v1 kind: Pod metadata: name: nginx-volume-pvc namespace: default spec: containers: - name: nginx-pvc image: nginx ports: - containerPort: 80 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ volumes: - name: html persistentVolumeClaim: #指定pvc類型 claimName: mypvc
添加文件測試:
[root@node v1]# echo "hello pvc01" >>index.html [root@master volumes]# curl 172.17.8.8 hello pvc01
顯示pvc建立好了,同時pvc綁定了一個pv,到此pv和pvc的安裝部署完成。
ps:pv和pvc是一對一綁定的。可是多個pod能夠掛載同一個pvc
一般使用的流程是,首先建立存儲,在建立pv,接着建立pvc,pod掛載到相應的pvc。
pv與pvc的完整生命週期:準備--綁定--使用--釋放--回收
存在問題,pvc申請時未必存在恰好符合需求的pv,由於設計了一種動態的申請pv叫StroageClass。
不把配置寫死在鏡像中而是引入配置中心configMap
configMap:明文
配置容器化應用的方式:
一、自定義命令行參數: args: [] 二、把配置文件直接配進鏡像。 三、環境變量 (1)Cloud Mative的應用程序通常可直接經過環境變量加載配置; (2)經過entrypoint腳原本預處理變量爲配置文件中的配置信息; 四、存儲卷
configMap使用
第一種方式:
[root@master volumes]# kubectl create configmap nginx-config --from-literal=nginx_port=8080 --from-literal=server_name=myapp.com configmap/nginx-config created [root@master volumes]# kubectl get cm NAME DATA AGE nginx-config 2 9s [root@master volumes]# kubectl describe cm nginx-config Name: nginx-config Namespace: default Labels: <none> Annotations: <none> Data ==== nginx_port: ---- 8080 server_name: ---- myapp.com Events: <none>
第二種方式:
[root@master configmap]# cat www.conf server { server_name myapp.com listen 80 root /data/web/html/; } [root@master configmap]# kubectl create configmap nginx-www --from-file=./www.conf configmap/nginx-www created [root@master configmap]# kubectl get cm NAME DATA AGE nginx-config 2 4m nginx-www 1 8s [root@master configmap]# kubectl get cm nginx-www -o yaml apiVersion: v1 data: www.conf: "server {\n\tserver_name myapp.com\n\tlisten 80\n\troot /data/web/html/;\n}\n" kind: ConfigMap metadata: creationTimestamp: 2019-03-17T15:30:46Z name: nginx-www namespace: default resourceVersion: "276960" selfLink: /api/v1/namespaces/default/configmaps/nginx-www uid: a29267cb-48c9-11e9-9672-000c291e0db8
建立pod
[root@master configmap]# cat pod-configmap-demo.yaml apiVersion: v1 kind: Pod metadata: name: pod-cm-1 namespace: default labels: app: myapp tier: frontend spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 env: - name: NGINX_SERVER_PORT valueFrom: configMapKeyRef: name: nginx-config key: nginx_port - name: NGINX_SERVER_NAME valueFrom: configMapKeyRef: name: nginx-config key: server_name [root@master configmap]# kubectl exec -it pod-cm-1 -- /bin/sh / # printenv MYAPP_SVC_PORT_80_TCP_ADDR=10.98.57.156 KUBERNETES_SERVICE_PORT=443 KUBERNETES_PORT=tcp://10.10.10.1:443 MYAPP_SVC_PORT_80_TCP_PORT=80 HOSTNAME=pod-cm-1 SHLVL=1 MYAPP_SVC_PORT_80_TCP_PROTO=tcp HOME=/root NGINX_SERVER_PORT=8080 EUREKA_SERVICE_HOST=10.10.10.192 NGINX_SERVER_NAME=myapp.com [root@master configmap]# cat pod-configmap-2.yaml apiVersion: v1 kind: Pod metadata: name: pod-cm-2 namespace: default labels: app: myapp tier: frontend spec: containers: - name: myapp image: ikubernetes/myapp:v1 ports: - name: http containerPort: 80 volumeMounts: - name: nginx-config mountPath: /etc/nginx/config.d/ readOnly: true volumes: - name: nginx-config configMap: name: nginx-config