Skip to content

Instantly share code, notes, and snippets.

@avoidik
Last active November 17, 2021 19:14
Show Gist options
  • Save avoidik/97d27132a3ba5bd633797a789f950c73 to your computer and use it in GitHub Desktop.
Save avoidik/97d27132a3ba5bd633797a789f950c73 to your computer and use it in GitHub Desktop.
K3S on Vagrant with MetalLB
apiVersion: v1
kind: ConfigMap
metadata:
namespace: metallb-system
name: config
data:
config: |
address-pools:
- name: default
protocol: layer2
addresses:
- 192.168.0.100-192.168.0.199
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: nginx
annotations:
nginx.ingress.kubernetes.io/ssl-redirect: "false"
spec:
ingressClassName: nginx
rules:
- host: k8s.at.home
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: nginx
port:
number: 80
DEFAULT_BOX = 'ubuntu/focal64'
WORKER_NODES = 2
Vagrant.configure(2) do |config|
config.vm.define 'master' do |master|
master.vm.box = DEFAULT_BOX
master.vm.hostname = 'master'
master.vm.synced_folder '.', '/vagrant', type: 'virtualbox'
master.vm.network 'private_network', ip: '192.168.0.200'
master.vm.provider 'virtualbox' do |v|
v.memory = 2048
v.cpus = 2
v.name = 'k3s-master01'
v.customize ['modifyvm', :id, '--audio', 'none']
end
master.vm.provision 'shell', inline: <<-'SHELL'
export DEBIAN_FRONTEND="noninteractive"
apt-get update
apt-get install -y -q etcd-server etcd-client
IPADDR=$(ip a show enp0s8 | grep "inet " | awk '{print $2}' | cut -d / -f1)
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="v1.21.5+k3s2" INSTALL_K3S_EXEC="--node-ip="${IPADDR}" --flannel-iface=enp0s8 --tls-san="${IPADDR}" --tls-san="k8s.at.home" --write-kubeconfig-mode=644 --kube-apiserver-arg="service-node-port-range=30000-30100" --no-deploy=servicelb --no-deploy=traefik" K3S_STORAGE_BACKEND=etcd3 K3S_STORAGE_ENDPOINT="http://127.0.0.1:2379" K3S_TOKEN="43bf98d8fb25e7fd4275ae06f33adacd" sh -
cp /etc/rancher/k3s/k3s.yaml /tmp/
sed -i "s/127.0.0.1/${IPADDR}/" /tmp/k3s.yaml
cp /tmp/k3s.yaml /vagrant/
SHELL
end
(1..WORKER_NODES).each do |n|
config.vm.define "node#{n}" do |node|
node.vm.box = DEFAULT_BOX
node.vm.hostname = "node#{n}"
node.vm.synced_folder '.', '/vagrant', type: 'virtualbox', disabled: true
node.vm.network 'private_network', ip: "192.168.0.2%02d" % n
node.vm.provider 'virtualbox' do |v|
v.memory = 2048
v.cpus = 2
v.name = "k3s-node#{n}"
v.customize ['modifyvm', :id, '--audio', 'none']
end
node.vm.provision 'shell', inline: <<-'SHELL'
IPADDR=$(ip a show enp0s8 | grep "inet " | awk '{print $2}' | cut -d / -f1)
curl -sfL https://get.k3s.io | INSTALL_K3S_VERSION="v1.21.5+k3s2" INSTALL_K3S_EXEC="--node-ip=${IPADDR} --flannel-iface=enp0s8" K3S_URL="https://192.168.0.200:6443" K3S_TOKEN="43bf98d8fb25e7fd4275ae06f33adacd" sh -
SHELL
end
end
end
@avoidik
Copy link
Author

avoidik commented Nov 17, 2021

install metallb

kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.11.0/manifests/namespace.yaml
kubectl apply -f https://raw.githubusercontent.com/metallb/metallb/v0.11.0/manifests/metallb.yaml
kubectl create secret generic -n metallb-system memberlist --from-literal=secretkey="$(openssl rand -base64 64 | sed -r 's/[^a-zA-Z0-9]//g' | tr -d '\n')"
kubectl apply -f config.yaml

install nginx ingress controller

kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.0.5/deploy/static/provider/baremetal/deploy.yaml
kubectl patch service -n ingress-nginx ingress-nginx-controller -p '{"spec": {"type": "LoadBalancer"}}'

deploy sample workload

kubectl create deploy nginx --image=nginx --replicas=2 --port=80
kubectl create service clusterip nginx --tcp=80:80
kubectl apply -f ingress.yaml

test connection

curl -H "Host: k8s.at.home" $(kubectl get svc -n ingress-nginx ingress-nginx-controller -o jsonpath='{.status.loadBalancer.ingress[0].ip}')

@avoidik
Copy link
Author

avoidik commented Nov 17, 2021

continue with linkerd :) https://linkerd.io/2.11/getting-started/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment