centos 7は、クベルダmを使用して、高使用k 8 sクラスタを構成する.

23967 ワード

原文のリンク:https://juejin.im/post/5b460ec1e51d45195866c935
概要kubeadmを使用して、マルチmasterノードを構成し、高い利用可能性を実現する.
インストール
実験環境の説明
実験アーキテクチャ図
lab1: etcd master haproxy keepalived 11.11.11.111
lab2: etcd master haproxy keepalived 11.11.11.112
lab3: etcd master haproxy keepalived 11.11.11.113
lab4: node  11.11.11.114
lab5: node  11.11.11.115
lab6: node  11.11.11.116

vip(loadblancer ip): 11.11.11.110
実験に使うVagrantfile
# -*- mode: ruby -*-
# vi: set ft=ruby :

ENV["LC_ALL"] = "en_US.UTF-8"

Vagrant.configure("2") do |config|
    (1..6).each do |i|
      config.vm.define "lab#{i}" do |node|
        node.vm.box = "centos-7.4-docker-17"
        node.ssh.insert_key = false
        node.vm.hostname = "lab#{i}"
        node.vm.network "private_network", ip: "11.11.11.11#{i}"
        node.vm.provision "shell",
          inline: "echo hello from node #{i}"
        node.vm.provider "virtualbox" do |v|
          v.cpus = 2
          v.customize ["modifyvm", :id, "--name", "lab#{i}", "--memory", "2048"]
        end
      end
    end
end
すべてのマシンにkubeadmを取り付けます.
前の文章「centos 7インストールkubeadm」を参考にしてください.
すべてのノードを配置するkubelet
#   kubelet        
#   /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
#        
Environment="KUBELET_EXTRA_ARGS=--pod-infra-container-image=registry.cn-shanghai.aliyuncs.com/gcr-k8s/pause-amd64:3.0"

#     
sed -i '/ExecStart=$/i Environment="KUBELET_EXTRA_ARGS=--pod-infra-container-image=registry.cn-shanghai.aliyuncs.com/gcr-k8s/pause-amd64:3.0"'  /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

#       
systemctl daemon-reload
すべてのノードのホームを設定します.
cat >>/etc/hosts<
起動etcdクラスタlab1,lab2,lab3ノードでetcdクラスタを起動する.
# lab1
docker stop etcd && docker rm etcd
rm -rf /data/etcd
mkdir -p /data/etcd
docker run -d \
--restart always \
-v /etc/etcd/ssl/certs:/etc/ssl/certs \
-v /data/etcd:/var/lib/etcd \
-p 2380:2380 \
-p 2379:2379 \
--name etcd \
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd-amd64:3.1.12 \
etcd --name=etcd0 \
--advertise-client-urls=http://11.11.11.111:2379 \
--listen-client-urls=http://0.0.0.0:2379 \
--initial-advertise-peer-urls=http://11.11.11.111:2380 \
--listen-peer-urls=http://0.0.0.0:2380 \
--initial-cluster-token=9477af68bbee1b9ae037d6fd9e7efefd \
--initial-cluster=etcd0=http://11.11.11.111:2380,etcd1=http://11.11.11.112:2380,etcd2=http://11.11.11.113:2380 \
--initial-cluster-state=new \
--auto-tls \
--peer-auto-tls \
--data-dir=/var/lib/etcd

# lab2
docker stop etcd && docker rm etcd
rm -rf /data/etcd
mkdir -p /data/etcd
docker run -d \
--restart always \
-v /etc/etcd/ssl/certs:/etc/ssl/certs \
-v /data/etcd:/var/lib/etcd \
-p 2380:2380 \
-p 2379:2379 \
--name etcd \
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd-amd64:3.1.12 \
etcd --name=etcd1 \
--advertise-client-urls=http://11.11.11.112:2379 \
--listen-client-urls=http://0.0.0.0:2379 \
--initial-advertise-peer-urls=http://11.11.11.112:2380 \
--listen-peer-urls=http://0.0.0.0:2380 \
--initial-cluster-token=9477af68bbee1b9ae037d6fd9e7efefd \
--initial-cluster=etcd0=http://11.11.11.111:2380,etcd1=http://11.11.11.112:2380,etcd2=http://11.11.11.113:2380 \
--initial-cluster-state=new \
--auto-tls \
--peer-auto-tls \
--data-dir=/var/lib/etcd

# lab3
docker stop etcd && docker rm etcd
rm -rf /data/etcd
mkdir -p /data/etcd
docker run -d \
--restart always \
-v /etc/etcd/ssl/certs:/etc/ssl/certs \
-v /data/etcd:/var/lib/etcd \
-p 2380:2380 \
-p 2379:2379 \
--name etcd \
registry.cn-hangzhou.aliyuncs.com/google_containers/etcd-amd64:3.1.12 \
etcd --name=etcd2 \
--advertise-client-urls=http://11.11.11.113:2379 \
--listen-client-urls=http://0.0.0.0:2379 \
--initial-advertise-peer-urls=http://11.11.11.113:2380 \
--listen-peer-urls=http://0.0.0.0:2380 \
--initial-cluster-token=9477af68bbee1b9ae037d6fd9e7efefd \
--initial-cluster=etcd0=http://11.11.11.111:2380,etcd1=http://11.11.11.112:2380,etcd2=http://11.11.11.113:2380 \
--initial-cluster-state=new \
--auto-tls \
--peer-auto-tls \
--data-dir=/var/lib/etcd

#       
docker exec -ti etcd ash
etcdctl member list
etcdctl cluster-health
exit
最初のマスターノードで初期化
#   token
#   token      
token=$(kubeadm token generate)
echo $token

#       
cat >kubeadm-master.config<#imageRepository: registry.cn-shanghai.aliyuncs.com/gcr-k8s
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers

api:
  advertiseAddress: 11.11.11.111

apiServerExtraArgs:
  endpoint-reconciler-type: lease

controllerManagerExtraArgs:
  node-monitor-grace-period: 10s
  pod-eviction-timeout: 10s

networking:
  podSubnet: 192.168.0.0/16

etcd:
  endpoints:
  - "http://11.11.11.111:2379"
  - "http://11.11.11.112:2379"
  - "http://11.11.11.113:2379"

apiServerCertSANs:
- "lab1"
- "lab2"
- "lab3"
- "11.11.11.111"
- "11.11.11.112"
- "11.11.11.113"
- "11.11.11.110"
- "127.0.0.1"

token: $token
tokenTTL: "0"

featureGates:
  CoreDNS: true
EOF

#    
kubeadm init --config kubeadm-master.config
systemctl enable kubelet

#           join  
#           "kubeadm token list"  
# kubeadm join 11.11.11.111:6443 --token nevmjk.iuh214fc8i0k3iue --discovery-token-ca-cert-hash sha256:0e4f738348be836ff810bce754e059054845f44f01619a37b817eba83282d80f

#   kubectl  
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config


#       
#     
mkdir flannel && cd flannel
wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

#     
#    ip      kubeadm pod-network  
  net-conf.json: |
    {
      "Network": "192.168.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }

#     
image: registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel:v0.10.0-amd64

#   
kubectl apply -f kube-flannel.yml

#   Node       ,  flannel issues 39701,
# https://github.com/kubernetes/kubernetes/issues/39701
#      kube-flannel.yml   --iface               ,
#        dns    。         ,   kube-flannel.yml     ,
# flanneld      --iface=
    containers:
      - name: kube-flannel
        image: registry.cn-shanghai.aliyuncs.com/gcr-k8s/flannel:v0.10.0-amd64
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        - --iface=eth1

#   
kubectl get pods --namespace kube-system
kubectl get svc --namespace kube-system

#   master      pod,      ,            
#   dashboard, heapster, efk 
kubectl taint nodes --all node-role.kubernetes.io/master-
他のマスターノードを起動します.
#      master      /etc/kubernetes/pki  
cd /etc/kubernetes && tar czvf /root/pki.tgz pki/ && cd ~

#      master /etc/kubernetes   
tar xf pki.tgz -C /etc/kubernetes/

#   pki    apiserver.crt   apiserver.key  
rm -rf /etc/kubernetes/pki/{apiserver.crt,apiserver.key}

#       
#      master       
# token    
cat >kubeadm-master.config<#imageRepository: registry.cn-shanghai.aliyuncs.com/gcr-k8s
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers

#     IP
api:
  advertiseAddress: 11.11.11.112

apiServerExtraArgs:
  endpoint-reconciler-type: lease

controllerManagerExtraArgs:
  node-monitor-grace-period: 10s
  pod-eviction-timeout: 10s

networking:
  podSubnet: 192.168.0.0/16

etcd:
  endpoints:
  - "http://11.11.11.111:2379"
  - "http://11.11.11.112:2379"
  - "http://11.11.11.113:2379"

apiServerCertSANs:
- lab1
- lab2
- lab3
- "11.11.11.111"
- "11.11.11.112"
- "11.11.11.113"
- "11.11.11.110"
- "127.0.0.1"

token: nevmjk.iuh214fc8i0k3iue
tokenTTL: "0"

featureGates:
  CoreDNS: true
EOF

#    
kubeadm init --config kubeadm-master.config
systemctl enable kubelet

#     
kubectl get pod --all-namespaces -o wide | grep lab1
kubectl get pod --all-namespaces -o wide | grep lab2
kubectl get pod --all-namespaces -o wide | grep lab3
kubectl get nodes -o wide
haproxyエージェントとkeepalivedを設定します.lab1,lab2,lab3ノードでhaproxyおよびkeepalivedを起動する.
#   haproxy  
docker pull haproxy:1.7.8-alpine
mkdir /etc/haproxy
cat >/etc/haproxy/haproxy.cfg<log 127.0.0.1 local0 err
  maxconn 50000
  uid 99
  gid 99
  #daemon
  nbproc 1
  pidfile haproxy.pid

defaults
  mode http
  log 127.0.0.1 local0 err
  maxconn 50000
  retries 3
  timeout connect 5s
  timeout client 30s
  timeout server 30s
  timeout check 2s

listen admin_stats
  mode http
  bind 0.0.0.0:1080
  log 127.0.0.1 local0 err
  stats refresh 30s
  stats uri     /haproxy-status
  stats realm   Haproxy\ Statistics
  stats auth    will:will
  stats hide-version
  stats admin if TRUE

frontend k8s-https
  bind 0.0.0.0:8443
  mode tcp
  #maxconn 50000
  default_backend k8s-https

backend k8s-https
  mode tcp
  balance roundrobin
  server lab1 11.11.11.111:6443 weight 1 maxconn 1000 check inter 2000 rise 2 fall 3
  server lab2 11.11.11.112:6443 weight 1 maxconn 1000 check inter 2000 rise 2 fall 3
  server lab3 11.11.11.113:6443 weight 1 maxconn 1000 check inter 2000 rise 2 fall 3
EOF

#   haproxy
docker run -d --name my-haproxy \
-v /etc/haproxy:/usr/local/etc/haproxy:ro \
-p 8443:8443 \
-p 1080:1080 \
--restart always \
haproxy:1.7.8-alpine

#     
docker logs my-haproxy

#        
http://11.11.11.111:1080/haproxy-status
http://11.11.11.112:1080/haproxy-status

#   keepalived  
docker pull osixia/keepalived:1.4.4

#   
#         
lsmod | grep ip_vs
modprobe ip_vs

#   keepalived
# eth1     11.11.11.0/24       
docker run --net=host --cap-add=NET_ADMIN \
-e KEEPALIVED_INTERFACE=eth1 \
-e KEEPALIVED_VIRTUAL_IPS="#PYTHON2BASH:['11.11.11.110']" \
-e KEEPALIVED_UNICAST_PEERS="#PYTHON2BASH:['11.11.11.111','11.11.11.112','11.11.11.113']" \
-e KEEPALIVED_PASSWORD=hello \
--name k8s-keepalived \
--restart always \
-d osixia/keepalived:1.4.4

#     
#        backup     master
docker logs k8s-keepalived

#       11.11.11.110        
# ping  
ping -c4 11.11.11.110

#         ,    
docker rm -f k8s-keepalived
ip a del 11.11.11.110/32 dev eth1

#   ~/.kube/config   ip   ,    kubectl  
rm -rf .kube/cache .kube/http-cache
kubectl get pods -n kube-system -o wide
マスターノード関連コンポーネントの配置を変更してvipを指します.
# lab1 lab2 lab3
sed -i 's@server: https://11.11.11.*:6443@server: https://11.11.11.110:8443@g' /etc/kubernetes/{admin.conf,kubelet.conf,scheduler.conf,controller-manager.conf}

#   kubelet
systemctl daemon-reload
systemctl restart kubelet docker

#         
kubectl get nodes -o wide
kube-proxyの配置を修正します.
#   kube-proxy     vip
#           server: https://11.11.11.110:8443
kubectl edit -n kube-system configmap/kube-proxy

#     
kubectl get -n kube-system configmap/kube-proxy -o yaml

#     kube-proxy
kubectl get pods --all-namespaces -o wide | grep proxy
all_proxy_pods=$(kubectl get pods --all-namespaces -o wide | grep proxy | awk '{print $2}' | xargs)
echo $all_proxy_pods
kubectl delete pods $all_proxy_pods -n kube-system
kubectl get pods --all-namespaces -o wide | grep proxy
nodeノードを起動
#   master  
#           master   ,     
kubeadm join 11.11.11.110:8443 --token nevmjk.iuh214fc8i0k3iue --discovery-token-ca-cert-hash sha256:0e4f738348be836ff810bce754e059054845f44f01619a37b817eba83282d80f
systemctl enable kubelet
nodeノードkubeletの設定を修正して再起動します.
#     
sed -i 's@server: https://11.11.11.*:6443@server: https://11.11.11.110:8443@g' /etc/kubernetes/kubelet.conf

#   kubelet
systemctl daemon-reload
systemctl restart kubelet docker

#         
kubectl get nodes -o wide
マスターノードのアプリケーションのリリースを禁止します.
設定マスターは負荷を受け付けません.
#     
kubectl get nodes

#   
# kubectl patch node lab1 -p '{"spec":{"unschedulable":true}}'
kubectl taint nodes lab1 lab2 lab3 node-role.kubernetes.io/master=true:NoSchedule

#     
kubectl get nodes
テスト
複数のcorednsのコピーを再構築します.
#   coredns pods
kubectl get pods -n kube-system -o wide | grep coredns
all_coredns_pods=$(kubectl get pods -n kube-system -o wide | grep coredns | awk '{print $1}' | xargs)
echo $all_coredns_pods
kubectl delete pods $all_coredns_pods -n kube-system

#      
# replicas: 3
#      node     
kubectl edit deploy coredns -n kube-system

#     
kubectl get pods -n kube-system -o wide | grep coredns
基礎テスト
1.起動
#         
kubectl run nginx --replicas=2 --image=nginx:alpine --port=80
kubectl expose deployment nginx --type=NodePort --name=example-service-nodeport
kubectl expose deployment nginx --name=example-service

#         
cat >example-nginx.yml<type: NodePort
    ports:
      - name: http-nodeport
        port: 80
        nodePort: 32223
EOF
kubectl apply -f example-nginx.yml
2.状態を確認する
kubectl get deploy
kubectl get pods
kubectl get svc
kubectl describe svc example-service
3.DNS解析
kubectl run curl --image=radial/busyboxplus:curl -i --tty
nslookup kubernetes
nslookup example-service
curl example-service

#            ,             
curlPod=$(kubectl get pod | grep curl | awk '{print $1}')
kubectl exec -ti $curlPod -- sh
4.アクセステスト
# 10.96.59.56    svc     clusterip
curl "10.96.59.56:80"

# 32223    svc      nodeport
http://11.11.11.114:32223/
http://11.11.11.115:32223/
3.消去
kubectl delete svc example-service example-service-nodeport
kubectl delete deploy nginx curl
利用可能なテストmasterノードを閉じて、クラスタをテストすることは、前のステップの を正常に実行するかどうかであり、関連情報を確認し、lab1およびlab2を同時にオフすることができない.
kubectl get pod --all-namespaces -o wide
kubectl get pod --all-namespaces -o wide | grep lab1
kubectl get pod --all-namespaces -o wide | grep lab2
kubectl get pod --all-namespaces -o wide | grep lab3
kubectl get nodes -o wide
kubectl get deploy
kubectl get pods
kubectl get svc
kubectl describe svc example-service
注意事項
  • は、haproxyノードを直接オフにすると、keepalivedを過ぎた後に、上記のpodだけが問題が検出され、他のノード
  • に移る.
    急速な移行を望むならnodeを実行することができます.5 kubectl delete nodeパラメータ、標準5 mを変更することもできます.controller-manager パラメータ、デフォルト40 s
    参考文献
  • https://kubernetes.io/docs/admin/high-availability/
  • https://www.kubernetes.org.cn/3536.html
  • https://github.com/indiketa/kubeadm-ha
  • https://zhuanlan.zhihu.com/p/34740013
  • https://github.com/cookeem/kubeadm-ha/blob/master/README_CN.md
  • https://blog.frognew.com/2017/04/install-etcd-cluster.html
  • https://blog.frognew.com/2017/04/install-ha-kubernetes-1.6-cluster.html
  • https://medium.com/@bambosh/ha-kubergnetes-cluster-via-kubeadm-b 213360 b 198
  • https://github.com/kubernetes/kubeadm/issues/546
  • https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm-init/#config-file