kubespray-2.14.2安裝kubernetes-1.18.10(ubuntu-20.04.1)

程式設計師欣宸發表於2021-07-23

歡迎訪問我的GitHub

https://github.com/zq2599/blog_demos

內容:所有原創文章分類彙總及配套原始碼,涉及Java、Docker、Kubernetes、DevOPS等;

關於kubespray

Kubespray是開源的kubernetes部署工具,整合了ansible,可以方便的部署高可用叢集環境,官網地址:https://github.com/kubernetes-sigs/kubespray,本文是用kubespray-2.14.2版本部署kuberneteskubernetes-1.18.10版本的實戰;

重要前提

本次實戰採用官方推薦的線上安裝,因此會去谷歌映象倉庫下載映象,需要您的網路可以訪問谷歌服務

機器資訊

  • 因為作者太窮,本次實戰籌集到共計兩臺機器,它們的主機名、IP地址和作用描述如下:
主機名 IP地址 作用 作業系統
ansible 192.168.50.134 ansible主機 CentOS7
node1 192.168.50.27 k8s伺服器 ubuntu-20.04.1
  • 可見kubernetes是被部署在ubuntu電腦上;

標準化設定

ubuntu電腦要做以下設定:

  1. 修改/etc/hostname,設定好主機名
  2. 修改/etc/hosts,將自己的主機名和IP地址新增進去
  3. 關閉防火牆
ufw disable
  1. 再次檢視應該是關閉狀態
root@ideapad:~# ufw status
狀態:不活動
  1. 關閉selinux,如果提示安裝selinux-utils,表示selinux沒有安裝,就不用關係了
setenforce 0
  1. ipv4網路設定
modprobe br_netfilter
echo '1' > /proc/sys/net/bridge/bridge-nf-call-iptables
sysctl -w net.ipv4.ip_forward=1
  1. 立即禁用交換分割槽
swapoff -a
  1. 我的電腦上,禁用前的記憶體情況
root@ideapad:~# free -m
              總計         已用        空閒      共享    緩衝/快取    可用
記憶體:       31913         551       30288         137        1073       30839
交換:        2047           0        2047
  1. 執行了swapoff -a後再看,可見全部為0了
root@ideapad:~# free -m
              總計         已用        空閒      共享    緩衝/快取    可用
記憶體:       31913         557       30281         137        1073       30833
交換:           0           0           0
  1. 以上禁用交換分割槽的方法,雖然立即生效了,但是重啟電腦後依舊恢復了交換分割槽的使用,要徹底禁用,請開啟檔案/etc/fstab,在下圖紅框這一行最前面新增#
    在這裡插入圖片描述

ansible主機免密碼ssh登入

  1. ssh登入ansible主機;
  2. 生成ssh公私鑰,輸入命令ssh-keygen,然後連續四次回車:
  3. 輸入命令ssh-copy-id root@192.168.50.27,將ansible的ssh分發給ubuntu主機,會要求輸入yes和ubuntu主機的root賬號的密碼,完成輸入後,以後ansible就可以免密碼ssh登入ubuntu主機了:

ansible主機操作

  1. ssh登入ansible主機;
  2. 安裝ansible應用:
yum install -y epel-release ansible
  1. 安裝pip:
easy_install pip
  1. 通過pip安裝jinja2:
pip2 install jinja2 --upgrade
  1. 安裝python36:
yum install python36 -y
  1. 建立工作目錄,進入工作目錄:
mkdir /usr/local/kubespray && cd /usr/local/kubespray/
  1. 下載kubespray,我這裡下載的是v2.14.2版本:
wget https://github.com/kubernetes-sigs/kubespray/archive/v2.14.2.tar.gz
  1. 解壓:
tar -zxvf v2.14.2.tar.gz
  1. 進入解壓後的目錄:
cd kubespray-2.14.2/
  1. 安裝kubespray所需的應用(注意是pip3):
pip3 install -r requirements.txt
  1. 複製一份demo配置資訊到目錄inventory/mycluster
cp -rfp inventory/sample inventory/mycluster
  1. 進去看一下,可見mycluster目錄下複製了很多檔案:
[root@kubespray kubespray-2.14.2]# tree inventory/
inventory/
├── local
│   ├── group_vars -> ../sample/group_vars
│   └── hosts.ini
├── mycluster
│   ├── group_vars
│   │   ├── all
│   │   │   ├── all.yml
│   │   │   ├── aws.yml
│   │   │   ├── azure.yml
│   │   │   ├── containerd.yml
│   │   │   ├── coreos.yml
│   │   │   ├── docker.yml
│   │   │   ├── gcp.yml
│   │   │   ├── oci.yml
│   │   │   ├── openstack.yml
│   │   │   └── vsphere.yml
│   │   ├── etcd.yml
│   │   └── k8s-cluster
│   │       ├── addons.yml
│   │       ├── k8s-cluster.yml
│   │       ├── k8s-net-calico.yml
│   │       ├── k8s-net-canal.yml
│   │       ├── k8s-net-cilium.yml
│   │       ├── k8s-net-contiv.yml
│   │       ├── k8s-net-flannel.yml
│   │       ├── k8s-net-kube-router.yml
│   │       ├── k8s-net-macvlan.yml
│   │       └── k8s-net-weave.yml
│   └── inventory.ini
└── sample
    ├── group_vars
    │   ├── all
    │   │   ├── all.yml
    │   │   ├── aws.yml
    │   │   ├── azure.yml
    │   │   ├── containerd.yml
    │   │   ├── coreos.yml
    │   │   ├── docker.yml
    │   │   ├── gcp.yml
    │   │   ├── oci.yml
    │   │   ├── openstack.yml
    │   │   └── vsphere.yml
    │   ├── etcd.yml
    │   └── k8s-cluster
    │       ├── addons.yml
    │       ├── k8s-cluster.yml
    │       ├── k8s-net-calico.yml
    │       ├── k8s-net-canal.yml
    │       ├── k8s-net-cilium.yml
    │       ├── k8s-net-contiv.yml
    │       ├── k8s-net-flannel.yml
    │       ├── k8s-net-kube-router.yml
    │       ├── k8s-net-macvlan.yml
    │       └── k8s-net-weave.yml
    └── inventory.ini

10 directories, 45 files
  1. 設定叢集資訊(當前目錄仍舊是kubespray-2.14.2):
declare -a IPS=(192.168.50.27)
  1. 配置ansible:
CONFIG_FILE=inventory/mycluster/hosts.yml python3 contrib/inventory_builder/inventory.py ${IPS[@]}
  1. 此時kubespray的指令碼根據輸入的IP資訊做好了叢集規劃,具體資訊可見inventory/mycluster/hosts.yml,如下所示,您也可以自行修改此檔案:
[root@kubespray kubespray-2.14.2]# cat inventory/mycluster/hosts.yml
all:
  hosts:
    node1:
      ansible_host: 192.168.50.27
      ip: 192.168.50.27
      access_ip: 192.168.50.27
  children:
    kube-master:
      hosts:
        node1:
    kube-node:
      hosts:
        node1:
    etcd:
      hosts:
        node1:
    k8s-cluster:
      children:
        kube-master:
        kube-node:
    calico-rr:
      hosts: {}
  1. 執行以下命令即可開始安裝,線上安裝比較耗時請耐心等待:
ansible-playbook -i inventory/mycluster/hosts.yml --become --become-user=root cluster.yml
  1. 遇到網路問題失敗退出時很常見的事情,此時將上述命令重複執行即可,ansible對於已經執行過的命令會跳過的;
  2. 安裝完成時控制檯輸出類似如下的資訊(太多了,省略了一些):
Saturday 21 November 2020  17:47:18 +0800 (0:00:00.025)       0:30:03.154 ***** 
Saturday 21 November 2020  17:47:18 +0800 (0:00:00.024)       0:30:03.179 ***** 

PLAY RECAP **********************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
node1                      : ok=591  changed=95   unreachable=0    failed=0    skipped=1131 rescued=0    ignored=0   

Saturday 21 November 2020  17:47:18 +0800 (0:00:00.021)       0:30:03.200 ***** 
=============================================================================== 
download : download_file | Download item ------------------------------------------------------------------------------------------------------------------ 1008.61s
kubernetes/preinstall : Update package management cache (APT) ---------------------------------------------------------------------------------------------- 119.25s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 42.36s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 38.26s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 37.31s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 36.60s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 35.01s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 34.00s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 30.55s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 27.47s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 26.78s
kubernetes/master : kubeadm | Initialize first master ------------------------------------------------------------------------------------------------------- 25.98s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 23.42s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 22.14s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 21.50s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 20.17s
download : download_container | Download image if required -------------------------------------------------------------------------------------------------- 17.55s
container-engine/docker : ensure docker packages are installed ----------------------------------------------------------------------------------------------- 9.73s
kubernetes/master : Master | wait for kube-scheduler --------------------------------------------------------------------------------------------------------- 7.83s
kubernetes-apps/ansible : Kubernetes Apps | Lay Down CoreDNS Template ---------------------------------------------------------------------------------------- 6.93s
  1. 至此,kubernetes叢集環境部署完成,接下來簡單驗證一下環境是否可用;

檢查環境

  1. ssh登入ubuntu機器;
  2. 檢視節點、service、pod:
root@node1:~# kubectl get node -o wide
NAME    STATUS   ROLES    AGE    VERSION    INTERNAL-IP     EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION     CONTAINER-RUNTIME
node1   Ready    master   104m   v1.18.10   192.168.50.27   <none>        Ubuntu 20.04.1 LTS   5.4.0-54-generic   docker://19.3.12
root@node1:~# kubectl get node -o wide
NAME    STATUS   ROLES    AGE    VERSION    INTERNAL-IP     EXTERNAL-IP   OS-IMAGE             KERNEL-VERSION     CONTAINER-RUNTIME
node1   Ready    master   105m   v1.18.10   192.168.50.27   <none>        Ubuntu 20.04.1 LTS   5.4.0-54-generic   docker://19.3.12
root@node1:~# kubectl get services --all-namespaces
NAMESPACE     NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes                  ClusterIP   10.233.0.1      <none>        443/TCP                  105m
kube-system   coredns                     ClusterIP   10.233.0.3      <none>        53/UDP,53/TCP,9153/TCP   104m
kube-system   dashboard-metrics-scraper   ClusterIP   10.233.12.230   <none>        8000/TCP                 104m
kube-system   kubernetes-dashboard        ClusterIP   10.233.61.24    <none>        443/TCP                  104m
root@node1:~# kubectl get pods --all-namespaces
NAMESPACE     NAME                                          READY   STATUS    RESTARTS   AGE
kube-system   calico-kube-controllers-6ccb68f9b5-kwqck      1/1     Running   0          104m
kube-system   calico-node-4lmpf                             1/1     Running   0          104m
kube-system   coredns-dff8fc7d-2gnl8                        1/1     Running   0          104m
kube-system   coredns-dff8fc7d-4vthn                        0/1     Pending   0          104m
kube-system   dns-autoscaler-66498f5c5f-qh4vb               1/1     Running   0          104m
kube-system   kube-apiserver-node1                          1/1     Running   0          105m
kube-system   kube-controller-manager-node1                 1/1     Running   0          105m
kube-system   kube-proxy-kk84b                              1/1     Running   0          105m
kube-system   kube-scheduler-node1                          1/1     Running   0          105m
kube-system   kubernetes-dashboard-667c4c65f8-8ckf5         1/1     Running   0          104m
kube-system   kubernetes-metrics-scraper-54fbb4d595-dk42t   1/1     Running   0          104m
kube-system   nodelocaldns-d69h9                            1/1     Running   0          104m
  • 可見一些必須的pod和服務都已經啟動了,接下來試試dashboard能否正常訪問;

訪問dashboard

dashboard可以檢視kubernetes系統的整體情況,為了訪問dashboard頁面,需要增加RBAC:

  1. ssh登入ubuntu機器;
  2. 執行以下命令,建立檔案admin-user.yaml
tee admin-user.yaml <<-'EOF'
apiVersion: v1
kind: ServiceAccount
metadata:
  name: admin-user
  namespace: kube-system
EOF
  1. 執行以下命令,建立檔案admin-user-role.yaml
tee admin-user-role.yaml <<-'EOF'
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
  name: admin-user
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: cluster-admin
subjects:
- kind: ServiceAccount
  name: admin-user
  namespace: kube-system
EOF
  1. 建立ServiceAccount和ClusterRoleBinding:
kubectl create -f admin-user.yaml && kubectl create -f admin-user-role.yaml
  1. kubernetes-dashboard這個服務的型別從ClusterIP改為NodePort,這樣我們就能從瀏覽器訪問dashboard了:
kubectl  patch svc kubernetes-dashboard -n kube-system \
> -p '{"spec":{"type":"NodePort","ports":[{"port":443,"targetPort":8443,"nodePort":30443}]}}'
  1. 再看服務,已經成功改為NodePort
root@node1:~# kubectl get service --all-namespaces
NAMESPACE     NAME                        TYPE        CLUSTER-IP      EXTERNAL-IP   PORT(S)                  AGE
default       kubernetes                  ClusterIP   10.233.0.1      <none>        443/TCP                  132m
kube-system   coredns                     ClusterIP   10.233.0.3      <none>        53/UDP,53/TCP,9153/TCP   131m
kube-system   dashboard-metrics-scraper   ClusterIP   10.233.12.230   <none>        8000/TCP                 131m
kube-system   kubernetes-dashboard        NodePort    10.233.61.24    <none>        443:30443/TCP            131m
  1. 獲取token看,用於登入dashboard頁面:
kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')
  1. 下圖紅框中就是token的內容:

在這裡插入圖片描述
9. 現在通過瀏覽器訪問dashboard頁面了,地址是:https://192.168.50.27:30443 ,其中192.168.50.27是ubuntu機器的IP地址;
10. 由於不是https協議,因此瀏覽器可能彈出安全提示,如下圖,選擇繼續前往

在這裡插入圖片描述
11. 此時頁面會讓您選擇登入方式,選擇令牌並輸入前面得到的token,即可登入:

在這裡插入圖片描述

  1. 登入成功後可以見到系統資訊,如下圖:

在這裡插入圖片描述
至此,kubespray-2.14.2安裝kubernetes-1.18.10完成,希望本文能給您一些參考。

你不孤單,欣宸原創一路相伴

  1. Java系列
  2. Spring系列
  3. Docker系列
  4. kubernetes系列
  5. 資料庫+中介軟體系列
  6. DevOps系列

歡迎關注公眾號:程式設計師欣宸

微信搜尋「程式設計師欣宸」,我是欣宸,期待與您一同暢遊Java世界...
https://github.com/zq2599/blog_demos

相關文章