我正在尝试让 Jgroups 集群在 Istio 上工作。如果没有 istio,我可以让它工作。总之,下面的代码将尝试将 2 个 pod 放入一个 jgroups 集群。当他们在集群中时,我希望看到
NEIGHBORS:
hello-0-19533
hello-1-21469
但是,当我使用 istio 注入将相同的代码部署到命名空间中时,我从未看到过这一点。我只看到独立隔离的 POD。我错过了什么?我需要进行哪些配置才能使其正常工作?可能吗 ?
我知道这可能与如何通过 istio-proxy 代理通信有关。
我使用以下清单部署了 istio
apiVersion: v1
kind: Service
metadata:
name: hello
labels:
app: hello
spec:
clusterIP: None
ports:
- name: tcp-ping
protocol: TCP
port: 7950
targetPort: 7950
selector:
app: hello
---
apiVersion: apps/v1
kind: StatefulSet
metadata:
name: hello
spec:
serviceName: "hello"
replicas: 2
selector:
matchLabels:
app: hello
template:
metadata:
labels:
app: hello
version: v1
spec:
containers:
- name: hello
image: jgroups_stu:latest
imagePullPolicy: IfNotPresent
ports:
- containerPort: 7950
- containerPort: 7951
- containerPort: 7952
我的图像有以下代码
public class main {
public static void main(String[] args) throws Exception {
System.setProperty("java.net.preferIPv4Stack" , "true");
String basePath = main.class.getProtectionDomain().getCodeSource().getLocation().getPath();
System.out.println("PATH: ".concat(basePath));
String inputFile = basePath.concat("test.xml");
String outputFile = basePath.concat("test2.xml");
Map<String, String> env = System.getenv();
String bind_addr = env.get("HOSTNAME");
String bind_port;
if(bind_addr == null) {
bind_addr = "localhost";
}
bind_port = "7950";
if(bind_addr.contains("1")) {
bind_addr = "172.17.0.4";
}else {
bind_addr = "172.17.0.3";
}
System.out.println("HOST: ".concat(bind_addr));
updateXml(inputFile, inputFile, "bind_addr", bind_addr);
updateXml(inputFile, inputFile, "bind_port", bind_port);
updateXml(inputFile, outputFile, "initial_hosts","127.0.0.1[7950],172.17.0.3[7950],172.17.0.4[7950]");
JChannel ch = new JChannel(outputFile);
ch.connect("TestCluster");
final int SLEEP_TIME_IN_MILLIS = 1000;
while (true) {
checkNeighbors(ch);
try {
Thread.sleep(SLEEP_TIME_IN_MILLIS);
} catch (InterruptedException e) {
// Ignored
}
}
}
private static void updateXml(String inputFile, String outputFile, String replaceKey, String replaceVal) throws SAXException, IOException, ParserConfigurationException, XPathExpressionException, TransformerFactoryConfigurationError, TransformerException {
// 1- Build the doc from the XML file
org.w3c.dom.Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(new InputSource(inputFile));
// 2- Locate the node(s) with xpath
XPath xpath = XPathFactory.newInstance().newXPath();
NodeList nodes = (NodeList)xpath.evaluate("//*[contains(@".concat(replaceKey).concat(", '!Here')]"),
doc, XPathConstants.NODESET);
//System.out.println( nodes.getLength());
// 3- Make the change on the selected nodes
for (int idx = 0; idx < nodes.getLength(); idx++) {
Node value = nodes.item(idx).getAttributes().getNamedItem(replaceKey);
String val = value.getNodeValue();
value.setNodeValue(val.replaceAll("!Here", replaceVal));
}
DOMSource domSource = new DOMSource(doc);
StringWriter outWriter = new StringWriter();
// 4- Save the result to a new XML doc
Transformer xformer = TransformerFactory.newInstance().newTransformer();
xformer.transform(domSource, new StreamResult(outWriter));
xformer.transform(domSource, new StreamResult(new File(outputFile)));
StringBuffer sb = outWriter.getBuffer();
String finalstring = sb.toString();
System.out.println(finalstring);
}
private static void checkNeighbors(JChannel channel) {
View view = channel.getView();
List<Address> addresses = view.getMembers();
System.out.println("NEIGHBORS:");
for (Address address : addresses) {
System.out.println(" " + address);
}
}
}
测试.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:org:jgroups"
xsi:schemaLocation="urn:org:jgroups http://www.jgroups.org/schema/jgroups.xsd">
<TCP bind_addr="!Here"
bind_port="!Here"
recv_buf_size="${tcp.recv_buf_size:130k}"
send_buf_size="${tcp.send_buf_size:130k}"
max_bundle_size="64K"
sock_conn_timeout="300"
thread_pool.min_threads="0"
thread_pool.max_threads="20"
thread_pool.keep_alive_time="30000"/>
<TCPPING async_discovery="true"
initial_hosts="${jgroups.tcpping.initial_hosts:!Here}"
port_range="2"/>
<MERGE3 min_interval="10000"
max_interval="30000"/>
<FD_SOCK/>
<FD timeout="3000" max_tries="3" />
<VERIFY_SUSPECT timeout="1500" />
<BARRIER />
<pbcast.NAKACK2 use_mcast_xmit="false"
discard_delivered_msgs="true"/>
<UNICAST3 />
<pbcast.STABLE desired_avg_gossip="50000"
max_bytes="4M"/>
<pbcast.GMS print_local_addr="true" join_timeout="2000"
view_bundling="true"/>
<MFC max_credits="2M"
min_threshold="0.4"/>
<FRAG2 frag_size="60K" />
<!--RSVP resend_interval="2000" timeout="10000"/-->
<pbcast.STATE_TRANSFER/>
</config>