Skip to content

Commit 3b933bd

Browse files
authored
[FRP]: Create FRP task and demo (#674)
* [FRP]: Create FRP task and demo Create a FRP task and demo for setting up ClusterLink behind a NAT or firewall. Signed-off-by: Kfir Toledo <kfir.toledo@ibm.com>
1 parent 8b29ca0 commit 3b933bd

File tree

14 files changed

+868
-0
lines changed

14 files changed

+868
-0
lines changed

demos/frp/kind/test.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) The ClusterLink Authors.
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
################################################################
16+
# Name: FRP demo that connect 3 Kind clusters using FRP:
17+
# Desc: create 3 kind clusters :
18+
# 1) GW, iPerf3 client, FRP client, and FRP server
19+
# 2) GW, iPerf3 server, and FRP client
20+
# 3) GW, iPerf3 client, and FRP client
21+
###############################################################
22+
import os
23+
import sys
24+
import time
25+
projDir = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname( os.path.abspath(__file__)))))
26+
sys.path.insert(0,f'{projDir}')
27+
28+
from demos.utils.common import printHeader
29+
from demos.utils.kind import Cluster
30+
from demos.iperf3.kind.iperf3_client_start import directTestIperf3,testIperf3Client
31+
from demos.frp.test import frpTest
32+
33+
testOutputFolder = f"{projDir}/bin/tests/frp"
34+
35+
############################### MAIN ##########################
36+
if __name__ == "__main__":
37+
printHeader("\n\nStart Kind Test\n\n")
38+
printHeader("Start pre-setting")
39+
40+
# cl parameters
41+
cl1= Cluster("peer1")
42+
cl2= Cluster("peer2")
43+
cl3= Cluster("peer3")
44+
srcSvc = "iperf3-client"
45+
destSvc = "iperf3-server"
46+
destPort = 5000
47+
iperf3DirectPort = "30001"
48+
49+
# Setup
50+
frpTest(cl1, cl2, cl3, testOutputFolder)
51+
#Testing
52+
printHeader("\n\nStart Iperf3 testing")
53+
cl2.useCluster()
54+
cl2.setKindIp()
55+
directTestIperf3(cl1, srcSvc, cl2.ip, iperf3DirectPort)
56+
time.sleep(5)
57+
testIperf3Client(cl1, srcSvc, destSvc, destPort)
58+
testIperf3Client(cl3, srcSvc, destSvc, destPort)
59+
60+
61+

demos/frp/test.py

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
#!/usr/bin/env python3
2+
# Copyright (c) The ClusterLink Authors.
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
################################################################
16+
# Name: FRP demo that connect 3 clusters using FRP:
17+
# Desc: create 3 kind clusters :
18+
# 1) GW, iPerf3 client, FRP client, and FRP server
19+
# 2) GW, iPerf3 server, and FRP client
20+
# 3) GW, iPerf3 client, and FRP client
21+
###############################################################
22+
import os
23+
import sys
24+
import time
25+
import hashlib
26+
projDir = os.path.dirname(os.path.dirname(os.path.dirname( os.path.abspath(__file__))))
27+
28+
sys.path.insert(0,f'{projDir}')
29+
30+
from demos.utils.common import printHeader, runcmd
31+
from demos.utils.kind import Cluster as KindCluster
32+
from demos.utils.common import printHeader
33+
34+
# Folders
35+
folCl=f"{projDir}/demos/iperf3/testdata/manifests/iperf3-client"
36+
folSv=f"{projDir}/demos/iperf3/testdata/manifests/iperf3-server"
37+
folFrp=f"{projDir}/demos/frp/testdata/manifests/"
38+
39+
# Services
40+
srcSvc = "iperf3-client"
41+
destSvc = "iperf3-server"
42+
destPort = 5000
43+
namespace = "default"
44+
frpNamespace= "frp"
45+
46+
def frpTest(cl1:KindCluster, cl2:KindCluster,cl3:KindCluster, testOutputFolder):
47+
print(f'Working directory {projDir}')
48+
os.chdir(projDir)
49+
50+
# Build docker environment
51+
printHeader("Build docker image")
52+
os.system("make docker-build")
53+
os.system("make install")
54+
55+
# Create Kind clusters environment
56+
cl1.createCluster(runBg=True)
57+
cl3.createCluster(runBg=True)
58+
cl2.createCluster(runBg=False)
59+
60+
# Start Kind clusters environment
61+
cl1.create_fabric(testOutputFolder)
62+
cl1.startCluster(testOutputFolder)
63+
cl2.startCluster(testOutputFolder)
64+
cl3.startCluster(testOutputFolder)
65+
66+
# Create iPerf3 micro-services
67+
cl1.loadService(srcSvc, "taoyou/iperf3-alpine",f"{folCl}/iperf3-client.yaml" )
68+
cl2.loadService(destSvc, "taoyou/iperf3-alpine",f"{folSv}/iperf3.yaml" )
69+
cl3.loadService(srcSvc, "taoyou/iperf3-alpine",f"{folCl}/iperf3-client.yaml" )
70+
os.environ['FRP_SERVER_IP'] = cl1.ip
71+
os.environ['FRP_SECRET_KEY'] = hashlib.sha256(str(time.time_ns()).encode()).hexdigest()[:10]
72+
# Use envsubst to replace the placeholder and apply the ConfigMap
73+
cl1.useCluster()
74+
runcmd(f"kubectl apply -f {folFrp}/frp-ns.yaml")
75+
runcmd(f"kubectl apply -f {folFrp}/server/frps-configmap.yaml")
76+
cl1.loadService("frps", "snowdreamtech/frps",f"{folFrp}/server/frps.yaml", frpNamespace)
77+
78+
# Create peers
79+
printHeader("Create peers")
80+
cl1.useCluster()
81+
runcmd(f"envsubst < {folFrp}/client/peer1/frpc-configmap.yaml| kubectl apply -f -")
82+
cl1.loadService("frpc", "snowdreamtech/frpc",f"{folFrp}/client/frpc.yaml",frpNamespace )
83+
runcmd(f"kubectl apply -f {folFrp}/client/peer1/peer.yaml")
84+
cl2.useCluster()
85+
runcmd(f"kubectl apply -f {folFrp}/frp-ns.yaml")
86+
runcmd(f"envsubst < {folFrp}/client/peer2/frpc-configmap.yaml| kubectl apply -f -")
87+
cl2.loadService("frpc", "snowdreamtech/frpc",f"{folFrp}/client/frpc.yaml",frpNamespace )
88+
runcmd(f"kubectl apply -f {folFrp}/client/frpc.yaml")
89+
runcmd(f"kubectl apply -f {folFrp}/client/peer2/peer.yaml")
90+
cl3.useCluster()
91+
runcmd(f"kubectl apply -f {folFrp}/frp-ns.yaml")
92+
runcmd(f"envsubst < {folFrp}/client/peer3/frpc-configmap.yaml| kubectl apply -f -")
93+
cl3.loadService("frpc", "snowdreamtech/frpc",f"{folFrp}/client/frpc.yaml" ,frpNamespace)
94+
runcmd(f"kubectl apply -f {folFrp}/client/peer3/peer.yaml")
95+
# Create exports
96+
cl2.exports.create(destSvc, namespace, destPort)
97+
98+
# Import destination service
99+
printHeader(f"\n\nStart Importing {destSvc} service to {cl1.name}")
100+
cl1.imports.create(destSvc,namespace,destPort,cl2.name,destSvc,namespace)
101+
cl3.imports.create(destSvc,namespace,destPort,cl2.name,destSvc,namespace)
102+
103+
# Add policy
104+
printHeader("Applying policies")
105+
cl1.policies.create(name="allow-all",namespace=namespace, action="allow", from_attribute=[{"workloadSelector": {}}],to_attribute=[{"workloadSelector": {}}])
106+
cl2.policies.create(name="allow-all",namespace=namespace, action="allow", from_attribute=[{"workloadSelector": {}}],to_attribute=[{"workloadSelector": {}}])
107+
cl3.policies.create(name="allow-all",namespace=namespace, action="allow", from_attribute=[{"workloadSelector": {}}],to_attribute=[{"workloadSelector": {}}])
108+
109+
110+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: frpc
5+
namespace: frp
6+
spec:
7+
replicas: 1
8+
selector:
9+
matchLabels:
10+
app: frpc
11+
template:
12+
metadata:
13+
labels:
14+
app: frpc
15+
spec:
16+
# hostNetwork: true
17+
containers:
18+
- name: frpc
19+
image: snowdreamtech/frpc
20+
imagePullPolicy: IfNotPresent
21+
volumeMounts:
22+
- name: frpc-config-volume
23+
mountPath: /etc/frp
24+
volumes:
25+
- name: frpc-config-volume
26+
configMap:
27+
name: frpc-config
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: frpc-config
5+
namespace: frp
6+
data:
7+
frpc.toml: |
8+
# Set server address
9+
serverAddr = "${FRP_SERVER_IP}"
10+
serverPort = 30444
11+
12+
[[proxies]]
13+
name = "clusterlink-peer1"
14+
type = "stcp"
15+
localIP = "clusterlink.clusterlink-system.svc.cluster.local"
16+
localPort = 443
17+
secretKey = "${FRP_SECRET_KEY}"
18+
19+
[[visitors]]
20+
name = "clusterlink-peer1-to-peer2-visitor"
21+
type = "stcp"
22+
serverName = "clusterlink-peer2"
23+
secretKey = "${FRP_SECRET_KEY}"
24+
bindAddr = "::"
25+
bindPort = 6002
26+
27+
[[visitors]]
28+
name = "clusterlink-peer1-to-peer3-visitor"
29+
type = "stcp"
30+
serverName = "clusterlink-peer3"
31+
secretKey = "${FRP_SECRET_KEY}"
32+
bindAddr = "::"
33+
bindPort = 6003
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
apiVersion: clusterlink.net/v1alpha1
2+
kind: Peer
3+
metadata:
4+
name: peer2
5+
namespace: clusterlink-system
6+
spec:
7+
gateways:
8+
- host: frp-peer2-clusterlink.frp.svc.cluster.local
9+
port: 6002
10+
11+
---
12+
apiVersion: v1
13+
kind: Service
14+
metadata:
15+
name: frp-peer2-clusterlink
16+
namespace: frp
17+
spec:
18+
type: ClusterIP
19+
selector:
20+
app: frpc
21+
ports:
22+
- port: 6002
23+
targetPort: 6002
24+
---
25+
apiVersion: clusterlink.net/v1alpha1
26+
kind: Peer
27+
metadata:
28+
name: peer3
29+
namespace: clusterlink-system
30+
spec:
31+
gateways:
32+
- host: frp-peer3-clusterlink.frp.svc.cluster.local
33+
port: 6003
34+
35+
---
36+
apiVersion: v1
37+
kind: Service
38+
metadata:
39+
name: frp-peer3-clusterlink
40+
namespace: frp
41+
spec:
42+
type: ClusterIP
43+
selector:
44+
app: frpc
45+
ports:
46+
- port: 6003
47+
targetPort: 6003
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: frpc-config
5+
namespace: frp
6+
data:
7+
frpc.toml: |
8+
# Set server address
9+
serverAddr = "${FRP_SERVER_IP}"
10+
serverPort = 30444
11+
12+
[[proxies]]
13+
name = "clusterlink-peer2"
14+
type = "stcp"
15+
localIP = "clusterlink.clusterlink-system.svc.cluster.local"
16+
localPort = 443
17+
secretKey = "${FRP_SECRET_KEY}"
18+
19+
[[visitors]]
20+
name = "clusterlink-peer2-to-peer1-visitor"
21+
type = "stcp"
22+
serverName = "clusterlink-peer1"
23+
secretKey = "${FRP_SECRET_KEY}"
24+
bindAddr = "::"
25+
bindPort = 6001
26+
27+
[[visitors]]
28+
name = "clusterlink-peer2-to-peer3-visitor"
29+
type = "stcp"
30+
serverName = "clusterlink-peer3"
31+
secretKey = "${FRP_SECRET_KEY}"
32+
bindAddr = "::"
33+
bindPort = 6003
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
apiVersion: clusterlink.net/v1alpha1
2+
kind: Peer
3+
metadata:
4+
name: peer1
5+
namespace: clusterlink-system
6+
spec:
7+
gateways:
8+
- host: frp-peer1-clusterlink.frp.svc.cluster.local
9+
port: 6001
10+
---
11+
apiVersion: v1
12+
kind: Service
13+
metadata:
14+
name: frp-peer1-clusterlink
15+
namespace: frp
16+
spec:
17+
type: ClusterIP
18+
selector:
19+
app: frpc
20+
ports:
21+
- port: 6001
22+
targetPort: 6001
23+
---
24+
apiVersion: clusterlink.net/v1alpha1
25+
kind: Peer
26+
metadata:
27+
name: peer3
28+
namespace: clusterlink-system
29+
spec:
30+
gateways:
31+
- host: frp-peer3-clusterlink.frp.svc.cluster.local
32+
port: 6003
33+
---
34+
apiVersion: v1
35+
kind: Service
36+
metadata:
37+
name: frp-peer3-clusterlink
38+
namespace: frp
39+
spec:
40+
type: ClusterIP
41+
selector:
42+
app: frpc
43+
ports:
44+
- port: 6003
45+
targetPort: 6003
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
apiVersion: v1
2+
kind: ConfigMap
3+
metadata:
4+
name: frpc-config
5+
namespace: frp
6+
data:
7+
frpc.toml: |
8+
# Set server address
9+
serverAddr = "${FRP_SERVER_IP}"
10+
serverPort = 30444
11+
12+
[[proxies]]
13+
name = "clusterlink-peer3"
14+
type = "stcp"
15+
localIP = "clusterlink.clusterlink-system.svc.cluster.local"
16+
localPort = 443
17+
secretKey = "${FRP_SECRET_KEY}"
18+
19+
[[visitors]]
20+
name = "clusterlink-peer3-to-peer1-visitor"
21+
type = "stcp"
22+
serverName = "clusterlink-peer1"
23+
secretKey = "${FRP_SECRET_KEY}"
24+
bindAddr = "::"
25+
bindPort = 6001
26+
27+
[[visitors]]
28+
name = "clusterlink-peer3-to-peer2-visitor"
29+
type = "stcp"
30+
serverName = "clusterlink-peer2"
31+
secretKey = "${FRP_SECRET_KEY}"
32+
bindAddr = "::"
33+
bindPort = 6002

0 commit comments

Comments
 (0)