# kubectl explain pods.spec.volumes #查看k8s支持的存儲
apiVersion: v1 kind: Pod metadata: name: pod-vol-demo namespace: default labels: app: myapp tier: frontend annotations: dongfei.tech/created-by: "cluster admin" spec: containers: - name: myapp image: dongfeimg/myapp:v1 imagePullPolicy: IfNotPresent ports: - name: http containerPort: 80 volumeMounts: - name: htmlvomumes mountPath: /data/web/html2/ volumes: - name: htmlvomumes emptyDir: medium: "" #使用宿主機內存當作磁盤掛載 sizeLimit: "1024" #限制使用的內存
apiVersion: v1 kind: Pod metadata: name: pod-vol-hostpath namespace: default spec: containers: - name: myapp image: dongfeimg/myapp:v1 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ volumes: - name: html hostPath: path: /data/pod/volume1 #node節點路徑 type: DirectoryOrCreate #文件夾不存在則建立
# yum install nfs-utils -y # mkdir /data/volumes/ # vim /etc/exports /data/volumes 192.168.100.0/24(rw,no_root_squash) # systemctl start nfs # systemctl enable nfs # mount -t nfs 192.168.100.1:/data/volumes /mnt/
apiVersion: v1 kind: Pod metadata: name: pod-vol-nfs namespace: default spec: containers: - name: myapp image: dongfeimg/myapp:v1 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ volumes: - name: html nfs: path: /data/volumes server: 192.168.100.1
# kubectl explain pv # kubectl explain pvc # kubectl get pv # kubectl get pvc
# mkdir -p /data/volumes/v{1,2,3,4,5} # vim /etc/exports /data/volumes/v1 192.168.100.0/24(rw,no_root_squash) /data/volumes/v2 192.168.100.0/24(rw,no_root_squash) /data/volumes/v3 192.168.100.0/24(rw,no_root_squash) /data/volumes/v4 192.168.100.0/24(rw,no_root_squash) /data/volumes/v5 192.168.100.0/24(rw,no_root_squash) # exportfs -arv # showmount -e
訪問模型(accessModes <[]string>):https://kubernetes.io/docs/concepts/storage/persistent-volumes/#access-modeshtml
apiVersion: v1 kind: PersistentVolume metadata: name: pv001 labels: name: pv001 spec: nfs: path: /data/volumes/v1 server: 192.168.100.1 accessModes: ["ReadWriteMany","ReadWriteOnce"] capacity: storage: 3Gi --- apiVersion: v1 kind: PersistentVolume metadata: name: pv002 labels: name: pv002 spec: nfs: path: /data/volumes/v2 server: 192.168.100.1 accessModes: ["ReadWriteOnce"] capacity: storage: 5Gi --- apiVersion: v1 kind: PersistentVolume metadata: name: pv003 labels: name: pv003 spec: nfs: path: /data/volumes/v3 server: 192.168.100.1 accessModes: ["ReadWriteOnce"] capacity: storage: 50Gi
apiVersion: v1 kind: PersistentVolumeClaim metadata: name: pvc001 namespace: default spec: accessModes: ["ReadWriteOnce"] resources: requests: storage: 6Gi #要求綁定大於6G的pv --- apiVersion: v1 kind: Pod metadata: name: pod-vol-pvc namespace: default spec: containers: - name: myapp image: dongfeimg/myapp:v1 volumeMounts: - name: html mountPath: /usr/share/nginx/html/ volumes: - name: html persistentVolumeClaim: claimName: pvc001
配置容器化應用的方式:node
- 自定義命令行參數:args: []
- 把配置文件直接copy進鏡像
- 環境變量加載配置
- cloud native的應用程序通常可經過環境變量加載配置
- 經過entrypoint腳原本預處理變量爲配置文件中的配置信息
- 存儲卷
# kubectl explain cm
# kubectl create configmap nginx-config --from-literal=nginx_port=80 --from-literal=server_name=myapp.dongfei.tech # kubectl get cm # kubectl describe cm nginx-config
# kubectl create secret generic mysql-root-password --from-literal=password=My@Pass # kubectl get secret # kubectl describe secret mysql-root-password # kubectl get secret mysql-root-password -o yaml # echo TXlAUGFzcw== |base64 -d #解碼 My@Pass
# cat www.conf server { server_name myapp.dongfei.tech; listen 80; root /data/web/html; } # kubectl create configmap nginx-www --from-file=./www.conf #不指定key則將文件名當作key,文件內容當作value
apiVersion: v1 kind: Pod metadata: name: pod-cm-1 namespace: default labels: app: myapp tier: frontend annotations: dongfei.tech/created-by: "cluster admin" spec: containers: - name: myapp image: dongfeimg/myapp:v1 imagePullPolicy: IfNotPresent 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
apiVersion: v1 kind: Pod metadata: name: pod-cm-2 namespace: default labels: app: myapp tier: frontend annotations: dongfei.tech/created-by: "cluster admin" spec: containers: - name: myapp image: dongfeimg/myapp:v1 imagePullPolicy: IfNotPresent ports: - name: http containerPort: 80 volumeMounts: - name: nginxconf mountPath: /etc/nginx/conf.d/ readOnly: true volumes: - name: nginxconf configMap: name: nginx-www
apiVersion: v1 kind: Pod metadata: name: pod-secret-1 namespace: default labels: app: myapp tier: frontend annotations: dongfei.tech/created-by: "cluster admin" spec: containers: - name: myapp image: dongfeimg/myapp:v1 imagePullPolicy: IfNotPresent ports: - name: http containerPort: 80 env: - name: MYSQL_ROOT_PASSWORD valueFrom: secretKeyRef: name: mysql-root-password key: password