Skip to content

Commit d695fbe

Browse files
Kristian-ZHhardys
authored andcommitted
Document MetalLB in front of RKE2 process
1 parent da8d3b9 commit d695fbe

File tree

1 file changed

+118
-18
lines changed

1 file changed

+118
-18
lines changed

asciidoc/guides/metallb-kube-api.adoc

Lines changed: 118 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,49 @@ ifdef::env-github[]
1111
:warning-caption: :warning:
1212
endif::[]
1313

14-
This guide demonstrates using a MetalLB service to expose the K3s API externally on an HA K3s cluster with three control-plane nodes.
14+
This guide demonstrates using a MetalLB service to expose the RKE2/K3s API externally on an HA cluster with three control-plane nodes.
1515
To achieve this, a Kubernetes Service of type `LoadBalancer` and Endpoints will be manually created. The Endpoints keep the IPs of all control plane nodes available in the cluster.
1616
For the Endpoint to be continuously synchronized with the events occurring in the cluster (adding/removing a node or a node goes offline), the https://github.com/suse-edge/endpoint-copier-operator[Endpoint Copier Operator] will be deployed. The operator monitors the events happening in the default `kubernetes` Endpoint and updates the managed one automatically to keep them in sync.
1717
Since the managed Service is of type `LoadBalancer`, `MetalLB` assigns it a static `ExternalIP`. This `ExternalIP` will be used to communicate with the API Server.
1818

1919
== Prerequisites
2020

21-
* Three hosts to deploy K3s on top.
21+
* Three hosts to deploy RKE2/K3s on top.
2222
** Ensure the hosts have different host names.
2323
** For testing, these could be virtual machines
24-
* At least 2 available IPs in the network (one for the Traefik and one for the managed service).
24+
* At least 2 available IPs in the network (one for the Traefik/Nginx and one for the managed service).
2525
* Helm
2626

27-
== Installing K3s
27+
== Installing RKE2/K3s
2828

2929
[NOTE]
3030
====
31-
If you do not want a fresh cluster but want to use an existing one, skip this step and proceed to the next one.
31+
If you do not want to use a fresh cluster but want to use an existing one, skip this step and proceed to the next one.
3232
====
3333

3434
First, a free IP in the network must be reserved that will be used later for `ExternalIP` of the managed Service.
3535

36-
SSH to the first host and install `K3s` in cluster mode as:
36+
SSH to the first host and install the wanted distribution in cluster mode.
3737

38+
For RKE2:
39+
[,bash]
40+
----
41+
# Export the free IP mentioned above
42+
export VIP_SERVICE_IP=<ip>
43+
44+
curl -sfL https://get.rke2.io | INSTALL_RKE2_EXEC="server \
45+
--write-kubeconfig-mode=644 --tls-san=${VIP_SERVICE_IP} \
46+
--tls-san=https://${VIP_SERVICE_IP}.sslip.io" sh -
47+
48+
systemctl enable rke2-server.service
49+
systemctl start rke2-server.service
50+
51+
# Fetch the cluster token:
52+
RKE2_TOKEN=$(tr -d '\n' < /var/lib/rancher/rke2/server/node-token)
53+
----
54+
55+
56+
For K3s:
3857
[,bash]
3958
----
4059
# Export the free IP mentioned above
@@ -56,29 +75,43 @@ Make sure that `--disable=servicelb` flag is provided in the `k3s server` comman
5675
From now on, the commands should be run on the local machine.
5776
====
5877

59-
To access the API server from outside, the IP of the K3s VM will be used.
78+
To access the API server from outside, the IP of the RKE2/K3s VM will be used.
6079

6180
[,bash]
6281
----
6382
# Replace <node-ip> with the actual IP of the machine
6483
export NODE_IP=<node-ip>
65-
scp ${NODE_IP}:/etc/rancher/k3s/k3s.yaml ~/.kube/config && sed \
84+
export KUBE_DISTRIBUTION=<k3s/rke2>
85+
86+
scp ${NODE_IP}:/etc/rancher/${KUBE_DISTRIBUTION}/${KUBE_DISTRIBUTION}.yaml ~/.kube/config && sed \
6687
-i '' "s/127.0.0.1/${NODE_IP}/g" ~/.kube/config && chmod 600 ~/.kube/config
6788
----
6889

69-
== Configuring an existing K3s cluster
90+
== Configuring an existing cluster
7091

7192
[NOTE]
7293
====
73-
This step is valid only if you intend to use an existing K3s cluster.
94+
This step is valid only if you intend to use an existing RKE2/K3s cluster.
7495
====
7596

76-
To use an existing K3s cluster, the `servicelb` LB should be disabled and also `tls-san` flags modified.
97+
To use an existing cluster the `tls-san` flags should be modified and also, `servicelb` LB should be disabled for K3s.
7798

78-
To change the K3s flags, `/etc/systemd/system/k3s.service` should be modified on all the VMs in the cluster.
99+
To change the flags for RKE2 or K3s servers, you need to modify either the `/etc/systemd/system/rke2.service` or `/etc/systemd/system/k3s.service` file on all the VMs in the cluster, depending on the distribution.
79100

80101
The flags should be inserted in the `ExecStart`. For example:
81102

103+
For RKE2:
104+
[,shell]
105+
----
106+
# Replace the <vip-service-ip> with the actual ip
107+
ExecStart=/usr/local/bin/rke2 \
108+
server \
109+
'--write-kubeconfig-mode=644' \
110+
'--tls-san=<vip-service-ip>' \
111+
'--tls-san=https://<vip-service-ip>.sslip.io' \
112+
----
113+
114+
For K3s:
82115
[,shell]
83116
----
84117
# Replace the <vip-service-ip> with the actual ip
@@ -91,12 +124,12 @@ ExecStart=/usr/local/bin/k3s \
91124
'--tls-san=https://<vip-service-ip>.sslip.io' \
92125
----
93126

94-
Then the following commands should be executed for K3s to load the new configurations:
127+
Then the following commands should be executed to load the new configurations:
95128

96129
[,bash]
97130
----
98131
systemctl daemon-reload
99-
systemctl restart k3s
132+
systemctl restart ${KUBE_DISTRIBUTION}
100133
----
101134

102135
== Installing MetalLB
@@ -154,11 +187,57 @@ endpoint-copier-operator oci://registry.suse.com/edge/endpoint-copier-operator-c
154187
--create-namespace
155188
----
156189

157-
The command above will deploy three different resources in the cluster:
190+
The command above will deploy the `endpoint-copier-operator` operator Deployment with two replicas. One will be the leader and the other will take over the leader role if needed.
191+
192+
Now, the `kubernetes-vip` Service should be deployed, which will be reconciled by the operator and an Endpoint with the configured ports and IP will be created.
193+
194+
For RKE2:
195+
[,bash]
196+
----
197+
cat <<-EOF | kubectl apply -f -
198+
apiVersion: v1
199+
kind: Service
200+
metadata:
201+
name: kubernetes-vip
202+
namespace: default
203+
spec:
204+
ports:
205+
- name: rke2-api
206+
port: 9345
207+
protocol: TCP
208+
targetPort: 9345
209+
- name: k8s-api
210+
port: 6443
211+
protocol: TCP
212+
targetPort: 6443
213+
type: LoadBalancer
214+
EOF
215+
----
216+
217+
For K3s:
218+
[,bash]
219+
----
220+
cat <<-EOF | kubectl apply -f -
221+
apiVersion: v1
222+
kind: Service
223+
metadata:
224+
name: kubernetes-vip
225+
namespace: default
226+
spec:
227+
internalTrafficPolicy: Cluster
228+
ipFamilies:
229+
- IPv4
230+
ipFamilyPolicy: SingleStack
231+
ports:
232+
- name: https
233+
port: 443
234+
protocol: TCP
235+
targetPort: 6443
236+
sessionAffinity: None
237+
type: LoadBalancer
238+
EOF
239+
----
158240

159-
. The `endpoint-copier-operator` operator Deployment with two replicas. One will be the leader and the other will take over the leader role if needed.
160-
. A Kubernetes service called `kubernetes-vip` in the `default` namespace that will be a copy of the `kubernetes` Service but from type `LoadBalancer`.
161-
. An Endpoint resource called `kubernetes-vip` in the `default` namespace that will be a copy of the `kubernetes` Endpoint.
162241

163242
Verify that the `kubernetes-vip` Service has the correct IP address:
164243

@@ -204,6 +283,27 @@ watch kubectl get endpoints
204283

205284
Now execute the commands below on the second and third nodes.
206285

286+
For RKE2:
287+
[,bash]
288+
----
289+
# Export the VIP_SERVICE_IP in the VM
290+
# Replace with the actual IP
291+
export VIP_SERVICE_IP=<ip>
292+
293+
curl -sfL https://get.rke2.io | INSTALL_RKE2_TYPE="server" sh -
294+
systemctl enable rke2-server.service
295+
296+
297+
mkdir -p /etc/rancher/rke2/
298+
cat <<EOF > /etc/rancher/rke2/config.yaml
299+
server: https://${VIP_SERVICE_IP}:9345
300+
token: ${RKE2_TOKEN}
301+
EOF
302+
303+
systemctl start rke2-server.service
304+
----
305+
306+
For K3s:
207307
[,bash]
208308
----
209309
# Export the VIP_SERVICE_IP in the VM

0 commit comments

Comments
 (0)