Skip to content

Commit 9c156ef

Browse files
cross namespace network traffic allow plugin (#1399)
1 parent a3019ea commit 9c156ef

File tree

5 files changed

+375
-106
lines changed

5 files changed

+375
-106
lines changed

examples/getting-started.md

Lines changed: 155 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -1,109 +1,110 @@
1-
# Kubeplus
1+
```markdown
2+
# KubePlus
23

3-
## Getting Started with an example
4+
## Getting Started with an Example
45

5-
Let’s look at an example of creating a multi-instance WordPress Service using KubePlus. The WordPress service provider goes through the following steps towards this on their cluster:
6+
Let’s look at an example of creating a multi-instance WordPress Service using KubePlus. The WordPress service provider goes through the following steps on their cluster.
67

78
**NOTE:** If you have not set up KubePlus, follow the [Installation](../README.md#installation) steps to set up KubePlus.
89

9-
1. Create Kubernetes CRD representing WordPress Helm chart.
10+
### 1. Create Kubernetes CRD Representing WordPress Helm Chart
1011

11-
*The WordPress Helm chart can be specified as a [public url](./examples/multitenancy/application-hosting/wordpress/wordpress-service-composition.yaml) or can be [available locally](./examples/multitenancy/application-hosting/wordpress/wordpress-service-composition-localchart.yaml).*
12+
*The WordPress Helm chart can be specified as a [public URL](./examples/multitenancy/application-hosting/wordpress/wordpress-service-composition.yaml) or can be [available locally](./examples/multitenancy/application-hosting/wordpress/wordpress-service-composition-localchart.yaml).*
1213

13-
```sh
14-
kubectl create -f https://raw.githubusercontent.com/cloud-ark/kubeplus/master/examples/multitenancy/application-hosting/wordpress/wordpress-service-composition.yaml --kubeconfig=kubeplus-saas-provider.json
15-
kubectl get resourcecompositions
16-
kubectl describe resourcecomposition wordpress-service-composition
17-
```
18-
19-
If the status of the `wordpress-service-composition` indicates that the new CRD has been created successfully, verify it:
20-
21-
```sh
22-
kubectl get crds
23-
```
24-
25-
You should see `wordpressservices.platformapi.kubeplus` CRD registered.
26-
27-
2. Create WordpressService instance `wp-tenant1`
28-
29-
```sh
30-
kubectl create -f https://raw.githubusercontent.com/cloud-ark/kubeplus/master/examples/multitenancy/application-hosting/wordpress/tenant1.yaml --kubeconfig=kubeplus-saas-provider.json
31-
```
14+
```sh
15+
kubectl create -f https://raw.githubusercontent.com/cloud-ark/kubeplus/master/examples/multitenancy/application-hosting/wordpress/wordpress-service-composition.yaml --kubeconfig=kubeplus-saas-provider.json
16+
kubectl get resourcecompositions
17+
kubectl describe resourcecomposition wordpress-service-composition
18+
```
3219

33-
3. Create WordpressService instance `wp-tenant2`
20+
If the status of the `wordpress-service-composition` indicates that the new CRD has been created successfully, verify it:
3421

35-
```sh
36-
kubectl create -f https://raw.githubusercontent.com/cloud-ark/kubeplus/master/examples/multitenancy/application-hosting/wordpress/tenant2.yaml --kubeconfig=kubeplus-saas-provider.json
37-
```
22+
```sh
23+
kubectl get crds
24+
```
3825

39-
4. Check created WordpressService instances
26+
You should see `wordpressservices.platformapi.kubeplus` CRD registered.
4027

41-
```sh
42-
kubectl get wordpressservices
28+
### 2. Create WordpressService Instance `wp-tenant1`
4329

44-
NAME AGE
45-
wp-tenant1 86s
46-
wp-tenant2 26s
47-
```
30+
```sh
31+
kubectl create -f https://raw.githubusercontent.com/cloud-ark/kubeplus/master/examples/multitenancy/application-hosting/wordpress/tenant1.yaml --kubeconfig=kubeplus-saas-provider.json
32+
```
4833

49-
5. Check the details of created instance
34+
### 3. Create WordpressService Instance `wp-tenant2`
5035

51-
```sh
52-
kubectl describe wordpressservices wp-tenant1
53-
```
36+
```sh
37+
kubectl create -f https://raw.githubusercontent.com/cloud-ark/kubeplus/master/examples/multitenancy/application-hosting/wordpress/tenant2.yaml --kubeconfig=kubeplus-saas-provider.json
38+
```
5439

55-
6. Check created application resources
40+
### 4. Check Created WordpressService Instances
5641

57-
* Notice that the `WordpressService` instance resources are deployed in a Namespace `wp-tenant1`, which was created by KubePlus.
42+
```sh
43+
kubectl get wordpressservices
5844

59-
```sh
60-
kubectl appresources WordpressService wp-tenant1 –k kubeplus-saas-provider.json
45+
NAME AGE
46+
wp-tenant1 86s
47+
wp-tenant2 26s
48+
```
6149

62-
NAMESPACE KIND NAME
63-
default WordpressService wp-tenant1
64-
wp-tenant1 PersistentVolumeClaim mysql-pv-claim
65-
wp-tenant1 PersistentVolumeClaim wp-for-tenant1
66-
wp-tenant1 Service wordpress-mysql
67-
wp-tenant1 Service wp-for-tenant1
68-
wp-tenant1 Deployment mysql
69-
wp-tenant1 Deployment wp-for-tenant1
70-
wp-tenant1 Pod mysql-76d6d9bdfd-2wl2p
71-
wp-tenant1 Pod wp-for-tenant1-87c4c954-s2cct
72-
wp-tenant1 NetworkPolicy allow-external-traffic
73-
wp-tenant1 NetworkPolicy restrict-cross-ns-traffic
74-
wp-tenant1 ResourceQuota wordpressservice-wp-tenant1
75-
```
50+
### 5. Check the Details of the Created Instance
7651

77-
7. Check application resource consumption
52+
```sh
53+
kubectl describe wordpressservices wp-tenant1
54+
```
7855

79-
```sh
80-
kubectl metrics WordpressService wp-tenant1 $KUBEPLUS_NS -k kubeplus-saas-provider.json
56+
### 6. Check Created Application Resources
57+
58+
Notice that the `WordpressService` instance resources are deployed in a Namespace `wp-tenant1`, which was created by KubePlus.
59+
60+
```sh
61+
kubectl appresources WordpressService wp-tenant1 -k kubeplus-saas-provider.json
62+
63+
NAMESPACE KIND NAME
64+
default WordpressService wp-tenant1
65+
wp-tenant1 PersistentVolumeClaim mysql-pv-claim
66+
wp-tenant1 PersistentVolumeClaim wp-for-tenant1
67+
wp-tenant1 Service wordpress-mysql
68+
wp-tenant1 Service wp-for-tenant1
69+
wp-tenant1 Deployment mysql
70+
wp-tenant1 Deployment wp-for-tenant1
71+
wp-tenant1 Pod mysql-76d6d9bdfd-2wl2p
72+
wp-tenant1 Pod wp-for-tenant1-87c4c954-s2cct
73+
wp-tenant1 NetworkPolicy allow-external-traffic
74+
wp-tenant1 NetworkPolicy restrict-cross-ns-traffic
75+
wp-tenant1 ResourceQuota wordpressservice-wp-tenant1
76+
```
8177

82-
----------------------------------------------------------
83-
Kubernetes Resources created:
84-
Number of Sub-resources: -
85-
Number of Pods: 2
86-
Number of Containers: 2
87-
Number of Nodes: 1
88-
Number of Not Running Pods: 0
89-
Underlying Physical Resoures consumed:
90-
Total CPU(cores): 0.773497m
91-
Total MEMORY(bytes): 516.30859375Mi
92-
Total Storage(bytes): 40Gi
93-
Total Network bytes received: 0
94-
Total Network bytes transferred: 0
95-
----------------------------------------------------------
96-
```
78+
### 7. Check Application Resource Consumption
79+
80+
```sh
81+
kubectl metrics WordpressService wp-tenant1 $KUBEPLUS_NS -k kubeplus-saas-provider.json
82+
83+
----------------------------------------------------------
84+
Kubernetes Resources created:
85+
Number of Sub-resources: -
86+
Number of Pods: 2
87+
Number of Containers: 2
88+
Number of Nodes: 1
89+
Number of Not Running Pods: 0
90+
Underlying Physical Resources consumed:
91+
Total CPU(cores): 0.773497m
92+
Total MEMORY(bytes): 516.30859375Mi
93+
Total Storage(bytes): 40Gi
94+
Total Network bytes received: 0
95+
Total Network bytes transferred: 0
96+
----------------------------------------------------------
97+
```
9798

98-
8. Cleanup
99+
### 8. Cleanup
99100

100-
```sh
101-
kubectl delete wordpressservice wp-tenant1 --kubeconfig=kubeplus-saas-provider.json
102-
kubectl delete wordpressservice wp-tenant2 --kubeconfig=kubeplus-saas-provider.json
103-
kubectl delete resourcecomposition wordpress-service-composition --kubeconfig=kubeplus-saas-provider.json
104-
helm delete kubeplus -n $KUBEPLUS_NS
105-
python3 provider-kubeconfig.py delete $KUBEPLUS_NS
106-
```
101+
```sh
102+
kubectl delete wordpressservice wp-tenant1 --kubeconfig=kubeplus-saas-provider.json
103+
kubectl delete wordpressservice wp-tenant2 --kubeconfig=kubeplus-saas-provider.json
104+
kubectl delete resourcecomposition wordpress-service-composition --kubeconfig=kubeplus-saas-provider.json
105+
helm delete kubeplus -n $KUBEPLUS_NS
106+
python3 provider-kubeconfig.py delete $KUBEPLUS_NS
107+
```
107108

108109
## Network Isolation Testing
109110

@@ -115,61 +116,111 @@ This section verifies that the network policies are correctly isolating applicat
115116

116117
On Minikube, install a network driver capable of recognizing `NetworkPolicy` objects (e.g., Cilium):
117118

118-
```bash
119-
$ minikube start --cni=cilium
120-
$ eval $(minikube docker-env)
119+
```sh
120+
minikube start --cni=cilium
121+
eval $(minikube docker-env)
121122
```
122123

123-
#### Refer main README for installing the kubeplus operator and plugings
124+
#### Refer to Main README for Installing the KubePlus Operator and Plugins
124125

125126
#### Create HelloWorldService Instances
126127

127-
```bash
128-
$ kubectl create -f hello-world-service-composition.yaml --kubeconfig=provider.conf
129-
$ kubectl create -f hs1.yaml --kubeconfig=provider.conf
130-
$ kubectl create -f hs2.yaml --kubeconfig=provider.conf
128+
```sh
129+
kubectl create -f hello-world-service-composition.yaml --kubeconfig=provider.conf
130+
kubectl create -f hs1.yaml --kubeconfig=provider.conf
131+
kubectl create -f hs2.yaml --kubeconfig=provider.conf
131132
```
132133

133134
#### Test Network Isolation
134135

135136
- **Ping/HTTP Test from `hs1` to `hs2`:**
136137

137-
```bash
138+
```sh
138139
# Get the Pod name for hs1
139140
HELLOWORLD_POD_HS1=$(kubectl get pods -n hs1 --kubeconfig=provider.conf -o jsonpath='{.items[0].metadata.name}')
140-
141+
141142
# Get the Pod IP for hs2
142143
HS2_POD_IP=$(kubectl get pods -n hs2 --kubeconfig=provider.conf -o jsonpath='{.items[0].status.podIP}')
143-
144+
145+
# Update and install curl on hs1 pod
146+
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- apt update
147+
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- apt install curl -y
148+
144149
# Test connectivity from hs1 to hs2 using the IP
145-
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- curl $HS2_POD_IP
150+
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- curl $HS2_POD_IP:5000
146151
```
147152

148153
The connection should be denied.
149154

150155
- **Ping/HTTP Test from `hs2` to `hs1`:**
151156

152-
```bash
157+
```sh
153158
# Get the Pod name for hs2
154159
HELLOWORLD_POD_HS2=$(kubectl get pods -n hs2 --kubeconfig=provider.conf -o jsonpath='{.items[0].metadata.name}')
155-
160+
156161
# Get the Pod IP for hs1
157162
HS1_POD_IP=$(kubectl get pods -n hs1 --kubeconfig=provider.conf -o jsonpath='{.items[0].status.podIP}')
158-
163+
164+
# Update and install curl on hs2 pod
165+
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- apt update
166+
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- apt install curl -y
167+
159168
# Test connectivity from hs2 to hs1 using the IP
160-
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- curl $HS1_POD_IP
169+
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- curl $HS1_POD_IP:5000
161170
```
162171

163172
The connection should be denied.
164173

165-
## Clean Up
174+
### Allowing Cross Namespace Traffic
166175

176+
In some scenarios, you might want to enable controlled communication between instances running in different namespaces. KubePlus provides a custom kubectl plugin for this purpose. To allow bi-directional traffic between the two HelloWorldService instances (deployed in namespaces `hs1` and `hs2`), run:
167177

168-
```bash
169-
$ kubectl delete -f hs1-no-replicas.yaml --kubeconfig=provider.conf
170-
$ kubectl delete -f hs2-no-replicas.yaml --kubeconfig=provider.conf
171-
$ kubectl delete -f hello-world-service-composition.yaml --kubeconfig=provider.conf
178+
```sh
179+
kubectl allow network traffic hs1 hs2 -k provider.conf
172180
```
173181

182+
```sh
183+
# Test connectivity from hs1 to hs2 using the IP
184+
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- curl $HS2_POD_IP:5000
185+
186+
# Test connectivity from hs2 to hs1 using the IP
187+
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- curl $HS1_POD_IP:5000
188+
189+
kubectl get networkpolicy -o yaml restrict-cross-ns-traffic -n hs1
190+
kubectl get networkpolicy -o yaml restrict-cross-ns-traffic -n hs2
191+
```
192+
193+
194+
You should see that each policy’s ingress section now includes a rule that uses a namespaceSelector matching the other namespace (using the label `kubernetes.io/metadata.name`).
195+
196+
197+
The connection should be allowed
198+
199+
200+
To deny the traffic between namespace
201+
202+
```sh
203+
kubectl deny network traffic hs1 hs2 -k provider.conf
204+
```
205+
206+
```sh
207+
# Test connectivity from hs1 to hs2 using the IP
208+
kubectl exec -it $HELLOWORLD_POD_HS1 -n hs1 --kubeconfig=provider.conf -- curl $HS2_POD_IP:5000
209+
210+
# Test connectivity from hs2 to hs1 using the IP
211+
kubectl exec -it $HELLOWORLD_POD_HS2 -n hs2 --kubeconfig=provider.conf -- curl $HS1_POD_IP:5000
212+
```
213+
214+
215+
216+
## Clean Up
217+
218+
```sh
219+
kubectl delete -f hs1-no-replicas.yaml --kubeconfig=provider.conf
220+
kubectl delete -f hs2-no-replicas.yaml --kubeconfig=provider.conf
221+
kubectl delete -f hello-world-service-composition.yaml --kubeconfig=provider.conf
222+
```
174223

175224
Ensure the `helloworldservices.platformapi.kubeplus` CRD is removed.
225+
```
226+
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
source utils.sh # if you have common utility functions; else remove this line
4+
5+
# This wrapper passes all arguments to our Python script.
6+
python3 "$KUBEPLUS_HOME/plugins/network_traffic.py" allow "$@"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/bash
2+
3+
source utils.sh # if you have common utility functions; else remove this line
4+
5+
# This wrapper passes all arguments to our Python script.
6+
python3 "$KUBEPLUS_HOME/plugins/network_traffic.py" deny "$@"

plugins/kubectl-kubeplus-commands

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ print_help () {
2020
echo " kubectl license create"
2121
echo " kubectl license get"
2222
echo " kubectl license delete"
23+
echo " kubectl allow network traffic <ns1> <ns2> [-k <kubeconfig>]"
24+
echo " Allows bidirectional network traffic between the two namespaces"
25+
echo " by adding ingress rules for the 'restrict-cross-ns-traffic' NetworkPolicy"
26+
echo " in both namespaces."
27+
echo " kubectl deny network traffic <ns1> <ns2> [-k <kubeconfig>]"
28+
echo " Denies bidirectional network traffic between the two namespaces"
29+
echo " by removing the specific ingress rules that allow cross-namespace traffic"
30+
echo " from the 'restrict-cross-ns-traffic' NetworkPolicy."
2331
echo ""
2432
echo "DESCRIPTION"
2533
echo " KubePlus provides a suite of kubectl plugins to discover, monitor and troubleshoot Kubernetes applications."
@@ -38,10 +46,10 @@ print_help () {
3846
echo " - kubectl retrieve kubeconfig consumer"
3947
echo " These kubeconfig files are provided with limited RBAC permissions appropriate for the persona."
4048
echo " "
41-
echo " - kubectl grantpermission consumer "
49+
echo " - kubectl grantpermission consumer"
4250
echo " This plugin enables provider to grant permission for the the created service to the consumer."
4351
echo " A consumer will be able to create service instances only after that."
44-
echo " - kubectl appurl "
52+
echo " - kubectl appurl"
4553
echo " This plugin retrieves application url by searching for Service object of type NodePort in an application's"
4654
echo " resource relationship graph and constructing the url."
4755
echo " A consumer will be able to create service instances only after that."

0 commit comments

Comments
 (0)