k8s第二回之k8s叢集的安裝

MarkGuo發表於2020-12-20

1. k8s叢集的安裝


Kubernetes借鑑了Borg的設計理念,比如Pod、Service、Labels和單Pod單IP等。Kubernetes的整體架構跟Borg非常像,如下圖所示:

1、架構:

除了核心元件,還有一些推薦的Add-ons:

元件名稱 說明
kube-dns 負責為整個叢集提供DNS服務
Ingress Controller 為服務提供外網入口
Heapster 提供資源監控
Dashboard 提供GUI
Federation 提供跨可用區的叢集
Fluentd-elasticsearch 提供叢集日誌採集、儲存與查詢

2、環境準備

3臺主機,IP地址、主機名的host解析分別如下(最好再做一下SSH免祕鑰的登入,實驗環境方便操作!)

10.0.0.11  k8s-master
10.0.0.12  k8s-node-1
10.0.0.13  k8s-node-2

3、master節點安裝etcd

1. 安裝 etcd
yum install etcd -y

2. 編輯配置檔案
6行:ETCD_LISTEN_CLIENT_URLS="http://0.0.0.0:2379"
21行:ETCD_ADVERTISE_CLIENT_URLS="http://10.0.0.11:2379"

3. 啟動並自啟服務
systemctl start etcd.service
systemctl enable etcd.service

----------------------------------------------------------
etcdctl set testdir/testkey0 0
etcdctl get testdir/testkey0

etcdctl -C http://10.0.0.11:2379 cluster-health

注: etcd原生支援做叢集,etcd服務可以安裝在其他伺服器做成etcd叢集,通過kubernets連線

4、 master節點安裝kubernetes

1. 安裝kubernetes
yum install kubernetes-master.x86_64 -y

2. 編輯配置
vim /etc/kubernetes/apiserver 
8行:  KUBE_API_ADDRESS="--insecure-bind-address=0.0.0.0"
11行:KUBE_API_PORT="--port=8080"
14行: KUBELET_PORT="--kubelet-port=10250"
17行:KUBE_ETCD_SERVERS="--etcd-servers=http://10.0.0.11:2379"
23行:KUBE_ADMISSION_CONTROL="--admission-control=NamespaceLifecycle,NamespaceExists,LimitRanger,SecurityContextDeny,ResourceQuota"

vim /etc/kubernetes/config
22行:KUBE_MASTER="--master=http://10.0.0.11:8080"

3. 重啟並自啟k8s
systemctl enable kube-apiserver.service
systemctl restart kube-apiserver.service
systemctl enable kube-controller-manager.service
systemctl restart kube-controller-manager.service
systemctl enable kube-scheduler.service
systemctl restart kube-scheduler.service

檢查服務是否安裝正常

[root@k8s-master ~]# kubectl get componentstatus 
NAME                 STATUS    MESSAGE             ERROR
scheduler            Healthy   ok                  
controller-manager   Healthy   ok                  
etcd-0               Healthy   {"health":"true"} 

5、node節點安裝kubernetes

#1. 安裝kubernetes-node
yum install kubernetes-node.x86_64 -y

#2. 編輯配置
vim /etc/kubernetes/config 
22行:KUBE_MASTER="--master=http://10.0.0.11:8080"

vim /etc/kubernetes/kubelet
5行:KUBELET_ADDRESS="--address=0.0.0.0"
8行:KUBELET_PORT="--port=10250"
11行:KUBELET_HOSTNAME="--hostname-override=10.0.0.12"
14行:KUBELET_API_SERVER="--api-servers=http://10.0.0.11:8080"

#3. 啟動並自啟服務
systemctl enable kubelet.service
systemctl restart kubelet.service
systemctl enable kube-proxy.service
systemctl restart kube-proxy.service
systemctl enable docker

在master節點檢查驗證:

[root@k8s-master  /etc/kubernetes ]#: kubectl get nodes
NAME        STATUS    AGE
10.0.0.12   Ready     2m
10.0.0.13   Ready     5s

6、所有節點配置flannel網路

flannel和overlay網路類似需要資料庫,flannel需要etcd資料庫

yum install flannel -y
sed -i 's#http://127.0.0.1:2379#http://10.0.0.11:2379#g' /etc/sysconfig/flanneld

##master節點:
etcdctl mk /atomic.io/network/config   '{ "Network": "172.18.0.0/16","Backend": {"Type": "vxlan"} }'

#所有節點執行
systemctl enable flanneld.service 
systemctl restart flanneld.service 

##node節點:
systemctl enable flanneld.service 
systemctl restart flanneld.service 
systemctl  restart  docker
systemctl restart kubelet.service
systemctl restart kube-proxy.service

vim /usr/lib/systemd/system/docker.service
#在[Service]區域下增加一行
ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT
systemctl daemon-reload 
systemctl restart docker
systemd中有這樣功能的配置:
1) ExexStart 
2) ExexStartPost 

最後在master節點驗證結果如下:

7、配置master為映象倉庫

#所有節點執行
##1. 配置映象加速
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"insecure-registries": ["10.0.0.11:5000"]
}

##2. 過載docker
systemctl restart docker

#master節點
yum install docker -y
systemctl enable docker
systemctl start docker
docker run -d -p 5000:5000 --restart=always --name registry -v /opt/myregistry:/var/lib/registry  registry

相關文章