|
| 1 | +// Module included in the following assemblies: |
| 2 | +// |
| 3 | +// * microshift_networking/microshift-networking.adoc |
| 4 | + |
| 5 | +:_content-type: PROCEDURE |
| 6 | +[id="microshift-deploying-a-load-balancer_{context}"] |
| 7 | += Deploying a TCP load balancer on a workload |
| 8 | + |
| 9 | +{product-title} offers a built-in implementation of network load balancers. The following example procedure uses the node IP address as the external IP address for the `LoadBalancer` service configuration file. |
| 10 | + |
| 11 | +.Prerequisites |
| 12 | + |
| 13 | +* You installed the OpenShift CLI (`oc`) |
| 14 | +* You need access to the cluster as a user with the cluster-admin role. |
| 15 | +* You installed a cluster on an infrastructure configured with the OVN-Kubernetes network plugin. |
| 16 | +* The `KUBECONFIG` environment variable is set. |
| 17 | +
|
| 18 | +.Procedure |
| 19 | + |
| 20 | +. Verify that your pods are running by running the following command: |
| 21 | ++ |
| 22 | +[source,terminal] |
| 23 | +---- |
| 24 | +$ oc get pods -A |
| 25 | +---- |
| 26 | + |
| 27 | +. Create a namespace by running the following commands: |
| 28 | ++ |
| 29 | +[source,terminal] |
| 30 | +---- |
| 31 | +$ NAMESPACE=nginx-lb-test |
| 32 | +---- |
| 33 | ++ |
| 34 | +[source,terminal] |
| 35 | +---- |
| 36 | +$ oc create ns $NAMESPACE |
| 37 | +---- |
| 38 | +. The following example deploys three replicas of the test `nginx` application in your namespace. |
| 39 | ++ |
| 40 | +[source,terminal] |
| 41 | +---- |
| 42 | +$ oc apply -n $NAMESPACE -f - <<EOF |
| 43 | +apiVersion: v1 |
| 44 | +kind: ConfigMap |
| 45 | +metadata: |
| 46 | + name: nginx |
| 47 | +data: |
| 48 | + headers.conf: | |
| 49 | + add_header X-Server-IP \$server_addr always; |
| 50 | +--- |
| 51 | +apiVersion: apps/v1 |
| 52 | +kind: Deployment |
| 53 | +metadata: |
| 54 | + name: nginx |
| 55 | +spec: |
| 56 | + replicas: 3 |
| 57 | + selector: |
| 58 | + matchLabels: |
| 59 | + app: nginx |
| 60 | + template: |
| 61 | + metadata: |
| 62 | + labels: |
| 63 | + app: nginx |
| 64 | + spec: |
| 65 | + containers: |
| 66 | + - image: quay.io/packit/nginx-unprivileged |
| 67 | + imagePullPolicy: Always |
| 68 | + name: nginx |
| 69 | + ports: |
| 70 | + - containerPort: 8080 |
| 71 | + volumeMounts: |
| 72 | + - name: nginx-configs |
| 73 | + subPath: headers.conf |
| 74 | + mountPath: /etc/nginx/conf.d/headers.conf |
| 75 | + securityContext: |
| 76 | + allowPrivilegeEscalation: false |
| 77 | + seccompProfile: |
| 78 | + type: RuntimeDefault |
| 79 | + capabilities: |
| 80 | + drop: ["ALL"] |
| 81 | + runAsNonRoot: true |
| 82 | + volumes: |
| 83 | + - name: nginx-configs |
| 84 | + configMap: |
| 85 | + name: nginx |
| 86 | + items: |
| 87 | + - key: headers.conf |
| 88 | + path: headers.conf |
| 89 | +EOF |
| 90 | +---- |
| 91 | + |
| 92 | +. You can verify that the three sample replicas started successfully by running the following command: |
| 93 | ++ |
| 94 | +[source,terminal] |
| 95 | +---- |
| 96 | +$ oc get pods -n $NAMESPACE |
| 97 | +---- |
| 98 | + |
| 99 | +. Create a `LoadBalancer` service for the `nginx` test application with the following sample commands: |
| 100 | ++ |
| 101 | +[source,terminal] |
| 102 | +---- |
| 103 | +$ oc create -n $NAMESPACE -f - <<EOF |
| 104 | +apiVersion: v1 |
| 105 | +kind: Service |
| 106 | +metadata: |
| 107 | + name: nginx |
| 108 | +spec: |
| 109 | + ports: |
| 110 | + - port: 81 |
| 111 | + targetPort: 8080 |
| 112 | + selector: |
| 113 | + app: nginx |
| 114 | + type: LoadBalancer |
| 115 | +EOF |
| 116 | +---- |
| 117 | ++ |
| 118 | +[NOTE] |
| 119 | +==== |
| 120 | +You must ensure that the `port` parameter is a host port that is not occupied by other `LoadBalancer` services or {product-title} components |
| 121 | +==== |
| 122 | + |
| 123 | +. To verify that the service file exists and the external IP address is properly assigned, and the external IP is identical to the node IP, run the following command: |
| 124 | ++ |
| 125 | +[source,terminal] |
| 126 | +---- |
| 127 | +$ oc get svc -n $NAMESPACE |
| 128 | +---- |
| 129 | ++ |
| 130 | +.Example output |
| 131 | +[source,terminal] |
| 132 | +---- |
| 133 | +NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE |
| 134 | +nginx LoadBalancer 10.43.183.104 192.168.1.241 81:32434/TCP 2m |
| 135 | +---- |
| 136 | + |
| 137 | +.Verification |
| 138 | + |
| 139 | +* The following command forms five connections to the `nginx` application using the external IP address of the `LoadBalancer` service config. You can verify that the load balancer sends requests to all the running applications with the following command: |
| 140 | ++ |
| 141 | +[source,terminal] |
| 142 | +---- |
| 143 | +EXTERNAL_IP=192.168.1.241 |
| 144 | +seq 5 | xargs -Iz curl -s -I http://$EXTERNAL_IP:81 | grep X-Server-IP |
| 145 | +---- |
| 146 | ++ |
| 147 | +Your output should contain different IP addresses, this shows that the load balancer is successfully distributing the traffic to the applications. |
| 148 | ++ |
| 149 | +.Example output |
| 150 | +[source,terminal] |
| 151 | +---- |
| 152 | +X-Server-IP: 10.42.0.41 |
| 153 | +X-Server-IP: 10.42.0.41 |
| 154 | +X-Server-IP: 10.42.0.43 |
| 155 | +X-Server-IP: 10.42.0.41 |
| 156 | +X-Server-IP: 10.42.0.43 |
| 157 | +---- |
0 commit comments