1. Introduction
1.1 Introduction
In the last lesson, Fu Ge led everyone to work hard to finally build the K8S environment based on Rancher/Rancher! The next step is to design the project and namespace according to our needs! So what is a project and what is a namespace?
According to the design concept of k8s, namespace is to classify our various services, such as: database class, web service class, statistics class, monitoring class and so on. Then the following is to group these categories again, such as: system functions, user functions, production functions, grayscale functions, and so on.
According to the design concept of k8s, the various services we deployed before are called workloads in k8s, and the various services in k8s are deployed under a specific namespace.
So today Fugo will take you to learn how to configure our project and namespace with rancher, and how to deploy our workloads with rancher!
1.2 Environment
Image version | rancher/rancher:v2.5.17-rc4 |
operating system | CentOS 7 x86_64 2009 |
server | TFCentOS7x64、TFCentOS7x64Node1 |
IP | 192.168.168.68、192.168.168.69 |
port | 9443 |
2. Installation
2.1 Close NetworkManager
There is a NetworkManager service in the CentOS7 operating system, which will add the search option to the /etc/resolv.conf of the system, and this search option will be inherited by k8s, which will eventually cause the services deployed on k8s to not work properly.
There are two solutions, either use the DOMAIN parameter to set the available DNS server addresses in the network card configuration, or turn off the NetworkManager service directly, and Fugo chooses to turn it off directly.
cat /etc/resolv.conf
systemctl stop NetworkManager
systemctl disable NetworkManager
systemctl restart network
cat /etc/resolv.conf
Whether you set the DOMAIN parameter or shut down the NetworkManager service, you will eventually need to restart the TFCentOS7x64Node1 server for the changes to take effect.
reboot
2.2 vm.max_map_count
For Elasticsearch services, we also need to increase the value of vm.max_map_count on the TFCentOS7x64Node1 server.
echo "vm.max_map_count=262144" >> /etc/sysctl.conf
sysctl -p
3. Use
3.1 Design Projects and Namespaces
Fogo intended to design a simple cluster structure.
The project is set up with two, one Data (data) and one App (application).
Two namespaces are established under the data project, one database (database) and one cache (cache); The App project establishes a namespace web (web program).
3.2 Create a project
Click the "Project/Namespace" menu to enter the project/namespace management interface.
Click the "Add Project" button to create a project.
Project name "Data".
Click the "Create" button to finish creating the project. You can see that the project Data is created, and it is still an empty project.
Create another project app.
3.3 Create a namespace
Click the "Add Namespace" button on the right side of the project app to create a namespace.
The name "web".
Click the "Create" button to complete the namespace creation. You can see that there is a web namespace under the project app.
Then create the remaining namespaces.
3.4 Deploying Workloads
Now let's deploy the previous services deployed using docker-composite in k8s!
According to the namespace designed earlier, the service TFPHP is deployed to the web, the service TFMYSQL and TFELASTICSEARCH are deployed to the database, and the service TFREDIS is deployed to the cache.
Expand the "Data" menu from the top menu and click the "Data" menu to enter the Data project management interface.
3.5 Deploy the tfmySQL service
By default, under the Workloads tab, click the Deploy Service button to start deploying the service.
The name "tfmysql", the workload type "StatefulSet", the Docker image "registry.tongfu.net:5000/tfmysql:5.7-1.0.0", and the namespace "database".
Set the port mapping, container port 3306, bind cluster IP, and host port is the same as the container port.
Set the environment variable "MYSQL_ROOT_PASSWORD=abcdef".
To set up a data volume, create a MySQL data directory on TFCentOS7x64Node1.
mkdir -p /tongfu.net/data/docker/data/tfmysql/
chown 999.999 /tongfu.net/data/docker/data/tfmysql/
Expand Data Volumes, click Add Volume and select Map Host Directory.
The volume name "data", the host path "/tongfu.net/data/docker/data/tfmysql", and the container path "/var/lib/mysql".
Click the "Start" button at the bottom to start the tfmysql service.
At this time, the service cannot be started, because the used image has not been pushed to the private image repository.
Push the image to the private image repository, and push the required image to the private image repository on TFCentOS7x64.
docker push registry.tongfu.net:5000/tfmysql:5.7-1.0.0
After waiting for a while, it started up!
Click the "three dots" on the right and select "Execute Command Line" to enter the tfmysql container.
Test it through MySQL Terminal, no problem!
3.6 Deploy the tfelasticsearch service
Now start the tfelasticsearch service as well.
The name "tfelasticsearch", the workload type "StatefulSet", the Docker image "registry.tongfu.net:5000/tfelasticsearch:6-1.0.0", and the namespace "database".
Set the port mapping, container port 9200, bind the cluster IP, and the host port is the same as the container port.
To set up the data volume, first create the Elasticsearch data directory on TFCentOS7x64Node1.
mkdir -p /tongfu.net/data/docker/data/tfelasticsearch
chown 1000.1000 /tongfu.net/data/docker/data/tfelasticsearch
The volume name "data", the host path "/tongfu.net/data/docker/data/tfelasticsearch", and the container path "/usr/share/elasticsearch/data".
Push the image to the private image repository, and push the required image to the private image repository on TFCentOS7x64.
docker push registry.tongfu.net:5000/tfelasticsearch:6-1.0.0
Start the service, you succeed!
Go inside the container and test it with curl, no problem!
3.7 Deploy the tfredis service
Now start the tfredis service as well.
The name "tfredis", the workload type "StatefulSet", the Docker image "registry.tongfu.net:5000/tfredis:6.0-1.0.0", and the namespace "cache".
Set the port mapping, container port 6379, bind cluster IP, and host port is the same as container port.
To set up the data volume, create the Redis data directory on TFCentOS7x64Node1.
mkdir -p /tongfu.net/data/docker/data/tfredis
chown 999.999 /tongfu.net/data/docker/data/tfredis
The volume name "data", the host path "/tongfu.net/data/docker/data/tfredis", and the container path "/var/lib/redis".
Push the image to the private image repository, and push the required image to the private image repository on TFCentOS7x64.
docker push registry.tongfu.net:5000/tfredis:6.0-1.0.0
Start the service, you succeed!
Go inside the container and test it with the redis-cli terminal, no problem!
3.8 Deploy TFPHP services
Because tfphp needs to be deployed under the web, switch to the app project first.
Now let's start the tfphp service.
The name "tfphp", the workload type "Deployment", the Docker image "registry.tongfu.net:5000/tfphp:7.4-nginx-1.0.0", and the namespace "web".
Set the port mapping, container port 80, use NodePort, host port random.
Set up the data volume, first create a PHP program directory on TFCentOS7x64Node1, and then use the scp command to copy the PHP program files on TFCentOS7x64.
mkdir -p /tongfu.net/data/docker/data/tfphp/html/
cd /tongfu.net/data/docker/data/tfphp/html/
scp [email protected]:/tongfu.net/data/docker/data/tfphp/html/* .
Volume name "data", host path "/tongfu.net/data/docker/data/tfphp/html", container path "/var/www/html".
Push the image to the private image repository, and push the required image to the private image repository on TFCentOS7x64.
docker push registry.tongfu.net:5000/tfphp:7.4-nginx-1.0.0
Start the service, you succeed! K8S gave us a random 31209 port!
3.8.1 Test tfmysql connectivity
Open a browser to access http://192.168.168.69:31209/tfmysql.php, alas? Error!
Later, Fu Ge remembered that now TFPHP and TFMYSQL are under different namespaces, of course, direct access will not work! You need to add the namespace database where tfmysql is located!
Modify tfmysql .php under the tfphp data directory of TFCentOS7x64Node1 and change tfmysql to tfmysql.database.
<?php
header("Content-Type: text/html; charset=utf-8");
try{
// 连接数据库
$pdo = new \PDO("mysql:host=tfmysql.database;dbname=tfmysql", "root", "abcdef", [\PDO::MYSQL_ATTR_INIT_COMMAND => "set names utf8mb4"]);
$pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
// 获取数据库列表
$rs = $pdo->prepare("show databases");
$rs->execute();
$dbs = $rs->fetchAll(\PDO::FETCH_ASSOC);
// 获取数据表列表
$rs = $pdo->prepare("show tables");
$rs->execute();
$tables = $rs->fetchAll(\PDO::FETCH_ASSOC);
// 获取数据表数据
$rs = $pdo->prepare("select * from user");
$rs->execute();
$data = $rs->fetchAll(\PDO::FETCH_ASSOC);
// 数据库
echo "<table border=\"1\"><tbody>";
echo "<tr><th>Database</th></tr>";
foreach ($dbs as $db){
echo "<tr><td>". $db['Database']. "</td>";
}
echo "</tbody></table>";
// 数据表
echo "<table border=\"1\"><tbody>";
echo "<tr><th>Table</th></tr>";
foreach ($tables as $table){
echo "<tr><td>". $table['Tables_in_tfmysql']. "</td>";
}
echo "</tbody></table>";
// 数据
echo "<table border=\"1\"><tbody>";
echo "<tr>";
foreach ($data[0] as $field => $value){
echo "<th>". $field. "</th>";
}
echo "</tr>";
foreach ($data as $row){
echo "<tr>";
foreach ($row as $field => $value){
echo "<td>". $value. "</td>";
}
echo "</tr>";
}
echo "</tbody></table>";
}
catch (\PDOException $e){
echo $e->getMessage();
}
Visit tfmysql again .php have a try! Alas? This time it says that the database tfmysql cannot be found!
Oh yes! There is no data for MySQL in the k8s environment! You can only put some data in it manually!
create database tfmysql default charset utf8mb4;
use tfmysql;
CREATE TABLE user (
userId INT NOT NULL AUTO_INCREMENT,
userName VARCHAR(45) NOT NULL,
userPwd CHAR(32) NOT NULL,
createDT DATETIME,
updateDT DATETIME,
lastLoginDT DATETIME,
PRIMARY KEY (userId),
KEY u_userName (userName)
);
INSERT INTO user (userName, userPwd, createDT) VALUES ('k8s.tongfu.net', md5('abcdef'), now());
INSERT INTO user (userName, userPwd, createDT) VALUES ('tfmysql.k8s.tongfu.net', md5('abcdef'), now());
Visit tfmysql again .php see? All right!
3.8.2 Test TFREDIS connectivity
Now let's debug the Unicom Redis database! This time I have experience, first change the configuration, and then add data!
Modify the tfredis .php under the tfphp data directory of TFCentOS7x64Node1 and change tfredis to tfredis.cache.
<?php
$redis = new \Redis();
$redis->connect("tfredis.cache", 6379);
$redis->auth("tongfu.net");
echo "<b>String</b><br>";
print_r($redis->get("userNick"));
echo "<br><br><b>Hash</b><br>";
print_r($redis->hgetall("user"));
echo "<br><br><b>Set inter</b><br>";
print_r($redis->sinter("userTags", "userTags2"));
echo "<br><br><b>Set diff</b><br>";
print_r($redis->sdiff("userTags", "userTags2"));
echo "<br><br><b>Set union</b><br>";
print_r($redis->sunion("userTags", "userTags2"));
echo "<br><br><b>ZSet</b><br>";
print_r($redis->zrevrange("hotSubDomains", 0, -1));
Now put some data in Redis in the k8s environment!
set userNick "福哥k8s"
hmset user nick "福哥k8s" gender male age 35
hset user descript "现在我们开始玩k8s了!"
sadd userTags "福哥k8s" tongfu tongfu.net "项目" "命名空间"
sadd userTags2 tongfu k8s.tongfu.net
zadd hotSubDomains 1 tongfu.net 1 k8s.tongfu.net 1 project.k8s.tongfu.net 1 namespace.k8s.tongfu.net 1 rancher.tongfu.net
zincrby hotSubDomains 1 tongfu.net
save
Open a browser to access http://192.168.168.69:31209/tfredis.php and pass it once!
4. Summary
Today, Fugo took you to learn the design skills of the project project/namespace namespace of k8s, and also learned to deploy workloads in k8s. Then we also deployed TFMYSQL, TFREDIS, TFELASTICSEARCH and TFPHP in our K8S environment again to experience the feeling of graphical deployment and operation environment!
Later, Fugo will teach you some basic skills of K8S little by little, so stay tuned~
https://m.tongfu.net/home/35/blog/514005.html