ABOUT ME

-

  • [K8S] Kubespray로 Kubernetes 설치하기 - Ubuntu
    Kubernetes 2025. 3. 20. 17:32

    설치 환경

    • OS : Rocky8.9
    • Kubernetes : v1.27.7
    • Kubespray : v2.23
      • ansible-core : v2.14
      • docker : v20.10
      • Kubernetes 설치 최소 버전 : v1.25
      • Python 필요 버전 : v3.9 - v3.11

     

     

    Kubernetes 설치하기

    • swap 끄기
      • swap : 메모리가 부족할 경우 사용되는 가상 메모리
        => kubernetes에서 메모리를 더 효율적으로 관리하므로 메모리 스와핑으로 인한 성능 저하 방지를 위해 off
    # sudo su -
    
    swapoff -a

     

     

    • 방화벽 끄기
      • 노드간의 원활한 통신을 위함
    sudo ufw disable

     

     

    • sudo 권한 설정 (모든 노드에 설정)
    vi /etc/sudoers
    
    # 파일 하단에 입력
    hyun ALL=(ALL:ALL) ALL
    hyun ALL=NOPASSWD: ALL

     

     

    • python 3.10설치
      • python 버전 확인
    python --version
    python3 --version

     

     

    • ubuntu22부터는 python3.10.12가 기본으로 설치되어 있으므로 pip3만 추가 설치
    apt install python3-pip -y

     

     

    • ubuntu20 이하일 경우 python3.10 설치 진행
    sudo add-apt-repository ppa:deadsnakes/ppa
    sudo apt install python3.10-full
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
    sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 2
    sudo update-alternatives --config python3
    
    sudo ln -s /usr/lib/python3/dist-packages/apt_inst.cpython-38-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_inst.so
    sudo ln -s /usr/lib/python3/dist-packages/apt_pkg.cpython-38-x86_64-linux-gnu.so /usr/lib/python3/dist-packages/apt_pkg.so
    
    python3.10 -m ensurepip --default-pip

     

     

    • kubespray 파일 다운로드 및 이동
    git clone https://github.com/kubernetes-sigs/kubespray
    
    # release 지정
    # git clone https://github.com/kubernetes-sigs/kubespray -b release-2.23
    
    cd kubespray

     

     

    • 필수 패키지 설치
      • sshpass : ssh 연결 시 비밀번호 자동 제공 (ansible이 ssh 기반으로 작업을 수행하기 때문에 필요)
      • virtualenv : 개발환경 격리 및 의존성 관리를 위함필수 패키지 설치
    yum install -y sshpass
    pip3 install -U pip virtualenv

     

     

    • virtualenv 생성 및 활성화
    # 생성
    virtualenv  --python=$(which python3) kubespray-venv
    
    # 활성화
    source kubespray-venv/bin/activate
    
    ### 비활성화 시 deactivate

     

     

    • ansible 설치
    pip install -U -r requirements.txt

     

     

    • inventory 폴더 복사 및 스크립트 실행
      • IP : 구성할 노드 ip (ex. 192.168.0.100 192.168.0.101 192.168.0.102)
      • HOST_PREFIX : hosts명
      • KUBE_CONTROL_HOSTS : Master 갯수
      • CONFIG_FILE : 설정할 hosts.yaml 파일 경로
    # 복사
    cp -rfp inventory/sample inventory/mycluster
    
    # 환경변수 지정 및 스크립트 실행
    # Update Ansible inventory file with inventory builder
    # declare -a IPS=(Master_IP Node1_IP Node2_IP)
    declare -a IPS=(IP)
    HOST_PREFIX=test KUBE_CONTROL_HOSTS=1 CONFIG_FILE=inventory/mycluster/hosts.yaml python3 contrib/inventory_builder/inventory.py ${IPS[@]}

     

     

    • hosts.yaml 파일 수정
      • vars : 모든 hosts에 공통변수 작성 (공통변수가 아닐 경우 hosts아래에 개별 작성)
      • ansible_user : 앤서블 실행 유저
      • ansible_password : 실행 유저 비밀번호
      • ansible_become_password: root계정 비밀번호
      • master, etcd는 홀수로 구성
    all:
      vars:
        ansible_user: 'hyun'
        ansible_password: 1234
        ansible_become_password: 1234
      hosts:
        master-name:
          ansible_host: {Master_IP}
          ip: {Master_IP}
          access_ip: {Master_IP}
        node1-name:
          ansible_host: {Node1_IP}
          ip: {Node1_IP}
          access_ip: {Node1_IP}
        node2-name:
          ansible_host: {Node2_IP}
          ip: {Node2_IP}
          access_ip: {Node2_IP}
      children:
        kube_control_plane:
          hosts:
            master-name:
        kube_node:
          hosts:
            master-name:
            node1-name:
            node2-name:
        etcd:
          hosts:
            master-name:
        k8s_cluster:
          children:
            kube_control_plane:
            kube_node:
        calico_rr:
          hosts: {}

     

     

    • +) pem키 사용 방법
      • ansible_ssh_private_key_file : pem 키 경로
    all:
      vars:
        ansible_user: "hyun"
        ansible_ssh_private_key_file: '/home/hyun/test.pem'

     

     

    • +) label 추가
      • hosts.yaml에서 전체(vars에 기재) 또는 개별로 node label 부여 가능
        node1-name:
          ansible_host: {Node1_IP}
          ip: {Node1_IP}
          access_ip: {Node1_IP}
          node_labels:
            nodetype: 'worker'
        node2-name:
          ansible_host: {Node2_IP}
          ip: {Node2_IP}
          access_ip: {Node2_IP}
          node_labels:
            gputype: 'V100'

     

     

    • 설치 (cluster.yml 파일이 위치한 kubespray 디렉토리 안에서 명령어 실행)
      • ansible-playbook : ansible 실행 명령어
      • -i 경로 : hosts파일 경로 지정 
      • --become  : root 권한으로 수행
      • cluster.yml : kubernetes 배포 yml
    ansible-playbook -i inventory/mycluster/hosts.yaml  --become cluster.yml

     

     

    • 설치 확인
    # 노드 확인
    kubectl get nodes
    
    # 모든 파드 확인
    kubectl get pod -A

     

Designed by Tistory.