• Home
  • About
    • lahuman photo

      lahuman

      열심히 사는 아저씨

    • Learn More
    • Facebook
    • LinkedIn
    • Github
  • Posts
    • All Posts
    • All Tags
  • Projects

라즈베리파이에 ubuntu에 k3S 설치 하기

16 Jan 2021

Reading time ~5 minutes

라즈베리파이에 ubuntu에 k3S 설치 하기

이 글을 읽기 전에 라즈베리파이에 k3s 설치 하기 - 실패기 와 라즈베리파이에 ubuntu 설치 하기 를 읽어 보시기를 추천 드립니다. 또한 라즈베리파이에 ubuntu 설치 하기의 설정 이후 진행 하셔야 합니다.

hostsname 설정하기

저는 2대의 라즈베리 파이를 가지고 main(라즈베리4)과 node1(라즈베리3+)으로 구성을 하려고 합니다. 먼저 구분을 하기 위해서 사용하려는 라즈베리파이의 hostname을 변경합니다.

sudo vi /etc/hosts

# mian / node1 으로 수정

그리고 다른 모듈에서 hostsname을 변경하지 못하도록 /etc/cloud/cloud.cfg 파일의 preserve_hostname 옵션을 true로 변경합니다.

# The top level settings are used as module
# and system configuration.

# A set of users which may be applied and/or used by various modules
# when a 'default' entry is found it will reference the 'default_user'
# from the distro configuration specified below
users:
   - default

# If this is set, 'root' will not be able to ssh in and they
# will get a message to login instead as the default $user
disable_root: true

# This will cause the set+update hostname module to not operate (if true)
# 여기를 true로 변경
preserve_hostname: true

Linux 컨테이너 기능 활성화

/boot/firmware/cmdline.txt 파일의 마지막에 라인에 cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory 을 이어서 작성합니다.

sudo vi /boot/firmware/cmdline.txt

# 라인 마지막에 추가 처리 
net.ifnames=0 dwc_otg.lpm_enable=0 console=serial0,115200 console=tty1 root=LABEL=writable rootfstype=ext4 elevator=deadline rootwait fixrtc cgroup_enable=cpuset cgroup_memory=1 cgroup_enable=memory

그리고 재부팅을 해야 합니다.

sudo reboot

docker 설치

k3s 설치 이전에 docker를 설치 합니다. docker 설치와 활성화는 다음의 명령어로 쉽게 할 수 있습니다.

# docker 설치
sudo apt install docker.io 

# docker 서비스 시작
sudo systemctl start docker

# 부팅시 docker 서비스 시작
sudo systemctl enable docker

k3s 설치

k3s 설치도 다음 스크립트로 쉽게 설치 할 수 있습니다.

# 설치 스크립트
curl -sfL https://get.k3s.io | sh -s - --docker

# 서비스 확인
sudo systemctl status k3s
● k3s.service - Lightweight Kubernetes
     Loaded: loaded (/etc/systemd/system/k3s.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2021-01-16 13:26:34 UTC; 14min ago
       Docs: https://k3s.io
    Process: 1697 ExecStartPre=/sbin/modprobe br_netfilter (code=exited, status=0/SUCCESS)
    Process: 1733 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)
   Main PID: 1734 (k3s-server)
      Tasks: 29
     Memory: 571.3M
     CGroup: /system.slice/k3s.service
             └─1734 /usr/local/bin/k3s server

Jan 16 13:36:39 main k3s[1734]: W0116 13:36:39.184245    1734 machine.go:253] Cannot determine CPU /sys/bus/cpu/devices>
Jan 16 13:36:39 main k3s[1734]: W0116 13:36:39.184308    1734 machine.go:253] Cannot determine CPU /sys/bus/cpu/devices>
Jan 16 13:36:39 main k3s[1734]: W0116 13:36:39.184360    1734 machine.go:253] Cannot determine CPU /sys/bus/cpu/devices>
Jan 16 13:36:39 main k3s[1734]: W0116 13:36:39.184412    1734 machine.go:253] Cannot determine CPU /sys/bus/cpu/devices>
Jan 16 13:36:39 main k3s[1734]: E0116 13:36:39.184432    1734 machine.go:72] Cannot read number of physical cores corre>
Jan 16 13:36:39 main k3s[1734]: W0116 13:36:39.184669    1734 machine.go:253] Cannot determine CPU /sys/bus/cpu/devices>
Jan 16 13:36:39 main k3s[1734]: W0116 13:36:39.184720    1734 machine.go:253] Cannot determine CPU /sys/bus/cpu/devices>
Jan 16 13:36:39 main k3s[1734]: W0116 13:36:39.184768    1734 machine.go:253] Cannot determine CPU /sys/bus/cpu/devices>
Jan 16 13:36:39 main k3s[1734]: W0116 13:36:39.184816    1734 machine.go:253] Cannot determine CPU /sys/bus/cpu/devices>
Jan 16 13:36:39 main k3s[1734]: E0116 13:36:39.184836    1734 machine.go:86] Cannot read number of sockets correctly, n>
lines 1-22/22 (END)

# k3s node 상태 확인
sudo kubectl get ndoes
sudo kubectl get nodes
NAME    STATUS     ROLES                  AGE   VERSION
main    Ready      control-plane,master   24h   v1.20.0+k3s2

방화벽 비활성화

이제 main node의 설정이 끝났습니다.

worker node와 통신을 위해서 방화벽을 내려둡니다.

# 방화벽 비활성화
sudo ufw disable

“축하합니다!” 여기까지 진행이 되었다면, main node의 설치가 완료 한 것입니다. 이제 worker node 설치를 진행합니다.

woker node를 설치하기 위해서는 main node의 token이 필요 합니다.

token은 다음의 명령어로 확인이 가능합니다.

sudo cat /var/lib/rancher/k3s/server/node-token
K10db4cc46dfccb627942c905744dce8ee3099f1153d85c741f21b8c9420febc9db::server:58054e0acd0a3508bc4381e43e74b2ef

worker node 설치 하기

우선 main node에서 진행했던, linux 컨테이너 활성화와 docker를 설치해줍니다. 그리고 방화벽 비활성화처리 합니다.

위의 단계가 끝났다면, worker node를 다음의 명령어로 설치합니다.

# curl -sfL http://get.k3s.io | K3S_URL=https://<master_IP>:6443 K3S_TOKEN=<join_token> sh -s - --docker
curl -sfL http://get.k3s.io | K3S_URL=https://192.168.0.30:6443 K3S_TOKEN=K10db4cc46dfccb627942c905744dce8ee3099f1153d85c741f21b8c9420febc9db::server:58054e0acd0a3508bc4381e43e74b2ef sh -s - --docker

설치가 완료되었다면, 다음 명령어로 상태를 확인하세요.

sudo systemctl status k3s-agent
● k3s-agent.service - Lightweight Kubernetes
     Loaded: loaded (/etc/systemd/system/k3s-agent.service; enabled; vendor preset: enabled)
     Active: active (running) since Fri 2021-01-15 13:35:53 UTC; 24h ago
       Docs: https://k3s.io
    Process: 1674 ExecStartPre=/sbin/modprobe br_netfilter (code=exited, status=0/SUCCESS)
    Process: 1706 ExecStartPre=/sbin/modprobe overlay (code=exited, status=0/SUCCESS)
   Main PID: 1707 (k3s-agent)
      Tasks: 16
     Memory: 143.6M
     CGroup: /system.slice/k3s-agent.service
             └─1707 /usr/local/bin/k3s agent

Jan 16 13:54:00 node1 k3s[1707]: I0116 13:54:00.243366    1707 policy_none.go:43] [cpumanager] none policy: Start
Jan 16 13:54:00 node1 k3s[1707]: E0116 13:54:00.265387    1707 kubelet.go:1823] skipping pod synchronization - containe>
Jan 16 13:54:00 node1 k3s[1707]: W0116 13:54:00.322546    1707 manager.go:594] Failed to retrieve checkpoint for "kubel>
Jan 16 13:54:00 node1 k3s[1707]: I0116 13:54:00.340881    1707 plugin_manager.go:114] Starting Kubelet Plugin Manager
Jan 16 13:54:01 node1 k3s[1707]: I0116 13:54:01.066655    1707 topology_manager.go:187] [topologymanager] Topology Admi>
Jan 16 13:54:01 node1 k3s[1707]: W0116 13:54:01.067797    1707 pod_container_deletor.go:79] Container "3d18023da3813e1c>
Jan 16 13:54:01 node1 k3s[1707]: I0116 13:54:01.253297    1707 reconciler.go:224] operationExecutor.VerifyControllerAtt>
Jan 16 13:54:01 node1 k3s[1707]: I0116 13:54:01.253506    1707 reconciler.go:157] Reconciler: start to sync state
Jan 16 13:54:01 node1 k3s[1707]: W0116 13:54:01.718236    1707 cni.go:333] CNI failed to retrieve network namespace pat>
Jan 16 13:54:04 node1 k3s[1707]: W0116 13:54:04.529789    1707 pod_container_deletor.go:79] Container "5124f74a4a45f4e

main node에서 worker node가 연결되었는지 확인합니다.

# node1 이 연결되어 있는 것을 확인합니다.
sudo kubectl get nodes
NAME    STATUS   ROLES                  AGE   VERSION
node1   Ready    <none>                 24h   v1.20.0+k3s2
main    Ready    control-plane,master   24h   v1.20.0+k3s2

서비스 올려 보기

먼저 서비스 yaml 파일을 생성합니다.

# k3s-demo.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: k3s-demo
  namespace: default
spec:
  replicas: 1
  selector:
    matchLabels:
      app: k3s-demo
  template:
    metadata:
      labels:
        app: k3s-demo
    spec:
      containers:
      - name: k3s-demo
        image: ruanbekker/rpi-hostname:latest
---
apiVersion: v1
kind: Service
metadata:
  name: k3s-demo
  namespace: default
spec:
  ports:
  - name: http
    targetPort: 8000
    port: 80
  selector:
    app: k3s-demo
---
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: k3s-demo
  annotations:
    kubernetes.io/ingress.class: "traefik"

spec:
  rules:
  - host: k3s-demo.192.168.0.30.xip.io
    http:
      paths:
      - path: /
        pathType: Prefix
        backend:
          service: 
            name: k3s-demo
            port: 
              number: 80

k3s-demo.192.168.0.30.xip.io에서 사용한 xip.io는 서브 도메인에 설정된 IP로 루프백 해주는 서비스입니다. 도메인기반으로 동작하는 Ingress에서 활용하면 하나의 서비스에 여러 서브 도메인을 테스트 할때 좋습니다. k3s-demo.192.168.0.30.xip.io 을 호출하면 192.168.0.30으로 전달합니다.

# 서비스 배포
sudo kubectl apply -f k3s-demo.yaml

# 배포된 서비스 확인 (웹 브라우저에서도 확인이 가능합니다.)
curl http://k3s-demo.192.168.0.30.xip.io/
Hostname: k3s-demo-65c8cf57c-2s2tr

여기까지 k3s 설치를 마칩니다.

참고자료

  • Installing Kubernetes on Raspberry Pi, K3s and Docker on Ubuntu 20.04
  • Develop Build and Deploy a Golang App to K3S


raspberrypiubuntuk3s Share Tweet +1