title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
20:601 | Using cloud storage: Not being tied to a physical cluster node, cloud storage can be associated permanently with specific Pod instances of StatefulSets. Cloud storage is discussed in the Redis and Azure storage accounts sections of Chapter 12. |
20:602 | |
20:603 | Since access to external databases doesn’t require any Kubernetes-specific techniques but can be done with the usual connection strings, we will concentrate on cloud storage. |
20:604 | Kubernetes offers an abstraction of storage called PersistentVolumeClaim (PVC) that is independent of the underlying storage provider. More specifically, PVCs are allocation requests that are either matched to predefined resources or allocated dynamically. When the Kubernetes cluster is in the cloud, typically, you use dynamic allocation carried out by dynamic providers installed by the cloud provider. For more information on cloud storage, please refer to Chapter 12. |
20:605 | Cloud providers such as Azure offer different storage classes with different performance and different costs. Moreover, the PVC can also specify the accessMode, which can be: |
20:606 | |
20:607 | ReadWriteOnce: The volume can be mounted as read-write by a single Pod. |
20:608 | ReadOnlyMany: The volume can be mounted as read-only by many Pods. |
20:609 | ReadWriteMany: The volume can be mounted as read-write by many Pods. |
20:610 | |
20:611 | Volume claims can be added to StatefulSets in a specific spec->volumeClaimTemplates object: |
20:612 | volumeClaimTemplates: |
20:613 | - metadata: |
20:614 | name: my-claim-template-name |
20:615 | spec: |
20:616 | resources: |
20:617 | request: |
20:618 | storage: 5Gi |
20:619 | volumeMode: Filesystem |
20:620 | accessModes: |
20:621 | - ReadWriteOnce |
20:622 | storageClassName: my-optional-storage-class |
20:623 | |
20:624 | The storage property contains the storage requirements. volumeMode set to Filesystem is a standard setting that means the storage will be available as a file path. The other possible value is Block, which allocates the memory as unformatted. storageClassName must be set to an existing storage class offered by the cloud provider. If it’s omitted, the default storage class will be assumed. |
20:625 | All available storage classes can be listed with the following command: |
20:626 | kubectl get storageclass |
20:627 | |
20:628 | Once volumeClaimTemplates has defined how to create permanent storage, then each container must specify which file path to attach that permanent storage to in the spec->containers->volumeMounts property: |
20:629 | ... |
20:630 | volumeMounts |
20:631 | - name: my-claim-template-name |
20:632 | mountPath: /my/requested/storage |
20:633 | readOnly: false |
20:634 | ... |
20:635 | |
20:636 | Here, name must correspond to the name given to the PVC. |
20:637 | The following subsection shows how to use Kubernetes secrets. |
20:638 | Kubernetes secrets |
20:639 | Some data, such as passwords and connection strings, cannot be exposed but need to be protected by some kind of encryption. Kubernetes handles private sensitive data that need encryption through specific objects called secrets. Secrets are sets of key-value pairs that are encrypted to protect them. They can be created by putting each value in a file, and then invoking the following kubectl command: |
20:640 | kubectl create secret generic my-secret-name |
20:641 | --from-file=./secret1.bin |
20:642 | --from-file=./secret2.bin |
20:643 | |
20:644 | In this case, the filenames become the keys and the files’ contents are the values. |
20:645 | When the values are strings, they can be specified directly in the kubectl command, as shown here: |
20:646 | kubectl create secret generic dev-db-secret |
20:647 | --from-literal=username=devuser |
20:648 | --from-literal=password='$dsd_weew1' |
20:649 | |
20:650 | In this case, keys and values are listed one after the other, separated by the = character. In the previous example, the actual password is enclosed between single quotes to escape special characters like $ that are usually required to build strong passwords. |
20:651 | Once defined, secrets can be referred to in the spec->volume property of a Pod (Deployment or StatefulSet template), as shown here: |
20:652 | ... |
20:653 | volumes: |
20:654 | - name: my-volume-with-secrets |
20:655 | secret: |
20:656 | secretName: my-secret-name |
20:657 | ... |
20:658 | |
20:659 | After that, each container can specify on which path to mount them in the spec->containers->volumeMounts property: |
20:660 | ... |
20:661 | volumeMounts: |
20:662 | - name: my-volume-with-secrets |
20:663 | mountPath: `/my/secrets` |
20:664 | readOnly: true |
20:665 | ... |
20:666 | |
20:667 | In the preceding example, each key is seen as a file with the same name as the key. The content of the file is the secret value, base64-encoded. Therefore, the code that reads each file must decode its content (in .NET, Convert.FromBase64 will do the job). |
20:668 | When secrets contain strings, they can also be passed as environment variables in the spec->containers->env object: |
20:669 | env: |
20:670 | - name: SECRET_USERNAME |
20:671 | valueFrom: |
20:672 | secretKeyRef: |
20:673 | name: dev-db-secret |
20:674 | key: username |
20:675 | - name: SECRET_PASSWORD |
20:676 | valueFrom: |
20:677 | secretKeyRef: |
20:678 | name: dev-db-secret |
20:679 | key: password |
20:680 | |
20:681 | Here, the name property must match the secret’s name. Passing secrets as environment variables is very convenient when containers host ASP.NET Core applications, since, in this case, environment variables are all immediately available in the configuration object (see the Loading configuration data and using it with the options framework section of Chapter 17, Presenting ASP.NET Core). |
20:682 | Secrets can also encode the key/certificate pair of an HTTPS certificate with the following kubectl command: |
20:683 | kubectl create secret tls test-tls --key=`tls.key` --cert=`tls.crt` |
20:684 | |
20:685 | Secrets defined in this way can be used to enable HTTPS termination in Ingresses. You can do this by placing the secret names in the spec->tls->hosts->secretName properties of an Ingress. |
20:686 | Liveness and readiness checks |
20:687 | Kubernetes automatically monitors all containers to ensure they are still alive and that they keep their resource consumption within the limits declared in the spec->containers->resources->limits object. When some conditions are violated, the container is either throttled, or restarted, or the whole Pod instance is restarted on a different node. How does Kubernetes know that a container is in a healthy state? While it can use the operating system to check the healthy state of nodes, it has no universal check that works with all containers. |
20:688 | Therefore, the containers themselves must inform Kubernetes of their health, otherwise Kubernetes cannot verify them. Containers can inform Kubernetes of their health in two ways: either by declaring a console command that returns their health, or by declaring an endpoint that provides the same information. |
20:689 | Both declarations are provided in the spec-> containers-> livenessProb object. The console command check is declared as shown here: |
20:690 | ... |
20:691 | livenessProbe: |
20:692 | exec: |
20:693 | command: |
20:694 | - cat |
20:695 | - /tmp/healthy |
20:696 | initialDelaySeconds: 10 |
20:697 | periodSeconds: 5 |
20:698 | ... |
20:699 | |
20:700 | If command returns 0, the container is considered healthy. In the preceding example, the software running in the container records its state of health in the /tmp/healthy file, so the cat/tmp/healthy command returns it. PeriodSeconds is the time between checks, while initialDelaySeconds is the initial delay before performing the first check. An initial delay is always necessary so as to give the container time to start. |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.