-
Notifications
You must be signed in to change notification settings - Fork 0
KubeEdge
Use KVM to run KubeEdge on your local machine
- Get the Ubuntu 20.04 server iso: https://releases.ubuntu.com/20.04/
- Install KVM: https://linuxize.com/post/how-to-install-kvm-on-ubuntu-20-04/
To manage VMs, either use the virsh
command, the qemu-system-x86_64
command , or use the Virtual Machine Manager application (GUI).
- Create a VM called 'template' using the Ubuntu 20.04 iso. Install the OpenSSH server (this is an option during the installation). Keep this VM as is, we will clone it to create VMs we will actually use. In this way if a VM gets corrupted in some way and you need a new one, you can simply clone this template without having to install Ubuntu from scratch.
- Clone the template VM three times, call the first VM 'cloud', the second VM 'edge1' and the third 'endpoint1'. These will represent a single KubeEdge cloudcore, edgecore and endpoint system.
- Change the IP of the VMs: https://bobcares.com/blog/virsh-set-ip-address/ and https://serverfault.com/questions/627238/kvm-libvirt-how-to-configure-static-guest-ip-addresses-on-the-virtualisation-ho
To login via SSH, start the VM and do ssh username@ip
with the username being the one you chose during installation, and the ip you can find via: https://www.cyberciti.biz/faq/find-ip-address-of-linux-kvm-guest-virtual-machine/
- Change the hostname of the VMs to 'cloud', 'edge1' and 'endpoint1': Either use
sudo hostnamectl set-hostname 'name'
or https://www.cyberciti.biz/faq/ubuntu-change-hostname-command/ - Check if the host can see both VMs and the VMs can see each other and the host by using the 'ping' command.
Source: https://www.linuxtechi.com/install-kubernetes-k8s-on-ubuntu-20-04/
Do the following steps on the cloud and edge VM:
- Disable swap. First check if swap is enabled:
swapon -s
. If there is no output, swap is disabled. Otherwise, edit /etc/fstab by commenting out the line with 'swap' in it. - Enable ipv4 forwarding: Uncomment the line 'net.ipv4.ip_forward=1' in /etc/sysctl.conf
Do the following step on the cloud, edge and endpoint VM:
- Install docker:
sudo apt update
andsudo apt install -y docker.io
, then start dockersudo systemctl enable docker.service --now
and check if it is correctly startedsystemctl status docker
. If docker doesn't work for some reason, try to install version 19.03.
Do the following steps on the cloud VM only:
- Install kubernetes (version 1.19.0 for KubeEdge 1.6.1):
sudo apt install -y apt-transport-https curl
andcurl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | sudo apt-key add
andsudo apt-add-repository "deb http://apt.kubernetes.io/ kubernetes-xenial main"
andsudo apt update
and finallysudo apt install -y kubelet=1.19.0-00 kubeadm=1.19.0-00 kubectl=1.19.0-00
- Initialize kubernetes:
sudo kubeadm init
(note: kubernetes now uses cgroup as the docker driver, not the recommended systemd) - Start using the cluster:
mkdir -p $HOME/.kube
andsudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
andsudo chown $(id -u):$(id -g) $HOME/.kube/config
Source: https://kubeedge.io/en/docs/ and https://kubeedge.io/en/docs/setup/keadm/
Do the following step on the cloud and edge VM:
- Get keadm:
wget https://github.com/kubeedge/kubeedge/releases/download/v1.6.1/keadm-v1.6.1-linux-amd64.tar.gz
. Extract it:tar -xvf keadm-v1.6.1-linux-amd64.tar.gz
. Make it an executable:cd keadm-v1.6.1-linux-amd64/keadm && chmod +x keadm && sudo mv keadm /usr/local/bin
Do the following steps on the cloud VM only:
- Create a symbolic link so keadm can find kubernetes:
sudo ln -s ~/.kube /root
- Start keadm:
sudo keadm init --advertise-address="cloud-ip-here" --kubeedge-version=1.6.1
- Get the token which is used to join the edge VM:
sudo keadm gettoken
. Don't forget this token.
Do the following steps on the edge VM only:
- Join the cloudcore and install mandatory software (such as mosquito):
sudo keadm join --cloudcore-ipport=cloud_ip_here:10000 --token=token_from_previous_step
Do the following step on the cloud VM only:
- Check if the setup has succeeded:
kubectl get nodes -o wide
. The cloud VM should have status 'NotReady', while the edge VM should have status 'Ready' and as version kubeedge.kubectl describe nodes edge1
should show the resources of the edge VM that are available to the cloudcore.
TODO: see https://github.com/kubeedge/kubeedge/blob/master/docs/setup/keadm.md
Do the following step on both the cloud and edge VM:
- Stop KubeEdge:
sudo keadm reset
. To start KubeEdge next time, repeat step 3, 4 and 5 from the "Setting up KubeEdge" section.
Do the following step on the cloud VM only:
- Delete the edge node from Kubernetes:
kubectl delete node edge1
- (Optional) Stop Kubernetes:
sudo kubeadm reset
andrm -rf ~/.kube
. To start Kubernetes next time, repeat step 5 and 6 from the "Setting up Kubernetes" section.
Source: https://kubeedge.io/en/docs/advanced/edgemesh/
This example application will run an nginx webserver.
Do the following steps on the cloud VM:
- Deploy the webserver:
kubectl apply -f https://raw.githubusercontent.com/kubeedge/kubeedge/master/build/deployment.yaml
- Check if the pod is running on the edge:
kubectl get pods -o wide
Do the following step on the edge VM:
- Check if the webserver works:
curl 172.17.0.2
. You should get a welcome message
Do the following steps on the cloud VM:
- Create a service for it:
$ cat <<EOF | kubectl apply -f -
apiVersion: v1
kind: Service
metadata:
name: nginx-svc
namespace: default
spec:
clusterIP: None
selector:
app: nginx
ports:
- name: http-0
port: 12345
protocol: TCP
targetPort: 80
EOF
- Check if the service is up
kubectl get service
and the endpoint is upkubectl get endpoints
Do the following step on the edge VM:
- Check if you can access the webserver via the service:
curl http://nginx-svc.default.svc.cluster.local:12345
Do the following steps on the cloud VM:
- Stop the service:
kubectl delete service nginx-svc
- Stop the deployment / pod:
kubectl delete -f https://raw.githubusercontent.com/kubeedge/kubeedge/master/build/deployment.yaml
This example application will send images from an endpoint device to an edge device one by one over MQTT. The edge device runs TFLite to classify the incoming images.
Do the following step on the endpoint VM:
- Pull the publisher image:
sudo docker pull redplanet00/kubeedge-tflite-mqtt:publisher
Do the following steps on the cloud VM:
- Deploy the subscriber application in the cloud, which will run it on the edge:
$ cat <<EOF | kubectl apply -f -
apiVersion: apps/v1
kind: Deployment
metadata:
name: img-class-sub-deployment
labels:
app: img-class-sub
spec:
replicas: 1
selector:
matchLabels:
app: img-class-sub
template:
metadata:
labels:
app: img-class-sub
spec:
containers:
- name: img-class-sub
image: redplanet00/kubeedge-tflite-mqtt:subscriber
ports:
- containerPort: 1883
EOF
- Check if the pod is running on the edge:
kubectl get pods -o wide
Do the following step on the endpoint VM:
- Run the endpoint application which will send data to the edge over MQTT:
sudo docker container run redplanet00/kubeedge-tflite-mqtt:publisher
Do the following steps on the cloud VM:
- Stop the deployment / pod:
kubectl delete -f redplanet00/kubeedge-tflite-mqtt:subscriber
For the cloud VM only:
- Get deployment plans / pods:
kubectl get pods -o wide
- Get all kubernetes connected nodes:
kubectl get nodes -o wide
- Delete pod / deployment:
kubectl delete -f <path-to-config-yaml>
- Delete node:
kubectl delete node <nodename>
- Get pod details:
kubectl describe pods <podname>
- Get logs:
less /var/log/kubeedge/cloudcore.log
For the edge VM only:
- Get logs:
journalctl -u edgecore.service -b --since "2021-04-30 12:45"
- Clear old logs:
journalctl --vacuum-time=2d --vacuum-size=500M
Images
- List docker images
sudo docker image ls
- Remove (referenced) docker image
sudo docker image rm (-f) <image_id>
Containers
- List all (running and stopped) containers
sudo docker container ls -a
- Remove a stopped (/ running) docker container
sudo docker container rm (-f) <container_id>
- Remove all running and stopped containers
sudo docker container rm -f $(docker ps -aq)
Building and running
- Build image in current directory
sudo docker build -t myimage:1.0 .
- Retag image
sudo docker tag this_image:1.0 that_image:2.0
- Run a container named 'x' based on an image myimage:1.0
sudo docker container run --name x myimage:1.0
- Stop running container
sudo docker container stop x
Saving an image
- Save and zip
sudo docker save myimage:version | gzip > myimage_latest.tar.gz
- Load saved image
sudo docker load -i ./myimage_latest
Upload an image
- Login to docker (if it doesn't work, first do 'docker logout')
docker login
- Push image to repository
docker push user/repository:tagname