Connect to Cassandra using cqlsh within Kubernetes Cluster

These steps describe using cqlsh to connect to a deployed Apache Cassandra® 3.11.7 database from within a Kubernetes cluster.

If you have not already, create a Kubernetes cluster, and apply the pre-configured quick-start YAML files to the Kubernetes cluster.

Prerequisites

  • The kubectl CLI tool.

  • Kubeconfig file or context pointing to a Control Plane Kubernetes cluster.

  • A Kubernetes cluster must exist.

Get credentials and start cqlsh shell within Kubernetes

  1. On a local machine where you have authorized access to the Kubernetes cluster, as described in Create a Kubernetes cluster, run the following command to get the cluster1-superuser secret. The command must also identify the cass-operator namespace as defined in the sample YAML configuration files.

    kubectl get secret -o yaml cluster1-superuser -n cass-operator

    Sample output:

    apiVersion: v1
    data:
      password: <password-string>
      username: <user-name>
    kind: Secret
    metadata:
      creationTimestamp: "2021-05-04T23:55:23Z"
      name: cluster1-superuser
      namespace: cass-operator
      resourceVersion: "1479618"
      selfLink: /api/v1/namespaces/cass-operator/secrets/cluster1-superuser
      uid: <uid-string>
    type: Opaque

    In the command results for data: password:, notice and copy the secret’s encoded password.

  2. Paste the secret’s encoded password into and run the following command that assumes a macOS machine and issues base64 -d to decode the string.

    echo '<password-string>' | base64 -d

    Sample output:

    <decoded-password-string>
  3. Specify the cass-operator namespace, the authenticated username (as defined in the sample YAML configuration files), and the decoded password.

    For example:

    kubectl exec -n cass-operator -i -t -c cassandra cluster1-dc1-default-sts-0 -- /opt/cassandra/bin/cqlsh -u cluster1-superuser -p

    Sample output:

    Connected to cluster1 at 127.0.0.1:9042.
    [cqlsh 5.0.1 | Cassandra 3.11.7 | CQL spec 3.4.4 | Native protocol v4]
    Use HELP for help.
    cluster1-superuser@cqlsh>

Submit DDL and DML commands to the connected Cassandra database

With the connection to Cassandra’s cqlsh established inside the Kubernetes cluster, you can submit Data Definition Language (DDL) and Data Manipulation Language (DML) commands.

In the following steps, the cluster1-superuser@cqlsh> prompt is simply shown as cqlsh>.

  1. First, create a keyspace.

    In the CREATE KEYSPACE command, remember to specify the relevant datacenter name. In this example, as defined by the provided YAML, it is dc1.

    cqlsh> CREATE KEYSPACE IF NOT EXISTS cycling WITH replication = { 'class' : 'NetworkTopologyStrategy', 'dc1' : '3' };
  2. Define a table.

    cqlsh> CREATE TABLE IF NOT EXISTS cycling.cyclist_semi_pro (
       id int,
       firstname text,
       lastname text,
       age int,
       affiliation text,
       country text,
       registration date,
       PRIMARY KEY (id));
  3. Load some data.

    cqlsh> INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (1, 'Carlos', 'Perotti', 22, 'Recco Club', 'ITA', '2020-01-12');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (2, 'Giovani', 'Pasi', 19, 'Venezia Velocità', 'ITA', '2016-05-15');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (3, 'Frances', 'Giardello', 24, 'Menaggio Campioni', 'ITA', '2018-07-29');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (4, 'Mark', 'Pastore', 19, 'Portofino Ciclisti', 'ITA', '2017-06-16');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (5, 'Irene', 'Cantona', 24, 'Como Velocità', 'ITA', '2012-07-22');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (6, 'Hugo', 'Herrera', 23, 'Bellagio Ciclisti', 'ITA', '2004-02-12');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (7, 'Marcel', 'Silva', 21, 'Paris Cyclistes', 'FRA', '2018-04-28');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (8, 'Theo', 'Bernat', 19, 'Nice Cavaliers', 'FRA', '2007-05-15');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (9, 'Richie', 'Draxler', 24, 'Normandy Club', 'FRA', '2011-02-26');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (10, 'Agnes', 'Cavani', 22, 'Chamonix Hauteurs', 'FRA', '2020-01-02');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (11, 'Pablo', 'Verratti', 19, 'Chamonix Hauteurs', 'FRA', '2006-05-15');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (12, 'Charles', 'Eppinger', 24, 'Chamonix Hauteurs', 'FRA', '2018-07-29');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (13, 'Stanley', 'Trout', 30, 'Bolder Boulder', 'USA', '2016-02-12');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (14, 'Juan', 'Perez', 31, 'Rutgers Alumni Riders', 'USA', '2017-06-16');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (15, 'Thomas', 'Fulton', 27, 'Exeter Academy', 'USA', '2012-12-15');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (16, 'Jenny', 'Hamler', 28, 'CU Alums Crankworkz', 'USA', '2012-07-22');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (17, 'Alice', 'McCaffrey', 26, 'Pennan Power', 'GBR', '2020-02-12');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (18, 'Nicholas', 'Burrow', 26, 'Aberdeen Association', 'GBR', '2016-02-12');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (19, 'Tyler', 'Higgins', 24, 'Highclere Agents', 'GBR', '2019-07-31');
    INSERT INTO cycling.cyclist_semi_pro (id, firstname, lastname, age, affiliation, country, registration) VALUES (20, 'Leslie', 'Boyd', 18, 'London Cyclists', 'GBR', '2012-12-15');
  4. Run a query.

    cqlsh> SELECT * FROM cycling.cyclist_semi_pro;

    Sample output:

 id | affiliation           | age | country | firstname | lastname  | registration
----+-----------------------+-----+---------+-----------+-----------+--------------
  5 |         Como Velocità |  24 |     ITA |     Irene |   Cantona |   2012-07-22
 10 |     Chamonix Hauteurs |  22 |     FRA |     Agnes |    Cavani |   2020-01-02
 16 |   CU Alums Crankworkz |  28 |     USA |     Jenny |    Hamler |   2012-07-22
 13 |        Bolder Boulder |  30 |     USA |   Stanley |     Trout |   2016-02-12
 11 |     Chamonix Hauteurs |  19 |     FRA |     Pablo |  Verratti |   2006-05-15
  1 |            Recco Club |  22 |     ITA |    Carlos |   Perotti |   2020-01-12
 19 |      Highclere Agents |  24 |     GBR |     Tyler |   Higgins |   2019-07-31
  8 |        Nice Cavaliers |  19 |     FRA |      Theo |    Bernat |   2007-05-15
  2 |      Venezia Velocità |  19 |     ITA |   Giovani |      Pasi |   2016-05-15
  4 |    Portofino Ciclisti |  19 |     ITA |      Mark |   Pastore |   2017-06-16
 18 |  Aberdeen Association |  26 |     GBR |  Nicholas |    Burrow |   2016-02-12
 15 |        Exeter Academy |  27 |     USA |    Thomas |    Fulton |   2012-12-15
 20 |       London Cyclists |  18 |     GBR |    Leslie |      Boyd |   2012-12-15
  7 |       Paris Cyclistes |  21 |     FRA |    Marcel |     Silva |   2018-04-28
  6 |     Bellagio Ciclisti |  23 |     ITA |      Hugo |   Herrera |   2004-02-12
  9 |         Normandy Club |  24 |     FRA |    Richie |   Draxler |   2011-02-26
 14 | Rutgers Alumni Riders |  31 |     USA |      Juan |     Perez |   2017-06-16
 17 |          Pennan Power |  26 |     GBR |     Alice | McCaffrey |   2020-02-12
 12 |     Chamonix Hauteurs |  24 |     FRA |   Charles |  Eppinger |   2018-07-29
  3 |     Menaggio Campioni |  24 |     ITA |   Frances | Giardello |   2018-07-29

What’s next?

Now that Kubernetes Operator for Apache Cassandra and Cassandra are deployed to your Kubernetes cluster and you are connected to cqlsh from within the Kubernetes cluster, proceed to the next topic. It explains how to connect to Apache Cassandra resources and applications that are in the Kubernetes cluster from outside of the cluster.

Was this helpful?

Give Feedback

How can we improve the documentation?

© 2024 DataStax | Privacy policy | Terms of use

Apache, Apache Cassandra, Cassandra, Apache Tomcat, Tomcat, Apache Lucene, Apache Solr, Apache Hadoop, Hadoop, Apache Pulsar, Pulsar, Apache Spark, Spark, Apache TinkerPop, TinkerPop, Apache Kafka and Kafka are either registered trademarks or trademarks of the Apache Software Foundation or its subsidiaries in Canada, the United States and/or other countries. Kubernetes is the registered trademark of the Linux Foundation.

General Inquiries: +1 (650) 389-6000, info@datastax.com