k8s的部署

k8s集羣解決的問題

Docker之間跨節點的通訊

動態管理集羣負載,使集羣工作在期望的狀態

集羣之間資源的調度

集羣的運行方式

k8s的節點類型

master節點負責整個集羣的控制和管理

node節點是負載節點,運行pod。

master節點所用到的組件

kube-apiserver提供整個集羣資源的操作入口(增刪改查),也是集羣的控制入口,提供http rest接口。

etcd保存整個集羣的狀態,也即是資源信息。KEY/VALUE模式的存儲系統

kube-schedule負載pod(資源)的調度,根據設置策略調度pod到指定的node上運行。

kube-controller-manager維護集羣,所有資源的自動化控制中心。當整個集羣的狀態與期望不符合的時候,組件會努力讓集羣恢復期望狀態,比如:當一個pod死亡,組件新建一個pod恢復對應replicas set期望的狀態。

node節點所用到的組件

kubelet負責pod的生命週期管理,同時與master密切協作,實現集羣管理的基本功能

kube-proxy實現service的內部服務發現與負載均衡機制的重要組件,主要通過設置iptables規則實現

docker engin負責docker的生命週期管理

命名空間

k8s的namespace是在將系統內部的對象“分配”到不同的空間下,形成邏輯上的分組,便於不同的分組在共享使用整個集羣的資源同時還能被分別管理。

POD RC Service Deployment等資源都會運行在特定的namespace中,默認的namerspace是default。

resource quotas

空白

lable

通過lable進行弱關聯,靈活的分類或者選擇不同服務或者業務,可以採用鬆耦合方式進行服務部署。label是一對key/value(想到etcd也是一對KEY/VALUE模式的存儲系統

),對k8s沒有什麼意義,但是對於用戶意義很大

replication controller(RC)

RC是K8S另一種核心概念,應用託管到K8s後,他會確保任何時間K8S都有指定數量的Pod

Deployment

deployment同樣是爲了確保pod的數量和健康,90%與RC一樣。

service

kubernetes中service是一種抽象的概念,它定義了一個pod的邏輯集合以及訪問他們的策略,service同pod的關聯是居於label完成。service的目標是提供一種橋樑,它會爲訪問者提供一個固定的ip,用於在訪問時重定向到相應的後端,使得非K8s原生應用程序,在無需爲kubemces編寫特定代碼的前提下,輕鬆訪問。

volume

容器內數據隨着容器的生命週期而存在,如果需要持久化的保存數據,則需要掛載數據卷

emptyDir

hostPath(local)

NFS

glusterFS

cephFS

網絡通信場景

容器到容器之間的直接通信

POD到POD之間的通信(同一節點/不同節點)

POD到service之間的通信

集羣外部與內部組件之間的通信

安裝

安裝準備

•操作系統:CentOS7.3

[root@centos7-base-ok]# cat /etc/redhat-release

CentOS Linux release 7.3.1611 (Core)

•安裝機器:k8s-1爲master節點,k8s-2、k8s-3爲slave節點

[root@centos7-base-ok]# cat /etc/hosts

127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4

::1 localhost localhost.localdomain localhost6 localhost6.localdomain6

k8s-1 192.168.80.28

k8s-2 192.168.80.35

k8s-3 192.168.80.14

安裝步驟

安裝docker 1.12(所有節點)

注意:現在docker已經更新到CE版本,但是kubernetes官方文檔說在1.12上測試通過,最近版本的兼容性未測試,爲了避免後面出現大坑,我們還是乖乖安裝1.12版本的docker。

1.新建docker.repo文件,將文件移動到/etc/yum.repos.d/目錄下

[root@centos7-base-ok]# cat /etc/yum.repos.d/docker.repo

[dockerrepo]

name=Docker Repository

baseurl=https://yum.dockerproject.org/repo/main/centos/7/

enabled=1

gpgcheck=1

gpgkey=https://yum.dockerproject.org/gpg

2.運行yum命令,找到需要安裝的docker版本

10:21 [root@centos7-base-ok]# yum list|grep docker | sort -r

python2-avocado-plugins-runner-docker.noarch

python-dockerpty.noarch 0.4.1-6.el7 epel

python-dockerfile-parse.noarch 0.0.5-1.el7 epel

python-docker-scripts.noarch 0.4.4-1.el7 epel

python-docker-pycreds.noarch 1.10.6-1.el7 extras

python-docker-py.noarch 1.10.6-1.el7 extras

kdocker.x86_64 4.9-1.el7 epel

golang-github-fsouza-go-dockerclient-devel.x86_64

docker.x86_64 2:1.12.6-32.git88a4867.el7.centos

docker-v1.10-migrator.x86_64 2:1.12.6-32.git88a4867.el7.centos

docker-unit-test.x86_64 2:1.12.6-32.git88a4867.el7.centos

docker-registry.x86_64 0.9.1-7.el7 extras

docker-registry.noarch 0.6.8-8.el7 extras

docker-python.x86_64 1.4.0-115.el7 extras

docker-novolume-plugin.x86_64 2:1.12.6-32.git88a4867.el7.centos

docker-lvm-plugin.x86_64 2:1.12.6-32.git88a4867.el7.centos

docker-logrotate.x86_64 2:1.12.6-32.git88a4867.el7.centos

docker-latest.x86_64 1.13.1-13.gitb303bf6.el7.centos

docker-latest-v1.10-migrator.x86_64 1.13.1-13.gitb303bf6.el7.centos

docker-latest-logrotate.x86_64 1.13.1-13.gitb303bf6.el7.centos

docker-forward-journald.x86_64 1.10.3-44.el7.centos extras

docker-engine.x86_64 17.05.0.ce-1.el7.centos dockerrepo

docker-engine.x86_64 1.12.6-1.el7.centos @dockerrepo

docker-engine-selinux.noarch 17.05.0.ce-1.el7.centos @dockerrepo

docker-engine-debuginfo.x86_64 17.05.0.ce-1.el7.centos dockerrepo

docker-distribution.x86_64 2.6.1-1.el7 extras

docker-devel.x86_64 1.3.2-4.el7.centos extras

docker-compose.noarch 1.9.0-5.el7 epel

docker-common.x86_64 2:1.12.6-32.git88a4867.el7.centos

docker-client.x86_64 2:1.12.6-32.git88a4867.el7.centos

docker-client-latest.x86_64 1.13.1-13.gitb303bf6.el7.centos

cockpit-docker.x86_64 141-3.el7.centos extras

3.找到對應版本後,執行yum install -y 包名+版本號,安裝1.12版本的docker-engine

[root@centos7-base-ok]# yum install -y docker-engine.x86_64-1.12.6-1.el7.centos

4.執行docker version命令,驗證docker安裝版本,執行docker run命令,驗證docker是否安裝成功

[root@centos7-base-ok]# docker version

Client:

Version: 1.12.6

API version: 1.24

Go version: go1.6.4

Git commit: 78d1802

Built: Tue Jan 10 20:20:01 2017

OS/Arch: linux/amd64

Server:

Version: 1.12.6

API version: 1.24

Go version: go1.6.4

Git commit: 78d1802

Built: Tue Jan 10 20:20:01 2017

OS/Arch: linux/amd64

5.設置開機啓動,啓動容器,docker安裝完成

[root@centos7-base-ok]# systemctl enbale docker && systemctl start docker

安裝kubectl、kubelet、kubeadm(根據需求在不同節點安裝)

注意:此步驟是填坑的開始,因爲官方文檔的yum源在國內無法使用,安裝完成後注意觀察你的/var/log/message日誌,會瘋狂報錯,彆着急,跟着我一步一步來填坑。

1.新建kubernetes.repo文件,將文件移動到/etc/yum.repos.d/目錄下(所有節點)

[root@centos7-base-ok]# cat /etc/yum.repos.d/kubernetes.repo

[kubernetes]

name=Kubernetes

baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64

enabled=1

gpgcheck=0

2.通過yum安裝kubectl、kubelet、kubeadm(所有節點)

[root@centos7-base-ok]# cat /etc/yum.repos.d/kubernetes.repo

[kubernetes]

name=Kubernetes

baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64

enabled=1

gpgcheck=0

3.修改kubelet配置,啓動kubelet(所有節點)

注意:時刻查看/var/log/message的日誌輸出,會看到kubelet一直啓動失敗。

編輯10-kubeadm.conf的文件,修改cgroup-driver配置:

[root@centos7-base-ok]# cat /etc/systemd/system/kubelet.service.d/10-kubeadm.conf

[Service]

Environment=”KUBELET_KUBECONFIG_ARGS=–kubeconfig=/etc/kubernetes/kubelet.conf –require-kubeconfig=true”

Environment=”KUBELET_SYSTEM_PODS_ARGS=–pod-manifest-path=/etc/kubernetes/manifests –allow-privileged=true”

Environment=”KUBELET_NETWORK_ARGS=–network-plugin=cni –cni-conf-dir=/etc/cni/net.d –cni-bin-dir=/opt/cni/bin”

Environment=”KUBELET_DNS_ARGS=–cluster-dns=10.96.0.10 –cluster-domain=cluster.local”

Environment=”KUBELET_AUTHZ_ARGS=–authorization-mode=Webhook –client-ca-file=/etc/kubernetes/pki/ca.crt”

Environment=”KUBELET_CADVISOR_ARGS=–cadvisor-port=0”

Environment=”KUBELET_CGROUP_ARGS=–cgroup-driver=cgroupfs”

ExecStart=

ExecStart=/usr/bin/kubelet KUBELETKUBECONFIGARGSKUBELETKUBECONFIGARGSKUBELET_SYSTEM_PODS_ARGS KUBELETNETWORKARGSKUBELETNETWORKARGSKUBELET_DNS_ARGS KUBELETAUTHZARGSKUBELETAUTHZARGSKUBELET_CADVISOR_ARGS KUBELETCGROUPARGSKUBELETCGROUPARGSKUBELET_EXTRA_ARGS

將“–cgroup-driver=systems”修改成爲“–cgroup-driver=cgroupfs”,重新啓動kubelet。

[root@centos7-base-ok]# systemctl restart kubelet

4.下載安裝k8s依賴鏡像

注意:此步驟非常關鍵,kubenetes初始化啓動會依賴這些鏡像,天朝的網絡肯定是拉不下來google的鏡像的,一般人過了上一關,這一關未必過的去,一定要提前把鏡像下載到本地,kubeadm安裝纔會繼續,下面我會列出來master節點和node依賴的鏡像列表。(備註:考慮到隨着kubernetes版本一直更新,鏡像也可能會有變化,大家可以先執行 kubeadm init 生成配置文件,日誌輸出到 [apiclient] Created API client, waiting for the control plane to become ready 這一行就會卡住不動了,你可以直接執行 ctrl + c 中止命令執行,然後查看 ls -ltr /etc/kubernetes/manifests/

yaml文件列表,每個文件都會寫着鏡像的地址和版本)

在這裏我提一個可以解決下載google鏡像的方法,就是買一臺可以下載的機器,安裝代理軟件,在需要下載google鏡像的機器的docker設置 HTTP_PROXY 配置項,配置好自己的服務代理即可(也可以直接買可以訪問到google的服務器安裝).

master節點:

REPOSITORY TAG IMAGE ID CREATED SIZE

quay.io/calico/kube-policy-controller v0.7.0 fe3174230993 3 days ago 21.94 MB

kubernetesdashboarddev/kubernetes-dashboard-amd64 head e2cadb73b2df 5 days ago 136.5 MB

quay.io/calico/node v2.4.1 7643422fdf0f 6 days ago 277.4 MB

gcr.io/google_containers/kube-controller-manager-amd64 v1.7.3 d014f402b272 11 days ago 138 MB

gcr.io/google_containers/kube-apiserver-amd64 v1.7.3 a1cc3a3d8d0d 11 days ago 186.1 MB

gcr.io/google_containers/kube-scheduler-amd64 v1.7.3 51967bf607d3 11 days ago 77.2 MB

gcr.io/google_containers/kube-proxy-amd64 v1.7.3 54d2a8698e3c 11 days ago 114.7 MB

quay.io/calico/cni v1.10.0 88ca805c8ddd 13 days ago 70.25 MB

gcr.io/google_containers/kubernetes-dashboard-amd64 v1.6.3 691a82db1ecd 2 weeks ago 139 MB

quay.io/coreos/etcd v3.1.10 47bb9dd99916 4 weeks ago 34.56 MB

gcr.io/google_containers/k8s-dns-sidecar-amd64 1.14.4 38bac66034a6 7 weeks ago 41.81 MB

gcr.io/google_containers/k8s-dns-kube-dns-amd64 1.14.4 a8e00546bcf3 7 weeks ago 49.38 MB

gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64 1.14.4 f7f45b9cb733 7 weeks ago 41.41 MB

gcr.io/google_containers/etcd-amd64 3.0.17 243830dae7dd 5 months ago 168.9 MB

gcr.io/google_containers/pause-amd64 3.0 99e59f495ffa 15 months ago 746.9 kB

node節點:

[root@centos7-base-ok]# docker images

REPOSITORY TAG IMAGE ID CREATED SIZE

kubernetesdashboarddev/kubernetes-dashboard-amd64 head e2cadb73b2df 5 days ago 137MB

quay.io/calico/node v2.4.1 7643422fdf0f 6 days ago 277MB

gcr.io/google_containers/kube-proxy-amd64 v1.7.3 54d2a8698e3c 11 days ago 115MB

quay.io/calico/cni v1.10.0 88ca805c8ddd 13 days ago 70.3MB

gcr.io/google_containers/kubernetes-dashboard-amd64 v1.6.3 691a82db1ecd 2 weeks ago 139MB

nginx latest b8efb18f159b 2 weeks ago 107MB

hello-world latest 1815c82652c0 2 months ago 1.84kB

gcr.io/google_containers/pause-amd64 3.0 99e59f495ffa 15 months ago 747kB

5.利用kubeadm初始化服務(master節點)

注意:如果你在上一步執行過 kubeadm init 命令,沒有關係,此步執行只需要執行時加上 –skip-preflight-checks 這個配置項即可。

注意:執行 kubeadm init 的 –pod-network-cidr 參數和選擇的網絡組件有關係,詳細可以看官方文檔說明,本文選用的網絡組件爲 Calico

[root@centos7-base-ok]# kubeadm init –pod-network-cidr=192.168.0.0/16 –apiserver-advertise-address=0.0.0.0 –apiserver-cert-extra-sans=192.168.80.28,192.168.80.14,192.168.80.35,127.0.0.1,k8s-1,k8s-2,k8s-3,192.168.0.1 –skip-preflight-checks

參數說明:

參數名稱

必選

參數說明

pod-network-cidr Yes For certain networking solutions the Kubernetes master can also play a role in allocating network ranges (CIDRs) to each node. This includes many cloud providers and flannel. You can specify a subnet range that will be broken down and handed out to each node with the –pod-network-cidr flag. This should be a minimum of a /16 so controller-manager is able to assign /24 subnets to each node in the cluster. If you are using flannel with this manifest you should use –pod-network-cidr=10.244.0.0/16. Most CNI based networking solutions do not require this flag.

apiserver-advertise-address Yes This is the address the API Server will advertise to other members of the cluster. This is also the address used to construct the suggested kubeadm join line at the end of the init process. If not set (or set to 0.0.0.0) then IP for the default interface will be used.

apiserver-cert-extra-sans Yes Additional hostnames or IP addresses that should be added to the Subject Alternate Name section for the certificate that the API Server will use. If you expose the API Server through a load balancer and public DNS you could specify this with.

其它的 kubeadm 參數設置請參照 官方文檔

6.做一枚安靜的美男子,等待安裝成功,安裝成功後你會看到日誌如下(master節點):

注意:記錄這段日誌,後面添加node節點要用到。

[apiclient] All control plane components are healthy after 22.003243 seconds

[token] Using token: 33729e.977f7b5d0a9b5f3e

[apiconfig] Created RBAC rules

[addons] Applied essential addon: kube-proxy

[addons] Applied essential addon: kube-dns

Your Kubernetes master has initialized successfully!

To start using your cluster, you need to run (as a regular user):

mkdir -p HOME/.kubesudocp−i/etc/kubernetes/admin.confHOME/.kubesudocp−i/etc/kubernetes/admin.confHOME/.kube/config

sudo chown (id−u):(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 node

as root:

kubeadm join –token xxxxxxx 192.168.80.28:6443

7.創建kube的目錄,添加kubectl配置(master節點)

mkdir -p HOME/.kubesudocp−i/etc/kubernetes/admin.confHOME/.kubesudocp−i/etc/kubernetes/admin.confHOME/.kube/config

sudo chown (id−u):(id−u):(id -g) $HOME/.kube/config

8.用 kubectl 添加網絡組件Calico(master節點)

kubectl apply -f http://docs.projectcalico.org/v2.4/getting-started/kubernetes/installation/hosted/kubeadm/1.6/calico.yaml

注意:此處坑爲該文件未必下載的到,建議還是提前下載到本地,然後執行 kubectl apply -f <本地路徑>

9.確認安裝是否成功(master節點)

9.1 打開你的/var/log/messages,查看是否有報錯,理論上,執行完上一步過去5分鐘,日誌應該不會有任何錯誤出現,如果持續報錯,並且過了10分鐘錯誤依然沒有消失,檢查之前的步驟是否有問題

9.2 運行 kubectl get pods –all-namespaces 查看結果,如果STATUS都爲Running,恭喜你,你的master已經安裝成功了。

注意:你的結果顯示的條數未必和我完全一樣,因爲我這裏有node節點的相關信息,而你還沒有添加node節點。

[root@centos7-base-ok]# kubectl get pods –all-namespaces

NAMESPACE NAME READY STATUS RESTARTS AGE

default nginx-app-1666850838-4z2tb 1/1 Running 0 3d

kube-system calico-etcd-0ssdd 1/1 Running 0 3d

kube-system calico-node-1zfxd 2/2 Running 1 3d

kube-system calico-node-s2gfs 2/2 Running 1 3d

kube-system calico-node-xx30v 2/2 Running 1 3d

kube-system calico-policy-controller-336633499-wgl8j 1/1 Running 0 3d

kube-system etcd-k8s-1 1/1 Running 0 3d

kube-system kube-apiserver-k8s-1 1/1 Running 0 3d

kube-system kube-controller-manager-k8s-1 1/1 Running 0 3d

kube-system kube-dns-2425271678-trmxx 3/3 Running 1 3d

kube-system kube-proxy-79kkh 1/1 Running 0 3d

kube-system kube-proxy-n1g6j 1/1 Running 0 3d

kube-system kube-proxy-vccr6 1/1 Running 0 3d

kube-system kube-scheduler-k8s-1 1/1 Running 0 3d

10.安裝node節點,執行在master節點執行成功輸出的日誌語句(node節點執行)

注意:執行如下語句的之前,一定要確認node節點下載了上文提到的鏡像,否則因爲鏡像下載不成功會導致node節點初始化失敗;第二點,一定要時刻查看/var/log/messages日誌,如果鏡像版本發生變化,在日誌裏會提示需要下載的鏡像;第三點,就是要有耐心,如果你的網絡可以下載到鏡像,你當個安靜的美男子就可以了,因爲 kubeadm 會幫你做一切,知道你發現/var/log/messages不再有錯誤日誌出現,說明它已經幫你搞定了所有事情,你可以開心的玩耍了。

[root@centos7-base-ok]# kubeadm join –token xxxxxxxx 192.168.80.28:6443

1.驗證子節點,在master節點執行 kubectl get nodes 查看節點狀態。

注意:node的狀態會變化,添加成功後纔是Ready。

[root@centos7-base-ok]# kubectl get nodes

NAME STATUS AGE VERSION

k8s-1 Ready 3d v1.7.3

k8s-2 Ready 3d v1.7.3

k8s-3 Ready 3d v1.7.3

12.恭喜你,你可以開心的進行kubernetes1.7.3之旅了

安裝後記

Kubernetes,想說愛你不容易啊 ,歡迎其它團隊或者個人與我們團隊進行交流,有意向可以評論區給我留言。

補充:目前官方說dashboard的HEAD版本支持1.7,但是我試了下dashboard確實不行,希望官方加快修復,還有就是多些錯誤定位的方法,否則很難提出具體的問題。

本文轉自CSDN-k8s的部署

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章