This document describes how to use Tencent Cloud TKE to construct a simple Web application.
Web applications are divided into the following two parts:
Frontend service, used to handle query and write requests from clients.
Data storage service, which uses redis to store data written into the storage to redis-master, while read operations access redis-slave. Redis-master and redis-slave ensure data synchronization through master-slave replication.
This application is an example provided by the Kubernetes project. For more information, please refer to the Guestbook App.
You have created a cluster. For more information, see Creating a Cluster.
Instructions
Creating redis-master Service
1. Log in to the TKE console and click Cluster in the left sidebar.
2. Click on the cluster ID where you want to create the application, navigate to the cluster's basic information page, and click Create in Workload > Deployment.
3. On the Create Deployment page, configure basic information of the workload. The main parameters are as follows. Retain the default settings for other parameters.
Workload Name: This document uses redis-master as an example.
4. Configure Containers in Pod as instructed. The main parameters are as follows. Retain the default settings for other parameters.
Name: Enter the name of the container in the pod. In this document, "master" is used as an example.
Image: Enter ccr.ccs.tencentyun.com/library/redis.
Image Version (Tag): Enter "latest".
Image Pull Policy: in this example, you do not need to specify this field, but simply use the default policy.
5. Configure Access Settings (Service) for the workload as instructed below.
Service: Select "Enable".
Service Access Method: Select "Access within the cluster only".
Port Mapping: Select TCP protocol, and set both the service port and container port to 6379. Other services can access the master container through the service name redis-master and port 6379.
6. Click Create Deployment.
Creating redis-slave Service
1. Log in to the TKE console and click Cluster in the left sidebar.
2. Click on the cluster ID where you want to create the application, navigate to the cluster's basic information page, and click Create in Workload > Deployment.
3. On the Create Deployment page, configure basic information of the workload. The main parameters are as follows. Retain the default settings for other parameters.
Workload Name: The name of the workload to be created, using redis-slave as an example in this document.
4. Configure Containers in Pod as instructed. The main parameters are as follows. Retain the default settings for other parameters.
Name: Enter the name of the container in the pod. In this document, "slave" is used as an example.
Image: Enter ccr.ccs.tencentyun.com/library/gb-redisslave.
Image Version (Tag): Enter "latest".
Image Pull Policy: select the value as required. In this example, you do not need to specify this field, but simply use the default policy.
Environment Variable: enter GET_HOSTS_FROM = dns.
5. Configure Access Settings (Service) for the workload as instructed below.
Service: Select "Enable".
Service Access Method: Select "Access within the cluster only".
Port Mapping: Select TCP protocol, and set both the service port and container port to 6379. Other services can access the master container through the service name redis-master and port 6379.
6. Click Create Deployment.
Create Frontend Service
1. Log in to the TKE console and click Cluster in the left sidebar.
2. Click on the cluster ID where you want to create the application, navigate to the cluster's basic information page, and click Create in Workload > Deployment.
3. On the Create Deployment page, configure basic information of the workload. The main parameters are as follows. Retain the default settings for other parameters.
Workload Name: The name of the workload to be created, using frontend as an example in this document.
4. Configure Containers in Pod as instructed. The main parameters are as follows. Retain the default settings for other parameters.
Name: Enter the name of the container in the pod. In this document, "frontend" is used as an example.
Image: Enter ccr.ccs.tencentyun.com/library/gb-frontend.
Image Version (Tag): Enter "latest".
Image Pull Policy: select the value as required. In this example, you do not need to specify this field, but simply use the default policy.
Environment Variable: enter GET_HOSTS_FROM = dns.
5. Configure Access Settings (Service) for the workload as instructed below.
Service: Select "Enable".
Service Access: Select "Via Internet".
Port Mapping: Select TCP protocol, and set both the service port and container port to 80. Users can access the frontend container by visiting the load balancer IP through a browser.
6. Click Create Deployment.
Verify Web Application
1. Go to the cluster details page, and choose Services and Routes > Service in the left sidebar.
2. On the Service page, copy the CLB IP of the frontend service as shown below:
Note
When creating redis-master and redis-slave services, the access mode is set to cluster-internal access only, which means the services only have a private IP and can only be accessed by other services within the cluster.
When creating the frontend service, because the access mode Via Internet was set, this service has a cloud load balancer (public IP) and a private IP, so it can be accessed by other services in the cluster and can also be accessed via public network.
3. Access the frontend service's load balancing IP using a browser. If the page returns as shown below, it indicates that the frontend service can be accessed normally.
4. Enter any string in the input box and click Submit. You will find that the entered record has been saved and displayed at the bottom of the page. Refresh the browser page and revisit the service IP address. The original input data still exists, indicating that the entered string has been saved to redis.
Development Practices
The following sample code is the complete code for the frontend service of Guestbook App. After frontend service receives an HTTP request, it determines whether it is a set command:
If it is a set command, it takes the key and value in the parameters, and links it to the redis-master service. It also sets the key and value in redis-master.
If it is not a set command, it links it to the redis-slave service, obtains the parameter key and corresponding value, and returns it to the client to display.
<?php
error_reporting(E_ALL);
ini_set('display_errors',1);
require'Predis/Autoloader.php';
Predis\Autoloader::register();
if(isset($_GET['cmd'])===true){
$host='redis-master';
if(getenv('GET_HOSTS_FROM')=='env'){
$host=getenv('REDIS_MASTER_SERVICE_HOST');
}
header('Content-Type: application/json');
if($_GET['cmd']=='set'){
$client=newPredis\Client([
'scheme'=>'tcp',
'host'=>$host,
'port'=>6379,
]);
$client->set($_GET['key'],$_GET['value']);
print('{"message": "Updated"}');
}else{
$host='redis-slave';
if(getenv('GET_HOSTS_FROM')=='env'){
$host=getenv('REDIS_SLAVE_SERVICE_HOST');
}
$client=newPredis\Client([
'scheme'=>'tcp',
'host'=>$host,
'port'=>6379,
]);
$value=$client->get($_GET['key']);
print('{"data": "'.$value.'"}');
}
}else{
phpinfo();
}?>
Notes
When the frontend service accesses redis-master and redis-slave services, it connects to the service name and port. The built-in cluster DNS service resolves the service name into the corresponding service IP and performs load balancing based on the service IP. For example, if the redis-slave service has three instances, when accessing the redis-slave service, connect directly to redis-slave and port 6379. The DNS will automatically resolve redis-slave into the service IP of redis-slave (i.e., a floating IP, similar to the load balancer's IP) and perform load balancing based on the service IP of redis-slave, directing the request to a specific redis-slave service instance.
Container environment variable settings:
Use default setting (recommended setting): When the frontend container runs, if it reads the pre-set GET_HOSTS_FROM environment variable as dns, then it directly connects using the service name.
Other settings: to obtain the domain name of the redis-master or redis-slave service, you must specify another environment variable.