1. 程式人生 > >kubeadm安裝kubernetes環境初始化

kubeadm安裝kubernetes環境初始化

ges you serve ane coredns one watermark tps netfilter

服務器環境
192.168.1.252 k8s-node02
192.168.1.73 k8s-node01
192.168.1.101 k8s-master

環境設置

 a) 設定時間同步
 b) 本地hosts文件DNS解析
 c) 關閉iptables或者firewwalld並確保開啟不會自啟動
 d) 關閉selinux  禁用swap設備
 e) 如果使用ipvs的proxy模型  各節點需要載入ipvs的各模塊  

2: 設定時間同步

[[email protected] ~]# systemctl  start chronyd
[[email protected] ~]# systemctl  enable chronyd

3: 加載ipvs模塊

net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
    vm.swappiness = 0
執行命令讓參數生效
modprobe br_netfilter
sysctl -p 

1.2kube-proxy開啟ipvs的前置條件A,B兩種加載方式

A)  ipvs目錄:/usr/lib/modules/$(uname -r)/kernel/net/netfilter/ipvs
裝入ip_vs模塊:
  [[email protected] ipvs]# vim /etc/sysconfig/modules/ipvs.modules
    #!/bin/bash
        for i in `ls /usr/lib/modules/3.10.0-957.el7.x86_64/kernel/net/netfilter/ipvs |grep -o "^[^.]*"`; do /sbin/modinfo -F filename $i &>/dev/null; if [ $? -eq 0 ]; then   /sbin/modprobe $i; fi; don
  [[email protected] ipvs]# chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4               

B) 由於ipvs已經加入到了內核的主幹,所以為kube-proxy開啟ipvs的前提需要加載以下的內核模塊:
   ip_vs
   ip_vs_rr
   ip_vs_wrr
   ip_vs_sh
   nf_conntrack_ipv4       
在所有的Kubernetes節點node1和node2上執行以下腳本:
 cat > /etc/sysconfig/modules/ipvs.modules <<EOF
  #!/bin/bash
  modprobe -- ip_vs
  modprobe -- ip_vs_rr
  modprobe -- ip_vs_wrr
  modprobe -- ip_vs_sh
  modprobe -- nf_conntrack_ipv4
  EOF
  chmod 755 /etc/sysconfig/modules/ipvs.modules && bash /etc/sysconfig/modules/ipvs.modules && lsmod | grep -e ip_vs -e nf_conntrack_ipv4      

上面腳本創建了的/etc/sysconfig/modules/ipvs.modules文件,保證在節點重啟後能自動加載所需模塊。 使用lsmod | grep -e ip_vs -e nf_conntrack_ipv4命令查看是否已經正確加載所需的內核模塊。

接下來還需要確保各個節點上已經安裝了ipset軟件包yum install ipset。 為了便於查看ipvs的代理規則,最好安裝一下管理工具ipvsadm yum install ipvsadm。
如果以上前提條件如果不滿足,則即使kube-proxy的配置開啟了ipvs模式,也會退回到iptables模式。

安裝程序包

安裝docker(最新版本)(所有環境都需要安裝docker環境)
[[email protected] ~]# wget  https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
[[email protected] ~]# mv docker-ce.repo  /etc/yum.repos.d/
[[email protected] yum.repos.d]# scp docker-ce.repo  k8s-node01:/etc/yum.repos.d/
[[email protected] yum.repos.d]# scp docker-ce.repo  k8s-master:/etc/yum.repos.d/
同時安裝docker-ce(master和node01&node02)
 [[email protected] ~]# yum install -y  docker-ce
 [[email protected] yum.repos.d]# yum install -y  docker-ce       
 [[email protected] k8s-node02 yum.repos.d]# yum install -y  docker-ce      
 [[email protected] ~]# docker -v
      Docker version 18.09.5, build e8ff056
配置啟動docker (master和node01&node02)
[[email protected] ~]# vim /usr/lib/systemd/system/docker.service
        Environment="HTTPS_PROXY=http://www.ik8s.io:10070" #設置代理
    Environment="NO_PROXY=127.0.0.0/8,192.168.1.0/24"   #訪問次網絡 不需要代理
        ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
    ExecstartPost=/usr/sbin/iptables -P FORWARD ACCEPT #新增 意思:啟動docker的時候自動執行這個命令
啟動docker (master和node01&node02)
  [[email protected] ~]# systemctl  daemon-reload
  [[email protected] ~]# systemctl  start docker  
  [[email protected] yum.repos.d]# ps -ef|grep docker
     root      6617     1  1 10:55 ?        00:00:00 /usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock
查看iptables
       [[email protected] ~]# iptables -vnL
安裝kuberbetes依賴包 (master和node01&node02)
  #阿裏雲的開源鏡像站:https://mirrors.aliyun.com/kubernetes/
        添加阿裏雲的鏡像k8s:
[[email protected] yum.repos.d]# vim kubernetes.repo
   [kubernetes]
   name=k8s repo
   baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
   gpgcheck=1
   gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg           
                https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
查看repo文件
     [[email protected] yum.repos.d]# yum repolist  

技術分享圖片

查看kube的文件包

    [[email protected] yum.repos.d]# yum list|grep "^kube"

技術分享圖片
#master節點安裝
[[email protected] yum.repos.d]# yum install -y kubeadm kubelet kubectl
[[email protected] yum.repos.d]# rpm -qa kubelet
kubelet-1.14.1-0.x86_64
[[email protected] yum.repos.d]# rpm -ql kubelet
/etc/kubernetes/manifests
/etc/sysconfig/kubelet
/usr/bin/kubelet
/usr/lib/systemd/system/kubelet.service
[[email protected] yum.repos.d]#

如果你沒有設置swap禁用 此處應該設置 否則啟動會報錯
 [[email protected] yum.repos.d]# vim /etc/sysconfig/kubelet 
    KUBELET_EXTRA_ARGS="--fail-swap-on=false"
初始化master
 A) 使用命令初始化
        kubeadm init --kubernetes-version=v1.14.1 --apiserver-advertise-address=10.26.3.184 --pod-network-cidr=10.244.0.0/16  --ignore-preflight-errors=Swap
B)自己定義配置文件 使用--config來實現
使用kubeadm初始化 需要下載kube依賴包 我這裏不能訪問k8s.gcr.io於是從阿裏雲上拉去別人的放到了我的dockerhub 參考:https://blog.51cto.com/nhylovezyh/2377982?source=drt
k8s.gcr.io/kube-apiserver:v1.14.1
k8s.gcr.io/kube-controller-manager:v1.14.1
k8s.gcr.io/kube-scheduler:v1.14.1
k8s.gcr.io/kube-proxy:v1.14.1
k8s.gcr.io/pause:3.1
k8s.gcr.io/etcd:3.3.10
k8s.gcr.io/coredns:1.3.1
獲取鏡像
[[email protected] ~]# MY_REGISTRY=registry.cn-hangzhou.aliyuncs.com/openthings
[[email protected] ~]#docker pull ${MY_REGISTRY}/k8s-gcr-io-kube-apiserver:v1.14.1
[[email protected] ~]# docker pull ${MY_REGISTRY}/k8s-gcr-io-kube-controller-manager:v1.14.1
[[email protected] ~]# docker pull ${MY_REGISTRY}/k8s-gcr-io-kube-scheduler:v1.14.1
[[email protected] ~]# docker pull ${MY_REGISTRY}/k8s-gcr-io-kube-proxy:v1.14.1
[[email protected] ~]# docker pull ${MY_REGISTRY}/k8s-gcr-io-etcd:3.3.10
[[email protected] ~]# docker pull ${MY_REGISTRY}/k8s-gcr-io-pause:3.1
[[email protected] ~]#docker pull ${MY_REGISTRY}/k8s-gcr-io-coredns:1.3.1
添加Tag
[[email protected] ~]#docker tag ${MY_REGISTRY}/k8s-gcr-io-kube-apiserver:v1.14.1 k8s.gcr.io/kube-apiserver:v1.14.1
[[email protected] ~]#docker tag ${MY_REGISTRY}/k8s-gcr-io-kube-scheduler:v1.14.1 k8s.gcr.io/kube-scheduler:v1.14.1
[[email protected] ~]#docker tag ${MY_REGISTRY}/k8s-gcr-io-kube-controller-manager:v1.14.1 k8s.gcr.io/kube-controller-manager:v1.14.1
[[email protected] ~]#docker tag ${MY_REGISTRY}/k8s-gcr-io-kube-proxy:v1.14.1 k8s.gcr.io/kube-proxy:v1.14.1
[[email protected] ~]#docker tag ${MY_REGISTRY}/k8s-gcr-io-etcd:3.3.10 k8s.gcr.io/etcd:3.3.10
[[email protected] ~]# docker tag ${MY_REGISTRY}/k8s-gcr-io-pause:3.1 k8s.gcr.io/pause:3.1
[[email protected] ~]#docker tag ${MY_REGISTRY}/k8s-gcr-io-coredns:1.3.1 k8s.gcr.io/coredns:1.3.1    
初始化節點
 [[email protected] ~]# kubeadm init --kubernetes-version=v1.14.1 --apiserver-advertise-address=10.26.3.184 --pod-network-cidr=10.244.0.0/16

技術分享圖片
最好找個文件記錄下 防止後續使用到

 Your Kubernetes control-plane has initialized successfully!

 To start using your cluster, you need to run the following as a regular user:

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

 You should now deploy a pod network to the cluster.
 Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
   https://kubernetes.io/docs/concepts/cluster-administration/addons/

 Then you can join any number of worker nodes by running the following on each as root:

 kubeadm join 192.168.1.101:6443 --token lzc69y.4gapyv735z0stisl      --discovery-token-ca-cert-hash sha256:34ef3c3d00123569822285df5d084b3c062249e1a6726364821460d1a3833e1a 
 我這裏直接使用root用戶進行操作
 [[email protected] ~]# mkdir .kube
 [[email protected] ~]# cp -i /etc/kubernetes/admin.conf .kube/config     
     查看k8s的nodes
 [[email protected] ~]# kubectl  get nodes
      NAME         STATUS     ROLES    AGE   VERSION
      k8s-master   NotReady   master   18m   v1.14.1      
創建flannel 參考:https://github.com/coreos/flannel
A)  [[email protected] ~]#kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

技術分享圖片
#B) 下載kube-flannel.yml部署flannel #參考:https://blog.51cto.com/nhylovezyh/2377982?source=drt
鏈接:https://pan.baidu.com/s/1qqNuWOEeNu_k3HDacNJqcw
提取碼:b0dh
[[email protected] ~]# kubectl apply -f kube-flannel.yml

查看pod創建
[[email protected] ~]# kubectl  get pods -n kube-system

技術分享圖片

一旦處理完畢 查看node就變成了 READY了

技術分享圖片

剩余的就是添加節點了

kubeadm安裝kubernetes環境初始化