kubernetes中掛載glusterfs並使用

一、所有k8s節點安裝glusterfs客戶端

安裝客戶端

yum install -y glusterfs glusterfs-fuse

在hosts中加入所有gluster的節點

vim /etc/hosts
172.19.12.193  gluster-manager
172.19.12.194  gluster-node1
172.19.12.195  gluster-node2

二、在kubernetes主節點部署

新建名稱空間

vim portal-ns1.yaml
piVersion: v1
kind: Namespace
metadata:
  name: gcgj-portal

新建endpoints

cd /opt/glusterfs
curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/examples/volumes/glusterfs/glusterfs-endpoints.json
vim glusterfs-endpoints.json
{
  "kind": "Endpoints",
  "apiVersion": "v1",
  "metadata": {
	"name": "glusterfs-cluster",
	"namespace": "gcgj-portal"	#如果後面要調用的pod有ns則一定要寫ns
  },
  "subsets": [
	{
	  "addresses": [
		{
		  "ip": "172.19.12.193"
		}
	  ],
	  "ports": [
		{
		  "port": 1990	#這個端口自己隨便寫
		}
	  ]
	}
  ]
}

kubectl apply -f glusterfs-endpoints.json
kubectl get ep

新建服務

curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/examples/volumes/glusterfs/glusterfs-service.json
vim glusterfs-service.json
{
  "kind": "Service",
  "apiVersion": "v1",
  "metadata": {
	"name": "glusterfs-cluster",
	"namespace": "gcgj-portal"
  },
  "spec": {
	"ports": [
	  {"port": 1990}
	]
  }
}

kubectl apply -f glusterfs-service.json
kubectl get svc

新建glusterfs的pod

curl -O https://raw.githubusercontent.com/kubernetes/kubernetes/master/examples/volumes/glusterfs/glusterfs-pod.json
vim glusterfs-pod.json
{
	"apiVersion": "v1",
	"kind": "Pod",
	"metadata": {
		"name": "glusterfs",
		"namespace": "gcgj-portal"
	},
	"spec": {
		"containers": [
			{
				"name": "glusterfs",
				"image": "nginx",
				"volumeMounts": [
					{
						"mountPath": "/mnt/glusterfs",	#自定義本地掛載glusterfs的目錄
						"name": "glusterfsvol"
					}
				]
			}
		],
		"volumes": [
			{
				"name": "glusterfsvol",
				"glusterfs": {
					"endpoints": "glusterfs-cluster",
					"path": "models",
					"readOnly": true
				}
			}
		]
	}
}


kubectl apply -f glusterfs-pod.json
kubectl get pods

創建pv

vim glusterfs-pv.yaml
apiVersion: v1
kind: PersistentVolume
metadata:
  name: gluster-dev-volume
spec:
  capacity:
	storage: 8Gi	#pv申請的容量大小
  accessModes:
	- ReadWriteMany
  glusterfs:
	endpoints: "glusterfs-cluster"
	path: "models"
	readOnly: false

kubectl apply -f glusterfs-pv.yaml
kubectl get pv

創建pvc

vim glusterfs-pvc.yaml
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: glusterfs-gcgj
  namespace: gcgj-portal
spec:
  accessModes:
	- ReadWriteMany
  resources:
	requests:
	  storage: 8Gi

kubectl apply -f glusterfs-pvc.yaml
kubectl get pvc

新建應用,測試能否正常掛載

cd /opt/kube-gcgj/portal-test
vim portal-rc1.yaml
apiVersion: v1
kind: ReplicationController
metadata:
  name: gcgj-portal
  namespace: gcgj-portal
spec:
  replicas: 1
  selector:
	app: portal
  template:
	metadata:
	  labels:
		app: portal
	spec:
	  containers:
	  - image: 172.19.2.139/gcgj/portal:latest
		name: portal
		resources:
		  limits:
			cpu: "1"
			memory: 2Gi
		ports:
		- containerPort: 8080
		volumeMounts:
		- mountPath: /usr/local/tomcat/logs		#需要掛載的目錄
		  name: gcgj-portal-log			#這裏的名字和下面的volumes的name要一致
	  volumes:
	  - name: gcgj-portal-log
		persistentVolumeClaim:
		  claimName: glusterfs-gcgj		#這裏爲pvc的名字

vim portal-svc1.yaml
apiVersion: v1
kind: Service
metadata:
  name: gcgj-portal
  namespace: gcgj-portal
spec:
  ports:
  - name: portal-svc
	port: 8080
	targetPort: 8080
	nodePort: 30082
  selector:
	app: portal
  type: NodePort

kubectl create -f /opt/kube-gcgj/portal-test

應用啓動後到gluster集羣對應的目錄中查看是否有新日誌生成


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章