K8s之deploy挂载Volumes
一. 创建Host类型pv
1.1 创建pv yaml文件
-bash-4.2# cat iam_mount_pvc.yaml apiVersion: apps/v1 kind: Deployment metadata: creationTimestamp: null labels: app: iam name: iam spec: replicas: 3 selector: matchLabels: app: iam strategy: {} template: metadata: creationTimestamp: null labels: app: iam spec: containers: - image: registry.ncloud.navinfo.com/iam/iam:1260cd6 name: iam imagePullPolicy: Always resources: {} volumeMounts: - name: iamvolume mountPath: /data/static volumes: - name: iamvolume persistentVolumeClaim: claimName: mypvc status: {}
本地挂载目录路径: `/data/static` 数据将会持久化到该路径下面
#### 1.2 kubectl创建pv
```shell script
-bash-4.2# kubectl create namespace iam #### 创建namespace做资源隔离
-bash-4.2# kubectl create -f iam_pv.yaml -n iam
rsistentvolume/myvolume created
-bash-4.2# kubectl get pv -n iam
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
myvolume 5Gi RWO,RWX Retain Available normal 4s
-bash-4.2#
```
### 二、创建pvc
#### 2.1 创建pvc yaml文件
```shell script
-bash-4.2# vim iam_pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
name: mypvc
spec:
storageClassName: normal
accessModes:
- ReadWriteOnce
resources:
requests:
storage: 3Gi
```
#### 2.2 kubectl创建pvc
```shell script
-bash-4.2# kubectl create -f iam_pvc.yaml -n iam
persistentvolumeclaim/mypvc created
-bash-4.2# kubectl get pvc -n iam
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mypvc Bound myvolume 5Gi RWO,RWX normal 6s
-bash-4.2#
-bash-4.2# kubectl get pv -n iam
NAME CAPACITY ACCESS MODES RECLAIM POLICY STATUS CLAIM STORAGECLASS REASON AGE
myvolume 5Gi RWO,RWX Retain Bound iam/mypvc normal 73s
-bash-4.2#
```
当`STATUS`状态为`Bound`说明`pvc`已经绑定到`pv`上,到这里已完成初步的`pvc`、`pv`的绑定
### 三、创建deploy yaml文件
#### 3.1 挂载pvc
```yaml
-bash-4.2# cat iam_mount_pvc.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
labels:
app: iam
name: iam
spec:
replicas: 3
selector:
matchLabels:
app: iam
strategy: {}
template:
metadata:
creationTimestamp: null
labels:
app: iam
spec:
containers:
- image: registry.ncloud.navinfo.com/iam/iam:1260cd6
name: iam
imagePullPolicy: Always
resources: {}
volumeMounts:
- name: iamvolume
mountPath: /data/static
volumes:
- name: iamvolume
persistentVolumeClaim:
claimName: mypvc
status: {}
-bash-4.2#
```
#### 3.2 kubectl创建deploy
```shell script
-bash-4.2# kubectl create -f iam_mount_pvc.yaml -n iam
deployment.apps/iam created
-bash-4.2#
-bash-4.2#
-bash-4.2# kubectl get po -n iam
NAME READY STATUS RESTARTS AGE
iam-666475bcf9-gxgfb 1/1 Running 0 8s
iam-666475bcf9-lcljd 1/1 Running 0 8s
iam-666475bcf9-t6tbv 1/1 Running 0 8s
-bash-4.2#
```
#### 3.3 检查是否挂载成功
##### a. 进入port检查
```shell script
-bash-4.2# kubectl -n iam exec -it iam-666475bcf9-t6tbv -- /bin/sh
~/iam # ls
AUTHORS api-ref doc nohup.out test-requirements.txt
ChangeLog babel.cfg docker requirements.txt tools
Dockerfile build etc setup tox.ini
LICENSE build.sh iam setup.cfg
README.rst build_k8s.sh iam.egg-info setup.py
~/iam #
```
##### b. 创建一个文件
```shell script
~/iam # ls /data/static/
~/iam # cat /etc/passwd >/data/static/pwd.txt
~/iam # ls /data/static/
pwd.txt
```
###### c. 检查持久化
```shell script
-bash-4.2# ll /data/static/
total 8
-rw-r--r-- 1 root root 1230 Jul 19 22:44 pwd.txt
```
完成调试。