[K8s] I tried communication between pods!

Introduction

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.

What to make this time

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

For the time being, try communicating

--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

[K8s] I tried communication between pods!
I tried UDP communication with Java
I tried Spring.
I tried tomcat
I tried youtubeDataApi.
I tried refactoring ①
I tried FizzBuzz.
I tried JHipster 5.1
[I tried] Spring tutorial
I tried using Gson
I tried QUARKUS immediately
I tried using TestNG
I tried Spring Batch
I tried using Galasa
I tried node-jt400 (Programs)
I tried node-jt400 (execute)
About communication between containers
Difference between i ++ and ++ i
I tried node-jt400 (Transactions)
I tried to implement the like function by asynchronous communication
I tried node-jt400 (Environment construction)
I tried DI with Ruby
I tried node-jt400 (IFS write)
I tried using azure cloud-init
I tried Spring State machine
I tried Drools (Java, InputStream)
I tried Rails beginner [Chapter 1]
I tried the Docker tutorial!
I tried using Apache Wicket
I tried the VueJS tutorial!
I tried node-jt400 (SQL query)
I tried using Java REPL
I tried source code analysis
I tried the FizzBuzz problem
I tried node-jt400 (SQL stream)
I tried node-jt400 (IFS read)
I tried putting XcodeGen + SwiftPM
I tried Rails beginner [Chapter 2]
I tried UPSERT with PostgreSQL.
I tried BIND with Docker
I tried to verify yum-cron
I tried Jets (ruby serverless)
I tried metaprogramming in Java