Currently, various services are becoming microservices. At that time, one of the important parts is communication between services. Also, when introducing microservices, development using kubernetes is often adopted.
Therefore, this time, as a method of communication between services, let's look at the communication method between pods, which is the minimum resource of kuberntes.
Regarding the communication method between services, in addition to communication between pods, There are communication methods such as gRPC.
This time, as a very simple thing, I will try to communicate the pod of the httpd server created by the deployment of k8s and the pod of nginx
httpd
httpd_deployment.yml
apiVersion: v1
kind: Service
metadata:
name: httpd-svc
spec:
selector:
app: httpd
ports:
- port: 8090
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: httpd
spec:
replicas: 1
selector:
matchLabels:
app: httpd
template:
metadata:
labels:
app: httpd
spec:
containers:
- name: httpd
image: httpd:2-alpine
ports:
- containerPort: 80
nginx
nginx_deployment.yml
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
spec:
selector:
app: nginx
ports:
- port: 8080
targetPort: 80
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx
spec:
replicas: 1
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.18-alpine
ports:
- containerPort: 80
--Launch the deployments created above
$ kubectl apply -f httd_deployment.yml -f nginx_deployment.yml
――Check if you have stood up properly
$ kubectl get po
NAME READY STATUS RESTARTS AGE
httpd-7f8bd9884-jptwt 1/1 Running 0 4m26s
nginx-d69ddfd76-r4lhq 1/1 Running 0 4m26s
OK if both statuses are Running
httpd→nginx
--Enter the httpd pod
$ kubectl exec -it httpd-7f8bd9884-jptwt sh
/ #
--Try accessing the nginx pod from within the httpd pod
/ # curl http://nginx-svc:8080
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
<style>
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>
nginx→httpd --Enter the nginx pod
$ kubectl exec -it nginx-d69ddfd76-r4lhq sh
--Try accessing the httpd pod from within the nginx pod
/ # curl http://httpd-svc:8090
<html><body><h1>It works!</h1></body></html>
I was able to confirm that I was able to access it.
Recommended Posts