Commit 06ba3f12 authored by wutu's avatar wutu

添加启动自检魔抗,并优化日志

parent 7277cf02
......@@ -2,10 +2,10 @@ server:
port: 9090
zookeeper:
url: 192.168.31.154:2181
url: 192.168.1.5:2181
bs:
cloudcenter:
name: my-bs-cloud-center
ipservice:
url: 127.0.0.1:23333
\ No newline at end of file
url: 192.168.1.5:23333
\ No newline at end of file
package top.ninwoo.edgecenter.check;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.junit.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import top.ninwoo.common.entity.ClusterConfig;
import top.ninwoo.common.entity.ContainerDescription;
import top.ninwoo.common.entity.DockerContainer;
import top.ninwoo.common.entity.NetworkTopology;
import top.ninwoo.edgecenter.service.ClusterService;
import top.ninwoo.edgecenter.service.IpService;
import top.ninwoo.edgecenter.service.TopologyService;
import top.ninwoo.utils.service.DockerService;
import top.ninwoo.utils.util.LinuxCtlUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
/**
* Docker容器集群服务自检程序
*
* @author joliu
*/
@Slf4j
@Component
public class ClusterServiceCheck {
@Autowired
private ClusterService clusterService;
@Autowired
private DockerService dockerService;
@Autowired
private TopologyService topologyService;
@Autowired
private LinuxCtlUtils linuxCtlUtils;
@Autowired
private IpService ipService;
private String container1Id;
private String container2Id;
private final static Long CLUSTERID = 12345L;
public void createCluster() {
log.info("启动容器自检服务");
// 构建一个容器组配置
ClusterConfig clusterConfig = new ClusterConfig();
clusterConfig.setId(CLUSTERID);
clusterConfig.setOwner("joliu");
ContainerDescription containerDescription = new ContainerDescription();
containerDescription.setMode("normal");
containerDescription.setReplicas(2);
DockerContainer container = new DockerContainer();
container.setName("Test1");
container.setCommand("sh");
container.setImage("joliu/networktest");
containerDescription.setDockerContainer(container);
List<ContainerDescription> cds = new ArrayList<>();
cds.add(containerDescription);
ContainerDescription containerDescription1 = new ContainerDescription();
containerDescription1.setMode("normal");
containerDescription1.setReplicas(1);
DockerContainer container1 = new DockerContainer();
container1.setName("Test2");
container1.setCommand("sh");
container1.setImage("joliu/networktest");
containerDescription1.setDockerContainer(container1);
cds.add(containerDescription1);
clusterConfig.setDockers(cds);
NetworkTopology topo = new NetworkTopology();
topo.setAppNames(new String[]{"Test1", "Test2", "br:ovs1"});
// 这个参数好像没啥用
topo.setTopologyId(11);
topo.setTopology(new int[][]{{0,0,0},{0,0,0},{1,1,0}});
clusterConfig.setTopology(topo);
//long id = clusterService.initContainers(clusterConfig).getId();
//Assert.assertEquals(id, clusterId);
log.info("创建自检容器集群【Test1_0,Test1_1,Test2】");
try {
clusterService.initContainers(clusterConfig);
topologyService.createTopo(clusterConfig.getId(), clusterConfig.getTopology());
} catch (Exception e) {
log.error("容器启动失败");
throw new RuntimeException("容器启动失败");
}
if(!clusterService.getClusterIds().contains(CLUSTERID)) {
log.error("集群创建失败%", CLUSTERID);
}
log.info("容器集群[{}]创建成功", CLUSTERID);
Set<String> test1 = clusterService.getContainerIdsByClusterId(CLUSTERID, "Test1");
log.info("容器Test1副本创建成功:[{}]", test1);
Set<String> test2 = clusterService.getContainerIdsByClusterId(CLUSTERID, "Test2");
log.info("容器Test2副本创建成功:[{}]", test2);
log.info("容器集群网络创建成功");
container1Id = ((String) test1.toArray()[0]);
container2Id = ((String) test2.toArray()[0]);
}
public void pingTest() {
// 测试网络
String ovsResult = linuxCtlUtils.runCmd("echo Vudo3423 | sudo -S ovs-vsctl show");
log.info("Open vSwitch:{}", ovsResult);
// 获取container2的ip
// ifconfig eth1 |sed -rn '2s#^.*dr:(.*) Bc.*$#\1#gp'
try {
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
String containerIp = ipService.getContainerIp(container2Id);
if(StringUtils.isEmpty(containerIp)) {
log.error("没有查询对应的容器IP,请检查IpService服务以及CreateTopo的服务");
}
String cid2Ip = containerIp.split("/")[0];
log.info("容器1{} ping 容器2{} IP {}", container1Id, container2Id, cid2Ip);
String pingResult = dockerService.execInDocker(container1Id, new String[]{"ping", cid2Ip, "-c", "4"});
log.info("Ping Result:{}", pingResult);
log.info("容器网络连通性测试成功");
}
public void delCluster() {
try {
clusterService.removeContainersByClusterId(CLUSTERID);
} catch (Exception e) {
log.warn("容器删除失败");
}
log.info("容器集群[{}]删除成功", CLUSTERID);
}
public void delNetworkTopo() {
log.info("开始删除测试容器网络");
topologyService.removeTopoByCluterId(12345L);
log.info("测试集群:{}:删除成功");
}
}
package top.ninwoo.edgecenter.config;
import javafx.scene.control.cell.TextFieldListCell;
import lombok.extern.slf4j.Slf4j;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
......@@ -13,10 +15,11 @@ import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.context.support.WebApplicationContextUtils;
import top.ninwoo.edgecenter.check.ClusterServiceCheck;
import top.ninwoo.edgecenter.register.EdgeNodeRegister;
import javax.annotation.Resource;
@Slf4j
@Configuration
public class ZookeeperConfiguration implements ApplicationContextAware {
private static final Logger LOG = LoggerFactory.getLogger(ZookeeperConfiguration.class);
......@@ -48,6 +51,19 @@ public class ZookeeperConfiguration implements ApplicationContextAware {
@Bean
public ApplicationRunner applicationRunner() {
return args -> {
// 这里加入新的启动自检
ClusterServiceCheck check = applicationContext.getBean(ClusterServiceCheck.class);
try {
check.createCluster();
check.pingTest();
} catch (Exception e) {
// 测试流程失败
log.error("测试流程失败{}", e);
throw new RuntimeException("Error");
} finally {
check.delCluster();
check.delNetworkTopo();
}
EdgeNodeRegister bean = applicationContext.getBean(EdgeNodeRegister.class);
bean.registerNode();
};
......
......@@ -87,20 +87,19 @@ public class TopologyServiceImpl implements TopologyService {
// 创建对应的虚拟交换机
// 在创建前需要检查,是否已经创建过对应的交换机
if(!appName1.contains("remote") && !ovsService.isBridge(appName1.substring(3))) {
LOG.info("Create Ovs Bridge!");
LOG.info("创建虚拟交换机:{}", appName1.substring(3));
ovsService.addBridge(appName1.substring(3));
}
LOG.info("Ovs Bridge[" + appName1 + "] is normal!");
//LOG.info("Ovs Bridge[" + appName1 + "] is normal!");
}
if(appName2.startsWith("br:")) {
flag++;
// 创建对应的虚拟交换机
if(!appName2.contains("remote") && !ovsService.isBridge(appName2.substring(3))) {
LOG.info("Create Ovs Bridge!");
LOG.info("创建虚拟交换机:{}", appName2.substring(3));
ovsService.addBridge(appName2.substring(3));
}
LOG.info("Ovs Bridge[" + appName2 + "] is normal!");
}
// 如果两个都是虚拟交换机,则需要创建交换机连接
......
......@@ -2,7 +2,7 @@ spring:
application:
name: bishe-edge-center
datasource:
url: jdbc:mysql://opengn.org:32306/bishe
url: jdbc:mysql://opengn.org:32306/bishe?useSSL=false
username: root
password: Vudo3423ljo
......@@ -21,6 +21,6 @@ bs:
name: random
ip-prefix: 192
ipservice:
ip: 192.168.31.154:23333
ip: 192.168.1.5:23333
zookeeper:
url: 192.168.31.154:2181
\ No newline at end of file
url: 192.168.1.5:2181
\ No newline at end of file
......@@ -224,7 +224,7 @@ public class DockerServiceImpl implements DockerService, InitializingBean {
throw new RuntimeException("容器不存在");
}
} else {
throw new RuntimeException("容器不存在");
throw new RuntimeException("容器不存在" + containerId);
}
}
String result = dockerUtils.execInDocker(containerId, args);
......
......@@ -169,6 +169,7 @@ public class OVSServiceImpl implements OVSService, InitializingBean {
// 先创建虚拟网卡对
String[] vethPair = ovsUtils.createVethPair(br1, br2);
// 将虚拟网卡对的两端分别接入到ovs上
boolean b = ovsUtils.addBridgePort(br1, vethPair[0]);
boolean b1 = ovsUtils.addBridgePort(br2, vethPair[1]);
......
......@@ -172,7 +172,7 @@ public class OvsUtilsImpl implements OvsUtils {
public boolean addBridgePort(String bridgeName, String port) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl add-port " + bridgeName + " " + port;
String res = linuxCtlUtils.runCmd(cmd);
LOG.info("为Bridge:{} 添加Port:{}", bridgeName, port);
if(!"".equals(res)) {
LOG.warn( "AddBridgePort:" + "[" + bridgeName + "," + port + "]" + res);
return false;
......@@ -183,6 +183,7 @@ public class OvsUtilsImpl implements OvsUtils {
@Override
public boolean delBridgePort(String bridgeName, String port) {
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl del-port " + bridgeName + " " + port;
LOG.info("删除Bridge:{} Port:{}", bridgeName, port);
String res = linuxCtlUtils.runCmd(cmd);
if(!"".equals(res)) {
LOG.warn(bridgeName + ":" + port + "[" + res + "]");
......@@ -202,7 +203,7 @@ public class OvsUtilsImpl implements OvsUtils {
String veth1 = br1 + "_" + br2;
String veth2 = br2 + "_" + br1;
String cmd = "echo 'Vudo3423' | sudo -S ip link add " + veth1 + " type veth peer name " + veth2;
LOG.info("创建虚拟网卡对[{},{}]", veth1, veth2);
String res = linuxCtlUtils.runCmd(cmd);
if(!"".equals(res)) {
LOG.warn("createVethPair:[" + br1 + "," + br2 + "]:" + res);
......@@ -220,7 +221,7 @@ public class OvsUtilsImpl implements OvsUtils {
String veth1 = veths[0];
String veth2 = veths[1];
String cmd = "echo 'Vudo3423' | sudo -S ip link del " + veth1 + " type veth peer name " + veth2;
LOG.info("删除虚拟网卡对[{},{}]", veth1, veth2);
String res = linuxCtlUtils.runCmd(cmd);
if(!"".equals(res)) {
LOG.warn("delVethPair:[" + veth1 + "," + veth2 + "]:" + res);
......@@ -238,6 +239,7 @@ public class OvsUtilsImpl implements OvsUtils {
@Override
public boolean enableLinuxPort(String port) {
String cmd = "echo 'Vudo3423' | sudo -S ifconfig " + port + " up";
LOG.info("启动Linux端口:{}",port);
String res = linuxCtlUtils.runCmd(cmd);
if(!"".equals(res)) {
LOG.warn("EnbaleLinuxPort:" + "[" + port + "]:" + port);
......@@ -273,6 +275,7 @@ public class OvsUtilsImpl implements OvsUtils {
// TODO: bug
String cmd = "echo 'Vudo3423' | sudo -S ovs-vsctl add-port " + bridgeName
+ " vxlan0 -- set interface vxlan0 type=vxlan options:remote_ip=" + remoteIp;
LOG.info("设置VXLAN[bridgeName:{},remoteIP:{}]", bridgeName, remoteIp);
String res = linuxCtlUtils.runCmd(cmd);
if(!"".equals(res)) {
LOG.warn("setVxlan:" + "[" + bridgeName + "]:" + res);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment