<p>Recently when practicing kubernetes , I found there is no doc and example specifically explaining how to use cinder correctly in kubernetes. </p>

<p>So how to setup cinder to be used in kubernetes ? </p>

<p>I did some experiment and worked out how to setup cinder with kubernetes. Just find a suitable to document and share.</p>

<p><strong>Preparation</strong></p>

<ul>
<li>kubernetes cluster </li>
<li>openstack environment and make sure cinder service is available</li>
</ul>

<p><strong>Background</strong></p>

<p>From my investigation, component <code>kube-controller-manager</code> is responsible for loading volume plugins and related in Kubernetes. So we could make cinder available by adjusting <code>kube-controller-manager</code> configuration.</p>

<p><strong>Steps</strong></p>

<ol>
<li><strong>Prepare <code>cloud.conf</code> file to contain your openstack creds</strong></li>
</ol>

<p>Prepare your openstack creds and saved as a file , for example <code>/etc/kubernetes/cloud.conf</code> in kubernetes control panel which <code>kube-controller-manager</code> locates. The following is example for <code>cloud.conf</code></p>

<pre><code>[Global]
auth-url=$your_openstack_auth_url
username=$your_openstack_user
password=$your_user_pw
region=$your_openstack_reigon
tenant-name=$your_project_name
domain-name=$your_domain_name
ca-file=$your_openstack_ca
</code></pre>

<p>Most could be found from your <code>stackrc</code> file. And <code>ca-file</code> item is optional, depending on if your openstack auth url is <code>http</code> or <code>https</code></p>

<ol start="2">
<li><strong>Adjust <code>kube-controller-manager</code> start configuration</strong></li>
</ol>

<p>This link is a full detail options for <code>kube-controller-manager</code> (<a href="https://kubernetes.io/docs/admin/kube-controller-manager/" rel="nofollow noreferrer">https://kubernetes.io/docs/admin/kube-controller-manager/</a>)</p>

<p>Actually we should add two extra parameters based on your current one</p>

<pre><code>--cloud-provider=openstack
--cloud-config=/etc/kubernetes/cloud.conf
</code></pre>

<p>There are mainly two ways to start <code>kube-controller-manager</code> : 1) using systemd 2) using static pod .</p>

<p>Just one tips, if you are using static pod for <code>kube-controller-manager</code> , make sure you have mount all files such as cloud.conf or openstack ca file into your container.</p>

<p><strong>Verification</strong></p>

<p>We will create a storageclass, and use this storageclass to create persistent volume dynamically.</p>

<ol>
<li>Create a storageclass named <code>standard</code>:</li>
</ol>

<p><strong>demo-sc.yml:</strong></p>

<pre><code>apiVersion: storage.k8s.io/v1beta1
kind: StorageClass
metadata:
  name: standard
  annotations:
    storageclass.beta.kubernetes.io/is-default-class: "true"
  labels:
    kubernetes.io/cluster-service: "true"
    addonmanager.kubernetes.io/mode: EnsureExists
provisioner: kubernetes.io/cinder
</code></pre>

<p>Using command <code>kubectl create -f demo-sc.yml</code> to create and using command <code>kubectl get sc</code> to verify if it created correctly</p>

<pre><code>NAME                 TYPE
standard (default)   kubernetes.io/cinder 
</code></pre>

<ol start="2">
<li>Create a PersistentVolumeClaim to use StorageClass provison a Persistent Volume in Cinder:</li>
</ol>

<p><strong>demo-pvc.yml:</strong></p>

<pre><code>kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: cinder-claim
  annotations:
    volume.beta.kubernetes.io/storage-class: "standard"
spec:
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 1Gi
</code></pre>

<p>Creating PVC by <code>kubectl create -f demo-pvc.yml</code></p>

<p>And now checking by command <code>kubectl get pvc</code></p>

<pre><code>NAME           STATUS    VOLUME                                         CAPACITY   ACCESSMODES   STORAGECLASS   AGE
cinder-claim   Bound     pvc-5dd3d62e-9204-11e7-bc43-fa163e0e0379   1Gi          RWO           standard       23h
</code></pre>

<p>And in openstack environment, checking by command <code>cinder list | grep pvc-5dd3d62e-9204-11e7-bc43-fa163e0e0379</code></p>

<pre><code>    root@ds0114:~# cinder list | grep pvc-5dd3d62e-9204-11e7-bc43- fa163e0e0379
| ddd8066d-2e16-4cb2-a89e-cd9d5b99ef1b | available | kubernetes-dynamic- pvc-5dd3d62e-9204-11e7-bc43-fa163e0e0379 |  1   |   CEPH_SSD  |  false   |                                       |
</code></pre>

<p>So now StorageClass is working well using Cinder in Kubernetes.</p>
