From 5976f614977cb34f5ef6becbc2762c4ba15f2bdf Mon Sep 17 00:00:00 2001 From: gjmzj Date: Sat, 28 Jul 2018 10:21:34 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4nfs=20=E5=8A=A8=E6=80=81?= =?UTF-8?q?=E5=AD=98=E5=82=A8=E7=9B=B8=E5=85=B3=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 ++++ manifests/storage/{test => }/test.yaml | 8 +++---- manifests/storage/test/test-claim.yaml | 11 --------- manifests/storage/test/test-pod.yaml | 22 ----------------- roles/cluster-addon/tasks/main.yml | 4 ++-- .../templates/{dns => }/coredns.yaml.j2 | 0 .../templates/{dns => }/kubedns.yaml.j2 | 0 roles/cluster-storage/cluster-storage.yml | 5 ---- roles/cluster-storage/defaults/main.yml | 13 ++++++++-- roles/cluster-storage/tasks/main.yml | 11 +++++---- .../nfs/nfs-client-provisioner.yaml.j2 | 24 ++++++++++++------- tools/init_vars.yml | 2 ++ 12 files changed, 46 insertions(+), 58 deletions(-) rename manifests/storage/{test => }/test.yaml (75%) delete mode 100644 manifests/storage/test/test-claim.yaml delete mode 100644 manifests/storage/test/test-pod.yaml rename roles/cluster-addon/templates/{dns => }/coredns.yaml.j2 (100%) rename roles/cluster-addon/templates/{dns => }/kubedns.yaml.j2 (100%) diff --git a/.gitignore b/.gitignore index be486dc66..08d9b63cd 100644 --- a/.gitignore +++ b/.gitignore @@ -13,6 +13,10 @@ manifests/coredns/coredns.yaml # ansible hosts hosts +# k8s storage manifests +manifests/storage/* +!manifests/storage/test.yaml + # k8s backup directory roles/cluster-backup/files/* !roles/cluster-backup/files/readme.md diff --git a/manifests/storage/test/test.yaml b/manifests/storage/test.yaml similarity index 75% rename from manifests/storage/test/test.yaml rename to manifests/storage/test.yaml index a78ef2e84..0618c06c5 100644 --- a/manifests/storage/test/test.yaml +++ b/manifests/storage/test.yaml @@ -1,9 +1,9 @@ kind: PersistentVolumeClaim apiVersion: v1 metadata: - name: test + name: test-claim spec: - storageClassName: alicloud-nas + storageClassName: nfs-dynamic-class-01 accessModes: - ReadWriteMany resources: @@ -24,7 +24,7 @@ spec: - "/bin/sh" args: - "-c" - - "touch /mnt/YES && exit 0 || exit 1" + - "echo 'hello k8s' > /mnt/SUCCESS && sleep 36000 || exit 1" volumeMounts: - name: nfs-pvc mountPath: "/mnt" @@ -32,4 +32,4 @@ spec: volumes: - name: nfs-pvc persistentVolumeClaim: - claimName: test + claimName: test-claim diff --git a/manifests/storage/test/test-claim.yaml b/manifests/storage/test/test-claim.yaml deleted file mode 100644 index 92d634439..000000000 --- a/manifests/storage/test/test-claim.yaml +++ /dev/null @@ -1,11 +0,0 @@ -kind: PersistentVolumeClaim -apiVersion: v1 -metadata: - name: test-claim -spec: - storageClassName: alicloud-nas - accessModes: - - ReadWriteMany - resources: - requests: - storage: 1Mi diff --git a/manifests/storage/test/test-pod.yaml b/manifests/storage/test/test-pod.yaml deleted file mode 100644 index 187482b19..000000000 --- a/manifests/storage/test/test-pod.yaml +++ /dev/null @@ -1,22 +0,0 @@ -kind: Pod -apiVersion: v1 -metadata: - name: test-pod -spec: - containers: - - name: test-pod - image: busybox - imagePullPolicy: IfNotPresent - command: - - "/bin/sh" - args: - - "-c" - - "touch /mnt/SUCCESS && exit 0 || exit 1" - volumeMounts: - - name: nfs-pvc - mountPath: "/mnt" - restartPolicy: "Never" - volumes: - - name: nfs-pvc - persistentVolumeClaim: - claimName: test-claim diff --git a/roles/cluster-addon/tasks/main.yml b/roles/cluster-addon/tasks/main.yml index cfee38559..94d433c3b 100644 --- a/roles/cluster-addon/tasks/main.yml +++ b/roles/cluster-addon/tasks/main.yml @@ -1,12 +1,12 @@ #-------------kube-dns 插件参数初始化 # kubedns.yaml文件中部分参数根据hosts文件设置而定,因此需要用template模块替换参数 - name: 准备 kubedns的部署文件 kubedns.yaml - template: src=dns/kubedns.yaml.j2 dest={{ base_dir }}/manifests/kubedns/kubedns.yaml + template: src=kubedns.yaml.j2 dest={{ base_dir }}/manifests/kubedns/kubedns.yaml when: "hostvars[inventory_hostname]['group_names'].count('deploy') == 1" # coredns.yaml文件中部分参数根据hosts文件设置而定,因此需要用template模块替换参数 - name: 准备 coredns的部署文件 coredns.yaml - template: src=dns/coredns.yaml.j2 dest={{ base_dir }}/manifests/coredns/coredns.yaml + template: src=coredns.yaml.j2 dest={{ base_dir }}/manifests/coredns/coredns.yaml when: "hostvars[inventory_hostname]['group_names'].count('deploy') == 1" - name: 获取所有已经创建的POD信息 diff --git a/roles/cluster-addon/templates/dns/coredns.yaml.j2 b/roles/cluster-addon/templates/coredns.yaml.j2 similarity index 100% rename from roles/cluster-addon/templates/dns/coredns.yaml.j2 rename to roles/cluster-addon/templates/coredns.yaml.j2 diff --git a/roles/cluster-addon/templates/dns/kubedns.yaml.j2 b/roles/cluster-addon/templates/kubedns.yaml.j2 similarity index 100% rename from roles/cluster-addon/templates/dns/kubedns.yaml.j2 rename to roles/cluster-addon/templates/kubedns.yaml.j2 diff --git a/roles/cluster-storage/cluster-storage.yml b/roles/cluster-storage/cluster-storage.yml index 72378b382..c98daf6ee 100644 --- a/roles/cluster-storage/cluster-storage.yml +++ b/roles/cluster-storage/cluster-storage.yml @@ -1,8 +1,3 @@ - hosts: deploy roles: - cluster-storage - vars: - storage_type: nfs - storage_server: 172.16.3.86 - storage_path: /data/nfs - storage_class_name: nfs-dynamic-class diff --git a/roles/cluster-storage/defaults/main.yml b/roles/cluster-storage/defaults/main.yml index 86fbe2ddd..cf3513536 100644 --- a/roles/cluster-storage/defaults/main.yml +++ b/roles/cluster-storage/defaults/main.yml @@ -1,5 +1,14 @@ -# 动态存储类型, 目前支持nfs和alicloud-nas -storage_type: nfs +# 动态存储类型, 目前支持自建nfs和aliyun_nas +storage_nfs_enabled: "no" +storage_aliyun_nas_enabled: "no" + +# nfs server 参数 +nfs_server: "192.168.1.8" +nfs_server_path: "/data/nfs" +nfs_storage_class: "nfs-dynamic-class-01" +nfs_provisioner_name: "nfs-provisioner-01" + +# aliyun_nas 参数 storage_server: 172.16.3.86 storage_path: /data/nfs storage_class_name: nfs-dynamic-class diff --git a/roles/cluster-storage/tasks/main.yml b/roles/cluster-storage/tasks/main.yml index 73d69756f..01d777a8f 100644 --- a/roles/cluster-storage/tasks/main.yml +++ b/roles/cluster-storage/tasks/main.yml @@ -1,11 +1,13 @@ - block: + - name: 准备nfs-client 配置目录 + file: name={{ base_dir }}/manifests/storage/nfs/{{ nfs_provisioner_name }} state=directory - name: 准备部署nfs-client动态存储 template: src: nfs/nfs-client-provisioner.yaml.j2 - dest: "{{ base_dir }}/manifests/storage/nfs/nfs-client-provisioner.yaml" + dest: "{{ base_dir }}/manifests/storage/nfs/{{ nfs_provisioner_name }}/nfs-client-provisioner.yaml" - name: 开始部署nfs-client动态存储 - shell: "{{ bin_dir }}/kubectl apply -f {{ base_dir }}/manifests/storage/nfs/nfs-client-provisioner.yaml" - when: 'storage_type == "nfs"' + shell: "{{ bin_dir }}/kubectl apply -f {{ base_dir }}/manifests/storage/nfs/{{ nfs_provisioner_name }}" + when: 'storage_nfs_enabled == "yes"' - block: - name: 准备部署alicloud-nas动态存储 @@ -16,7 +18,7 @@ shell: "{{ bin_dir }}/kubectl apply -f {{ base_dir }}/manifests/storage/alicloud-nas/alicloud-disk.yaml" - name: 开始部署alicloud-nas动态存储 shell: "{{ bin_dir }}/kubectl apply -f {{ base_dir }}/manifests/storage/alicloud-nas/alicloud-nas.yaml" - when: 'storage_type == "alicloud-nas"' + when: 'storage_aliyun_nas_enabled == "yes"' - block: - name: 准备部署动态存储类 @@ -25,3 +27,4 @@ dest: "{{ base_dir }}/manifests/storage/dynamic-storageclass.yaml" - name: 开始部署动态存储类 shell: "{{ bin_dir }}/kubectl apply -f {{ base_dir }}/manifests/storage/dynamic-storageclass.yaml" + when: 'storage_aliyun_nas_enabled == "yes"' diff --git a/roles/cluster-storage/templates/nfs/nfs-client-provisioner.yaml.j2 b/roles/cluster-storage/templates/nfs/nfs-client-provisioner.yaml.j2 index cd3351d08..a2320dfe8 100644 --- a/roles/cluster-storage/templates/nfs/nfs-client-provisioner.yaml.j2 +++ b/roles/cluster-storage/templates/nfs/nfs-client-provisioner.yaml.j2 @@ -41,7 +41,7 @@ roleRef: kind: Deployment apiVersion: apps/v1beta1 metadata: - name: nfs-client-provisioner + name: {{ nfs_provisioner_name }} namespace: kube-system spec: replicas: 1 @@ -49,11 +49,11 @@ spec: type: Recreate selector: matchLabels: - app: nfs-client-provisioner + app: {{ nfs_provisioner_name }} template: metadata: labels: - app: nfs-client-provisioner + app: {{ nfs_provisioner_name }} spec: serviceAccountName: nfs-client-provisioner containers: @@ -67,13 +67,21 @@ spec: env: - name: PROVISIONER_NAME # 此处供应者名字供storageclass调用 - value: prov + value: {{ nfs_provisioner_name }} - name: NFS_SERVER - value: {{ storage_server }} + value: {{ nfs_server }} - name: NFS_PATH - value: {{ storage_path }} + value: {{ nfs_server_path }} volumes: - name: nfs-client-root nfs: - server: {{ storage_server }} - path: {{ storage_path }} + server: {{ nfs_server }} + path: {{ nfs_server_path }} + +--- +apiVersion: storage.k8s.io/v1 +kind: StorageClass +metadata: + name: {{ nfs_storage_class }} +provisioner: {{ nfs_provisioner_name }} + diff --git a/tools/init_vars.yml b/tools/init_vars.yml index b732a71bc..31a9c5650 100644 --- a/tools/init_vars.yml +++ b/tools/init_vars.yml @@ -9,6 +9,7 @@ - calico - cluster-addon - cluster-restore + - cluster-storage - flannel - helm - kube-node @@ -24,6 +25,7 @@ - calico - cluster-addon - cluster-restore + - cluster-storage - flannel - helm - kube-node