K8S從入門到放棄系列-(1)環境初始化


一、系統規划

 主機名 IP   組件
 k8s-master01 10.10.0.18 etcd、kube-apiserver、kube-controller-manager、kube-scheduler
 k8s-master02 10.10.0.19 etcd、kube-apiserver、kube-controller-manager、kube-scheduler
 k8s-master03 10.10.0.20  etcd、kube-apiserver、kube-controller-manager、kube-scheduler
 k8s-node01 10.10.0.21  kubelet、kube-proxy、docker、dns、calico
 k8s-node02 10.10.0.22  kubelet、kube-proxy、docker、dns、calico

 

 

 

 

 

 

 

 

二、初始化系統基礎環境

系統初始化時由於5台機器大部分操作都相同,我這里在配置過程中,在一台主機上進行配置文件創建,然后使用ansible進行分發,當然你也可以直接在對應主機上進行操作。

 1)設置主機名

在五台機器分別執行對應設置主機名的命令

[root@localhost ~]# hostnamectl set-hostname k8s-master01
[root@localhost ~]# hostnamectl set-hostname k8s-master02
[root@localhost ~]# hostnamectl set-hostname k8s-master03
[root@localhost ~]# hostnamectl set-hostname k8s-node01
[root@localhost ~]# hostnamectl set-hostname k8s-node02

 2)配置免密鑰登陸

以k8s-master01為主機,對另外4台機器進行免密鑰登陸 

[root@k8s-master01 ~]# ssh-keygen ##一路回車進行公鑰私鑰創建
[root@k8s-master01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.0.18
[root@k8s-master01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.0.19
[root@k8s-master01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.0.20
[root@k8s-master01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.0.21
[root@k8s-master01 ~]# ssh-copy-id -i ~/.ssh/id_rsa.pub root@10.10.0.22

3、安裝ansible(可以不安裝,把生成文件或者命令在各節點執行即可)

這里只需在master01節點安裝即可,后續一些操作均在此機器上執行,然后把生成的文件分發至對應節點

[root@k8s-master01 ~]# yum install -y epel-release
[root@k8s-master01 ~]#  yum install ansible -y
[root@k8s-master01 ~]# ansible --version
ansible 2.7.10
    ......
    ......

定義主機組

[root@k8s-master01 ~]# vim /etc/ansible/hosts 
[k8s-master] #master節點服務器組
10.10.0.18
10.10.0.19
10.10.0.20

[k8s-node]  #node節點服務器組
10.10.0.21
10.10.0.22

[k8s-all]  #k8s集群服務器組
10.10.0.18
10.10.0.19
10.10.0.20
10.10.0.21
10.10.0.22
[root@k8s-master01 ~]# ansible k8s-all -m ping  #測試ansible是否正常
10.10.0.20 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.10.0.19 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.10.0.22 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.10.0.21 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
10.10.0.18 | SUCCESS => {
    "changed": false, 
    "ping": "pong"
}
4、關閉防火牆、selinux(5台機器都執行,我這里使用ansible)
##如果你不使用ansible,在各個機器執行一下命令
systemctl stop firewalld
systemctl disable firewalld
setenforce  0 
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/sysconfig/selinux 
sed -i "s/^SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
[root@k8s-master01 ~]# ansible k8s-all -m shell -a 'systemctl stop firewalld'
[root@k8s-master01 ~]# ansible k8s-all -m shell -a 'systemctl disable firewalld'
[root@k8s-master01 ~]# ansible k8s-all -m shell -a 'setenforce  0'
[root@k8s-master01 ~]# ansible k8s-all -m replace -a 'path=/etc/sysconfig/selinux regexp="SELINUX=enforcing" replace=SELINUX=disabled'
[root@k8s-master01 ~]# ansible k8s-all -m replace -a 'path=/etc/selinux/config regexp="SELINUX=enforcing" replace=SELINUX=disabled'
5、配置host主機域名解析

[root@k8s-master01 ~]# vim /etc/hosts
10.10.0.18 k8s-master01 10.10.0.19 k8s-master02 10.10.0.20 k8s-master03 10.10.0.21 k8s-node01 10.10.0.22 k8s-node02 [root@k8s-master01 ~]# ansible k8s-all -m copy -a "src=/etc/hosts dest=/etc/hosts" ##文件分發
6、設置內核

[root@k8s-master01 ~]# vim /etc/sysctl.d/k8s.conf
    net.ipv4.ip_forward = 1
    net.bridge.bridge-nf-call-ip6tables = 1
    net.bridge.bridge-nf-call-iptables = 1
[root@k8s-master01 ~]# ansible k8s-all -m copy -a "src=/etc/sysctl.d/k8s.conf dest=/etc/sysctl.d/k8s.conf"
[root@k8s-master01 ~]# ansible k8s-all -m shell -a 'modprobe br_netfilter'
[root@k8s-master01 ~]# ansible k8s-all -m shell -a 'sysctl -p /etc/sysctl.d/k8s.conf'
7、時間同步

[root@k8s-master01 ~]# ansible k8s-all -m yum -a "name=ntpdate state=latest" 
[root@k8s-master01 ~]# ansible k8s-all -m cron -a "name='k8s cluster crontab' minute=*/30 hour=* day=* month=* weekday=* job='ntpdate time7.aliyun.com >/dev/null 2>&1'"
[root@k8s-master01 ~]# ansible k8s-all -m shell -a "ntpdate time7.aliyun.com"

 8、創建集群目錄

在集群組件部署之前,先進行對應的目錄創建

## 所有節點所需目錄
[root@k8s-master01 ~]# ansible k8s-all -m file -a 'path=/etc/kubernetes/ssl state=directory'
[root@k8s-master01 ~]# ansible k8s-all -m file -a 'path=/etc/kubernetes/config state=directory'
## k8s-master01節點所需目錄
[root@k8s-master01 ~]# mkdir /opt/k8s/{certs,cfg,unit} -p

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM