前言
将两台N1刷入Armbian后安装k3s。
因有两台N1,又想尝试一下k3s,目标是希望在家庭环境中能够部署一些自己的小服务,遂开始折腾。
刷Armbian
macos下刷Armbian的方法,本文用的是Armbian_5.77_Aml-s905_Debian_stretch_default_5.0.2_20190401.img.7z 这个镜像,插入U盘后,运行以下命令:
1 2 3 4 5 6 7 8 9 10 > diskutil list ...... 这个是U盘 ........ /dev/disk3 (external, physical): # : TYPE NAME SIZE IDENTIFIER 0: FDisk_partition_scheme *7.7 GB disk3 1: Windows_FAT_32 BOOT 134.2 MB disk3s1 2: Linux 536.9 MB disk3s2 > diskutil unmountDisk /dev/disk3 > sudo dd if =Armbian_5.77_Aml-s905_Debian_stretch_default_5.0.2_20190401.img of=/dev/rdisk3 bs=1m
将U盘插入N1(靠近HDMI接口的那个USB接口)。
写入到emmc中:
1 2 3 4 root@aml:~# nand-sata-install # 这个命令和install.sh等价 root@aml:~# ls -al /usr/sbin/nand-sata-install lrwxrwxrwx 1 root root 16 Apr 1 2019 /usr/sbin/nand-sata-install -> /root/install.sh
修改/etc/apt/sources.list
为:
1 2 3 4 5 6 7 8 9 10 11 deb https://mirrors.ustc.edu.cn/debian stretch main contrib non-free # deb-src http://httpredir.debian.org/debian stretch main contrib non-free deb https://mirrors.ustc.edu.cn/debian stretch-updates main contrib non-free # deb-src http://httpredir.debian.org/debian stretch-updates main contrib non-free deb https://mirrors.ustc.edu.cn/debian stretch-backports main contrib non-free # deb-src http://httpredir.debian.org/debian stretch-backports main contrib non-free deb https://mirrors.ustc.edu.cn/debian-security/ stretch/updates main contrib non-free # deb-src http://security.debian.org/ stretch/updates main contrib non-free
先执行:
1 root@aml: apt-get update && apt-get upgrade
(可选)安装蓝牙模块[2]:ssh连接,然后执行sudo
armbian-config,进去后选择Network,接着选择BT
Install,耐心等待蓝牙组件安装完毕,然后退出。
接着执行sudo apt install pulseaudio-module-bluetooth
安装pulseaudio组件。安装完成后,分别执行sudo killall
pulseaudio和pulseaudio --start启动pulseaudio服务。
接着执行sudo apt install pulseaudio-module-bluetooth
安装pulseaudio组件。安装完成后,分别执行sudo killall
pulseaudio和pulseaudio --start (root用户需执行:pulseaudio --system)
启动pulseaudio服务。
开始进入蓝牙连接阶段,首先执行sudo hciconfig
-a查看蓝牙控制器信息,确认无误后,执行sudo hciconfig hci0
up打开蓝牙控制器,然后执行sudo bluetoothctl打开蓝牙管理器。
先后执行power on,discoverable on,agent on,然后执行scan
on搜集周围的蓝牙设备,记录下要连接的设备地址后,执行trust
<设备地址>信任设备,然后再执行pair
<设备地址>配对,此时,要配对的设备上可能会弹出提示,点确认。
如以上步骤都没有问题,则执行connect
<设备地址>,稍候即可顺利连接蓝牙,可以运行info
<设备地址>确认状态。
修改hostname
: armbian-config -> Personal
->Hostname,注意hostname不能 有下划线。
(可选)修改wifi,静态地址:
安装k3s
master node
【可选】为了下载速度快一点可以先安装proxychains:sudo apt install proxychains
,然后修改/etc/proxychains.conf
,末尾添加:socks5 代理IP 1080
,使用方法:proxychains wget https://www.google.com
。
先拿一台机器当master节点,执行:
1 root@aml-n1-0:~# curl -sfL https://get.k3s.io | sh -
验证k3s是否运行:
1 root@aml-n1-0:~# systemctl status k3s
记录k3s token,在worker节点上会用到:
1 root@aml-n1-0:~# cat /var/lib/rancher/k3s/server/node-token
worker node
1 2 3 4 5 curl -sfL https://get.k3s.io | K3S_URL="https://aml-n1-0:6443" K3S_TOKEN="your-token" sh - # or 使用代理运行下载好的k3s.sh K3S_URL="https://aml-n1-0:6443" K3S_TOKEN="your-token" proxychains sh k3s.sh # or 把已经安装过k3s的节点加入到当前集群 sudo k3s agent --server ${K3S_URL} --token ${K3S_TOKEN}
验证安装:
1 systemctl status k3s-agent
检查节点:
1 2 3 4 root@aml-n1-0:~# kubectl get nodes NAME STATUS ROLES AGE VERSION aml-n1-1 Ready <none> 8m53s v1.17.4+k3s1 aml-n1-0 Ready master 14m v1.17.4+k3s1
注意其中有none,根据官方issue 里说的,这个是feature
233333.
首次使用k3s,hello world
借鉴博客[3]在树莓派上的k3s实践。
创建1个app
创建arm的images还是挺烦的,先利用别人的hello
world .
部署app
利用Kubernetes的命令行接口,创建:
1 sudo kubectl create deployment hello-world --image=mirailabs/hello-world
kubernetes为这个app分配了一个pod:
1 2 3 root@aml-n1-0:~# kubectl get pods NAME READY STATUS RESTARTS AGE hello-world-6565c9d89f-kjhzp 0/1 ContainerCreating 0 2m46s
过几分钟,会发现app开始运行了:
1 2 3 4 5 6 7 root@aml-n1-0:~# kubectl get pods NAME READY STATUS RESTARTS AGE hello-world-6565c9d89f-kjhzp 1/1 Running 0 8m53s # 在worker节点aml-n1-1上 root@aml-n1-0:~# kubectl get pods -o wide NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES hello-world-6565c9d89f-kjhzp 1/1 Running 0 18m 10.42.0.8 aml-n1-1 <none> <none>
如果拉取镜像失败的话,查看日志:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 # 先查看pod属于哪个NAMESPACE root@aml-n1-0:~# kubectl get pods -A NAMESPACE NAME READY STATUS RESTARTS AGE kube-system metrics-server-6d684c7b5-c88q2 1/1 Running 0 47m kube-system local-path-provisioner-58fb86bdfd-2k9q8 1/1 Running 0 47m kube-system coredns-6c6bb68b64-vnh5h 1/1 Running 0 47m kube-system helm-install-traefik-hfgq4 0/1 Completed 0 47m kube-system svclb-traefik-s2slj 2/2 Running 0 31m kube-system traefik-7b8b884c8-7n6dm 1/1 Running 0 31m default hello-world-6565c9d89f-kjhzp 1/1 Running 0 12m # 执行以下命令看日志 root@aml-n1-0:~# kubectl describe pod hello-world-6565c9d89f-kjhzp -n default Name: hello-world-6565c9d89f-kjhzp Namespace: default Priority: 0 Node: aml-n1-1/192.168.1.4 Start Time: Tue, 28 Apr 2020 17:15:55 +0800 Labels: app=hello-world pod-template-hash=6565c9d89f Annotations: <none> Status: Running IP: 10.42.0.8 IPs: IP: 10.42.0.8 Controlled By: ReplicaSet/hello-world-6565c9d89f Containers: hello-world: Container ID: containerd://d4d518c2468e3e1600868047a4934568786c03b3468123457746665915afff2a Image: mirailabs/hello-world Image ID: docker.io/mirailabs/hello-world@sha256:409b575f248c52939f7c4d72dcb2762660fc16437416d76371fa12e715b66159 Port: <none> Host Port: <none> State: Running Started: Tue, 28 Apr 2020 17:24:46 +0800 Ready: True Restart Count: 0 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-8hcw7 (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-8hcw7: Type: Secret (a volume populated by a Secret) SecretName: default-token-8hcw7 Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: Type Reason Age From Message ---- ------ ---- ---- ------- Normal Scheduled <unknown> default-scheduler Successfully assigned default/hello-world-6565c9d89f-kjhzp to aml-n1-1 Warning Failed 8m49s kubelet, aml-n1-1 Failed to pull image "mirailabs/hello-world": rpc error: code = Unknown desc = failed to pull and unpack image "docker.io/mirailabs/hello-world:latest": failed to copy: read tcp 192.168.1.4:45516->104.18.125.25:443: read: connection timed out Warning Failed 8m49s kubelet, aml-n1-1 Error: ErrImagePull Normal BackOff 8m49s kubelet, aml-n1-1 Back-off pulling image "mirailabs/hello-world" Warning Failed 8m49s kubelet, aml-n1-1 Error: ImagePullBackOff Normal Pulling 8m36s (x2 over 14m) kubelet, aml-n1-1 Pulling image "mirailabs/hello-world" Normal Pulled 5m33s kubelet, aml-n1-1 Successfully pulled image "mirailabs/hello-world" Normal Created 5m31s kubelet, aml-n1-1 Created container hello-world Normal Started 5m30s kubelet, aml-n1-1 Started container hello-world
暴露app
将app从kubernetes的内部网络中暴露出来,相当于将它作为一个service暴露给外界。
简洁起见,用NodePort服务类型通过集群中任意节点的一个保留端口暴露service。(kubernetes的默认暴露端口范围是30000-32767)
1 2 3 root@aml-n1-0:~# kubectl expose deployment hello-world --type=NodePort --port=8080 --name=hello-world service/hello-world exposed root@aml-n1-0:~#
确认hello-world服务是否运行:
1 2 3 4 root@aml-n1-0:~# kubectl get svc NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 59m hello-world NodePort 10.43.123.18 <none> 8080:30978/TCP 62s
可以看出deployment在端口30978上作为一个NodePort
service被暴露出来。
接下来测试一下:
1 2 3 4 5 6 7 8 9 10 11 12 # 首先在master上查看日志 root@aml-n1-0:~# kubectl logs -f deployment/hello-world Received request for URL: /trying-node-0 Received request for URL: /trying-node-1 # 在局域网内的除k3s集群上的机器执行: OTRHER-MACHINE@bogon: ~/Downloads > curl 192.168.1.120:30550/trying-node-0 Hello, World! # 对应日志第一条 OTRHER-MACHINE@bogon: ~/Downloads > curl 192.168.1.121:30550/trying-node-1 Hello, World! # 对应日志第二条
清理现场
要删除服务,请输入以下命令:
1 2 root@aml-n1-0:~# kubectl delete services hello-world service "hello-world" deleted
要删除正在运行 Hello World 应用程序的 Deployment,ReplicaSet 和
Pod,请输入以下命令:
1 2 root@aml-n1-0:~# kubectl delete deployment hello-world deployment.apps "hello-world" deleted
踩坑记录
N1 ARMBIAN变成只读文件系统 Read-only file system,解决方法:
用U盘启动 ,运行一次e2fsck /dev/mmcblk1p2
hostname一开始要确定好,不要在安装中途改了一个节点的hostname,在每个节点的/etc/hosts
中设置好所有节点的hostname和ip对应关系,比如aml-n1-0节点:
1 2 3 4 5 6 7 8 9 10 11 root@aml-n1-0:~# cat /etc/hosts 127.0.0.1 localhost aml-n1-0 ::1 localhost aml-n1-0 ip6-localhost ip6-loopback fe00::0 ip6-localnet ff00::0 ip6-mcastprefix ff02::1 ip6-allnodes ff02::2 ip6-allrouters 192.168.1.120 aml-n1-0 192.168.1.121 aml-n1-1
Armbian的有线网卡每次重启后MAC地址都会变,解决方法是在/etc/network/interfaces
中固定mac地址:
1 2 3 4 5 6 7 source /etc/network/interfaces.d/* # Wired adapter allow-hotplug eth0 no-auto-down eth0 iface eth0 inet dhcp hwaddress xx:xx:xx:xx:xx:xx # 新增的
参考
[1]
https://yuerblog.cc/2019/10/23/%E6%96%90%E8%AE%AFn1-%E5%AE%8C%E7%BE%8E%E5%88%B7%E6%9C%BAarmbian%E6%95%99%E7%A8%8B/
[2]https://www.right.com.cn/forum/thread-517710-1-1.html
[3]https://mirailabs.io/blog/building-a-microcloud/