Connecting a Redis Client to DCS Through CCE
With the development of the container technology, more and more applications are deployed in containers. This section describes how to deploy a Redis client in a Cloud Container Engine (CCE) cluster container and connect it to DCS.
Prerequisites​
Prepare the following resources:
-
VPC and subnet, for example,
vpc-test
. For details, see Creating a VPC.(Optional) Create two subnets. Place your DCS instance in one subnet (subnet 1) and your CCE cluster in the other (subnet 2).
-
DCS instance, for example,
dcs-test
. For details, see Creating a DCS Redis Instance.When creating a DCS instance, select the created VPC (
vpc-test
) and subnet 1. -
CCE cluster, for example,
cce-test
. For details, see Creating a CCE Cluster.When creating a CCE cluster, set
Network Model
toVPC network
, and select VPCvpc-test
and subnet 2. -
CCE node pool, for example,
cce-test-nodepool
. For details, see Creating a Node Pool.When creating a node pool, set
Node Type
toElastic Cloud Server (VM)
,Container Engine
toDocker
,OS
toCentOS 7.7
, and bind an existing EIP or create one.
Solution Design​
Creating a Client Image​
-
Download a Redis client.
a. Log in to the CCE cluster node.
Click the name of the created node pool. On the displayed page, click Remote Login in the upper right corner.
b. Run the
gcc --version
command to check whether the GCC compiler for compiling the Redis program is installed in the OS. The following figure shows that the GCC compiler has been installed.If the GCC compiler is not installed, run the following commands to install it:
sudo yum update
yum -y install gcc
yum -y install gcc-c++c. Run the following command to create the redis directory in the home directory, and then go to the directory:
cd /home
mkdir redis
cd redisd. Run the following command to download the Redis client. The following takes version
5.0.13
as an example.sudo wget https://download.redis.io/releases/redis-5.0.13.tar.gz
e. Decompress the Redis package, go to the Redis directory, run the compilation command, and return to the Redis directory.
tar xvzf redis-5.0.13.tar.gz
cd redis-5.0.13
make redis-cli
cd .. -
Create a Dockerfile.
Run the
vim Dockerfile
command to create a Dockerfile and enter the following information:DockerfileFROM centos:7
RUN useradd -d /home/redis -m redis
COPY ./redis-5.0.13 /home/redis/redis-5.0.13
RUN chown redis:redis /home/redis/redis-5.0.13 -R
USER redis
ENV HW_HOME=/home/redis/redis-5.0.13
ENV PATH=$HW_HOME/src:$PATH
WORKDIR /home/redis/Press ESC to exit the editing mode and type
:wq!
to save the configuration and exit the editing interface. -
Build a client image.
a. Choose Software Repository for Container from the service list to go to the SWR console.
b. Click Create Organization in the upper right corner and enter an organization name to create an organization. You can also use an existing organization. (Click Organization Management in the navigation pane to view organizations.)
c. On the SWR Dashboard page, click Generate Login Command in the upper right corner to obtain and copy the login command. (
swr.xxxxxx.com
at the end of the login command is the image repository address.)d. Run the copied login command on the CCE node to log in to SWR.
e. Run the following command to build an image:
docker build -t <Image repository address>/<Organization name>/<Image name :version>
Image repository address indicates the address of the image repository, which is at the end of the login command. Organization name indicates the name of the organization. Image name indicates the name of the image to be built. version indicates the image version. Replace them with the actual values.
-
Run the following command to upload the client image to SWR:
docker push <Image repository address>/<Organization name>/<Image name :version>
-
After the image is uploaded, you can view it on the My Images page of the SWR console.
Creating a Workload​
-
On the DCS console, click the created Redis instance
dcs-test
to go to the details page. -
In the Connection area, obtain the IP address and port number of the Redis instance.
-
Click Connect to Redis in the upper right corner to use the Web CLI.
-
On the Web CLI, run a
SET
command. The following figure usesSET hello "hello redis!"
as an example. -
On the CCE console, click the created CCE cluster
cce-test
. -
In the navigation pane, choose Workloads. Click Create Workload in the upper right corner. For details, see Creating a Workload.
-
In the Container Settings -> Basic Info area, set Image Name to the name of the created Redis image.
-
In the Container Settings -> Lifecycle area, configure the parameters as follows:
Command
:/bin/bash
Args
:-c
while true ;do sleep 10;/home/redis/redis-5.0.13/src/redis-cli h** **{10.0.0.0}** **-p** **6379** **-a** **{DCS instance password}** **get** **hello;done**
In the preceding command,
10.0.0.0
indicates the IP address of the DCS instance,6379
indicates the port number of the DCS instance,{DCS instance password}
indicates the password of the DCS instance, andhello
indicates the data set when you connect to Redis through the Web CLI. Replace them with the actual values.
-
-
If the workload is in the
Running
state, it has been successfully created.
Testing the Workload​
-
Log in to the CCE cluster node.
-
Download and configure the
kubectl
configuration file by referring to Connecting to a Cluster Using kubectl. -
Run the following command. If
Running
is returned, the Redis container is running.kubectl get pod -n default
-
Run the following command to view the logs of the Redis container:
kubectl logs --tail 10 -f redis-xxxxxxxx -n default
redis-xxxxxxxx
indicates the name of the created workload pod. (Click the workload name to view the workload pod name.)In the command output, the information returned by DCS is
hello redis!
, which is the data you've previously set. -
The test is complete.