經過docker,咱們能夠在單個主機上快速部署各個應用,可是實際的生產環境裏,不會單單存在一臺主機,這就須要用到docker集羣管理工具了,本文將簡單介紹使用docker集羣管理工具kubernetes進行集羣部署。html
本次搭建使用了三臺主機,其環境信息以下:
| 節點功能 主機名 IP
| master | master |192.168.1.11 |
| slave1 | slave1 |192.168.1.12 |
| slave2 | slave2 |192.168.1.13 |node
在三臺主機的/etc/hosts文件中添加如下內容linux
vim /etc/hosts #添加如下信息 192.168.1.11 master 192.168.1.12 slave1 192.168.1.13 slave2
swapoff -a
再把/etc/fstab文件中帶有swap的行註釋。git
setenforce 0 vim /etc/sysconfig/selinux #修改SELINUX屬性 SELINUX=disabled
cat <<EOF > /etc/sysctl.d/k8s.conf > net.bridge.bridge-nf-call-ip6tables = 1 > net.bridge.bridge-nf-call-iptables = 1 > EOF sysctl --system
yum install -y ebtables socat
官方推薦安裝docker版本爲1.12github
#yum安裝docker yum install -y docker #設置docker開機啓動 systemctl enable docker #啓動docker systemctl start docker
驗證docker版本docker
docker --version #如下爲輸出的版本信息 Docker version 1.12.6, build 85d7426/1.12.6
經常使用yum源均沒有這幾個安裝包,須要添加專門的yum源vim
cat <<EOF > /etc/yum.repos.d/kubernetes.repo [kubernetes] name=Kubernetes baseurl= enabled=1 gpgcheck=0 EOF #官方文檔中的yum源爲google,國內沒法使用
yum install -y kubelet kubeadm kubectl
#設置開機啓動kubelet systemctl enable kubelet #啓動kubelet systemctl start kubelet
查詢kubelet的狀態api
systemctl status kubelet
初次安裝的狀況下kubelet應未啓動成功,咱們會按下面的步驟初始化集羣后會自動啓動的。安全
根據官方文檔進行初始化:網絡
kubeadm init --apiserver-advertise-address 192.168.1.11 --pod-network-cidr 10.244.0.0/16 #--apiserver-advertise-address 192.168.1.11爲master節點IP,部分文檔也指定爲0.0.0.0 #--pod-network-cidr 10.244.0.0/16爲pod網絡cidr
出現以下錯誤:
[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters. unable to get URL "https://storage.googleapis.com/kubernetes-release/release/stable-1.7.txt": Get https://storage.googleapis.com/kubernetes-release/release/stable-1.7.txt: net/http: TLS handshake timeout
須要指定kubernetes-version。
首先查詢版本
kubeadm versionkubeadm version: &version.Info{Major:"1", Minor:"7", GitVersion:"v1.7.5",…………
版本爲1.7.5,而後啓動參數中加入版本:
kubeadm init --apiserver-advertise-address 192.168.1.11 --pod-network-cidr 10.244.0.0/16 --kubernetes-version=v1.7.5
執行過程當中會卡在以下步驟:
[apiclient] Created API client, waiting for the control plane to become ready
由於kubenetes初始化啓動會依賴某些鏡像,而這些鏡像默認會到google下載,咱們須要手動下載下來這些鏡像後再進行初始化。
使用CTRL+C結束當前進程,而後到/etc/kubernetes/manifests/目錄下查看各個yaml文件,還有其餘須要的鏡像文件,彙總後以下:
gcr.io/google_containers/etcd-amd64:3.0.17 gcr.io/google_containers/kube-apiserver-amd64:v1.7.5 gcr.io/google_containers/kube-controller-manager-amd64:v1.7.5 gcr.io/google_containers/kube-scheduler-amd64:v1.7.5 gcr.io/google_containers/pause-amd64:3.0 gcr.io/google_containers/kube-proxy-amd64:v1.7.5 quay.io/coreos/flannel gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.4 gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.4 gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.4
因直接下載這些google鏡像,下載不下來,咱們經過下載dockerHUB/阿里雲上的鏡像,而後更改tag。
#etcd-amd64:3.0.17 docker pull sylzd/etcd-amd64-3.0.17 docker tag docker.io/sylzd/etcd-amd64-3.0.17:latest gcr.io/google_containers/etcd-amd64:3.0.17 #kube-apiserver-amd64:v1.7.5 docker pull registry.cn-hangzhou.aliyuncs.com/google-containers/kube-apiserver-amd64:v1.7.5 docker tag registry.cn-hangzhou.aliyuncs.com/google-containers/kube-apiserver-amd64:v1.7.5 gcr.io/google_containers/kube-apiserver-amd64:v1.7.5 #kube-controller-manager-amd64:v1.7.5 docker pull registry.cn-hangzhou.aliyuncs.com/google-containers/kube-controller-manager-amd64:v1.7.5 docker tag registry.cn-hangzhou.aliyuncs.com/google-containers/kube-controller-manager-amd64:v1.7.5 gcr.io/google_containers/kube-controller-manager-amd64:v1.7.5 #kube-scheduler-amd64:v1.7.5 docker pull registry.cn-hangzhou.aliyuncs.com/google-containers/kube-scheduler-amd64:v1.7.5 docker tag registry.cn-hangzhou.aliyuncs.com/google-containers/kube-scheduler-amd64:v1.7.5 gcr.io/google_containers/kube-scheduler-amd64:v1.7.5 #pause-amd64:3.0 docker pull visenzek8s/pause-amd64:3.0 docker tag visenzek8s/pause-amd64:3.0 gcr.io/google_containers/pause-amd64:3.0 #kube-proxy-amd64:v1.7.5 docker pull mirrorgooglecontainers/kube-proxy-amd64:v1.7.5 docker tag mirrorgooglecontainers/kube-proxy-amd64:v1.7.5 gcr.io/google_containers/kube-proxy-amd64:v1.7.5 #quay.io/coreos/flannel docker pull quay.io/coreos/flannel #k8s-dns-kube-dns-amd64:1.14.4 docker pull registry.cn-hangzhou.aliyuncs.com/google-containers/k8s-dns-kube-dns-amd64:1.14.4 docker tag registry.cn-hangzhou.aliyuncs.com/google-containers/k8s-dns-kube-dns-amd64:1.14.4 gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.4 #k8s-dns-sidecar-amd64:1.14.4 docker pull registry.cn-hangzhou.aliyuncs.com/google-containers/k8s-dns-sidecar-amd64:1.14.4 docker tag registry.cn-hangzhou.aliyuncs.com/google-containers/k8s-dns-sidecar-amd64:1.14.4 gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.4 #k8s-dns-dnsmasq-nanny-amd64:1.14.4 docker pull mirrorgooglecontainers/k8s-dns-dnsmasq-nanny-amd64:1.14.4 docker tag mirrorgooglecontainers/k8s-dns-dnsmasq-nanny-amd64:1.14.4 gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.4
master節點初始化成功,結果以下:
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 options listed at: http://kubernetes.io/docs/admin/addons/You can now join any number of machines by running the following on each nodeas root: kubeadm join --token 3f1db4.9f7ba7d52de40996 192.168.1.11:6443
須要記住kubeadm join --token這句,後面會用到
#對於非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
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
安裝完network以後,你能夠經過kubectl get pods --all-namespaces來查看kube-dns是否在running來判斷network是否安裝成功。
kubectl get pods --all-namespaces #運行正常的結果以下: NAMESPACE NAME READY STATUS RESTARTS AGEkube-system etcd-localhost.localdomain 1/1 Running 0 1hkube-system kube-apiserver-localhost.localdomain 1/1 Running 0 1hkube-system kube-controller-manager-localhost.localdomain 1/1 Running 3 1hkube-system kube-dns-2425271678-27g6v 3/3 Running 0 1hkube-system kube-flannel-ds-1mjq3 1/1 Running 1 1hkube-system kube-proxy-mtjwb 1/1 Running 0 1hkube-system kube-scheduler-localhost.localdomain 1/1 Running 0 1h
若是以上STATUS中存在不是Running的須要再進行解決。
因爲安全緣由,默認狀況下pod不會被schedule到master節點上,能夠經過下面命令解除這種限制:
kubectl taint nodes --all node-role.kubernetes.io/master-
slave節點須要如下鏡像:
gcr.io/google_containers/kube-proxy-amd64:v1.7.5 quay.io/coreos/flannel gcr.io/google_containers/pause-amd64:3.0
在msater節點上導出鏡像
docker save -o /opt/kube-pause.tar gcr.io/google_containers/pause-amd64:3.0 docker save -o /opt/kube-proxy.tar gcr.io/google_containers/kube-proxy-amd64:v1.7.5 docker save -o /opt/kube-flannel.tar quay.io/coreos/flannel
複製到slave主機/opt目錄下,再導入便可:
docker load -i /opt/kube-flannel.tardocker load -i /opt/kube-proxy.tardocker load -i /opt/kube-pause.tar
在兩個slave節點上執行:
kubeadm join --token 3f1db4.9f7ba7d52de40996 192.168.1.11:6443
執行成功標誌:
Node join complete:* Certificate signing request sent to master and response received.* Kubelet informed of new secure connection details.Run 'kubectl get nodes' on the master to see this machine join.
在mster節點上執行kubectl get nodes查看是否成功:
kubectl get nodesNAME STATUS AGE VERSION master Ready 56m v1.7.5 slave1 Ready 1m v1.7.5 slave2 Ready 1m v1.7.5
能夠看到,kubernetes集羣已經部署成功,可使用了。
Installing kubeadm,https://kubernetes.io/docs/setup/independent/install-kubeadm/
使用kubeadm在Red Hat 7/CentOS 7快速部署Kubernetes 1.7集羣,http://dockone.io/article/2514
CentOS7.3利用kubeadm安裝kubernetes1.7.3完整版(官方文檔填坑篇),https://www.cnblogs.com/liangDream/p/7358847.html
How to execute 「kubeadm init」 v1.6.4 behind firewall,https://stackoverflow.com/questions/44432328/how-to-execute-kubeadm-init-v1-6-4-behind-firewall
使用 kubeadm 建立 kubernetes 1.9 集羣,https://www.kubernetes.org.cn/3357.html