Hướng dẫn cài đặt Kubernetes trên CentOS 7 tự động hoá với Ansible
BÀI VIẾT

Hướng dẫn cài đặt Kubernetes trên CentOS 7 tự động hoá với Ansible

6.0K
author Võ Trung Huy - 2021-11-29 10:21:30 (GMT+7)

Giới Thiệu

Ansible là một công cụ mã nguồn mở giúp bạn có thể tự động hoá các tác vụ trên nhiều máy chủ cùng một lúc thông qua giao thức SSH. Với việc sử dụng cấu trúc YAML cực kỳ đơn giản, Ansible ra mắt vào năm 2012 dần trở thành công cụ phổ biến bậc nhất trong giới SysOps/DevOps/SRE.

Trong bài viết này chúng ta sẽ thực hiện cài đặt Ansible và thao tác tự động hoá cài đặt Kubernetes cluster trên máy chủ CentOS 7.

10.0.0.2 # node1

10.0.0.3 # node2

10.0.0.4 # node3

Chuẩn bị

Các máy chủ thao tác cần được thêm public key từ máy tính cá nhân của bạn tới máy chủ và user này có thể thực thi được quyền root

Thao tác trên các server cần cài đặt Kubernetes:

Tạo user với tên userdeploy Tiến hành tạo user userdeploy

bash (root)
adduser userdeploy

Tiến hành thêm user userdeploy có quyền thực thi quyền root

bash (root)
visudo

Tiến hành thêm user userdeploy trong file /etc/sudoers/ có thể thực thi quyền root

/etc/sudoers
userdeploy ALL=(ALL) NOPASSWD:ALL

Cài đặt pip3 trên máy trạm :

Với MacOS

Đã có sẵn package pip3 trên hệ điều hành .

Với Ubuntu

bash (root)
apt-get install python3-pip

Với Centos

bash (root)
yum install python3 python3-wheel

Cài đặt Ansible

Đối với phần cài đặt này chúng ta sẽ cài đặt có thể là máy chủ hoặc máy tính cá nhân đề được.

Với CentOS

Cài đặt repo epel

bash (root)
yum install epel-release  

Cài đặt Ansible

bash (root)
yum install ansible 

Kiểm tra version của Ansible

bash (root)
ansible --version
Kết quả
ansible 2.9.25
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/site-packages/ansible
  executable location = /bin/ansible
  python version = 2.7.5 (default, Apr  2 2020, 13:16:51) [GCC 4.8.5 20150623 (Red Hat 4.8.5-39)]

Với Ubuntu

Cài đặt các phần mềm thuộc tính cần thiết

bash (root)
apt-get install software-properties-common

Thêm repo ansible

bash (root)
apt-add-repository ppa:ansible/ansible 

Cài đặt Ansible

bash (root)
apt-get install ansible

Kiểm tra version của Ansible

bash (root)
ansible --version
Kết quả
ansible [core 2.11.6]
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python3/dist-packages/ansible
  ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
  executable location = /usr/bin/ansible
  python version = 3.8.10 (default, Jun  2 2021, 10:49:15) [GCC 9.4.0]
  jinja version = 2.10.1
  libyaml = True

Với MacOS

Cài đặt ansible

bash (root)
brew install ansible

Kiểm tra version của Ansible

bash (root)
ansible --version
Kết quả
ansible 2.10.7
  config file = None
  configured module search path = ['~/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/local/Cellar/ansible/3.2.0/libexec/lib/python3.9/site-packages/ansible
  executable location = /usr/local/bin/ansible
  python version = 3.9.4 (default, Apr  5 2021, 01:47:16) [Clang 11.0.0 (clang-1100.0.33.17)]

Cấu trúc thư mục trong Ansible

Với Ansible chúng ta cần biết một số định nghĩa cơ bản sau : - group_vars : Chứa các biến được quy định bởi một group. Ví dụ ở đây chúng ta sẽ có một group các máy chủ chạy proxy nginx vậy chúng ta có thể define các biến chạy cho máy chủ này như NGINX_VERSION , WORKER_CONNECTION...

  • host_vars : Giống như group_vars nhưng ở phần này chúng ta sẽ chỉ định chính xác server nào sẽ có những biến nào.

  • inventories : Là nơi mà bạn có thể khai báo các thông tin của máy chủ mà mình muốn triển khai . Ví dụ : IP , PORT_SSH , USER_SSH ...

  • playbooks : Là nơi bạn có thể khai báo các file YAML để có thể gọi tới các roles theo chỉnh định.

  • roles : Là một tập các playbook chứa các files YAML để thực thi cài đặt , cấu hình cũng như thao tác với servers.

Tiếp theo ta sẽ đi sâu hơn về các cấu trúc của một role roles này chúng ta sẽ có các định nghĩa mới bao gồm:

  • defaults : Là nơi chứa các variables cục bộ trong roles này.

  • files : Là nơi chứa các files chúng ta cần sync lên hệ thống cần deploy trong role.

  • handlers : Là nơi chứa các thao tác xử lý các thao tác trên hệ thống như: reload, restart, start...

  • meta : Là nơi chứa các dependencies liên quan tới role.

  • templates: Là nơi chứa các files config mẫu theo qui định best practices thường các files sẽ được lưu theo đuôi là .j2

  • vars : Là nơi chứa các variables cục bộ trong roles này. Nó được ưu tiên hơn defaults nếu như tìm không thấy ở vars nó sẽ tìm đến defaults

Thông tin thêm: Bạn truy cập vào https://docs.ansible.com/ansible/2.8/user_guide/playbooks_best_practices.html để xem thêm chi tiết các thành phần.

Cài đặt Kubernetes cluster sử dụng Ansible

Đầu tiên ta sẽ clone Kubespray về trên máy trạm:

bash (root)
git clone https://github.com/kubernetes-sigs/kubespray.git
Kết quả
Cloning into 'kubespray'...
remote: Enumerating objects: 57192, done.
remote: Counting objects: 100% (795/795), done.
remote: Compressing objects: 100% (471/471), done.
remote: Total 57192 (delta 338), reused 617 (delta 246), pack-reused 56397
Receiving objects: 100% (57192/57192), 16.66 MiB | 4.62 MiB/s, done.
Resolving deltas: 100% (32282/32282), done.

Chúng ta cài đặt các dependencies có liên quan

bash (root)
cd kubespray 
pip3 install -r requirements.txt
Kết quả
 ~/De/F/1/kubespray | master <7  pip3 install -r requirements.txt                         ok | 14:20:46
Requirement already satisfied: ansible==3.4.0 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 1)) (3.4.0)
Requirement already satisfied: ansible-base==2.10.15 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 2)) (2.10.15)
Requirement already satisfied: cryptography==2.8 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 3)) (2.8)
Requirement already satisfied: jinja2==2.11.3 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 4)) (2.11.3)
Requirement already satisfied: netaddr==0.7.19 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 5)) (0.7.19)
Requirement already satisfied: pbr==5.4.4 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 6)) (5.4.4)
Requirement already satisfied: jmespath==0.9.5 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 7)) (0.9.5)
Requirement already satisfied: ruamel.yaml==0.16.10 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 8)) (0.16.10)
Requirement already satisfied: ruamel.yaml.clib==0.2.4 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 9)) (0.2.4)
Requirement already satisfied: MarkupSafe==1.1.1 in /usr/local/lib/python3.9/site-packages (from -r requirements.txt (line 10)) (1.1.1)
Requirement already satisfied: packaging in /usr/local/lib/python3.9/site-packages (from ansible-base==2.10.15->-r requirements.txt (line 2)) (21.3)
Requirement already satisfied: PyYAML in /usr/local/lib/python3.9/site-packages (from ansible-base==2.10.15->-r requirements.txt (line 2)) (6.0)
Requirement already satisfied: cffi!=1.11.3,>=1.8 in /usr/local/lib/python3.9/site-packages (from cryptography==2.8->-r requirements.txt (line 3)) (1.15.0)
Requirement already satisfied: six>=1.4.1 in /usr/local/lib/python3.9/site-packages (from cryptography==2.8->-r requirements.txt (line 3)) (1.15.0)
Requirement already satisfied: pycparser in /usr/local/lib/python3.9/site-packages (from cffi!=1.11.3,>=1.8->cryptography==2.8->-r requirements.txt (line 3)) (2.21)
Requirement already satisfied: pyparsing!=3.0.5,>=2.0.2 in /usr/local/lib/python3.9/site-packages (from packaging->ansible-base==2.10.15->-r requirements.txt (line 2)) (3.0.6)
WARNING: You are using pip version 21.0.1; however, version 21.3.1 is available.
You should consider upgrading via the '/usr/local/opt/python@3.9/bin/python3.9 -m pip install --upgrade pip' command

Chúng ta copy file inventory mẫu thành một file inventory mới

bash (root)
cp -rfp inventory/sample inventory/mycluster

Chúng ta sử dụng script có sẵn để update các server mình muốn cài đặt cluster Kubernetes.

bash (root)
declare -a IPS=(10.0.0.2 10.0.0.3 10.0.0.4)
CONFIG_FILE=inventory/mycluster/hosts.yaml python3 contrib/inventory_builder/inventory.py ${IPS[@]}
Kết quả
DEBUG: Adding group all
DEBUG: Adding group kube_control_plane
DEBUG: Adding group kube_node
DEBUG: Adding group etcd
DEBUG: Adding group k8s_cluster
DEBUG: Adding group calico_rr
DEBUG: adding host node1 to group all
DEBUG: adding host node2 to group all
DEBUG: adding host node3 to group all
DEBUG: adding host node1 to group etcd
DEBUG: adding host node2 to group etcd
DEBUG: adding host node3 to group etcd
DEBUG: adding host node1 to group kube_control_plane
DEBUG: adding host node2 to group kube_control_plane
DEBUG: adding host node1 to group kube_node
DEBUG: adding host node2 to group kube_node
DEBUG: adding host node3 to group kube_node

Chúng ta kiểm tra lại các files chúng ta đã config.

inventory/mycluster/hosts.yaml
all:
  hosts:
    node1:
      ansible_host: 10.0.0.2
      ip: 10.0.0.2
      access_ip: 10.0.0.2
    node2:
      ansible_host: 10.0.0.3
      ip: 10.0.0.3
      access_ip: 10.0.0.3
    node3:
      ansible_host: 10.0.0.4
      ip: 10.0.0.4
      access_ip: 10.0.0.4
  children:
    kube_control_plane:
      hosts:
        node1:
        node2:
    kube_node:
      hosts:
        node1:
        node2:
        node3:
    etcd:
      hosts:
        node1:
        node2:
        node3:
    k8s_cluster:
      children:
        kube_control_plane:
        kube_node:
    calico_rr:
      hosts: {}

Sau đó chúng ta tiến hành chạy playbook để tạo cluster Kubernetes

bash (root)
ansible-playbook -i inventory/mycluster/hosts.yaml  --become --become-user=root cluster.yml -u userdeploy

Sau khi đợi khoảng 25-30 phút thì chúng ta được kết quả thông báo quá trình cài đặt cluster Kubernetes đã thành công:

Kết quả
PLAY RECAP **********************************************************************************************
node1                      : ok=590  changed=130  unreachable=0    failed=0    skipped=1167 rescued=0    ignored=1
node2                      : ok=526  changed=117  unreachable=0    failed=0    skipped=1022 rescued=0    ignored=0
node3                      : ok=446  changed=98   unreachable=0    failed=0    skipped=705  rescued=0    ignored=0

Sau đó ta tiến hành ssh vào máy chủ có IP 10.0.0.2 cài đặt kubectl.

bash (root)
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
mv kubectl /usr/bin/
chmod +x /usr/bin/kubectl

Sau đó ta tiến hành export file KUBECONFIG .

bash (root)
export KUBECONFIG=/etc/kubernetes/admin.conf

Chúng ta kiểm tra các nodes của cluster .

bash (root)
kubectl get nodes
Kết quả
NAME    STATUS   ROLES                  AGE     VERSION
node1   Ready    control-plane,master   10m     v1.22.3
node2   Ready    control-plane,master   10m     v1.22.3
node3   Ready    <none>                 9m18s   v1.22.3

Chúng ta kiểm tra các pods đang chạy trên cluster.

bash (root)
kubectl get pods -A
Kết quả
NAMESPACE     NAME                                       READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-684bcfdc59-mqcbx   1/1     Running   0          10m
kube-system   calico-node-f6rl7                          1/1     Running   0          11m
kube-system   calico-node-sd7rv                          1/1     Running   0          11m
kube-system   calico-node-vt4sj                          1/1     Running   0          11m
kube-system   coredns-8474476ff8-l4gsx                   1/1     Running   0          10m
kube-system   coredns-8474476ff8-pfxt9                   1/1     Running   0          10m
kube-system   dns-autoscaler-5ffdc7f89d-66m7v            1/1     Running   0          10m
kube-system   kube-apiserver-node1                       1/1     Running   0          13m
kube-system   kube-apiserver-node2                       1/1     Running   0          12m
kube-system   kube-controller-manager-node1              1/1     Running   1          13m
kube-system   kube-controller-manager-node2              1/1     Running   1          12m
kube-system   kube-proxy-2tx6h                           1/1     Running   0          11m
kube-system   kube-proxy-cvtcx                           1/1     Running   0          11m
kube-system   kube-proxy-q9g2l                           1/1     Running   0          11m
kube-system   kube-scheduler-node1                       1/1     Running   1          13m
kube-system   kube-scheduler-node2                       1/1     Running   1          12m
kube-system   nginx-proxy-node3                          1/1     Running   0          11m
kube-system   nodelocaldns-5v55x                         1/1     Running   0          10m
kube-system   nodelocaldns-ctpfv                         0/1     Pending   0          10m
kube-system   nodelocaldns-tkjjn                         1/1     Running   0          10m

Tổng kết

Bên trên là toàn bộ hướng dẫn cài đặt thành công một cluster Kubernetes 3 nodes sử dụng Kubespray.

Thông tin chi tiết bạn có thể đọc tại repository Github chính thức của Kubespray: https://github.com/kubernetes-sigs/kubespray

Chúc các bạn thành công!


Bạn có làm được theo hướng dẫn này không?

Bài liên quan

Hướng dẫn cài đặt Cluster Kubernetes với RKE trên Ubuntu 20.04
Hướng dẫn cài đặt Cluster Kubernetes với RKE trên Ubuntu 20.04
Hướng dẫn cài đặt Cluster Kubernetes với RKE trên CentOS 7
Hướng dẫn cài đặt Cluster Kubernetes với RKE trên CentOS 7

Cloud VPS tại 123HOST

Hiệu năng, ổn định và bảo mật

Cloud VPS giá rẻ

Thuê Server Riêng

Chi phí thấp, cấu hình cao

Thuê Server Riêng
Thông tin tác giả
Võ Trung Huy

Tôi một người hiền lành , thân thiện , hài hước, và yêu công nghệ

Bình luận

Tính năng đang được phát triển

Đang tải bình luận
Liên hệ chúng tôi