34 【kubernetes】安裝手冊
全文參考了兩篇中文文檔:
1,https://www.cnblogs.com/RainingNight/p/using-kubeadm-to-create-a-cluster.html
2,http://running.iteye.com/blog/2322634
註意:
運行命令是一定要區分是在master節點還是在pods節點上運行的,有些命令只能在master節點執行,有些命令只能在pods節點執行。這個要區分。
運行命令一定要區分清用戶是誰,是root還是普通用戶。
大步驟:
1,在master節點和pods節點上安裝軟件;
2,在master節點上啟動kubernetes軟件,並初始化master節點;
3,在從節點上啟動kubernetes軟件,並連接到master節點進行註冊;
4,通過master啟動一個pods,執行一個應用程序(nginx為例);
5,通過master啟動一個服務,將剛才的應用程序關聯到這個服務項上;
6,測試master的scale能力,根據服務名瞬間啟動一個相同的pods;
1,在master節點和pods節點上安裝軟件
sudo apt-get update && sudo apt-get install -y apt-transport-https curl -s http://packages.faasx.com/google/apt/doc/apt-key.gpg | sudo apt-key add -sudo cat <<EOF >/etc/apt/sources.list.d/kubernetes.list deb http://mirrors.ustc.edu.cn/kubernetes/apt/ kubernetes-xenial main EOF sudo apt-get update sudo apt-get install -y kubelet kubeadm kubectl
2,在master節點上啟動kubernetes軟件,並初始化master節點;
2.1,在master節點初始化一個cluster
由於網絡原因,我們需要提前拉取k8s初始化需要用到的Images,並添加對應的k8s.gcr.io
## 拉取鏡像 docker pull reg.qiniu.com/k8s/kube-apiserver-amd64:v1.10.2 docker pull reg.qiniu.com/k8s/kube-controller-manager-amd64:v1.10.2 docker pull reg.qiniu.com/k8s/kube-scheduler-amd64:v1.10.2 docker pull reg.qiniu.com/k8s/kube-proxy-amd64:v1.10.2 docker pull reg.qiniu.com/k8s/etcd-amd64:3.1.12 docker pull reg.qiniu.com/k8s/pause-amd64:3.1 ## 添加Tag docker tag reg.qiniu.com/k8s/kube-apiserver-amd64:v1.10.2 k8s.gcr.io/kube-apiserver-amd64:v1.10.2 docker tag reg.qiniu.com/k8s/kube-scheduler-amd64:v1.10.2 k8s.gcr.io/kube-scheduler-amd64:v1.10.2 docker tag reg.qiniu.com/k8s/kube-controller-manager-amd64:v1.10.2 k8s.gcr.io/kube-controller-manager-amd64:v1.10.2 docker tag reg.qiniu.com/k8s/kube-proxy-amd64:v1.10.2 k8s.gcr.io/kube-proxy-amd64:v1.10.2 docker tag reg.qiniu.com/k8s/etcd-amd64:3.1.12 k8s.gcr.io/etcd-amd64:3.1.12 docker tag reg.qiniu.com/k8s/pause-amd64:3.1 k8s.gcr.io/pause-amd64:3.1 ## 在Kubernetes 1.10 中,增加了CoreDNS,如果使用CoreDNS(默認關閉),則不需要下面三個鏡像。 docker pull reg.qiniu.com/k8s/k8s-dns-sidecar-amd64:1.14.10 docker pull reg.qiniu.com/k8s/k8s-dns-kube-dns-amd64:1.14.10 docker pull reg.qiniu.com/k8s/k8s-dns-dnsmasq-nanny-amd64:1.14.10 docker tag reg.qiniu.com/k8s/k8s-dns-sidecar-amd64:1.14.10 k8s.gcr.io/k8s-dns-sidecar-amd64:1.14.10 docker tag reg.qiniu.com/k8s/k8s-dns-kube-dns-amd64:1.14.10 k8s.gcr.io/k8s-dns-kube-dns-amd64:1.14.10 docker tag reg.qiniu.com/k8s/k8s-dns-dnsmasq-nanny-amd64:1.14.10 k8s.gcr.io/k8s-dns-dnsmasq-nanny-amd64:1.14.10
2.2,初始化cluster
<master-node>: sudo kubeadm init --pod-network-cidr=192.168.0.0/16
註意這裏的輸出最好能記錄在text中,因為後面會用到
2.3,將kubernetes的配置項放到普通用戶目錄下
mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config
這樣kubectl會自動尋址到config文件,不用依賴一個嚴格的root可讀的config
2.4,安裝各種軟件(https://docs.projectcalico.org/v3.3/getting-started/kubernetes/)
2.4.1 安裝etcd kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/etcd.yaml 2.4.2 安裝rbac kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/rbac.yaml 2.4.3 安裝calico kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/calico.yaml 2.4.4 確認安裝成功 watch kubectl get pods --all-namespaces 結束後Ctrl + C 2.4.5 再次確認 kubectl get nodes -o wide
2.5 在從節點啟動kubernetes軟件
在從節點上執行:
sudo kubeadm join 192.168.0.8:6443 --token vtyk9m.g4afak37myq3rsdi --discovery-token-ca-cert-hash sha256:19246ce11ba3fc633fe0b21f2f8aaaebd7df9103ae47138dc0dd615f61a32d99
這裏的命令要和2.2的輸出保持一致(幾個參數可能不一致,按照自己的輸出自行修改即可)
如果2.2的輸出已經沒法找到,可以用以下命令再次得到join語句。
在主節點上執行: kubeadm token create --print-join-command
然後再在從節點上執行以上得到的join語句
2.6,確認主從節點已經完成啟動,需要等幾分鐘:
主節點上執行
kubectl get nodes
3,創建可用的pod
3.1,創建一個nginx的鏡像當做pod內的應用程序
主節點上執行:
kubectl run my-nginx --image=nginx --replicas=1 --port=80
3.2,確認pod已經生成
主節點上執行:
kubectl get pods
3.3,將該pods發布到kubernetes上,作為一個服務
luwenwei@localhost:~/download/k8s$ kubectl expose deployment my-nginx --port=8080 --target-port=80 service/my-nginx exposed
3.4,查看服務是否已經生成
luwenwei@localhost:~$ kubectl get services NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 108m my-nginx ClusterIP 10.98.38.80 <none> 8080/TCP 91m
3.5,訪問該服務
luwenwei@localhost:~$ kubectl describe service/my-nginx Name: my-nginx Namespace: default Labels: run=my-nginx Annotations: <none> Selector: run=my-nginx Type: ClusterIP IP: 10.98.38.80 Port: <unset> 8080/TCP TargetPort: 80/TCP Endpoints: 192.168.244.65:80 Session Affinity: None Events: <none>
獲取到IP和port
從節點上執行:
luwenwei@ubuntu:~$ curl 10.98.38.80:8080 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
4,擴容該service的pods數
4.1,查看擴容前的pods數
luwenwei@localhost:~$ kubectl get pods NAME READY STATUS RESTARTS AGE my-nginx-756f645cd7-mg45n 1/1 Running 0 98m
4.2,執行擴容
luwenwei@localhost:~$ kubectl scale deployment my-nginx --replicas=2 deployment.extensions/my-nginx scaled
4.3,查看擴容後的pods信息
luwenwei@localhost:~$ kubectl get pods NAME READY STATUS RESTARTS AGE my-nginx-756f645cd7-dww7g 0/1 ContainerCreating 0 6s my-nginx-756f645cd7-mg45n 1/1 Running 0 98m
4.4,查看service的信息
luwenwei@localhost:~$ kubectl describe service/my-nginx Name: my-nginx Namespace: default Labels: run=my-nginx Annotations: <none> Selector: run=my-nginx Type: ClusterIP IP: 10.98.38.80 Port: <unset> 8080/TCP TargetPort: 80/TCP Endpoints: 192.168.244.65:80,192.168.244.66:80 Session Affinity: None Events: <none>
4.4,反向驗證pods和service的對應
luwenwei@localhost:~$ kubectl describe pods | grep IP IP: 192.168.244.66 IP: 192.168.244.65
4.5,訪問新的service
luwenwei@ubuntu:~$ curl 10.98.38.80:8080 <!DOCTYPE html> <html> <head> <title>Welcome to nginx!</title> <style> body { width: 35em; margin: 0 auto; font-family: Tahoma, Verdana, Arial, sans-serif; } </style> </head> <body> <h1>Welcome to nginx!</h1> <p>If you see this page, the nginx web server is successfully installed and working. Further configuration is required.</p> <p>For online documentation and support please refer to <a href="http://nginx.org/">nginx.org</a>.<br/> Commercial support is available at <a href="http://nginx.com/">nginx.com</a>.</p> <p><em>Thank you for using nginx.</em></p> </body> </html>
5,使用完畢後清理cluster信息
5.1,清除node數據
主節點上執行
kubectl drain <node name> --delete-local-data --force --ignore-daemonsets
5.2,刪除node節點
主節點上執行
kubectl delete node <node name>
5.3,收回cluster信息
在要移除的節點上,執行: sudo kubeadm reset
輸出結果:
luwenwei@ubuntu:~$ sudo kubeadm reset [sudo] password for luwenwei: [reset] WARNING: changes made to this host by ‘kubeadm init‘ or ‘kubeadm join‘ will be reverted. [reset] are you sure you want to proceed? [y/N]: y [preflight] running pre-flight checks [reset] stopping the kubelet service [reset] unmounting mounted directories in "/var/lib/kubelet" [reset] no etcd manifest found in "/etc/kubernetes/manifests/etcd.yaml". Assuming external etcd [reset] please manually reset etcd to prevent further issues [reset] deleting contents of stateful directories: [/var/lib/kubelet /etc/cni/net.d /var/lib/dockershim /var/run/kubernetes] [reset] deleting contents of config directories: [/etc/kubernetes/manifests /etc/kubernetes/pki] [reset] deleting files: [/etc/kubernetes/admin.conf /etc/kubernetes/kubelet.conf /etc/kubernetes/bootstrap-kubelet.conf /etc/kubernetes/controller-manager.conf /etc/kubernetes/scheduler.conf
34 【kubernetes】安裝手冊