Kubernetes Complete Example Using GlusterFS

By | April 9, 2017


This topic provides an end-to-end example of how to use an existing Gluster cluster as an OpenShift Origin persistent store. It is assumed that a working Gluster cluster is already set up. If not, consult the Red Hat Gluster Storage Administration Guide.

Persistent Storage Using GlusterFS provides an explanation of persistent volumes (PVs), persistent volume claims (PVCs), and using GlusterFS as persistent storage.

For an end-to-end example of how to dynamically provision GlusterFS volumes, see Complete Example of Dynamic Provisioning Using GlusterFS. The persistent volume (PV) and endpoints are both created dynamically by GlusterFS.

All oc …​ commands are executed on the OpenShift Origin master host.

Installing the glusterfs-fuse Package

The glusterfs-fuse library must be installed on all schedulable OpenShift Origin nodes:

# yum install -y glusterfs-fuse

The OpenShift Origin all-in-one host is often not used to run pod workloads and, thus, is not included as a schedulable node.

Creating the Gluster Endpoints and Gluster Service for Persistence

The named endpoints define each node in the Gluster-trusted storage pool:

Example 1. GlusterFS Endpoint Definition
apiVersion: v1
kind: Endpoints
  name: gluster-cluster 
- addresses:              
  - ip:
  - port: 1
    protocol: TCP
- addresses:
  - ip:
  - port: 1
    protocol: TCP
The endpoints name. If using a service, then the endpoints name must match the service name.
An array of IP addresses for each node in the Gluster pool. Currently, host names are not supported.
The port numbers are ignored, but must be legal port numbers. The value 1 is commonly used.

Save the endpoints definition to a file, for example gluster-endpoints.yaml, then create the endpoints object:

# oc create -f gluster-endpoints.yaml
endpoints "gluster-cluster" created

Verify that the endpoints were created:

# oc get endpoints gluster-cluster
NAME                ENDPOINTS                           AGE
gluster-cluster,   1m

To persist the Gluster endpoints, you also need to create a service.

Endpoints are name-spaced. Each project accessing the Gluster volume needs its own endpoints.

Example 2. GlusterFS Service Definition
apiVersion: v1
kind: Service
  name: gluster-cluster 
  - port: 1  
The name of the service. If using a service, then the endpoints name must match the service name.
The port should match the same port used in the endpoints.

Save the service definition to a file, for example gluster-service.yaml, then create the endpoints object:

# oc create -f gluster-service.yaml
endpoints "gluster-cluster" created

Verify that the service was created:

# oc get service gluster-cluster
gluster-cluster   <none>        1/TCP     9s

Creating the Persistent Volume

Next, before creating the PV object, define the persistent volume in OpenShift Origin:

Example 3. Persistent Volume Object Definition Using GlusterFS
apiVersion: v1
kind: PersistentVolume
  name: gluster-pv   
    storage: 1Gi     
  - ReadWriteMany    
    endpoints: gluster-cluster 
    path: /HadoopVol 
    readOnly: false
  persistentVolumeReclaimPolicy: Retain  
The name of the PV, which is referenced in pod definitions or displayed in various oc volume commands.
The amount of storage allocated to this volume.
accessModes are used as labels to match a PV and a PVC. They currently do not define any form of access control.
This defines the volume type being used. In this case, the glusterfs plug-in is defined.
This references the endpoints named above.
This is the Gluster volume name, preceded by /.
A volume reclaim policy of retain indicates that the volume will be preserved after the pods accessing it terminate. Accepted values include Retain, Delete, and Recycle.

Save the PV definition to a file, for example gluster-pv.yaml, and create the persistent volume:

# oc create -f gluster-pv.yaml
persistentvolume "gluster-pv" created

Verify that the persistent volume was created:

# oc get pv
gluster-pv   <none>    1Gi        RWX           Available                       37s

Creating the Persistent Volume Claim

A persistent volume claim (PVC) specifies the desired access mode and storage capacity. Currently, based on only these two attributes, a PVC is bound to a single PV. Once a PV is bound to a PVC, that PV is essentially tied to the PVC’s project and cannot be bound to by another PVC. There is a one-to-one mapping of PVs and PVCs. However, multiple pods in the same project can use the same PVC.

Example 4. PVC Object Definition
apiVersion: v1
kind: PersistentVolumeClaim
  name: gluster-claim  
  - ReadWriteMany      
       storage: 1Gi     
The claim name is referenced by the pod under its volumes section.
As mentioned above for PVs, the accessModes do not enforce access rights, but rather act as labels to match a PV to a PVC.
This claim will look for PVs offering 1Gi or greater capacity.

Save the PVC definition to a file, for example gluster-claim.yaml, and create the PVC:

# oc create -f gluster-claim.yaml
persistentvolumeclaim "gluster-claim" created

Verify the PVC was created and bound to the expected PV:

# oc get pvc
gluster-claim   <none>    Bound     gluster-pv   1Gi        RWX           24s
The claim was bound to the gluster-pv PV.

Defining GlusterFS Volume Access

Access is necessary to a node in the Gluster-trusted storage pool. On this node, examine the glusterfs-fuse mount:

# ls -lZ /mnt/glusterfs/
drwxrwx---. yarn hadoop system_u:object_r:fusefs_t:s0    HadoopVol

# id yarn
uid=592(yarn) gid=590(hadoop) groups=590(hadoop)
The owner has ID 592.
The group has ID 590.

In order to access the HadoopVol volume, the container must match the SELinux label, and either run with a UID of 592, or with 590 in its supplemental groups. It is recommended to gain access to the volume by matching the Gluster mount’s groups, which is defined in the pod definition below.

By default, SELinux does not allow writing from a pod to a remote Gluster server. To enable writing to GlusterFS volumes with SELinux enforcing on each node, run:

# setsebool -P virt_sandbox_use_fusefs on

The virt_sandbox_use_fusefs boolean is defined by the docker-selinux package. If you get an error saying it is not defined, ensure that this package is installed.

Creating the Pod using NGINX Web Server image

A pod definition file or a template file can be used to define a pod. Below is a pod specification that creates a single container and mounts the Gluster volume for read-write access:

The NGINX image may require to run in privileged mode to create the mount and run properly. An easy way to accomplish this is to simply add your user to the privileged Security Context Constraint (SCC):

$ oadm policy add-scc-to-user privileged myuser

Then, add the privileged: true to the containers securityContext: section of the YAML file (as seen in the example below).

Managing Security Context Constraints provides additional information regarding SCCs.

Example 5. Pod Object Definition using NGINX image
apiVersion: v1
kind: Pod
  name: gluster-pod1
    name: gluster-pod1   
  - name: gluster-pod1
    image: nginx       
    - name: web
      containerPort: 80
      privileged: true
    - name: gluster-vol1 
      mountPath: /usr/share/nginx/html 
      readOnly: false
    supplementalGroups: [590]       
  - name: gluster-vol1   
      claimName: gluster-claim       
The name of this pod as displayed by oc get pod.
The image run by this pod. In this case, we are using a standard NGINX image.
The name of the volume. This name must be the same in both the containers and volumes sections.
The mount path as seen in the container.
The SupplementalGroup ID (Linux Groups) to be assigned at the pod level and as discussed this should match the POSIX permissions on the Gluster volume.
The PVC that is bound to the Gluster cluster.

Save the pod definition to a file, for example gluster-pod1.yaml, and create the pod:

# oc create -f gluster-pod1.yaml
pod "gluster-pod1" created

Verify the pod was created:

# oc get pod
gluster-pod1   1/1       Running   0          31s

After a minute or so, the pod will be in the Running state.

More details are shown in the oc describe pod command:

# oc describe pod gluster-pod1
Name:			gluster-pod1
Namespace:		default  
Security Policy:	privileged
Node:			ose1.rhs/
Start Time:		Wed, 24 Aug 2016 12:37:45 -0400
Labels:			name=gluster-pod1
Status:			Running
Controllers:		<none>
    Container ID:	docker://e67ed01729e1dc7369c5112d07531a27a7a02a7eb942f17d1c5fce32d8c31a2d
    Image:		nginx
    Image ID:		docker://sha256:4efb2fcdb1ab05fb03c9435234343c1cc65289eeb016be86193e88d3a5d84f6b
    Port:		80/TCP
    State:		Running
      Started:		Wed, 24 Aug 2016 12:37:52 -0400
    Ready:		True
    Restart Count:	0
    Volume Mounts:
      /usr/share/nginx/html/test from glustervol (rw)
      /var/run/secrets/kubernetes.io/serviceaccount from default-token-1n70u (ro)
    Environment Variables:	<none>
  Type		Status
  Initialized 	True
  Ready 	True
  PodScheduled 	True
    Type:	PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
    ClaimName:	gluster-claim  
    ReadOnly:	false
    Type:	Secret (a volume populated by a Secret)
    SecretName:	default-token-1n70u
QoS Tier:	BestEffort
  FirstSeen	LastSeen	Count	From			SubobjectPath			Type		Reason		Message
  ---------	--------	-----	----			-------------			--------	------		-------
  10s		10s		1	{default-scheduler }					Normal		Scheduled	Successfully assigned gluster-pod1 to ose1.rhs
  9s		9s		1	{kubelet ose1.rhs}	spec.containers{gluster-pod1}	Normal		Pulling		pulling image "nginx"
  4s		4s		1	{kubelet ose1.rhs}	spec.containers{gluster-pod1}	Normal		Pulled		Successfully pulled image "nginx"
  3s		3s		1	{kubelet ose1.rhs}	spec.containers{gluster-pod1}	Normal		Created		Created container with docker id e67ed01729e1
  3s		3s		1	{kubelet ose1.rhs}	spec.containers{gluster-pod1}	Normal		Started		Started container with docker id e67ed01729e1
The project (namespace) name.
The IP address of the OpenShift Origin node running the pod.
The PVC name used by the pod.
The list of events resulting in the pod being launched and the Gluster volume being mounted.

There is more internal information, including the SCC used to authorize the pod, the pod’s user and group IDs, the ⁠SELinux label, and more shown in the oc get pod <name> -o yaml command:

# oc get pod gluster-pod1 -o yaml
apiVersion: v1
kind: Pod
    openshift.io/scc: privileged  
  creationTimestamp: 2016-08-24T16:37:45Z
    name: gluster-pod1
  name: gluster-pod1
  namespace: default  
  resourceVersion: "482"
  selfLink: /api/v1/namespaces/default/pods/gluster-pod1
  uid: 15afda77-6a19-11e6-aadb-525400f7256d
  - image: nginx
    imagePullPolicy: Always
    name: gluster-pod1
    - containerPort: 80
      name: web
      protocol: TCP
    resources: {}
      privileged: true  
    terminationMessagePath: /dev/termination-log
    - mountPath: /usr/share/nginx/html
      name: glustervol
    - mountPath: /var/run/secrets/kubernetes.io/serviceaccount
      name: default-token-1n70u
      readOnly: true
  dnsPolicy: ClusterFirst
  host: ose1.rhs
  - name: default-dockercfg-20xg9
  nodeName: ose1.rhs
  restartPolicy: Always
    - 590   
  serviceAccount: default
  serviceAccountName: default
  terminationGracePeriodSeconds: 30
  - name: glustervol
      claimName: gluster-claim  
  - name: default-token-1n70u
      secretName: default-token-1n70u
  - lastProbeTime: null
    lastTransitionTime: 2016-08-24T16:37:45Z
    status: "True"
    type: Initialized
  - lastProbeTime: null
    lastTransitionTime: 2016-08-24T16:37:53Z
    status: "True"
    type: Ready
  - lastProbeTime: null
    lastTransitionTime: 2016-08-24T16:37:45Z
    status: "True"
    type: PodScheduled
  - containerID: docker://e67ed01729e1dc7369c5112d07531a27a7a02a7eb942f17d1c5fce32d8c31a2d
    image: nginx
    imageID: docker://sha256:4efb2fcdb1ab05fb03c9435234343c1cc65289eeb016be86193e88d3a5d84f6b
    lastState: {}
    name: gluster-pod1
    ready: true
    restartCount: 0
        startedAt: 2016-08-24T16:37:52Z
  phase: Running
  startTime: 2016-08-24T16:37:45Z
The SCC used by the pod.
The project (namespace) name.
The security context level requested, in this case privileged
The supplemental group ID for the pod (all containers).
The PVC name used by the pod.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.