Skip to content

Commit de14f52

Browse files
committed
Copy tutorials/stateful-application/cassandra.md from en/ directory.
1 parent 6eaacd5 commit de14f52

File tree

1 file changed

+276
-0
lines changed

1 file changed

+276
-0
lines changed
Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
---
2+
title: "Example: Deploying Cassandra with a StatefulSet"
3+
reviewers:
4+
- ahmetb
5+
content_type: tutorial
6+
weight: 30
7+
---
8+
9+
<!-- overview -->
10+
This tutorial shows you how to run [Apache Cassandra](http://cassandra.apache.org/) on Kubernetes. Cassandra, a database, needs persistent storage to provide data durability (application _state_). In this example, a custom Cassandra seed provider lets the database discover new Cassandra instances as they join the Cassandra cluster.
11+
12+
*StatefulSets* make it easier to deploy stateful applications into your Kubernetes cluster. For more information on the features used in this tutorial, see [StatefulSet](/docs/concepts/workloads/controllers/statefulset/).
13+
14+
{{< note >}}
15+
Cassandra and Kubernetes both use the term _node_ to mean a member of a cluster. In this
16+
tutorial, the Pods that belong to the StatefulSet are Cassandra nodes and are members
17+
of the Cassandra cluster (called a _ring_). When those Pods run in your Kubernetes cluster,
18+
the Kubernetes control plane schedules those Pods onto Kubernetes
19+
{{< glossary_tooltip text="Nodes" term_id="node" >}}.
20+
21+
When a Cassandra node starts, it uses a _seed list_ to bootstrap discovery of other
22+
nodes in the ring.
23+
This tutorial deploys a custom Cassandra seed provider that lets the database discover
24+
new Cassandra Pods as they appear inside your Kubernetes cluster.
25+
{{< /note >}}
26+
27+
28+
## {{% heading "objectives" %}}
29+
30+
* Create and validate a Cassandra headless {{< glossary_tooltip text="Service" term_id="service" >}}.
31+
* Use a {{< glossary_tooltip term_id="StatefulSet" >}} to create a Cassandra ring.
32+
* Validate the StatefulSet.
33+
* Modify the StatefulSet.
34+
* Delete the StatefulSet and its {{< glossary_tooltip text="Pods" term_id="pod" >}}.
35+
36+
37+
## {{% heading "prerequisites" %}}
38+
39+
{{< include "task-tutorial-prereqs.md" >}}
40+
41+
To complete this tutorial, you should already have a basic familiarity with {{< glossary_tooltip text="Pods" term_id="pod" >}}, {{< glossary_tooltip text="Services" term_id="service" >}}, and {{< glossary_tooltip text="StatefulSets" term_id="StatefulSet" >}}.
42+
43+
### Additional Minikube setup instructions
44+
45+
{{< caution >}}
46+
[Minikube](/docs/getting-started-guides/minikube/) defaults to 1024MiB of memory and 1 CPU. Running Minikube with the default resource configuration results in insufficient resource errors during this tutorial. To avoid these errors, start Minikube with the following settings:
47+
48+
```shell
49+
minikube start --memory 5120 --cpus=4
50+
```
51+
{{< /caution >}}
52+
53+
54+
55+
<!-- lessoncontent -->
56+
## Creating a headless Service for Cassandra {#creating-a-cassandra-headless-service}
57+
58+
In Kubernetes, a {{< glossary_tooltip text="Service" term_id="service" >}} describes a set of {{< glossary_tooltip text="Pods" term_id="pod" >}} that perform the same task.
59+
60+
The following Service is used for DNS lookups between Cassandra Pods and clients within your cluster:
61+
62+
{{< codenew file="application/cassandra/cassandra-service.yaml" >}}
63+
64+
Create a Service to track all Cassandra StatefulSet members from the `cassandra-service.yaml` file:
65+
66+
```shell
67+
kubectl apply -f https://k8s.io/examples/application/cassandra/cassandra-service.yaml
68+
```
69+
70+
71+
### Validating (optional) {#validating}
72+
73+
Get the Cassandra Service.
74+
75+
```shell
76+
kubectl get svc cassandra
77+
```
78+
79+
The response is
80+
81+
```
82+
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
83+
cassandra ClusterIP None <none> 9042/TCP 45s
84+
```
85+
86+
If you don't see a Service named `cassandra`, that means creation failed. Read [Debug Services](/docs/tasks/debug-application-cluster/debug-service/) for help troubleshooting common issues.
87+
88+
## Using a StatefulSet to create a Cassandra ring
89+
90+
The StatefulSet manifest, included below, creates a Cassandra ring that consists of three Pods.
91+
92+
{{< note >}}
93+
This example uses the default provisioner for Minikube. Please update the following StatefulSet for the cloud you are working with.
94+
{{< /note >}}
95+
96+
{{< codenew file="application/cassandra/cassandra-statefulset.yaml" >}}
97+
98+
Create the Cassandra StatefulSet from the `cassandra-statefulset.yaml` file:
99+
100+
```shell
101+
# Use this if you are able to apply cassandra-statefulset.yaml unmodified
102+
kubectl apply -f https://k8s.io/examples/application/cassandra/cassandra-statefulset.yaml
103+
```
104+
105+
If you need to modify `cassandra-statefulset.yaml` to suit your cluster, download
106+
https://k8s.io/examples/application/cassandra/cassandra-statefulset.yaml and then apply
107+
that manifest, from the folder you saved the modified version into:
108+
```shell
109+
# Use this if you needed to modify cassandra-statefulset.yaml locally
110+
kubectl apply -f cassandra-statefulset.yaml
111+
```
112+
113+
114+
## Validating the Cassandra StatefulSet
115+
116+
1. Get the Cassandra StatefulSet:
117+
118+
```shell
119+
kubectl get statefulset cassandra
120+
```
121+
122+
The response should be similar to:
123+
124+
```
125+
NAME DESIRED CURRENT AGE
126+
cassandra 3 0 13s
127+
```
128+
129+
The `StatefulSet` resource deploys Pods sequentially.
130+
131+
1. Get the Pods to see the ordered creation status:
132+
133+
```shell
134+
kubectl get pods -l="app=cassandra"
135+
```
136+
137+
The response should be similar to:
138+
139+
```shell
140+
NAME READY STATUS RESTARTS AGE
141+
cassandra-0 1/1 Running 0 1m
142+
cassandra-1 0/1 ContainerCreating 0 8s
143+
```
144+
145+
It can take several minutes for all three Pods to deploy. Once they are deployed, the same command
146+
returns output similar to:
147+
148+
```
149+
NAME READY STATUS RESTARTS AGE
150+
cassandra-0 1/1 Running 0 10m
151+
cassandra-1 1/1 Running 0 9m
152+
cassandra-2 1/1 Running 0 8m
153+
```
154+
155+
3. Run the Cassandra [nodetool](https://cwiki.apache.org/confluence/display/CASSANDRA2/NodeTool) inside the first Pod, to
156+
display the status of the ring.
157+
158+
```shell
159+
kubectl exec -it cassandra-0 -- nodetool status
160+
```
161+
162+
The response should look something like:
163+
164+
```
165+
Datacenter: DC1-K8Demo
166+
======================
167+
Status=Up/Down
168+
|/ State=Normal/Leaving/Joining/Moving
169+
-- Address Load Tokens Owns (effective) Host ID Rack
170+
UN 172.17.0.5 83.57 KiB 32 74.0% e2dd09e6-d9d3-477e-96c5-45094c08db0f Rack1-K8Demo
171+
UN 172.17.0.4 101.04 KiB 32 58.8% f89d6835-3a42-4419-92b3-0e62cae1479c Rack1-K8Demo
172+
UN 172.17.0.6 84.74 KiB 32 67.1% a6a1e8c2-3dc5-4417-b1a0-26507af2aaad Rack1-K8Demo
173+
```
174+
175+
## Modifying the Cassandra StatefulSet
176+
177+
Use `kubectl edit` to modify the size of a Cassandra StatefulSet.
178+
179+
1. Run the following command:
180+
181+
```shell
182+
kubectl edit statefulset cassandra
183+
```
184+
185+
This command opens an editor in your terminal. The line you need to change is the `replicas` field. The following sample is an excerpt of the StatefulSet file:
186+
187+
```yaml
188+
# Please edit the object below. Lines beginning with a '#' will be ignored,
189+
# and an empty file will abort the edit. If an error occurs while saving this file will be
190+
# reopened with the relevant failures.
191+
#
192+
apiVersion: apps/v1
193+
kind: StatefulSet
194+
metadata:
195+
creationTimestamp: 2016-08-13T18:40:58Z
196+
generation: 1
197+
labels:
198+
app: cassandra
199+
name: cassandra
200+
namespace: default
201+
resourceVersion: "323"
202+
uid: 7a219483-6185-11e6-a910-42010a8a0fc0
203+
spec:
204+
replicas: 3
205+
```
206+
207+
1. Change the number of replicas to 4, and then save the manifest.
208+
209+
The StatefulSet now scales to run with 4 Pods.
210+
211+
1. Get the Cassandra StatefulSet to verify your change:
212+
213+
```shell
214+
kubectl get statefulset cassandra
215+
```
216+
217+
The response should be similar to:
218+
219+
```
220+
NAME DESIRED CURRENT AGE
221+
cassandra 4 4 36m
222+
```
223+
224+
225+
226+
## {{% heading "cleanup" %}}
227+
228+
Deleting or scaling a StatefulSet down does not delete the volumes associated with the StatefulSet. This setting is for your safety because your data is more valuable than automatically purging all related StatefulSet resources.
229+
230+
{{< warning >}}
231+
Depending on the storage class and reclaim policy, deleting the *PersistentVolumeClaims* may cause the associated volumes to also be deleted. Never assume you’ll be able to access data if its volume claims are deleted.
232+
{{< /warning >}}
233+
234+
1. Run the following commands (chained together into a single command) to delete everything in the Cassandra StatefulSet:
235+
236+
```shell
237+
grace=$(kubectl get pod cassandra-0 -o=jsonpath='{.spec.terminationGracePeriodSeconds}') \
238+
&& kubectl delete statefulset -l app=cassandra \
239+
&& echo "Sleeping ${grace} seconds" 1>&2 \
240+
&& sleep $grace \
241+
&& kubectl delete persistentvolumeclaim -l app=cassandra
242+
```
243+
244+
1. Run the following command to delete the Service you set up for Cassandra:
245+
246+
```shell
247+
kubectl delete service -l app=cassandra
248+
```
249+
250+
## Cassandra container environment variables
251+
252+
The Pods in this tutorial use the [`gcr.io/google-samples/cassandra:v13`](https://github.com/kubernetes/examples/blob/master/cassandra/image/Dockerfile)
253+
image from Google's [container registry](https://cloud.google.com/container-registry/docs/).
254+
The Docker image above is based on [debian-base](https://github.com/kubernetes/kubernetes/tree/master/build/debian-base)
255+
and includes OpenJDK 8.
256+
257+
This image includes a standard Cassandra installation from the Apache Debian repo.
258+
By using environment variables you can change values that are inserted into `cassandra.yaml`.
259+
260+
| Environment variable | Default value |
261+
| ------------------------ |:---------------: |
262+
| `CASSANDRA_CLUSTER_NAME` | `'Test Cluster'` |
263+
| `CASSANDRA_NUM_TOKENS` | `32` |
264+
| `CASSANDRA_RPC_ADDRESS` | `0.0.0.0` |
265+
266+
267+
268+
## {{% heading "whatsnext" %}}
269+
270+
271+
* Learn how to [Scale a StatefulSet](/docs/tasks/run-application/scale-stateful-set/).
272+
* Learn more about the [*KubernetesSeedProvider*](https://github.com/kubernetes/examples/blob/master/cassandra/java/src/main/java/io/k8s/cassandra/KubernetesSeedProvider.java)
273+
* See more custom [Seed Provider Configurations](https://git.k8s.io/examples/cassandra/java/README.md)
274+
275+
276+

0 commit comments

Comments
 (0)