본문 바로가기
kubernetes

손쉬운 Kubernetes 설치 가이드

by aws-evan 2024. 4. 16.
728x90



실무,테스트든 쿠버네티스를 설치 하기 위해서는 공식문서를 통해서 설치를 진행해야한다.

그러나 처음 접하는분들인듯 공식문서만 보고 하게 되면 뭔가 잘안되는 경우가 많다.

 

처음에 설치할때는 많은 어려운이 생겻고 매뉴얼대로 따라하는데도 설치가 안되어서 혼자 고민을 많이 햇습니다.

그러나 테스트로 설치하는 경우도 많고 설치하는데 많은 시간을 쓰고 싶지 않아서 구글링을 통해서 나만의 kubernets 손쉬운 설치 가이드를 만들어 봤습니다.

 

  • 환경 구성
    • CRI : containerd
    • Kubernets Version 1.25 -> 상위 버전일 경우 하위 내용 참고
    • Ubuntu 20.04
    • 마스터 1대
    • 워커노드 2대

수동 설치

  • Kubernets 기본 환경 설치
    • CRI, kubeadm, kubelet, kubectl

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/

 

Installing kubeadm

This page shows how to install the kubeadm toolbox. For information on how to create a cluster with kubeadm once you have performed this installation process, see the Creating a cluster with kubeadm page. This installation guide is for Kubernetes v1.29. If

kubernetes.io

 

  • kubernets Cluster 구성하기

https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/

 

Creating a cluster with kubeadm

Using kubeadm, you can create a minimum viable Kubernetes cluster that conforms to best practices. In fact, you can use kubeadm to set up a cluster that will pass the Kubernetes Conformance tests. kubeadm also supports other cluster lifecycle functions, su

kubernetes.io

 

 

자동 설치(ansible)

 

  • 검토사항 : Ubuntu 22.04 이전 버전의 경우
    • "/etc/apt/keyrings" 기본적으로 존재하지 않아서 폴더를 생성해야한다
    • Ubutun 22.04 상위 버전일 경우에는 해당 부분은 제외해도 된다
    • ansible 코드에는 해당 부분이 포함된 상태이므로 내용적으로 알고만 있으면 된다.
sudo mkdir -p -m 755 /etc/apt/keyrings

 

  • ansible 설치
    • ansible
      • IT 자동화 도구로, 서버 설정, 배포 및 관리를 자동화를 도와주는 코드
apt update -y
apt install -y ansible

 

  • ansible host 적용하기
    • 워커노드가 필요한 만큼 node에 호스트 추가해서 진행하면 됩니다.
[master]
192.168.0.50

[node]
192.168.0.51
192.168.0.52

 

  • 테스트 단계
    • ansible 정상 동작하는지 체크
- name: Ping Test
  hosts: all
  tasks:
    - name: Ping hosts
      ping:

 

  • CRI 설치
- name: 도커, Containerd 설치
  hosts: all
  become: true
  tasks:
    - name: Docker GPG key
      apt_key:
        url: https://download.docker.com/linux/ubuntu/gpg
        state: present

    - name: Docker apt 레포지토리 추가
      apt_repository:
        repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu {{ ansible_distribution_release }} stable
        state: present

    - name: containerd 설치
      apt:
        name: containerd.io
        state: present

    - name: container cri 예외처리
      replace:
        path: /etc/containerd/config.toml
        regexp: '^(disabled_plugins = \["cri"\])$'
        replace: '#\g<1>'
        backup: yes

    - name : containerd 서비스 재시작
      service:
        name : containerd
        state : restarted

    - name : containerd 서비스 재시작
      service:
        name : containerd
        state : restarted

 

  • Kubernets 기본 환경 설치
    • kubernets 버전이 1.25이나 상위 버전으로 하고 싶을 경우에는 하단에 버전 별 테스트 방법 확인 하시면 됩니다.
- name: 쿠버네티스 컴포넌트 이름 ##PlayBook Name
  hosts: all    ##Host서버(/etc/ansible/hosts)
  become: true  ##실행 계정에 대한 sudo 실행
  tasks:  ##작업 대상
    - name: swapoff 처리
      command: swapoff -a

    - name : 저장소 업데이트 및 필수 패키지 추가한다
      shell:  apt install -y apt-transport-https ca-certificates curl

    - name : Kubernetes 패키지 저장소의 공개 서명 키 폴더 생성
      shell:  mkdir -p /etc/apt/keyrings/

    - name : Kubernetes 패키지 저장소의 공개 키 존재 시 삭제
      shell:  rm -rf  /etc/apt/keyrings/kubernetes-apt-keyring.gpg


    - name : Kubernetes 패키지 저장소의 공개 서명 키 등록!
      shell:  curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.25/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

    - name :  Kubernetes 1.25용 패키지
      shell:  echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.25/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

    - name: Update package cache
      apt:
        update_cache: yes

    - name: 쿠버네티스 툴 설치
      apt:
        name:
        - kubelet=1.25.0-2.1  ##kubeadm 먼저 설치시 의존성으로 설치
        - kubectl=1.25.0-2.1
        - kubeadm=1.25.0-2.1
        state: present 

    - name : kubelet 서비스 재시작
      service:
        name : kubelet
        state : restarted

 

  • kubernets Cluster 구성하기
- name: 쿠버네티스 마스터 & 노드
  hosts: master
  become: true
  tasks:
    - name: 마스터 초기화
      shell: kubeadm init
      args:
        creates: /etc/kubernetes/admin.conf

    - name: 마스터 디렉토리 권한
      file:
        path: /root/.kube/
        state : directory
        owner : root
        group : root
        mode : 0755

    - name: 쿠버네티스 마스터 권한 부여
      copy:
        src: /etc/kubernetes/admin.conf
        dest: /root/.kube/config
        remote_src: yes

    - name:  Weave CNI 설치
      command: kubectl apply -f https://github.com/weaveworks/weave/releases/download/v2.8.1/weave-daemonset-k8s.yaml
      tags: kubernetes

    - name: 쿠버네티스 토큰 생성 및 파일 저장
      shell: kubeadm token create  --print-join-command
      register: kubernetes_join_command

    - debug:
       msg: "{{ kubernetes_join_command.stdout }}"

    - name: 쿠버네티스 토큰 전송
      local_action: copy content="{{ kubernetes_join_command.stdout_lines[0] }}" dest="/home/kubernetes_join_command" mode=0777

- hosts: node
  become: true

  tasks:
   - name: Ansible -> 워커노드 전송
     become: yes
     copy:
       src: /home/kubernetes_join_command
       dest: /home/kubernetes_join_command
       mode: 0777

   - name: 워커노드 조인 실행
     become: yes
     command: sudo sh /home/kubernetes_join_command
     register: joined_or_not

 

  • kubernets Cluster 완료

 

 

 

  • 설치 외에 부가적인 내용들

  • kubernets 상위 버전 설치 방법
    • Kubernets 기본 환경 설치 설치 단계에서 ansible 코드를 수정해서 사용하면 됩니다.
#kubernets version 1.26

    - name : Kubernetes 패키지 저장소의 공개 서명 키 등록!
      shell:  curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.26/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

    - name :  Kubernetes 1.25용 패키지
      shell:  echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.26/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
      
    
#kubernets version 1.27

    - name : Kubernetes 패키지 저장소의 공개 서명 키 등록!
      shell:  curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.27/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

    - name :  Kubernetes 1.25용 패키지
      shell:  echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.27/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
      

#kubernets version 1.28

    - name : Kubernetes 패키지 저장소의 공개 서명 키 등록!
      shell:  curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

    - name :  Kubernetes 1.25용 패키지
      shell:  echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.28/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list
      
      

#kubernets version 1.29

    - name : Kubernetes 패키지 저장소의 공개 서명 키 등록!
      shell:  curl -fsSL https://pkgs.k8s.io/core:/stable:/v1.29/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg

    - name :  Kubernetes 1.25용 패키지
      shell:  echo 'deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://pkgs.k8s.io/core:/stable:/v1.29/deb/ /' | sudo tee /etc/apt/sources.list.d/kubernetes.list

 

  • 리소스 삭제 방법
- name: 쿠버네티스 환경 삭제
  hosts: all
  become: true
  tasks:
    - name: 클러스터 초기화
      shell: kubeadm reset -f

    - name: 쿠버네티스 삭제
      apt:
        name:
        - kubelet=1.25.0-2.1  
        - kubectl=1.25.0-2.1
        - kubeadm=1.25.0-2.1
        state: absent

    - name : Kubernetes 패키지 저장소의 공개 키 삭제
      shell:  rm -rf  /etc/apt/keyrings/kubernetes-apt-keyring.gpg


    - name: Docker 레포지토리 삭제
      shell : rm -rf /etc/apt/sources.list.d/download_docker_com_linux_ubuntu.list

    - name: kubernets 레포지토리 삭제
      shell : rm -rf /etc/apt/sources.list.d/kubernetes.list

    - name: containerd 삭제
      apt:
        name: containerd.io
        state: absent

 

  • 내용이 추가되는 부분이 있으면 추가 등록하겟습니다!

 

 

728x90

'kubernetes' 카테고리의 다른 글

Kubernetes LoadBalancer(MetalLB)  (0) 2024.04.23
CNI Flannel 설치 가이드  (0) 2024.04.22
kubespray 구축  (0) 2024.04.18
Pod 배포시 단일파드?, 여러개 파드? 선택  (0) 2024.04.16
Kubernetes Pod  (0) 2023.06.28

댓글