| | 61 | |
| | 62 | Apply the advertisement: |
| | 63 | |
| | 64 | `kubectl apply -f L2add.yaml` |
| | 65 | |
| | 66 | '''Step 3: Create a Service with a Virtual IP''' |
| | 67 | |
| | 68 | Let’s expose the wordpress application: Edit the service of type LoadBalancer on |
| | 69 | wordpress-service.yaml: |
| | 70 | |
| | 71 | {{{ |
| | 72 | apiVersion: v1 |
| | 73 | kind: Service |
| | 74 | metadata: |
| | 75 | name: wordpress |
| | 76 | spec: |
| | 77 | selector: |
| | 78 | app: wordpress |
| | 79 | ports: |
| | 80 | - protocol: TCP |
| | 81 | port: 80 |
| | 82 | targetPort: 80 |
| | 83 | type: LoadBalancer |
| | 84 | }}} |
| | 85 | |
| | 86 | Save and apply it: |
| | 87 | |
| | 88 | `kubectl apply -f wordpress-service.yaml` |
| | 89 | |
| | 90 | Check the Service: |
| | 91 | |
| | 92 | kubectl get svc wordpress-service |
| | 93 | |
| | 94 | 3. MetalLB will assign an external IP from the defined range to your service. |
| | 95 | |
| | 96 | '''Step 4: Access the Service''' |
| | 97 | |
| | 98 | • You can now access the wordpress server using the external IP provided by MetalLB. This IP is accessible within your network. |
| | 99 | |
| | 100 | '''Troubleshoot''' |
| | 101 | |
| | 102 | On a different VM than the master do the testing for ARP advertisements. |
| | 103 | |
| | 104 | Remove MetalLB (Only for the reference) |
| | 105 | |
| | 106 | {{{ |
| | 107 | arp -a |
| | 108 | ping 192.168.1.200 |
| | 109 | sudo apt install iputils-arping |
| | 110 | arping 192.168.1.200 |
| | 111 | }}} |
| | 112 | |
| | 113 | Remove MetalLB (Only for the reference) |
| | 114 | |
| | 115 | {{{ |
| | 116 | kubectl delete -f |
| | 117 | https://raw.githubusercontent.com/metallb/metallb/v0.13.12/con |
| | 118 | fig/manifests/metallb-native.yaml |
| | 119 | kubectl delete -f metallb-pool.yaml |
| | 120 | kubectl delete -f L2add.yaml |
| | 121 | kubectl get all -n metallb-system |
| | 122 | }}} |
| | 123 | |
| | 124 | === Kubernetes Ingress. (Optional) === |
| | 125 | |
| | 126 | In a Kubernetes environment, if you want to use an Ingress resource to direct traffic to a service that's exposed via NodePort, while still allowing users to access the service using a standard port (like port 80) without specifying the NodePort, you can set it up as follows: |
| | 127 | |
| | 128 | '''Step 1: Expose Your Service Using NodePort''' |
| | 129 | |
| | 130 | 1. Create a Service of Type NodePort for Your Web Application: Suppose you have a deployment named webapp. You'll need to create a service for it. Here's an example YAML for the service: |
| | 131 | |
| | 132 | {{{ |
| | 133 | apiVersion: v1 |
| | 134 | kind: Service |
| | 135 | metadata: |
| | 136 | name: webapp-nodeport-service |
| | 137 | spec: |
| | 138 | type: NodePort |
| | 139 | selector: |
| | 140 | app: webapp |
| | 141 | ports: |
| | 142 | - port: 80 |
| | 143 | targetPort: 80 |
| | 144 | protocol: TCP |
| | 145 | }}} |