---
# System prepended metadata

title: Hostpath

---

# Hostpath

1) Применяем манифест

deployment.yaml
```yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - image: nginx:1.20
        name: nginx
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 50m
            memory: 100Mi
          limits:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - name: data
          mountPath: /files
      volumes:
      - name: data
        hostPath:
          path: /data_pod
```

```bash
kubectl apply -f deployment.yaml
kubectl get pod
kubectl get all
kubectl describe replicaset <TAB>
```

2) Очищаем 
```bash
kubectl delete deployment my-deployment
```

# EmptyDir

1) Применяем манифест

deployment.yaml
```yaml
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: my-deployment
spec:
  replicas: 1
  selector:
    matchLabels:
      app: my-app
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: my-app
    spec:
      containers:
      - image: nginx:1.20
        name: nginx
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 50m
            memory: 100Mi
          limits:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - name: data
          mountPath: /files
      volumes:
      - name: data
        emptyDir: {}
```

```bash
kubectl apply -f deployment.yaml
```

2) Заходим в под и создаем файлик на томе empty dir

```bash
kubectl exec -it my-deployment-<TAB> -- sh -c 'echo "Some data" > /files/data.txt'

kubectl exec -it my-deployment-<TAB> -- cat /files/data.txt
```

3) Удаляем под и ищем данные

```bash
kubectl delete pod my-deployment-<TAB>
```

```bash
kubectl exec -it my-deployment-<TAB> -- cat /files/data.txt
```

4) Очищаем

```bash
kubectl delete deployment my-deployment
```

# PV/PVC

```yaml
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: fileshare
spec:
  accessModes:
  - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
```

1) Применяем манифест pvc.yml

```bash
kubectl apply -f pvc.yaml

kubectl get pvc
kubectl get pv
```

2) Запустим приложение, использующее PV
deployment.yml
```yaml
---
apiVersion: v1
kind: ConfigMap
metadata:
  name: fileshare
data:
  default.conf: |
    server {
      listen       80 default_server;
      server_name  _;

      default_type text/plain;

      location / {
        return 200 '$hostname\n';
      }

      location /files {
        alias /data;
        autoindex on;
        client_body_temp_path /tmp;
        dav_methods PUT DELETE MKCOL COPY MOVE;
        create_full_put_path on;
        dav_access user:rw group:rw all:r;
      }
    }

---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: fileshare
spec:
  replicas: 2
  selector:
    matchLabels:
      app: fileshare
  strategy:
    rollingUpdate:
      maxSurge: 1
      maxUnavailable: 1
    type: RollingUpdate
  template:
    metadata:
      labels:
        app: fileshare
    spec:
      initContainers:
      - image: busybox
        name: mount-permissions-fix
        command: ["sh", "-c", "chmod 777 /data"]
        volumeMounts:
        - name: data
          mountPath: /data
      containers:
      - image: centosadmin/reloadable-nginx:1.12
        name: nginx
        ports:
        - containerPort: 80
        resources:
          requests:
            cpu: 50m
            memory: 100Mi
          limits:
            cpu: 100m
            memory: 100Mi
        volumeMounts:
        - name: config
          mountPath: /etc/nginx/conf.d
        - name: data
          mountPath: /data
      volumes:
      - name: config
        configMap:
          name: fileshare
      - name: data
        persistentVolumeClaim:
          claimName: fileshare
```

```bash
kubectl apply -f deployment.yml
```

3) Посмотрим describe и смонтированные тома в контейнере

```bash
kubectl describe pod fileshare-<TAB>
kubectl exec -it fileshare-<TAB> -- df -h
```

4) Заходим в под и создаем файлик на томе 

```bash
kubectl exec -it fileshare-<TAB> -- sh -c 'echo "Some data" > /data/data.txt'

kubectl port-forward fileshare-<TAB> 8080:80 

curl 127.0.0.1:8080/files
```

5) Очищаем

```bash
kubectl delete -f .
```
