k8s 挂载阿里云 oss
创始人
2024-02-12 18:05:12
0

一、准备工作
1、创建一个k8s集群,搭建步骤参见我的另外一篇博文 k8s搭建文档

[root@kubernetes-master ~]# kubectl get nodes
NAME                STATUS   ROLES                  AGE    VERSION
kubernetes-master   Ready    control-plane,master   152d   v1.23.4
kubernetes-node1    Ready                     152d   v1.23.4
kubernetes-node2    Ready                     152d   v1.23.4

1.2、阿里云oss账号,并创建bucket。这个没什么好说的,参考阿里云官方文档

1.3、在每台机器上安装阿里云ossfs软件。这个软件必须要安装,因为pv/pvc如果想要用阿里云的oss的话,这是必须的软件。安装步骤参见阿里云oss安装文档,我的机器是三节点centos,下边我就写一下centos 该怎么安装ossfs。

# 下载安装包
wget https://gosspublic.alicdn.com/ossfs/ossfs_1.80.6_centos7.0_x86_64.rpm
yum install ossfs_1.80.6_centos7.0_x86_64.rpm

二、yaml文件准备
2.1、rbac.yaml

# This YAML file contains all RBAC objects that are necessary to run external
# CSI provisioner.
#
# In production, each CSI driver deployment has to be customized:
# - to avoid conflicts, use non-default namespace and different names
#   for non-namespaced entities like the ClusterRole
# - decide whether the deployment replicates the external CSI
#   provisioner, in which case leadership election must be enabled;
#   this influences the RBAC setup, see belowapiVersion: v1
kind: ServiceAccount
metadata:name: admin# replace with the same namespace name with pluginnamespace: kube-system---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: alicloud-csi-plugin
rules:- apiGroups: [""]resources: ["secrets"]verbs: ["get", "list"]- apiGroups: [""]resources: ["persistentvolumes"]verbs: ["get", "list", "watch", "update", "create", "delete"]- apiGroups: [""]resources: ["persistentvolumeclaims"]verbs: ["get", "list", "watch", "update"]- apiGroups: ["storage.k8s.io"]resources: ["storageclasses"]verbs: ["get", "list", "watch"]- apiGroups: ["storage.k8s.io"]resources: ["csinodes"]verbs: ["get", "list", "watch"]- apiGroups: [""]resources: ["events"]verbs: ["get", "list", "watch", "create", "update", "patch"]- apiGroups: [""]resources: ["endpoints"]verbs: ["get", "watch", "list", "delete", "update", "create"]- apiGroups: [""]resources: ["configmaps"]verbs: ["get", "watch", "list", "delete", "update", "create"]- apiGroups: [""]resources: ["nodes"]verbs: ["get", "list", "watch"]- apiGroups: ["csi.storage.k8s.io"]resources: ["csinodeinfos"]verbs: ["get", "list", "watch"]- apiGroups: ["storage.k8s.io"]resources: ["volumeattachments"]verbs: ["get", "list", "watch", "update"]- apiGroups: ["snapshot.storage.k8s.io"]resources: ["volumesnapshotclasses"]verbs: ["get", "list", "watch"]- apiGroups: ["snapshot.storage.k8s.io"]resources: ["volumesnapshotcontents"]verbs: ["create", "get", "list", "watch", "update", "delete"]- apiGroups: ["snapshot.storage.k8s.io"]resources: ["volumesnapshots"]verbs: ["get", "list", "watch", "update"]- apiGroups: ["apiextensions.k8s.io"]resources: ["customresourcedefinitions"]verbs: ["create", "list", "watch", "delete"]---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:name: alicloud-csi-plugin
subjects:- kind: ServiceAccountname: adminnamespace: kube-system
roleRef:kind: ClusterRolename: alicloud-csi-pluginapiGroup: rbac.authorization.k8s.io

2.2、oss-plugin.yaml

apiVersion: storage.k8s.io/v1beta1
kind: CSIDriver
metadata:name: ossplugin.csi.alibabacloud.com
spec:attachRequired: false
---
# This YAML defines all API objects to create RBAC roles for csi node plugin.
kind: DaemonSet
apiVersion: apps/v1
metadata:name: csi-osspluginnamespace: kube-system
spec:selector:matchLabels:app: csi-ossplugintemplate:metadata:labels:app: csi-osspluginspec:tolerations:- operator: ExistspriorityClassName: system-node-criticalserviceAccount: adminhostNetwork: truehostPID: truecontainers:- name: driver-registrarimage: registry.cn-hangzhou.aliyuncs.com/acs/csi-node-driver-registrar:v1.1.0imagePullPolicy: Alwayslifecycle:preStop:exec:command: ["/bin/sh", "-c", "rm -rf /registration/ossplugin.csi.alibabacloud.com /registration/ossplugin.csi.alibabacloud.com-reg.sock"]args:- "--v=5"- "--csi-address=/var/lib/kubelet/plugins/ossplugin.csi.alibabacloud.com/csi.sock"- "--kubelet-registration-path=/var/lib/kubelet/plugins/ossplugin.csi.alibabacloud.com/csi.sock"env:- name: KUBE_NODE_NAMEvalueFrom:fieldRef:apiVersion: v1fieldPath: spec.nodeNamevolumeMounts:- name: kubelet-dirmountPath: /var/lib/kubelet/- name: registration-dirmountPath: /registration- name: csi-osspluginsecurityContext:privileged: truecapabilities:add: ["SYS_ADMIN"]allowPrivilegeEscalation: trueimage: registry.cn-hangzhou.aliyuncs.com/acs/csi-plugin:v1.14.8.32-c77e277b-aliyunimagePullPolicy: "Always"args:- "--endpoint=$(CSI_ENDPOINT)"- "--v=5"- "--driver=ossplugin.csi.alibabacloud.com"- "--nodeid=$(KUBE_NODE_NAME)"env:- name: CSI_ENDPOINTvalue: unix://var/lib/kubelet/plugins/ossplugin.csi.alibabacloud.com/csi.sock- name: KUBE_NODE_NAMEvalueFrom:fieldRef:fieldPath: spec.nodeNamevolumeMounts:- name: kubelet-dirmountPath: /var/lib/kubelet/mountPropagation: "Bidirectional"- name: etcmountPath: /host/etc- mountPath: /var/log/name: host-log- mountPath: /host/usr/name: flexvolumedirvolumes:- name: kubelet-dirhostPath:path: /var/lib/kubelet/type: Directory- name: registration-dirhostPath:path: /var/lib/kubelet/plugins_registrytype: DirectoryOrCreate- name: etchostPath:path: /etc- name: flexvolumedirhostPath:path: /usr/- name: host-loghostPath:path: /var/log/updateStrategy:type: RollingUpdate

2.3、pv.yaml

apiVersion: v1
kind: PersistentVolume
metadata:name: oss-csi-pvlabels:alicloud-pvname: oss-csi-pv
spec:capacity:storage: 5GiaccessModes:- ReadWriteOncepersistentVolumeReclaimPolicy: Retaincsi:driver: ossplugin.csi.alibabacloud.com# set volumeHandle same value pv namevolumeHandle: oss-csi-pvvolumeAttributes:bucket: "*****" #重要url: "******" #重要otherOpts: "-o max_stat_cache_size=0 -o allow_other"akId: "****" #重要akSecret: "*******" #重要path: "/"

说明:

  • bucket:目前只支持挂载Bucket,不支持挂载Bucket下面的子目录或文件。
  • url:OSS endpoint,挂载OSS的接入域名,挂载节点和bucket相同region时,可使用内网地址。
  • akId:用户的access id值。
  • akSecret:用户的access secret值。
  • otherOpts:挂载OSS时支持定制化参数输入,格式为:-o *** -o ***。

2.4、pvc.yaml

apiVersion: v1
kind: PersistentVolumeClaim
metadata:name: oss-pvc
spec:accessModes:- ReadWriteOnceresources:requests:storage: 5Giselector:matchLabels:alicloud-pvname: oss-csi-pv

2.5、deploy.yaml

apiVersion: apps/v1
kind: Deployment
metadata:name: deployment-osslabels:app: nginx
spec:selector:matchLabels:app: nginxtemplate:metadata:labels:app: nginxspec:containers:- name: nginximage: nginx:1.7.9ports:- containerPort: 80volumeMounts:- name: oss-pvcmountPath: "/data"volumes:- name: oss-pvcpersistentVolumeClaim:claimName: oss-pvc

三、部署服务

#创建rbac权限
$ kubectl create -f ./rbac.yaml 
serviceaccount/admin created
clusterrole.rbac.authorization.k8s.io/alicloud-csi-plugin created
clusterrolebinding.rbac.authorization.k8s.io/alicloud-csi-plugin created#创建oss-plugin
$ kubectl create -f ./oss-plugin.yaml#检查创建情况
$ kubectl get pod -n kube-system | grep csi-oss
kube-system             csi-ossplugin-9jdhw                                  2/2     Running             0          55m
kube-system             csi-ossplugin-f7n5f                                  2/2     Running             0          55m
kube-system             csi-ossplugin-vgkcp                                  2/2     Running             0          55m#查验CSIDriver安装情况
$ kubectl get CSIDriver
NAME                             CREATED AT
ossplugin.csi.alibabacloud.com   2020-06-23T14:48:18Z#创建pv
$ kubectl create -f ./pv.yaml#创建pvc
$ kubectl create -f ./pvc.yaml#检验一下阿里云oss是否可以成功挂载到k8s集群中做pv使用
$ kubectl create -f ./deploy.yaml

四、验证

$ kubectl get pod
NAME                              READY   STATUS              RESTARTS   AGE
deployment-oss-795894886d-lhpsx   1/1     Running             0          11h#pod成功后通过kubectl exec 进入到pod中,你能看到你账号下bucket里边的所有文件。样例如下:
$ kubectl exec -it deployment-oss-795894886d-lhpsx -- sh
$ ls
bin  boot  data  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  selinux  srv  sys	tmp  usr  var
$ cd data	
$ ls
osstest

五、参考文档
5.1、 K8S有状态服务-OSS存储使用最佳实践
5.2、 阿里云oss CSI安装步骤
5.3、 阿里云oss官方文档

相关内容

热门资讯

怎么才能让鹦鹉说话? 怎么才能让鹦鹉说话?不是所有鹦鹉都会说话的,虎皮 牡丹 玄凤 之类的就不会说话,虽然有些资料上介绍 ...
孙悟空在南瞻部州被称呼为什么? 孙悟空在南瞻部州被称呼为什么?孙悟空在南瞻部州被称为孙大圣。他的称呼是比较威武凶猛的。因为他的人物形...
“每一个晚上”英语怎么讲 “每一个晚上”英语怎么讲every night
我在红袖写了一篇小说,只有签约... 我在红袖写了一篇小说,只有签约,没有上架,请问怎么样才能拿到稿费,我是签网协的每天都有更新的话。。达...
王朔:这辈子最对不起老婆和女儿... 王朔:这辈子最对不起老婆和女儿,但我死后财产全归徐静蕾,这是为何呢?因为在这个人的心中,徐静蕾才是自...
游戏《天地玄门》激活账号的激活... 游戏《天地玄门》激活账号的激活码怎么写?那个游戏得发短信到腾武才会获得激活码
孩子课后作业辅导,如何让老公平... 孩子课后作业辅导,如何让老公平心静气的教孩子?可以告诉老公,孩子的辅导需要老公有耐心一些,因为孩子犯...
《阿姨暖暖你的手》阅读题答案 《阿姨暖暖你的手》阅读题答案1、“春寒料峭”和“春暖花开”都是形容春天的词语。文中第(2)段中的“春...
想找到一本有声小说,喜马拉雅有... 想找到一本有声小说,喜马拉雅有,但是我忘记名字了。讲的是茅山道士的故事,但不是紫襟讲的,我问过了?书...
郝邵文合释小龙都演过什么电影我... 郝邵文合释小龙都演过什么电影我想知道,要全的1、笑林小子(又名:旋风小子)2、笑林小子2之乌龙院3、...
林黛玉放屁拉肚子 林黛玉放屁拉肚子林黛玉放屁拉肚子是《红楼梦》中的一个情节。在小说中,林黛玉因为情绪低落和身体不适而出...
克雷洛夫寓言有哪些寓言故事?故... 克雷洛夫寓言有哪些寓言故事?故事的主要内容及道理是什么?最起码说出3个,克雷洛夫寓言有哪些寓言故事?...
请告诉我一点笑话! 请告诉我一点笑话!告诉我一点笑话便可!如此美丽夜晚孤独是一种遗憾,想念着得不到的爱情已经成为习惯,情...
樱花、新年、任性、武士、爆破、... 樱花、新年、任性、武士、爆破、缇娜、武媚娘、土豪biangbiang面、萌萌哒、小鲜肉、醉了 用以上...
东北出马仙堂怎么写 东北出马仙堂怎么写去找高人查看 ,看看是否具备出马缘分,出马仙的堂单一般都是仙 或者给你立堂的师傅给...
探访鹿邑:漫步古城,寻迹历史名... 探访鹿邑:漫步古城,寻迹历史名人之路鹿邑是一个历史悠久的古城,有着丰富的文化底蕴和历史遗迹。探访鹿邑...
类似与儒道至圣的小说 类似与儒道至圣的小说[剑三+文修]自古名士真国色[系统]国士无双
星际争霸2人族怎么防隐形?怎么... 星际争霸2人族怎么防隐形?怎么看隐形?:轨道控制基地的雷达,防空塔和渡鸦自带的反隐效果人族有4招防隐...
谁有《从零开始学画漫画》电子书... 谁有《从零开始学画漫画》电子书 谢谢亲,去狗狗书籍呗,你去新浪爱问搜搜,应该能找到》
公主日记1里有首笨爱神的歌叫什... 公主日记1里有首笨爱神的歌叫什么名字stupid cupidstupid cupidstupid c...