Skip to content

Commit efbb49e

Browse files
authored
Merge pull request #15062 from presztak/rancher_local_path
Add `local-path-provisioner` addon
2 parents 0515996 + a51423d commit efbb49e

File tree

9 files changed

+360
-2
lines changed

9 files changed

+360
-2
lines changed

deploy/addons/assets.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ var (
4444
//go:embed storage-provisioner-gluster/*.tmpl
4545
StorageProvisionerGlusterAssets embed.FS
4646

47+
// StorageProvisionerRancherAssets assets for storage-provisioner-rancher addon
48+
//go:embed storage-provisioner-rancher/*.tmpl
49+
StorageProvisionerRancherAssets embed.FS
50+
4751
// EfkAssets assets for efk addon
4852
//go:embed efk/*.tmpl
4953
EfkAssets embed.FS
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
apiVersion: v1
2+
kind: Namespace
3+
metadata:
4+
name: local-path-storage
5+
6+
---
7+
apiVersion: v1
8+
kind: ServiceAccount
9+
metadata:
10+
name: local-path-provisioner-service-account
11+
namespace: local-path-storage
12+
13+
---
14+
apiVersion: rbac.authorization.k8s.io/v1
15+
kind: ClusterRole
16+
metadata:
17+
name: local-path-provisioner-role
18+
rules:
19+
- apiGroups: [ "" ]
20+
resources: [ "nodes", "persistentvolumeclaims", "configmaps" ]
21+
verbs: [ "get", "list", "watch" ]
22+
- apiGroups: [ "" ]
23+
resources: [ "endpoints", "persistentvolumes", "pods" ]
24+
verbs: [ "*" ]
25+
- apiGroups: [ "" ]
26+
resources: [ "events" ]
27+
verbs: [ "create", "patch" ]
28+
- apiGroups: [ "storage.k8s.io" ]
29+
resources: [ "storageclasses" ]
30+
verbs: [ "get", "list", "watch" ]
31+
32+
---
33+
apiVersion: rbac.authorization.k8s.io/v1
34+
kind: ClusterRoleBinding
35+
metadata:
36+
name: local-path-provisioner-bind
37+
roleRef:
38+
apiGroup: rbac.authorization.k8s.io
39+
kind: ClusterRole
40+
name: local-path-provisioner-role
41+
subjects:
42+
- kind: ServiceAccount
43+
name: local-path-provisioner-service-account
44+
namespace: local-path-storage
45+
46+
---
47+
apiVersion: apps/v1
48+
kind: Deployment
49+
metadata:
50+
name: local-path-provisioner
51+
namespace: local-path-storage
52+
spec:
53+
replicas: 1
54+
selector:
55+
matchLabels:
56+
app: local-path-provisioner
57+
template:
58+
metadata:
59+
labels:
60+
app: local-path-provisioner
61+
spec:
62+
serviceAccountName: local-path-provisioner-service-account
63+
containers:
64+
- name: local-path-provisioner
65+
image: {{.CustomRegistries.LocalPathProvisioner | default .ImageRepository | default .Registries.LocalPathProvisioner }}{{ .Images.LocalPathProvisioner }}
66+
imagePullPolicy: IfNotPresent
67+
command:
68+
- local-path-provisioner
69+
- --debug
70+
- start
71+
- --config
72+
- /etc/config/config.json
73+
volumeMounts:
74+
- name: config-volume
75+
mountPath: /etc/config/
76+
env:
77+
- name: POD_NAMESPACE
78+
valueFrom:
79+
fieldRef:
80+
fieldPath: metadata.namespace
81+
volumes:
82+
- name: config-volume
83+
configMap:
84+
name: local-path-config
85+
86+
---
87+
apiVersion: storage.k8s.io/v1
88+
kind: StorageClass
89+
metadata:
90+
name: local-path
91+
provisioner: rancher.io/local-path
92+
volumeBindingMode: WaitForFirstConsumer
93+
reclaimPolicy: Delete
94+
95+
---
96+
kind: ConfigMap
97+
apiVersion: v1
98+
metadata:
99+
name: local-path-config
100+
namespace: local-path-storage
101+
data:
102+
config.json: |-
103+
{
104+
"nodePathMap":[
105+
{
106+
"node":"DEFAULT_PATH_FOR_NON_LISTED_NODES",
107+
"paths":["/opt/local-path-provisioner"]
108+
}
109+
]
110+
}
111+
setup: |-
112+
#!/bin/sh
113+
set -eu
114+
mkdir -m 0777 -p "$VOL_DIR"
115+
teardown: |-
116+
#!/bin/sh
117+
set -eu
118+
rm -rf "$VOL_DIR"
119+
helperPod.yaml: |-
120+
apiVersion: v1
121+
kind: Pod
122+
metadata:
123+
name: helper-pod
124+
spec:
125+
containers:
126+
- name: helper-pod
127+
image: {{.CustomRegistries.Helper | default .ImageRepository | default .Registries.Helper }}{{ .Images.Helper }}
128+
imagePullPolicy: IfNotPresent
129+
130+
131+

pkg/addons/addons_storage_classes.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ func enableOrDisableStorageClasses(cc *config.ClusterConfig, name string, val st
3939
class := defaultStorageClassProvisioner
4040
if name == "storage-provisioner-gluster" {
4141
class = "glusterfile"
42+
} else if name == "storage-provisioner-rancher" {
43+
class = "local-path"
4244
}
4345

4446
api, err := machine.NewAPIClient()
@@ -62,6 +64,10 @@ func enableOrDisableStorageClasses(cc *config.ClusterConfig, name string, val st
6264
}
6365

6466
if enable {
67+
// Enable addon before marking it as default
68+
if err = EnableOrDisableAddon(cc, name, val); err != nil {
69+
return err
70+
}
6571
// Only StorageClass for 'name' should be marked as default
6672
err = storageclass.SetDefaultStorageClass(storagev1, class)
6773
if err != nil {
@@ -73,7 +79,10 @@ func enableOrDisableStorageClasses(cc *config.ClusterConfig, name string, val st
7379
if err != nil {
7480
return errors.Wrapf(err, "Error disabling %s as the default storage class", class)
7581
}
82+
if err = EnableOrDisableAddon(cc, name, val); err != nil {
83+
return err
84+
}
7685
}
7786

78-
return EnableOrDisableAddon(cc, name, val)
87+
return nil
7988
}

pkg/addons/config.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ var Addons = []*Addon{
166166
set: SetBool,
167167
callbacks: []setFn{enableOrDisableStorageClasses},
168168
},
169+
{
170+
name: "storage-provisioner-rancher",
171+
set: SetBool,
172+
callbacks: []setFn{enableOrDisableStorageClasses},
173+
},
169174
{
170175
name: "metallb",
171176
set: SetBool,

pkg/minikube/assets/addons.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ var Addons = map[string]*Addon{
208208
"GlusterfsServer": "docker.io",
209209
"GlusterfileProvisioner": "docker.io",
210210
}),
211+
"storage-provisioner-rancher": NewAddon([]*BinAsset{
212+
MustBinAsset(addons.StorageProvisionerRancherAssets,
213+
"storage-provisioner-rancher/storage-provisioner-rancher.yaml.tmpl",
214+
vmpath.GuestAddonsDir,
215+
"storage-provisioner-rancher.yaml",
216+
"0640"),
217+
}, false, "storage-provisioner-rancher", "3rd party (Rancher)", "", "", map[string]string{
218+
"LocalPathProvisioner": "rancher/local-path-provisioner:v0.0.22@sha256:e34c88ae0affb1cdefbb874140d6339d4a27ec4ee420ae8199cd839997b05246",
219+
"Helper": "busybox:stable@sha256:3fbc632167424a6d997e74f52b878d7cc478225cffac6bc977eedfe51c7f4e79",
220+
}, map[string]string{
221+
"LocalPathProvisioner": "docker.io",
222+
"Helper": "docker.io",
223+
}),
211224
"efk": NewAddon([]*BinAsset{
212225
MustBinAsset(addons.EfkAssets,
213226
"efk/elasticsearch-rc.yaml.tmpl",
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
---
2+
title: "Using Local Path Provisioner"
3+
linkTitle: "Using Local Path Provisioner"
4+
weight: 1
5+
date: 2022-10-05
6+
description: >
7+
Using Local Path Provisioner
8+
---
9+
10+
## Overview
11+
12+
[Local Path Provisioner](https://github.com/rancher/local-path-provisioner), provides a way for the Kubernetes users to utilize the local storage in each node. It supports multi-node setups. This tutorial will show you how to setup local-path-prvisioner on two node minikube cluster.
13+
14+
## Prerequisites
15+
16+
- Minikube version higher than v1.27.0
17+
- kubectl
18+
19+
## Tutorial
20+
21+
- Start a cluster with 2 nodes:
22+
23+
```shell
24+
$ minikube start -n 2
25+
```
26+
27+
- Enable `storage-provisioner-rancher` addon:
28+
29+
```
30+
$ minikube addons enable storage-provisioner-rancher
31+
```
32+
33+
- You should be able to see Pod in the `local-path-storage` namespace:
34+
35+
```
36+
$ kubectl get pods -n local-path-storage
37+
NAME READY STATUS RESTARTS AGE
38+
local-path-provisioner-7f58b4649-hcbk9 1/1 Running 0 38s
39+
```
40+
41+
- The `local-path` StorageClass should be marked as `default`:
42+
43+
```
44+
$ kubectl get sc
45+
NAME PROVISIONER RECLAIMPOLICY VOLUMEBINDINGMODE ALLOWVOLUMEEXPANSION AGE
46+
local-path (default) rancher.io/local-path Delete WaitForFirstConsumer false 107s
47+
standard k8s.io/minikube-hostpath Delete Immediate false 4m27s
48+
```
49+
50+
- The following `yaml` creates PVC and Pod that creates file with content on second node (minikube-m02):
51+
52+
```
53+
---
54+
apiVersion: v1
55+
kind: PersistentVolumeClaim
56+
metadata:
57+
name: test-pvc
58+
spec:
59+
accessModes:
60+
- ReadWriteOnce
61+
resources:
62+
requests:
63+
storage: 64Mi
64+
---
65+
apiVersion: v1
66+
kind: Pod
67+
metadata:
68+
name: test-local-path
69+
spec:
70+
restartPolicy: OnFailure
71+
nodeSelector:
72+
"kubernetes.io/hostname": "minikube-m02"
73+
containers:
74+
- name: busybox
75+
image: busybox:stable
76+
command: ["sh", "-c", "echo 'local-path-provisioner' > /test/file1"]
77+
volumeMounts:
78+
- name: data
79+
mountPath: /test
80+
volumes:
81+
- name: data
82+
persistentVolumeClaim:
83+
claimName: test-pvc
84+
```
85+
86+
```
87+
$ kubectl get pvc
88+
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
89+
test-pvc Bound pvc-f07e253b-fea7-433a-b0ac-1bcea3f77076 64Mi RWO local-path 5m19s
90+
```
91+
92+
```
93+
$ kubectl get pods -o wide
94+
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
95+
test-local-path 0/1 Completed 0 5m19s 10.244.1.5 minikube-m02 <none> <none>
96+
```
97+
98+
- On the second node we are able to see created file with content `local-path-provisioner`:
99+
100+
```
101+
$ minikube ssh -n minikube-m02 "cat /opt/local-path-provisioner/pvc-f07e253b-fea7-433a-b0ac-1bcea3f77076_default_test-pvc/file1"
102+
local-path-provisioner
103+
```

0 commit comments

Comments
 (0)