#database #devops #kubernetes

Step 1 - Create a secret for the Redis password

The first step is that we make a secret containing the password for our Redis instance.

1$ kubectl create secret generic redis --from-literal="REDIS_PASS=<password>"

Step 2 - Create the deployment description

Then, create the deployment file and deploy.

deployment-redis.yaml

 1apiVersion: apps/v1
 2kind: Deployment
 3metadata:
 4  labels:
 5    name: redis
 6    component: cache
 7  name: redis
 8spec:
 9  replicas: 1
10  selector:
11    matchLabels:
12      name: redis
13  template:
14    metadata:
15      labels:
16        name: redis
17        component: cache
18    spec:
19      containers:
20      - name: redis
21        image: redis
22        imagePullPolicy: Always
23        args: ["--requirepass", "$(REDIS_PASS)"]
24        ports:
25        - containerPort: 6379
26          name: redis
27        env:
28        - name: MASTER
29          value: "true"
30        - name: REDIS_PASS
31          valueFrom:
32            secretKeyRef:
33              name: redis
34              key: REDIS_PASS

Then, deploy the Redis instance:

1$ kubectl apply -f deployment-redis.yaml
2deployment.apps/redis created

Step 3 - Expose the service

If you want to make the Redis accessible from the outside world, you need to expose it.

If you only plan to use it from within your cluster, skip this step.

1$ kubectl expose deployment redis --type=LoadBalancer --name=redis
2service/redis exposed

Once it's exposed, you need to wait for it's external IP-address to be setup:

1$ kubectl get services redis --watch
2
3NAME    TYPE           CLUSTER-IP    EXTERNAL-IP   PORT(S)          AGE
4redis   LoadBalancer   10.0.43.144   <pending>     6379:30246/TCP   0s
5NAME    TYPE           CLUSTER-IP    EXTERNAL-IP     PORT(S)          AGE
6redis   LoadBalancer   10.0.43.144   104.40.246.55   6379:30246/TCP   4m54s

Step 4 - Add DNS record

Once you have the external IP address of the Redis load balancer, you can create an A record on the DNS server:

k8s-redis.twixlmedia.com A 104.40.246.55

Step 5 - Check access

Install a Redis client on your mac and check if you have access:

 1$ brew tap ringohub/redis-cli
 2==> Tapping ringohub/redis-cli
 3Cloning into '/usr/local/Homebrew/Library/Taps/ringohub/homebrew-redis-cli'...
 4brew updateremote: Enumerating objects: 5, done.
 5remote: Counting objects: 100% (5/5), done.
 6remote: Compressing objects: 100% (4/4), done.
 7remote: Total 5 (delta 0), reused 3 (delta 0), pack-reused 0
 8Unpacking objects: 100% (5/5), done.
 9Tapped 1 formula (31 files, 24.8KB).
10$ brew install redis-cli
11==> Installing redis-cli from ringohub/redis-cli
12==> Downloading https://github.com/antirez/redis/archive/4.0.1.tar.gz
13==> Downloading from https://codeload.github.com/antirez/redis/tar.gz/4.0.1
14######################################################################## 100.0%
15==> make redis-cli
16🍺  /usr/local/Cellar/redis-cli/4.0.1: 5 files, 150.5KB, built in 22 seconds
17$ redis-cli -h k8s-redis.twixlmedia.com -p 6379 -a "VQr9A2p/mJVCFhXKdI24SEAPxZHWMPLQhaP7GnLCDi8="
18k8s-redis.twixlmedia.com:6379> set foo 100
19OK
20k8s-redis.twixlmedia.com:6379> exit