2025-04-26 12:16:44 +08:00
|
|
|
# Create two namespaces and name them ns1 and ns2
|
|
|
|
```bash
|
2025-04-26 01:18:52 +08:00
|
|
|
k create ns ns1
|
|
|
|
k create ns ns2
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
# Create a deployment with a single replica in each of these namespaces with the image as nginx and name as deploy-ns1 and deploy-ns2, respectively
|
|
|
|
```bash
|
2025-04-26 01:18:52 +08:00
|
|
|
k create deployment deploy-ns1 --image=nginx --replicas=1 -n ns1
|
|
|
|
k create deployment deploy-ns2 --image=nginx --replicas=1 -n ns2
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
# Get the IP address of each of the pods (Remember the kubectl command for that?)
|
|
|
|
```bash
|
2025-04-26 01:18:52 +08:00
|
|
|
k get po -n ns1 -o wide
|
|
|
|
k get po -n ns2 -o wide
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
# Exec into the pod of deploy-ns1 and try to curl the IP address of the pod running on deploy-ns2
|
|
|
|
```bash
|
2025-04-26 01:18:52 +08:00
|
|
|
k exec -it -n ns1 deploy-ns1-56c4d4b7-xzvl2 -- sh
|
|
|
|
curl 10.244.2.4
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
# Your pod-to-pod connection should work, and you should be able to get a successful response back.
|
|
|
|
```html
|
2025-04-26 01:18:52 +08:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<title>Welcome to nginx!</title>
|
|
|
|
<style>
|
|
|
|
html { color-scheme: light dark; }
|
|
|
|
body { width: 35em; margin: 0 auto;
|
|
|
|
font-family: Tahoma, Verdana, Arial, sans-serif; }
|
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Welcome to nginx!</h1>
|
|
|
|
<p>If you see this page, the nginx web server is successfully installed and
|
|
|
|
working. Further configuration is required.</p>
|
|
|
|
|
|
|
|
<p>For online documentation and support please refer to
|
|
|
|
<a href="http://nginx.org/">nginx.org</a>.<br/>
|
|
|
|
Commercial support is available at
|
|
|
|
<a href="http://nginx.com/">nginx.com</a>.</p>
|
|
|
|
|
|
|
|
<p><em>Thank you for using nginx.</em></p>
|
|
|
|
</body>
|
|
|
|
</html>
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
# Now scale both of your deployments from 1 to 3 replicas.
|
|
|
|
```bash
|
2025-04-26 01:18:52 +08:00
|
|
|
k scale --replicas=3 -n ns1 deploy/deploy-ns1
|
|
|
|
k scale --replicas=3 -n ns2 deploy/deploy-ns2
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
deploy-ns1-56c4d4b7-xzvl2 10.244.2.3 kind-worker2
|
|
|
|
deploy-ns2-846f6b4c9b-r85f7 10.244.2.4 kind-worker2
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
# Create two services to expose both of your deployments and name them svc-ns1 and svc-ns2
|
|
|
|
```bash
|
2025-04-26 01:18:52 +08:00
|
|
|
k expose deployment deploy-ns1 -n ns1 --port=80 --target-port=80
|
|
|
|
k expose deployment deploy-ns2 -n ns2 --port=80 --target-port=80
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
# exec into each pod and try to curl the IP address of the service running on the other namespace.
|
|
|
|
```bash
|
2025-04-26 01:18:52 +08:00
|
|
|
k exec -it deploy-ns1-56c4d4b7-4tptj -n ns1 -- sh
|
|
|
|
curl 10.244.1.6
|
|
|
|
k exec -it deploy-ns2-846f6b4c9b-7dqwf -n ns2 -- sh
|
|
|
|
curl 10.244.1.4
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
# This curl should work.
|
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
Yes
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
# Now try curling the service name instead of IP. You will notice that you are getting an error and cannot resolve the host.
|
|
|
|
```bash
|
2025-04-26 01:18:52 +08:00
|
|
|
in deploy-ns2-846f6b4c9b-7dqwf:
|
|
|
|
curl deploy-ns1-56c4d4b7-4tptj not work
|
|
|
|
curl deploy-ns2-846f6b4c9b-7dqwf works
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
2025-04-26 01:18:52 +08:00
|
|
|
|
2025-04-26 12:16:44 +08:00
|
|
|
# Now use the FQDN of the service and try to curl again, this should work.
|
|
|
|
```bash
|
2025-04-26 01:18:52 +08:00
|
|
|
curl deploy-ns1.ns1.svc.cluster.local
|
|
|
|
works
|
2025-04-26 12:16:44 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
# In the end, delete both the namespaces, which should delete the services and deployments underneath them.
|
|
|
|
```bash
|
2025-04-26 01:18:52 +08:00
|
|
|
k delete svc deploy-ns1 -n ns1
|
|
|
|
k delete svc deploy-ns2 -n ns2
|
|
|
|
|
|
|
|
k delete deployment deploy-ns1 -n ns1
|
|
|
|
k delete deployment deploy-ns2 -n ns2
|
2025-04-26 12:16:44 +08:00
|
|
|
k delete ns ns1 ns2
|
|
|
|
```
|