|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * networking/understanding-networking.adoc |
| 4 | + |
| 5 | +:_mod-docs-content-type: PROCEDURE |
| 6 | +[id="nw-understanding-networking-dns-example_{context}"] |
| 7 | += Example: DNS use case |
| 8 | + |
| 9 | +For this example, a front-end application is running in one set of pods and a back-end service is running in another set of pods. The front-end application needs to communicate with the back-end service. You create a service for the back-end pods that gives it a stable IP address and DNS name. The front-end pods use this DNS name to access the back-end service regardless of changes to individual pod IP addresses. |
| 10 | + |
| 11 | +By creating a service for the back-end pods, you provide a stable IP and DNS name, `backend-service.default.svc.cluster.local`, that the front-end pods can use to communicate with the back-end service. This setup would ensure that even if individual pod IP addresses change, the communication remains consistent and reliable. |
| 12 | + |
| 13 | +The following steps demonstrate an example of how to configure front-end pods to communicate with a back-end service using DNS. |
| 14 | + |
| 15 | +. Create the back-end service. |
| 16 | + |
| 17 | +.. Deploy the back-end pods. |
| 18 | ++ |
| 19 | +[source, yaml] |
| 20 | +---- |
| 21 | +apiVersion: apps/v1 |
| 22 | +kind: Deployment |
| 23 | +metadata: |
| 24 | + name: backend-deployment |
| 25 | + labels: |
| 26 | + app: backend |
| 27 | +spec: |
| 28 | + replicas: 3 |
| 29 | + selector: |
| 30 | + matchLabels: |
| 31 | + app: backend |
| 32 | + template: |
| 33 | + metadata: |
| 34 | + labels: |
| 35 | + app: backend |
| 36 | + spec: |
| 37 | + containers: |
| 38 | + - name: backend-container |
| 39 | + image: your-backend-image |
| 40 | + ports: |
| 41 | + - containerPort: 8080 |
| 42 | +---- |
| 43 | + |
| 44 | +.. Define a service to expose the back-end pods. |
| 45 | ++ |
| 46 | +[source, yaml] |
| 47 | +---- |
| 48 | +apiVersion: v1 |
| 49 | +kind: Service |
| 50 | +metadata: |
| 51 | + name: backend-service |
| 52 | +spec: |
| 53 | + selector: |
| 54 | + app: backend |
| 55 | + ports: |
| 56 | + - protocol: TCP |
| 57 | + port: 80 |
| 58 | + targetPort: 8080 |
| 59 | +---- |
| 60 | + |
| 61 | +. Create the front-end pods. |
| 62 | + |
| 63 | +.. Define the front-end pods. |
| 64 | ++ |
| 65 | +[source, yaml] |
| 66 | +---- |
| 67 | +apiVersion: apps/v1 |
| 68 | +kind: Deployment |
| 69 | +metadata: |
| 70 | + name: frontend-deployment |
| 71 | + labels: |
| 72 | + app: frontend |
| 73 | + spec: |
| 74 | + replicas: 3 |
| 75 | + selector: |
| 76 | + matchLabels: |
| 77 | + app: frontend |
| 78 | + template: |
| 79 | + metadata: |
| 80 | + labels: |
| 81 | + app: frontend |
| 82 | + spec: |
| 83 | + containers: |
| 84 | + - name: frontend-container |
| 85 | + image: your-frontend-image |
| 86 | + ports: |
| 87 | + - containerPort: 80 |
| 88 | +---- |
| 89 | + |
| 90 | +.. Apply the pod definition to your cluster. |
| 91 | ++ |
| 92 | +[source,terminal] |
| 93 | +---- |
| 94 | +$ oc apply -f frontend-deployment.yaml |
| 95 | +---- |
| 96 | + |
| 97 | +. Configure the front-end to communicate with the back-end. |
| 98 | ++ |
| 99 | +In your front-end application code, use the DNS name of the back-end service to send requests. For example, if your front-end application needs to fetch data from the back-end pod, your application might include the following code: |
| 100 | ++ |
| 101 | +[source, JavaScript] |
| 102 | +---- |
| 103 | +fetch('http://backend-service.default.svc.cluster.local/api/data') |
| 104 | + .then(response => response.json()) |
| 105 | + .then(data => console.log(data)); |
| 106 | +---- |
0 commit comments