반응형

 

2019년 3월에 작성된 Web Docs이지만, 오늘(2021년 11월) 따라서 수행해봤는데 잘 동작한다.

 

 

https://kubernetes.io/blog/2019/03/15/kubernetes-setup-using-ansible-and-vagrant/

 

Kubernetes Setup Using Ansible and Vagrant

Author: Naresh L J (Infosys) Objective This blog post describes the steps required to setup a multi node Kubernetes cluster for development purposes. This setup provides a production-like cluster that can be setup on your local machine. Why do we require m

kubernetes.io

 

 

위 블로그를 읽으면서, 나한테 필요한 만큼 playbook을 다시 작성했다.

아래 Playbook의 내용을 간략하게 설명하면,

 

  1. Kubernetes Master Node에서  `kubeadm token create --print-join-command`를 수행하여 새로운 Node Join을 위한 `kubeadm join ....` 명령행을 생성하고,
    이렇게 얻은 kubeadm 명령행을 'join-command' 스크립트 파일에 Dump한다.
  2. 바로 위에서 얻은 'join-command'를 새롭게 추가할 Worker Node의 /tmp/join-command.sh 경로에 복사하고 실행(Run)한다.
    그러면 새로운 Worker Node가 Master Node에 추가(Join)될 것이다.

 

단, 아래 Playbook YAML 파일에서 inventory(즉, hosts를 정의한 항목)을 생략했으니까 그 부분을 감안하고 보면 좋을 듯~~~

 

 

## 위 Web docs에서 중요한 부분만 발췌했다.
## 원문 그대로는 아니고, 내가 필요한 만큼 각색했다.  

## Filename: my-playbook.yaml

- name: Joining Job Step 1 on Master node
  hosts: kubemaster
  tasks:
    - name: Generate join command
      command: kubeadm token create --print-join-command
      register: join_command
    - name: Copy join command to local file
      local_action: copy content="{{ join_command.stdout_lines[0] }}" dest="./join-command"

- name: Joining Job Step 2 on Worker node
  hosts: kubeworker
  tasks:
    - name: Copy the join command to server location
      copy: src=join-command dest=/tmp/join-command.sh mode=0777
    - name: Join the node to cluster
      command: sh /tmp/join-command.sh

 

반응형

Concept

  • Control node: Ansible을 실행하는 Node
  • Managed node: Ansible 관리 대상이 되는 Node

 

Installation

$ sudo yum install epel-release
$ sudo yum install ansible

## Adding Ansible command shell completion
##  -> 이것은 편의를 위한 기능이니, 꼭 설치할 필요는 없다
$ sudo yum install python-argcomplete
$ sudo activate-global-python-argcomplete

## Let's check the version
$  ansible --version
ansible 2.9.25
...

## host inventroy file을 편집
$ cat /etc/ansible/hosts
...
[kubeworker]
10.10.12.64
10.10.12.71

[mynode]
10.10.12.64
...
$

##
## TEST for installation
##

$ ansible all --list-hosts
  hosts (2):
    10.10.12.64
    10.10.12.71
    
$ ansible all -m ping
10.10.12.64 | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    },
    "changed": false,
    "ping": "pong"
}
10.10.12.71 | UNREACHABLE! => {
    "changed": false,
    "msg": "Failed to connect to the host via ssh: ssh: connect to host 10.10.12.71 port 22: No route to host",
    "unreachable": true
}
$

 

Run example playbook - 1

##
## Run this playbook on control node
##

$  cat  playbook-example.yaml
---
- name: Add_sample_contents_to_file
  hosts: mynode

  tasks:
  - name: Add sample contents to file
    blockinfile:
      path: /root/sample_a.txt
      block: |
        [my test section]
        best contents
        greeting=hello       
$
$  ansible-playbook playbook-example.yaml

PLAY [Add_sample_file] ************************************************************************************************************

TASK [Gathering Facts] ************************************************************************************************************
ok: [10.10.12.64]

TASK [Add sample file] ************************************************************************************************************
changed: [10.10.12.64]

PLAY RECAP ************************************************************************************************************************
10.10.12.64                : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

$

##
## Check result of test on managed node(10.10.12.64)
##

$  cat  /root/sample_a.txt
# BEGIN ANSIBLE MANAGED BLOCK
[my test section]
best contents
greeting=hello
# END ANSIBLE MANAGED BLOCK
$

 

Run example playbook - 2

##
## Run this playbook on control node
##

$  cat  playbook-example-2.yaml
---
- name: Install pkg and create a file
  hosts: mynode

  tasks:
  - name: Install kubernetes pkg
    yum: name=tree state=present
    when: ansible_distribution == 'CentOS'
  - name: Run my command
    command: "touch /root/mydate.txt"
    when: ansible_distribution == 'CentOS'     
$
$  ansible-playbook playbook-example-2.yaml
...
$

##
## Check result of test on managed node(10.10.12.64)
##

$  which tree
/usr/bin/tree

$  ls /root/mydate.txt
/root/mydate.txt

 

Tip:   특정 User로 Ansible 실행

managed node에서 특정 user(예: root)로 실행되길 원한다면, 아래와 같이 설정 파일을 작성한다.

$  cat  /etc/ansible/ansible.cfg
...

[privilege_escalation]
become=True
become_method=sudo
become_user=root        ## Root user로 명령을 수행한다.
become_ask_pass=False   ## password를 묻지 않고 명령을 수행한다.

...

$

 

 

 

Reference

https://young-dev.com/infra/ansible_02/#

 

[Ansible] Ansible-Playbook 사용하기 #2

Ansible-Playbook 활용

young-dev.com

 

 

 

 

'CentOS' 카테고리의 다른 글

CPU Pinning 예제 코드  (0) 2022.06.17
Install Ansible AWX (version 17.1.0)  (0) 2021.11.16
Install OS with PXE and kickstart  (0) 2021.11.12
Samba(SMB) on CentOS  (0) 2021.07.10
Network config on CentOS 8  (0) 2021.07.10

+ Recent posts