做者:曹守明
kubeadm是官方社區推出的一個用於快速部署kubernetes集羣的工具。node
這個工具能經過兩條指令完成一個kubernetes集羣的部署:linux
環境介紹:nginx
192.168.10.100 k8s-master 192.168.10.101 k8s-node01 192.168.10.102 k8s-node02
# 建立一個 Master 節點 $ kubeadm init # 將一個 Node 節點加入到當前集羣中 $ kubeadm join <Master節點的IP和端口 >
在開始以前,部署Kubernetes集羣機器須要知足如下幾個條件:git
Kubernetes 架構圖github
全部節點都進行配置:docker
關閉防火牆: $ systemctl stop firewalld $ systemctl disable firewalld 關閉selinux: $ sed -i 's/enforcing/disabled/' /etc/selinux/config $ setenforce 0 關閉swap: $ swapoff -a $ 臨時 $ vim /etc/fstab $ 永久 添加主機名與IP對應關係(記得設置主機名): $ cat /etc/hosts 192.168.10.100 k8s-master 192.168.10.101 k8s-node01 192.168.10.102 k8s-node02 將橋接的IPv4流量傳遞到iptables的鏈: $ cat > /etc/sysctl.d/k8s.conf << EOF net.bridge.bridge-nf-call-ip6tables = 1 net.bridge.bridge-nf-call-iptables = 1 EOF $ sysctl --system
Kubernetes默認CRI(容器運行時)爲Docker,所以先安裝Docker。shell
$ wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo $ yum -y install docker-ce-18.06.1.ce-3.el7 $ systemctl enable docker && systemctl start docker $ docker --version Docker version 18.06.1-ce, build e68fc7a
$ cat > /etc/yum.repos.d/kubernetes.repo << EOF [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
因爲版本更新頻繁,這裏指定版本號部署:vim
$ yum install -y kubelet-1.14.0 kubeadm-1.14.0 kubectl-1.14.0 $ systemctl enable kubelet
在192.168.10.100(Master)執行。centos
kubeadm init \ --apiserver-advertise-address=192.168.10.100 \ --token-ttl 0 \ --image-repository registry.aliyuncs.com/google_containers \ --kubernetes-version v1.14.0 \ --service-cidr=10.1.0.0/16 \ --pod-network-cidr=10.244.0.0/16
因爲默認拉取鏡像地址k8s.gcr.io國內沒法訪問,這裏指定阿里雲鏡像倉庫地址。api
使用kubectl工具:
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config $ kubectl get nodes
$ kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/a70459be0084506e4ec919aa1c114638878db11b/Documentation/kube-flannel.yml
確保可以訪問到quay.io這個registery。
若是下載失敗,能夠改爲這個鏡像地址:lizhenliang/flannel:v0.11.0-amd64
在全部節點執行,爲了可以快速的拉取鏡像!!!
docker pull lizhenliang/flannel:v0.11.0-amd64
向集羣添加新節點,執行在kubeadm init輸出的kubeadm join命令:
$ kubeadm join 192.168.31.63:6443 --token l79g5t.6ov4jkddwqki1dxe --discovery-token-ca-cert-hash sha256:4f07f9068c543130461c9db368d62b4aabc22105451057f887defa35f47fa076
在Kubernetes集羣中建立一個pod,驗證是否正常運行:
在192.168.10.100中執行
$ kubectl create deployment nginx --image=nginx $ kubectl expose deployment nginx --port=80 --type=NodePort $ kubectl get pod,svc
訪問地址:http://NodeIP:Port
在master節點進行執行 kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v1.10.1/src/deploy/recommended/kubernetes-dashboard.yaml
默認鏡像國內沒法訪問,修改鏡像地址爲: lizhenliang/kubernetes-dashboard-amd64:v1.10.1
默認Dashboard只能集羣內部訪問,修改Service爲NodePort類型,暴露到外部:
# ------------------- Dashboard Deployment ------------------- # spec: replicas: 1 revisionHistoryLimit: 10 selector: matchLabels: k8s-app: kubernetes-dashboard template: metadata: labels: k8s-app: kubernetes-dashboard spec: containers: - name: kubernetes-dashboard # image: k8s.gcr.io/kubernetes-dashboard-amd64:v1.10.1 image: lizhenliang/kubernetes-dashboard-amd64:v1.10.1 # ------------------- Dashboard Service ------------------- # kind: Service apiVersion: v1 metadata: labels: k8s-app: kubernetes-dashboard name: kubernetes-dashboard namespace: kube-system spec: type: NodePort ports: - port: 443 targetPort: 8443 nodePort: 30001 selector: k8s-app: kubernetes-dashboard
kubectl apply -f kubernetes-dashboard.yaml
在全部節點進行執行!!!
docker pull lizhenliang/kubernetes-dashboard-amd64:v1.10.1
訪問地址:https://NodeIP:30001
建立service account並綁定默認cluster-admin管理員集羣角色:
$ kubectl create serviceaccount dashboard-admin -n kube-system $ kubectl create clusterrolebinding dashboard-admin --clusterrole=cluster-admin --serviceaccount=kube-system:dashboard-admin $ kubectl describe secrets -n kube-system $(kubectl -n kube-system get secret | awk '/dashboard-admin/{print $1}')
踩坑記錄: