天天看點

CentOS7中用kubeadm安裝Kubernetes

準備

每個節點均要執行以下步驟

關閉防火牆

如果各個主機啟用了防火牆,需要開放Kubernetes各個元件所需要的端口,可以檢視

Installing kubeadm

中的”Check required ports”一節。 這裡簡單起見在各節點禁用防火牆:

systemctl stop firewalld
systemctl disable firewalld           

禁用SELINUX

# 臨時禁用
setenforce 0

# 永久禁用 
vim /etc/selinux/config    # 或者修改/etc/sysconfig/selinux
SELINUX=disabled           

修改k8s.conf檔案

cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
sysctl --system           

關閉swap

# 臨時關閉
swapoff -a           

修改 /etc/fstab 檔案,注釋掉 SWAP 的自動挂載(永久關閉swap,重新開機後生效)

# 注釋掉以下字段
/dev/mapper/cl-swap     swap                    swap    defaults        0 0           

安裝Docker

解除安裝老版本的Docker

如果有沒有老版本Docker,則不需要這步

yum remove docker \
           docker-common \
           docker-selinux \
           docker-engine           

使用yum進行安裝

每個節點均要安裝,目前官網建議安裝17.03版本的docker,

官網連結
# step 1: 安裝必要的一些系統工具
sudo yum install -y yum-utils device-mapper-persistent-data lvm2
# Step 2: 添加軟體源資訊
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
# Step 3: 更新并安裝 Docker-CE
sudo yum makecache fast
sudo yum -y install docker-ce docker-ce-selinux
# 注意:
# 官方軟體源預設啟用了最新的軟體,您可以通過編輯軟體源的方式擷取各個版本的軟體包。例如官方并沒有将測試版本的軟體源置為可用,你可以通過以下方式開啟。同理可以開啟各種測試版本等。
# vim /etc/yum.repos.d/docker-ce.repo
#   将 [docker-ce-test] 下方的 enabled=0 修改為 enabled=1
#
# 安裝指定版本的Docker-CE:
# Step 3.1: 查找Docker-CE的版本:
# yum list docker-ce.x86_64 --showduplicates | sort -r
#   Loading mirror speeds from cached hostfile
#   Loaded plugins: branch, fastestmirror, langpacks
#   docker-ce.x86_64            17.03.1.ce-1.el7.centos            docker-ce-stable
#   docker-ce.x86_64            17.03.1.ce-1.el7.centos            @docker-ce-stable
#   docker-ce.x86_64            17.03.0.ce-1.el7.centos            docker-ce-stable
#   Available Packages
# Step 3.2 : 安裝指定版本的Docker-CE: (VERSION 例如上面的 17.03.0.ce.1-1.el7.centos)
sudo yum -y --setopt=obsoletes=0 install docker-ce-[VERSION] \
docker-ce-selinux-[VERSION]

# Step 4: 開啟Docker服務
sudo systemctl enable docker && systemctl start docker           

docker安裝問題小結

錯誤資訊

Error: Package: docker-ce-17.03.2.ce-1.el7.centos.x86_64 (docker-ce-stable)
           Requires: docker-ce-selinux >= 17.03.2.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.0.ce-1.el7.centos.noarch (docker-ce-stable)
               docker-ce-selinux = 17.03.0.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.1.ce-1.el7.centos.noarch (docker-ce-stable)
               docker-ce-selinux = 17.03.1.ce-1.el7.centos
           Available: docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch (docker-ce-stable)
               docker-ce-selinux = 17.03.2.ce-1.el7.centos
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest           

解決辦法

# 要先安裝docker-ce-selinux-17.03.2.ce,否則安裝docker-ce會報錯
# 注意docker-ce-selinux的版本 要與docker的版本一直
yum install -y https://download.docker.com/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm

# 或者
yum -y install https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm           

參考連結

https://blog.csdn.net/csdn_duomaomao/article/details/79019764

安裝校驗

docker version
Client:
 Version:      17.03.2-ce
 API version:  1.27
 Go version:   go1.7.5
 Git commit:   f5ec1e2
 Built:        Tue Jun 27 02:21:36 2017
 OS/Arch:      linux/amd64

Server:
 Version:      17.03.2-ce
 API version:  1.27 (minimum version 1.12)
 Go version:   go1.7.5
 Git commit:   f5ec1e2
 Built:        Tue Jun 27 02:21:36 2017
 OS/Arch:      linux/amd64
 Experimental: false           

參考文獻

https://yq.aliyun.com/articles/110806

安裝kubeadm,kubelet,kubectl

在各節點安裝kubeadm,kubelet,kubectl

修改yum安裝源

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF           

安裝軟體

yum install -y kubelet kubeadm kubectl
systemctl enable kubelet && systemctl start kubelet           

初始化Master節點

配置kubeadm init 初始化檔案

配置檔案

官網介紹連結

建立配置檔案kubeadm-init.yaml檔案

apiVersion: kubeadm.k8s.io/v1alpha2
kind: MasterConfiguration
kubernetesVersion: v1.11.1    # kubernetes的版本
api:
  advertiseAddress: 172.16.242.129 # Master的IP位址
networking:
  podSubnet: 192.168.0.0/16    # pod網絡的網段
imageRepository: registry.cn-hangzhou.aliyuncs.com/google_containers # image的倉庫源           

運作初始化指令

kubeadm init --config kubeadm-init.yaml

......
......
Your Kubernetes master has initialized successfully!

To start using your cluster, you need to run (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 addon options listed at:
  http://kubernetes.io/docs/admin/addons/

You can now join any number of machines by running the following on each node
as root:

  kubeadm join --token <token> <master-ip>:<master-port> --discovery-token-ca-cert-hash sha256:<hash>           

使kubectl正常工作

非root使用者

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

root使用者

export KUBECONFIG=/etc/kubernetes/admin.conf           

問題解決

官網解決問題連結

多次運作kubeadm init指令,需要reset

kubeadm reset           

cgroup driver報錯

error: failed to run Kubelet: failed to create kubelet:
  misconfiguration: kubelet cgroup driver: "systemd" is different from docker cgroup driver: "cgroupfs"           

需要修改 /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

sed -i "s/cgroup-driver=systemd/cgroup-driver=cgroupfs/g" /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
systemctl daemon-reload && systemctl restart kubelet           

kubelet不能啟動成功原因之一

systemctl status docker

中如果出現需要鏡像

k8s.gcr.io/pause:3.1

,運作以下指令修改下标簽

docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.1 k8s.gcr.io/pause:3.1           

安裝Pod Network

接下來安裝pod network add-on,以安裝Calico為例:

kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
kubectl apply -f https://docs.projectcalico.org/v3.1/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml           

使用

kubectl get pod --all-namespaces -o wide

確定所有的Pod都處于Running狀态。

kubectl get pod --all-namespaces -o wide           

master node參與工作負載

使用kubeadm初始化的叢集,出于安全考慮Pod不會被排程到Master Node上,也就是說Master Node不參與工作負載。

這裡搭建的是測試環境可以使用下面的指令使Master Node參與工作負載:

kubectl taint nodes node1 node-role.kubernetes.io/master-
node "node1" untainted           

向Kubernetes叢集添加Node

  • 添加指令
    kubeadm join --token <token> <master-ip>:<master-port> --discovery-token-ca-cert-hash sha256:<hash>           
  • 檢視token的值,在master節點運作以下指令
    # 如果沒有token,請使用指令kubeadm token create 建立
    kubeadm token list           
  • 檢視hash值,在master節點運作以下指令
    openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | \
       openssl dgst -sha256 -hex | sed 's/^.* //'           
  • node2加入叢集很是順利,下面在master節點上執行指令檢視叢集中的節點:
    kubectl get nodes           
  • 如何從叢集中移除Node

在master節點上執行:

kubectl drain node2 --delete-local-data --force --ignore-daemonsets
kubectl delete node <node name>           

在node2上執行:

kubeadm reset           

附錄

配置代理

  • 配置全局代理
cat <<EOF >  ~/.bashrc
export http_proxy=http://username:password@ip:port
export https_proxy=http://username:password@ip:port
export no_proxy=localhost,127.0.0.1,<your-server-ip>(本機ip位址)
EOF
source ~/.bashrc           
  • 配置docker代理,拉谷歌鏡像要用到
mkdir -p /etc/systemd/system/docker.service.d/
cat <<EOF > /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://username:password@ip:port" "HTTPS_PROXY=http://username:password@ip:port" "NO_PROXY=localhost,127.0.0.1,<your-server-ip>"
EOF
systemctl daemon-reload
systemctl restart docker           

參考

繼續閱讀