how_to_use_on_kubernetes.md 7.8 KB
Newer Older
1 2
# Use JuiceFS on Kubernetes

3
JuiceFS provides the [CSI Driver](https://github.com/juicedata/juicefs-csi-driver) for Kubernetes.
4

R
Rui Su 已提交
5 6 7 8 9 10 11 12 13
## Table of Content
- [Prerequisites](#Prerequisites)
- [Installation](#Installation)
  - [Install with Helm](#Install-with-Helm)
  - [Install with kubectl](#Install-with-kubectl)
- [Use JuiceFS](#Use-JuiceFS)
- [Monitoring](#Monitoring)
  - [Configure Prometheus server](#Configure-Prometheus-server)
  - [Configure Grafana dashboard](#Configure-Grafana-dashboard)
14 15 16 17 18 19 20

## Prerequisites

- Kubernetes 1.14+

## Installation

21
### Install with Helm
22

23
To install Helm, refer to the [Helm install guide](https://github.com/helm/helm#install), Helm 3 is required.
24

25
1. Prepare a file `values.yaml` with access information about metadata engine (e.g. Redis) and object storage (take Amazon S3 `us-east-1` as an example):
26 27 28 29 30

```yaml
storageClasses:
- name: juicefs-sc
  enabled: true
31
  reclaimPolicy: Retain
32 33 34 35 36 37 38 39 40
  backend:
    name: "test"
    metaurl: "redis://juicefs.afyq4z.0001.use1.cache.amazonaws.com/3"
    storage: "s3"
    accessKey: ""
    secretKey: ""
    bucket: "https://juicefs-test.s3.us-east-1.amazonaws.com"
```

41
Here we assign AWS [IAM role](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_use_switch-role-ec2.html) for the EC2 Kubernetes node, otherwise the `accessKey` and `secretKey` cannot be empty. We use ElastiCache for Redis as the meta store.
42 43 44 45 46 47 48 49 50 51 52

2. Install

```shell
helm repo add juicefs-csi-driver https://juicedata.github.io/juicefs-csi-driver/
helm repo update
helm upgrade juicefs-csi-driver juicefs-csi-driver/juicefs-csi-driver --install -f ./values.yaml
```

3. Check the deployment

53 54 55 56 57 58 59 60 61 62
- Check pods are running: the deployment will launch a `StatefulSet` named `juicefs-csi-controller` with replica `1` and a `DaemonSet` named `juicefs-csi-node`, so run `kubectl -n kube-system get pods -l app.kubernetes.io/name=juicefs-csi-driver` should see `n+1` (where `n` is the number of worker nodes of the Kubernetes cluster) pods is running. For example:

```sh
$ kubectl -n kube-system get pods -l app.kubernetes.io/name=juicefs-csi-driver
NAME                       READY   STATUS    RESTARTS   AGE
juicefs-csi-controller-0   3/3     Running   0          22m
juicefs-csi-node-v9tzb     3/3     Running   0          14m
```

- Check secret: `kubectl -n kube-system describe secret juicefs-sc-secret` will show the secret with above `backend` fields in `values.yaml`:
63

64 65
```sh
$ kubectl -n kube-system describe secret juicefs-sc-secret
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
Name:         juicefs-sc-secret
Namespace:    kube-system
Labels:       app.kubernetes.io/instance=juicefs-csi-driver
              app.kubernetes.io/managed-by=Helm
              app.kubernetes.io/name=juicefs-csi-driver
              app.kubernetes.io/version=0.7.0
              helm.sh/chart=juicefs-csi-driver-0.1.0
Annotations:  meta.helm.sh/release-name: juicefs-csi-driver
              meta.helm.sh/release-namespace: default

Type:  Opaque

Data
====
access-key:  0 bytes
bucket:      47 bytes
metaurl:     54 bytes
name:        4 bytes
secret-key:  0 bytes
storage:     2 bytes
```

88
- Check storage class: `kubectl get sc juicefs-sc` will show the storage class like this:
89

90 91
```sh
$ kubectl get sc juicefs-sc
92 93
NAME         PROVISIONER       RECLAIMPOLICY   VOLUMEBINDINGMODE   ALLOWVOLUMEEXPANSION   AGE
juicefs-sc   csi.juicefs.com   Retain          Immediate           false                  69m
94 95
```

R
Rui Su 已提交
96 97
🏡 [Back to Top](#Table-of-Content)

98 99 100 101 102 103 104 105
### Install with kubectl

1. Deploy the driver:

```bash
kubectl apply -f https://raw.githubusercontent.com/juicedata/juicefs-csi-driver/master/deploy/k8s.yaml
```

106
Here we use the `juicedata/juicefs-csi-driver:latest` image, if you want to use the specified tag such as `v0.7.0`, you should download the deploy YAML file and modified it:
107 108 109 110 111 112 113

```bash
curl -sSL https://raw.githubusercontent.com/juicedata/juicefs-csi-driver/master/deploy/k8s.yaml | sed 's@juicedata/juicefs-csi-driver@juicedata/juicefs-csi-driver:v0.7.0@' | kubectl apply -f -
```

2. Create storage class

114
- Create secret `juicefs-sc-secret`:
115 116 117 118

```bash
kubectl -n kube-system create secret generic juicefs-sc-secret \
  --from-literal=name=test \
C
chnliyong 已提交
119
  --from-literal=metaurl=redis://juicefs.afyq4z.0001.use1.cache.amazonaws.com/3 \
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  --from-literal=storage=s3 \
  --from-literal=bucket=https://juicefs-test.s3.us-east-1.amazonaws.com \
  --from-literal=access-key="" \
  --from-literal=secret-key=""

```

- Create storage class use `kubectl apply`:

```yaml
apiVersion: storage.k8s.io/v1
kind: StorageClass
metadata:
  name: juicefs-sc
provisioner: csi.juicefs.com
parameters:
  csi.storage.k8s.io/node-publish-secret-name: juicefs-sc-secret
  csi.storage.k8s.io/node-publish-secret-namespace: kube-system
  csi.storage.k8s.io/provisioner-secret-name: juicefs-sc-secret
  csi.storage.k8s.io/provisioner-secret-namespace: kube-system
140
reclaimPolicy: Retain
141 142 143
volumeBindingMode: Immediate
```

R
Rui Su 已提交
144
🏡 [Back to Top](#Table-of-Content)
145

146
## Use JuiceFS
147

148
Now we can use JuiceFS in our pods. Here we create a `PersistentVolumeClaim` and refer to it in a pod as an example:
149 150 151 152 153

```yaml
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
154
  name: juicefs-pvc
155 156 157 158 159
spec:
  accessModes:
  - ReadWriteMany
  resources:
    requests:
160
      storage: 10Pi
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
  storageClassName: juicefs-sc
---
apiVersion: v1
kind: Pod
metadata:
  name: juicefs-app
spec:
  containers:
  - args:
    - -c
    - while true; do echo $(date -u) >> /data/out.txt; sleep 5; done
    command:
    - /bin/sh
    image: busybox
    name: app
    volumeMounts:
    - mountPath: /data
      name: juicefs-pv
  volumes:
  - name: juicefs-pv
    persistentVolumeClaim:
182
      claimName: juicefs-pvc
183 184
```

185
Save above content to a file named like `juicefs-app.yaml`, then use command `kubectl apply -f juicefs-app.yaml` to bootstrap the pod. After that, you can check the status of pod:
186

187 188 189 190 191
```sh
$ kubectl get pod juicefs-app
NAME          READY     STATUS    RESTARTS   AGE
juicefs-app   1/1       Running   0          10m
```
192

193
If the status of pod is not `Running` (e.g. `ContainerCreating`), there may have some issues. Please refer to the [troubleshooting](https://github.com/juicedata/juicefs-csi-driver/blob/master/docs/troubleshooting.md) document.
C
chnliyong 已提交
194

195
For more details about JuiceFS CSI Driver please refer to [project homepage](https://github.com/juicedata/juicefs-csi-driver).
C
chnliyong 已提交
196

R
Rui Su 已提交
197
🏡 [Back to Top](#Table-of-Content)
C
chnliyong 已提交
198 199 200

## Monitoring

201
JuiceFS CSI Driver can export [Prometheus](https://prometheus.io) metrics at port `9567`. For a description of all monitoring metrics, please refer to [JuiceFS Metrics](p8s_metrics.md).
C
chnliyong 已提交
202

203
### Configure Prometheus server
C
chnliyong 已提交
204

205
Add a job to `prometheus.yml`:
C
chnliyong 已提交
206 207 208 209 210 211 212

```yaml
scrape_configs:
  - job_name: 'juicefs'
    kubernetes_sd_configs:
    - role: pod
    relabel_configs:
W
Weiwei 已提交
213
    - source_labels: [__meta_kubernetes_pod_label_app_kubernetes_io_name]
C
chnliyong 已提交
214
      action: keep
W
Weiwei 已提交
215
      regex: juicefs-mount
C
chnliyong 已提交
216 217 218
    - source_labels: [__address__]
      action: replace
      regex: ([^:]+)(:\d+)?
219
      replacement: $1:9567
C
chnliyong 已提交
220 221 222 223 224 225
      target_label: __address__
    - source_labels: [__meta_kubernetes_pod_node_name]
      target_label: node
      action: replace
```

226
Here we assume the Prometheus server is running inside Kubernetes cluster, if your Prometheus server is running outside Kubernetes cluster, make sure Kubernetes cluster nodes are reachable from Prometheus server, refer to [this issue](https://github.com/prometheus/prometheus/issues/4633) to add the `api_server` and `tls_config` client auth to the above configuration like this:
C
chnliyong 已提交
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243

```yaml
scrape_configs:
  - job_name: 'juicefs'
    kubernetes_sd_configs:
    - api_server: <Kubernetes API Server>
      role: pod
      tls_config:
        ca_file: <...>
        cert_file: <...>
        key_file: <...>
        insecure_skip_verify: false
    relabel_configs:
    ...
    ...
```

244
### Configure Grafana dashboard
C
chnliyong 已提交
245

246
JuiceFS provides a [dashboard template](./grafana_template.json) for [Grafana](https://grafana.com), which can be imported to show the collected metrics in Prometheus.
R
Rui Su 已提交
247 248

🏡 [Back to Top](#Table-of-Content)