1. 程式人生 > >kubernetes1.9.2基於kubeadm的高可用安裝HA_Kubernetes中文社群

kubernetes1.9.2基於kubeadm的高可用安裝HA_Kubernetes中文社群

使用kubeadm安裝安全高可用kubernetes叢集
[安裝包地址]

非高可用安裝三步即可

  1. 在master上 cd shell && init.sh && master.sh
  2. 在node上 init.sh
  3. 在node上執行master輸出的join命令

1. 建議二進位制方法提前部署好docker-compose,步驟參考後文

2. 建議永久關閉selinux和swap以免後續問題

3. 建議停止並關閉firewalld/iptables等防火牆

 安裝包介紹

解壓完之後看到如下目錄:

├── bin 所需要的k8s相關的bin檔案
│ ├── kubeadm
│ ├── kubectl
│ └── kubelet
├── image 依賴的所有映象包
│ └── images.tar
├── out 所有的配置檔案
│ ├── dashboard dashboard相關配置
│ │ ├── dashboard-admin.yaml
│ │ └── kubernetes-dashboard.yaml
│ ├── etcd etcd相關配置
│ │ ├── etcd-docker-compose-0.yml
│ │ ├── etcd-docker-compose-1.yml
│ │ └── etcd-docker-compose-2.yml
│ ├── haproxy haproxy相關配置
│ │ └── haproxy.cfg
│ ├── heapster heapster相關yaml配置
│ │ ├── influxdb
│ │ │ ├── grafana.yaml
│ │ │ ├── heapster.yaml
│ │ │ └── influxdb.yaml
│ │ └── rbac
│ │ └── heapster-rbac.yaml
│ ├── kube k8s自身配置
│ │ ├── 10-kubeadm.conf
│ │ ├── config kubeadm配置
│ │ └── kubelet.service
│ ├── kubeinit.json 忽略
│ └── net 網路相關配置
│ ├── calico.yaml
│ └── calicoctl.yaml
└── shell 初始化指令碼
├── init.sh 初始化節點,安裝bin檔案,systemd配置等
└── master.sh 執行kubeadm init和其它元件

初始化節點

叢集中所有節點都需要執行`cd shell ; sh init.sh`

有以下需要注意的事項:
kubelet的啟動檔案裡面的cgroups配置和(修改位置/etc/systemd/system/kubelet.service.d) 與 docker info|grep Cg一致

 起動etcd叢集

etcd叢集安裝使用docker-compose方式部署

A.使用docker-compose啟動,如果沒裝:

$ pip install docker-compose

B.使用二進位制包啟動docker-compose(離線可選)

$ wget https://github.com/docker/compose/releases/download/1.18.0/docker-compose-Linux-x86_64
 $ mv docker-compose-Linux-x86_64 /usr/local/bin/docker-compose && chmod a+x /usr/local/bin/docker-compose

在out/etcd目錄下有相關模板etcd-docker-compose-x.yaml,啟動多個節點時修改成自己的ip地址 其它兩個節點照抄,修改ip即可, image那行 應改為 gcr.io/google_containers/etcd-

amd64:3.1.11
 version: '2.1'
 services:
 etcd0:
 container_name: etcd_infra0
 image: gcr.io/google_containers/etcd-amd64:3.0.17 #這裡最後改為3.1.11
 command: |
 etcd --name infra0
 --initial-advertisie-peer-urls http://10.230.204.160:2380
 --listen-peer-urls http://10.230.204.160:2380
 --listen-client-urls http://10.230.204.160:2379,http://127.0.0.1:2379
 --advertise-client-urls http://10.230.204.160:2379
 --data-dir /etcd-data.etcd
 --initial-cluster-token etcd-cluster-1
 --initial-cluster infra0=http://10.230.204.160:2380,infra1=http://10.230.204.165:2380,infra2=http://10.230.204.151:2380
 --initial-cluster-state new
 restart: always
 volumes:
 - /data/etcd-data.etcd:/etcd-data.etcd
 network_mode: "host"

三個節點分別啟動:

$ docker-compose -f out/etcd/etcd-docker-compose-x.yml up -d

檢查是不是安裝成功:

$ docker exec etcd_infra0 etcdctl member list
 5ded6dd284b89d31: name=infra1 peerURLs=http://10.230.204.153:2380 clientURLs=http://10.230.204.153:2379 isLeader=true
 6d4b5eee32c1497a: name=infra0 peerURLs=http://10.230.204.150:2380 clientURLs=http://10.230.204.150:2379 isLeader=false
 729d9cd56debb1a1: name=infra2 peerURLs=http://10.230.204.154:2380 clientURLs=http://10.230.204.154:2379 isLeader=false

 kubeadm配置

修改配置 out/kube/config 檔案

apiVersion: kubeadm.k8s.io/v1alpha1
 kind: MasterConfiguration
 apiServerCertSANs: #此處填所有的masterip和lbip和其它你可能需要通過它訪問apiserver的地址和域名或者主機名等,如阿里fip,證書中會允許這些ip
 - 172.31.244.231
 - 172.31.244.232
 - 172.31.244.233
 - 172.31.244.234
 - master1
 - master2
 - master3
 - node1
 - 47.75.1.72

etcd:
 endpoints: #這裡填之前安裝的etcd叢集地址列表,修改IP地址
 - http://172.31.244.232:2379
 - http://172.31.244.233:2379
 - http://172.31.244.234:2379

apiServerExtraArgs:
 endpoint-reconciler-type: lease

networking:
 podSubnet: 192.168.0.0/16 #不用改
 kubernetesVersion: v1.9.2 #不用改
 featureGates: #不用改
 CoreDNS: true

然後執行:
 $ kubeadm init --config out/kube/config

把成功後的kubeadm join命令存在檔案裡,那東西不能丟了

 啟動calico等

mkdir ~/.kube && cp /etc/kubernetes/admin.conf ~/.kube/config

修改calico配置,把etcd地址換成你安裝好的叢集地址:

out/net/calico.yaml:

kind: ConfigMap
 apiVersion: v1
 metadata:
 name: calico-config
 namespace: kube-system
 data:
 # The location of your etcd cluster. This uses the Service clusterIP
 # defined below.
 etcd_endpoints: "http://10.96.232.136:6666" # 這裡改成etcd叢集地址如 "http://172.31.244.232:2379,http://172.31.244.233:2379,http://172.31.244.234:2379"

$ kubectl apply -f out/net/calico.yaml -f out/heapster/influxdb -f out/heapster/rbac -f out/dashboard

1. 然後訪問https://master1IP:32000埠即可,在chrome下無法進入提示證書有誤,更換firefox可以,提示說證書日期不對(待修復)

啟動多個master

第一個master我們稱之為master0 (假設其他master已經init.sh過),現在把第一個master的/etc/kubernetes/pki目錄拷貝到別的master節點上

$ mkdir -p /etc/kubernetes
 $ scp -r /etc/kubernetes/pki [email protected]:/etc/kubernetes/pki

刪除pki目錄下的apiserver.crt 和 apiserver.key檔案rm -rf apiserver.crt apiserver.key,注意如果不刪除會只能看到一個master,是不正常的

同樣使用master0上的out/kube/config檔案,複製內容,拷貝到master1上,scp out/kube/config [email protected]:/root/執行kubeadm init –config ~/config

master2節點同master1

 啟動loadbalance

我比較推薦使用四層代理 HAproxy配置out/haproxy目錄:

`vi out/haproxy/haproxy.cfg`

global
 daemon
 log 127.0.0.1 local0
 log 127.0.0.1 local1 notice
 maxconn 4096

defaults
 log global
 retries 3
 maxconn 2000
 timeout connect 5s
 timeout client 50s
 timeout server 50s

frontend k8s
 bind *:6444
 mode tcp
 default_backend k8s-backend

backend k8s-backend
 balance roundrobin
 mode tcp
 #下面三個ip替換成三個你自己master的地址
 server k8s-1 10.1.245.93:6443 check
 server k8s-1 10.1.245.94:6443 check
 server k8s-2 10.1.245.95:6443 check
 ```

先` mkdir /etc/haproxy` 然後把這個檔案拷貝在`cp out/haproxy/haproxy.cfg /etc/haproxy/haproxy.cfg`
 $ docker run --net=host -v /etc/haproxy:/usr/local/etc/haproxy --name ha -d haproxy:1.7

修改kubeproxy配置

$ kubectl -n kube-system edit configmap kube-proxy

找到master地址,修改成LB地址,6444埠 (這裡關鍵在於怎麼知道LB的地址到底是哪一個呀?上面配置之後三個masterIP 輪詢並不知道哪個是LB地址)

#找到檔案的這一塊,第七行server 有個ip地址

apiVersion: v1
 kind: Config
 clusters:
 - cluster:
 certificate-authority: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
 server: https://10.230.204.151:6443 #修改為 LoadBalanceIP:6444
 name: default
 contexts:
 - context:
 cluster: default
 namespace: default
 user: default
 name: default
 current-context: default
 users:
 - name: default
 user:
 tokenFile: /var/run/secrets/kubernetes.io/serviceaccount/token

join node節點
還是在node節點執行第一個master輸出的命令

$ kubeadm join --token 10.1.245.94:6443 --discovery-token-ca-cert-hash sha256:

 修改node節點kubelet配置

`vi /etc/kubernetes/kubelet.conf ` 同樣把地址修改成LB地址,如:`10.1.245.94:6444` ,修改如下第五行(展示的例子已經修改過)

apiVersion: v1
 clusters:
 - cluster:
 certificate-authority-data: xxxxxx #此處省略幾百字元
 server: https://10.230.204.160:6444 #修改這裡為LB:6444,原本是另外的ip:6443
 name: default-cluster
 contexts:
 - context:
 cluster: default-cluster
 namespace: default
 user: default-auth
 name: default-context
 current-context: default-context

 kubectl配置

修改`~/.kube/config`檔案,server的ip改成LB的ip `10.1.245.94:6444`

或者通過命令修改:

$ kubectl config set-cluster kubernetes --server=https://47.52.227.242:6444 --kubeconfig=$HOME/.kube/config

 啟動多DNS副本

$ kubectl edit deploy coredns -n kube-system

replicas: 3

[[email protected] ~]$ kubectl get pod -n kube-system -o wide|grep core
 coredns-65dcdb4cf-4j5s8 1/1 Running 0 39m 192.168.137.65 master1
 coredns-65dcdb4cf-ngx4h 1/1 Running 0 38s 192.168.180.1 master2
 coredns-65dcdb4cf-qbsr6 1/1 Running 0 38s 192.168.166.132 node1

這樣,啟動了三個dns

 驗證與測試

$ kubectl run test --image busybox sleep 10000
 $ kubectl exec your-busybox-pod-name nslookup kubernetes

殺非LB的master,多次測試看建立pod與dns是否正常,還可以telnet 10.96.0.1 443 去驗證clusterip是否正常