9.2.1 Deploying StatefulSet
MariaDB
We can wrap the Pod from 9.1.2 into a StatefulSet:
kubectl create -f Chapter09/9.2.1_StatefulSet_MariaDB/mariadb-statefulset.yaml
$ kubectl get pvc
NAME STATUS VOLUME CAPACITY ACCESS MODES STORAGECLASS AGE
mariadb-pvc-mariadb-0 Bound pvc-baaf6655-992e-48f0-8912-cfa28fd70cd8 2Gi RWO standard-rwo 7s
This time let’s test it with an ephemeral container running in the cluster, rather than locally via Docker.
kubectl run my -it --rm --restart=Never --pod-running-timeout=3m \
--image mariadb -- mariadb -h mariadb-0.mariadb-service -P 3306 -u root -p
Since it’s in the cluster it may take a moment to spin up, watch in a separate tab
$ kubectl get pod/my -w
NAME READY STATUS RESTARTS AGE
my 0/1 Pending 0 16s
my 0/1 Pending 0 57s
my 0/1 ContainerCreating 0 57s
my 1/1 Running 0 2m13s
When it’s Running
, enter the password and test the DB like before
If you don't see a command prompt, try pressing enter.
It will show this, but don’t press enter (that sends the empty string as a password)
instead, type your database password
, as configured in the YAML.
If you don't see a command prompt, try pressing enter.
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 11.1.2-MariaDB-1:11.1.2+maria~ubu2204 mariadb.org binary distribution
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]> CREATE DATABASE foo;
Query OK, 1 row affected (0.002 sec)
MariaDB [(none)]> exit
Bye
pod "my" deleted
The ephemeral Pod is automatically deleted once you exit.
Redis
Another example is Redis. For this one we need to configure a file on the container, which we can do with a ConfigMap
And here’s the Redis StatefulSet that references the ConfigMap
Create both
kubectl create -f Chapter09/9.2.1_StatefulSet_Redis/
Once it’s ready, we can connect via the port-forward plus Docker method, like so:
$ k get pods -w
NAME READY STATUS RESTARTS AGE
redis-0 0/1 Pending 0 12s
redis-0 0/1 Pending 0 78s
redis-0 0/1 Pending 0 92s
redis-0 0/1 ContainerCreating 0 92s
redis-0 1/1 Running 0 2m8s
$ kubectl port-forward pod/redis-0 6379:637979
Forwarding from 127.0.0.1:6379 -> 6379
Handling connection for 6379
In a separate tab:
$ docker run --net=host -it --rm redis redis-cli
27.0.0.1:6379> INFO
# Server
redis_version:7.2.1
127.0.0.1:6379> exit
Note that Redis is configured with no password, as a purely internal service.
Cleanup
kubectl delete -f Chapter09/9.2.1_StatefulSet_MariaDB
kubectl delete -f Chapter09/9.2.1_StatefulSet_Redis