Kubernetes basics#

Your Kubernetes cluster supports basic Kubernetes resources, such as deployments and services. This section provides examples of the following basic operations for an Nginx Kubernetes deployment with the LoadBalancer service:

For more information about the Kubernetes objects, see the Kubernetes documentation.

Launch a new deployment#

Create a deployment called nginx using the official Nginx image with five replicas.

  1. Create a deployment:

    $ kubectl run nginx --image nginx --replicas 5
      deployment "nginx" created
  2. Verify by displaying the list of pods:

    $ kubectl get pods
      NAME                     READY     STATUS    RESTARTS   AGE
      nginx-2371676037-9ktzl   1/1       Running   0          27s
      nginx-2371676037-kdm13   1/1       Running   0          27s
      nginx-2371676037-l2x0c   1/1       Running   0          27s
      nginx-2371676037-tl21g   1/1       Running   0          27s
      nginx-2371676037-vwpr0   1/1       Running   0          27s
    $ kubectl get deployments nginx
    $ kubectl describe deployments nginx

Expose your deployment with a load balancer#

Expose the Nginx deployment by using the Kubernetes LoadBalancer service.

  1. Create a LoadBalancer service object by running the following command:

    $ kubectl expose deployment/nginx --port=80 --type=LoadBalancer
      service "nginx" exposed

    Kubernetes creates a load balancer that directs all traffic on port 80 to one of the five Nginx replicas.

  2. Verify that the service is created by running the following command:

    $ kubectl get service nginx
      NAME         CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
      nginx   148.62.x.x    80:32350/TCP   49s

Access your deployment#

To access your deployment, use the EXTERNAL-IP from the preceding Nginx service in a web browser or use the following cURL command with your EXTERNAL-IP and port:

$ curl http://<external-ip>:<port>

Add persistent storage to your deployment#

Rackspace KaaS enables you to add persistent storage to your pods so that you can run stateful applications. You can add a PersistentVolume by using a StorageClass and mounting the volume to a storage server, or by creating a hostPath PersistentVolume and mounting it to your Kubernetes master node storage. We support the latter, but because Kubernetes cannot remount the data volume and recover from node failures that might trigger application outages, we do not recommend that you create a hostPath PersistentVolume. However, if you use distributed, replicated, or ephemeral systems, such as Kafka or Redis, you might be able to use host local storage because the data is replicated within these systems and does not require Kubernetes to remount or recover the failed nodes. In other cases,use the default StorageClass for the OpenStack Block Storage service (cinder), which is backed by Ceph.

To add a persistent volume to a pod, you need to create a PersistentVolumeClaim (PVC). Because your Kubernetes cluster already has a default StorageClass, which is used by default, you do not need to specify it in the PVC.

Rackspace KaaS supports the following access modes to persistent volumes:

  • ReadWriteOnce (RWO): You can mount a volume with read and write access to a single node.
  • ReadOnlyMany (ROM): You can mount a volume with read-only permissions to many nodes.


Racksapce KaaS does not support automated volume expansion by using the allowVolumeExpansion parameter or PersistentVolumeClaimResize.

Add persistent storage example#

To add persistent storage to your cluster, use the following steps:

  1. Create a configuration file for the PVC with the following code:

    kind: PersistentVolumeClaim
    apiVersion: v1
      name: pvc-test
        - ReadWriteOnce
          storage: 32Gi


    You cannot specify a volumeName in the PVC. Instead, Rackspace KaaS assigns a name to the PersistentVolume (PV).

  2. Create the PersistentVolumeClaim by running the following command:

    kubectl create -f deployments/stable/storage/pvc-test1.yaml
  3. Verify that the claim was created by displaying the list of PVCs in the cluster by running the following command:

    kubectl get pvc

    Example of system response:

    NAME        STATUS    VOLUME                                     CAPACITY   ACCESS MODES   STORAGECLASS   AGE
    pvc-test    Bound     pvc-02a20856-6f39-11e8-b07f-fa163e9c3664   32Gi       RWO            openstack      3

    The status must be Bound. If you see the status set to Pending, your configuration file might be incorrect.

  4. Open an existing pod file for editing or create a deployment configuration file for your stateful application with the following code:


    apiVersion: apps/v1
    kind: Deployment
      name: mysql
          app: mysql
        type: Recreate
            app: mysql
          - image: mysql:5.6
            name: mysql
              # Use secret in real usage
            - name: MYSQL_ROOT_PASSWORD
              value: password
            - containerPort: 3306
              name: mysql
            - name: mysql-persistent-storage
              mountPath: /var/lib/mysql
          - name: mysql-persistent-storage
              claimName: pvc-test
  5. Verify that claimName is set to the claim that you created in previous steps.

  6. Create the pod or apply the changes to the existing pod by running one of the following commands:

    kubectl apply -f deployments/mysql-test.yaml

    System response:

    deployment "mysql" created
  7. Verify that the MySQL pod was created by running the following command:

    kubectl get pods

    Example of system response:

    NAME                     READY     STATUS              RESTARTS   AGE
    mysql-5f7dcd7b68-2rfx2   1/1       Running             0          1m
  8. Verify that the deployment uses the created persistent volume by running the following command:

    kubectl describe pod mysql-5f7dcd7b68-m97s9

    Example of system response:

    Name:           mysql-5f7dcd7b68-m97s9
    Namespace:      default
    Node:           kubernetes-test-worker-1/
    Start Time:     Wed, 13 Jun 2018 13:59:18 -0700
    Labels:         app=mysql
    Annotations:    <none>
    Status:         Running
    Controlled By:  ReplicaSet/mysql-5f7dcd7b68
     Container ID:   docker://4e22406c32cb2004ec32ffef1f2ca82630d4028027a21324460317afb440f857
     Image:          mysql:5.6
     Image ID:       docker-pullable://mysql@sha256:7e3575b7949a4f8712a47b0fc875022dcea5d84f4672b1532587b6933502f757
     Port:           3306/TCP
     State:          Running
       Started:      Wed, 13 Jun 2018 13:59:33 -0700
     Ready:          True
     Restart Count:  0
       MYSQL_ROOT_PASSWORD:  password
       /var/lib/mysql from mysql-persistent-storage (rw)
       /var/run/secrets/kubernetes.io/serviceaccount from default-token-szjzp (ro)
    Type           Status
    Initialized    True
    Ready          True
    PodScheduled   True
     Type:       PersistentVolumeClaim (a reference to a PersistentVolumeClaim in the same namespace)
     ClaimName:  pvc-test1
     ReadOnly:   false
     Type:        Secret (a volume populated by a Secret)
     SecretName:  default-token-szjzp
     Optional:    false

Use local storage#

While Kubernetes supports adding a persistent volume that uses storage space from a Kubernetes node, we do not recommend that you use such volumes in production environments. Instead, create a PersistentVolumeClaim by using the default StorageClass as described in Add persistent storage to your deployment. If you still decide to use storage on a Kubernetes worker node by creating a hostPath volume, you must verify that the node has sufficient disk space. You cannot resize this volume.

To verify storage space that is used by a pod on a Kubernetes node, run the following commands:

  1. Get the location of the storage by obtaining the information about the pod mount point:

    kubectl describe pod <pod-name>

    Example of system response:

      /usr/share/nginx/html from pv-volume (rw)
  2. Get the information about available disk space:

    kubectl exec -it <pod-name> -- /bin/bash -c 'df /usr/share/nginx/html'

    Example of system response:

    Filesystem     1K-blocks    Used Available Use% Mounted on
    /dev/vda9       99171716 9720480  85276368  11% /usr/share/nginx/html
  3. Create a hostPath persistent volume as described in the Kubernetes documentation.

Scale replicas for your deployment#

You can scale your Nginx deployment by increasing the number of replicas in the deployment by use the following steps:

  1. Increase the number of replicas by running the following command:

    $ kubectl scale deployment/nginx --replicas=10
      deployment "nginx" scaled
  2. View the status of the deployment update by running the following command:

    $ kubectl rollout status deployment/nginx

    System response:

    deployment "nginx" successfully rolled out
  3. Verify by displaying the list of pods by running the following command:

    $ kubectl get pods
      NAME                     READY     STATUS    RESTARTS   AGE
      nginx-2371676037-9ktzl   1/1       Running   0          11m
      nginx-2371676037-k22cm   1/1       Running   0          2m
      nginx-2371676037-kdm13   1/1       Running   0          11m
      nginx-2371676037-l2x0c   1/1       Running   0          11m
      nginx-2371676037-qzqh9   1/1       Running   0          2m
      nginx-2371676037-tl21g   1/1       Running   0          11m
      nginx-2371676037-tvkgz   1/1       Running   0          2m
      nginx-2371676037-vwpr0   1/1       Running   0          11m
      nginx-2371676037-ww7f7   1/1       Running   0          2m
      nginx-2371676037-xjkq9   1/1       Running   0          2m

Clean up the deployment#

When you finish this Nginx example, you can delete the deployment and service objects by running the following commands:

$ kubectl delete service nginx
$ kubectl delete deployment nginx