1. 程式人生 > 實用技巧 >K8S記錄

K8S記錄

1. 環境準備

2. K8S安裝

2-1.安裝方式介紹

  • 使用現成的二進位制檔案
  • 使用原始碼編譯為二進位制檔案
  • 使用映象的方式

使用映象優雅,不適用於初學者。因為會花費很多時間去配置相關的容器等。這裡使用編譯好的二進位制檔案。

2-2.二進位制安裝步驟

  1. 下載二進位制檔案地址:
  2. 上傳至三臺伺服器:我是使用XFTP工具上傳
  3. tar -xvf 解壓縮
  4. 修改解壓後的資料夾名為bin 因為bin預設在系統的環境中變數中,如果換成其他的資料夾名, 可以自己配置一下環境變數。

2-3.設定環境變數

# 首先用命令find / -name kubectl   查詢kubectl所在的位置
root@server02:~# find / -name kubectl /root/bin/kubectl # 修改/etc/profile 檔案 root@server02:~# vim /etc/profile # 根據這個找到的路勁在最下面新增 export PATH="/root/bin/:$PATH" # 執行下面的命令使配置立即生效 source /etc/profile

2-4.測試安裝結果

測試一下K8S的命令:
source /etc/profilesource /etc/profile

root@server02:~# kubectl -h
kubectl controls the Kubernetes cluster manager. 

Find more information at https://github.com/kubernetes/kubernetes.

Basic Commands (Beginner):
  create         Create a resource from a file or from stdin.
  expose         Take a replication controller, service, deployment or pod and expose it as a new Kubernetes Service
  run            Run a particular image on the cluster
  set
Set specific features on objects run-container Run a particular image on the cluster. This command is deprecated, use "run" instead Basic Commands (Intermediate): get Display one or many resources explain Documentation of resources edit Edit a resource on the server delete Delete resources by filenames, stdin, resources and names, or by resources and label selector Deploy Commands: rollout Manage the rollout of a resource rolling-update Perform a rolling update of the given ReplicationController scale Set a new size for
a Deployment, ReplicaSet, Replication Controller, or Job autoscale Auto-scale a Deployment, ReplicaSet, or ReplicationController Cluster Management Commands: certificate Modify certificate resources. cluster-info Display cluster info top Display Resource (CPU/Memory/Storage) usage. cordon Mark node as unschedulable uncordon Mark node as schedulable drain Drain node in preparation for maintenance taint Update the taints on one or more nodes Troubleshooting and Debugging Commands: describe Show details of a specific resource or group of resources logs Print the logs for a container in a pod attach Attach to a running container exec Execute a command in a container port-forward Forward one or more local ports to a pod proxy Run a proxy to the Kubernetes API server cp Copy files and directories to and from containers. auth Inspect authorization Advanced Commands: apply Apply a configuration to a resource by filename or stdin patch Update field(s) of a resource using strategic merge patch replace Replace a resource by filename or stdin convert Convert config files between different API versions Settings Commands: label Update the labels on a resource annotate Update the annotations on a resource completion Output shell completion code for the specified shell (bash or zsh) Other Commands: api-versions Print the supported API versions on the server, in the form of "group/version" config Modify kubeconfig files help Help about any command plugin Runs a command-line plugin version Print the client and server version information Use "kubectl <command> --help" for more information about a given command. Use "kubectl options" for a list of global command-line options (applies to all commands).

出現以上資訊證明成功。

3. K8S叢集部署

3-1.配置檔案

上一步我們下載了kubernetes各個元件的二進位制檔案,這些可執行檔案的執行也是需要新增很多引數的,包括有的還會依賴一些配置檔案。現在我們就把執行它們需要的引數和配置檔案都準備好。

3-1-1. 下載配置檔案

#到home目錄下載專案
$ cd
$ git clone https://github.com/liuyi01/kubernetes-starter.git
#看看git內容
$ cd ~/kubernetes-starter && ls

3-1-2. 配置檔案說明

  • /gen-config.sh
    shell指令碼,用來根據每個人自己的叢集環境(ip,hostname等),根據下面的模板,生成適合大家各自環境的配置檔案。生成的檔案會放到target資料夾下。

  • kubernetes-simple
    簡易版kubernetes配置模板(剝離了認證授權)。 適合剛接觸kubernetes的同學,首先會讓大家在和kubernetes初次見面不會印象太差(太複雜啦~~),再有就是讓大家更容易抓住kubernetes的核心部分,把注意力集中到核心元件及元件的聯絡,從整體上把握kubernetes的執行機制。

  • kubernetes-with-ca
    在simple基礎上增加認證授權部分。大家可以自行對比生成的配置檔案,看看跟simple版的差異,更容易理解認證授權的(認證授權也是kubernetes學習曲線較高的重要原因)

  • service-config
    這個先不用關注,它是我們曾經開發的那些微服務配置。 等我們熟悉了kubernetes後,實踐用的,通過這些配置,把我們的微服務都執行到kubernetes叢集中。

3-2.基礎叢集部署(不含認證)

3-2-1. 生成基礎版本配置

使用shell指令碼來生成一個不帶認證授權的配置檔案

root@server02:~# ls
bin  kubernetes-starter
# 進入clone下的資料夾
root@server02:~# cd kubernetes-starter/
# 編輯 config.properties
root@server02:~/kubernetes-starter# vi config.properties 
#執行shell生成簡單版本的K8S配置檔案
root@server02:~/kubernetes-starter# ./gen-config.sh simple
====替換變數列表====
BIN_PATH=/root/bin
NODE_IP=192.168.0.169
ETCD_ENDPOINTS=http://192.168.0.169:2379
MASTER_IP=192.168.0.169
====================
====替換配置檔案====
all-node/kube-calico.service
master-node/etcd.service
master-node/kube-apiserver.service
master-node/kube-controller-manager.service
master-node/kube-scheduler.service
services/kube-dns.yaml
worker-node/10-calico.conf
worker-node/kubelet.kubeconfig
worker-node/kubelet.service
worker-node/kube-proxy.kubeconfig
worker-node/kube-proxy.service
=================
配置生成成功,位置: /root/kubernetes-starter/target

#使用find命令 看一下target下面的配置檔案
root@server02:~/kubernetes-starter# find target/ -type f
target/worker-node/kubelet.kubeconfig
target/worker-node/10-calico.conf
target/worker-node/kube-proxy.kubeconfig
target/worker-node/kube-proxy.service
target/worker-node/kubelet.service
target/services/kube-dns.yaml
target/master-node/kube-controller-manager.service
target/master-node/etcd.service
target/master-node/kube-scheduler.service
target/master-node/kube-apiserver.service
target/all-node/kube-calico.service

3-2-2. 部署ETCD

3-2-2-1. 介紹

kubernetes需要儲存很多東西,像它本身的節點資訊,元件資訊,還有通過kubernetes執行的pod,deployment,service等等。都需要持久化。etcd就是它的資料中心。生產環境中為了保證資料中心的高可用和資料的一致性,一般會部署最少三個節點。我們這裡以學習為主就只在主節點部署一個例項

3-2-2-2. 部署

etcd的二進位制檔案及配置都已經生成了, 只需要把他作為服務執行起來

#把服務配置檔案copy到系統服務目錄
$ cp ~/kubernetes-starter/target/master-node/etcd.service /lib/systemd/system/
# enable服務
$ systemctl enable etcd.service
#建立工作目錄(儲存資料的地方)
$ mkdir -p /var/lib/etcd
# 啟動服務
$ service etcd start
# 檢視服務日誌,看是否有錯誤資訊,確保服務正常
$ journalctl -f -u etcd.service 
3-2-2-3. 配置說明

3-2-3. 部署APIServer(主節點)

3-2-3-1. 介紹

kube-apiserver是Kubernetes最重要的核心元件之一,主要提供以下的功能

  • 提供叢集管理的REST API介面,包括認證授權(我們現在沒有用到)資料校驗以及叢集狀態變更等
  • 提供其他模組之間的資料互動和通訊的樞紐(其他模組通過API Server查詢或修改資料,只有API Server才直接操作etcd)

生產環境為了保證apiserver的高可用一般會部署2+個節點,在上層做一個lb做負載均衡,比如haproxy。由於單節點和多節點在apiserver這一層說來沒什麼區別,所以我們目前部署一個節點就足夠了

3-2-3-2. 部署

部署和上面部署etcd差不多 最後看一哈日誌 無錯即可

$ cp target/master-node/kube-apiserver.service /lib/systemd/system/
$ systemctl enable kube-apiserver.service
$ service kube-apiserver start
$ journalctl -f -u kube-apiserver
3-2-3-3. 配置說明

[Unit]
Description=Kubernetes API Server
...
[Service]
#可執行檔案的位置
ExecStart=/home/michael/bin/kube-apiserver
#非安全埠(8080)繫結的監聽地址 這裡表示監聽所有地址
--insecure-bind-address=0.0.0.0
#不使用https
--kubelet-https=false
#kubernetes叢集的虛擬ip的地址範圍
--service-cluster-ip-range=10.68.0.0/16
#service的nodeport的埠範圍限制
--service-node-port-range=20000-40000
#很多地方都需要和etcd打交道,也是唯一可以直接操作etcd的模組
--etcd-servers=http://192.168.1.102:2379
...

3-2-4. 部署ControllerManager(主節點)

3-2-4-1. 簡介

Controller Manager由kube-controller-manager和cloud-controller-manager組成,是Kubernetes的大腦,它通過apiserver監控整個叢集的狀態,並確保叢集處於預期的工作狀態。 kube-controller-manager由一系列的控制器組成,像Replication Controller控制副本,Node Controller節點控制,Deployment Controller管理deployment等等 cloud-controller-manager在Kubernetes啟用Cloud Provider的時候才需要,用來配合雲服務提供商的控制

controller-manager、scheduler和apiserver 三者的功能緊密相關,一般執行在同一個機器上,我們可以把它們當做一個整體來看,所以保證了apiserver的高可用即是保證了三個模組的高可用。也可以同時啟動多個controller-manager程序,但只有一個會被選舉為leader提供服務

3-2-4-2. 部署
$ cp target/master-node/kube-controller-manager.service /lib/systemd/system/
$ systemctl enable kube-controller-manager.service
$ service kube-controller-manager start
$ journalctl -f -u kube-controller-manager
3-2-4-3. 配置說明

[Unit]
Description=Kubernetes Controller Manager
...
[Service]
ExecStart=/home/michael/bin/kube-controller-manager
#對外服務的監聽地址,這裡表示只有本機的程式可以訪問它
--address=127.0.0.1
#apiserver的url
--master=http://127.0.0.1:8080
#服務虛擬ip範圍,同apiserver的配置
--service-cluster-ip-range=10.68.0.0/16
#pod的ip地址範圍
--cluster-cidr=172.20.0.0/16
#下面兩個表示不使用證書,用空值覆蓋預設值
--cluster-signing-cert-file=
--cluster-signing-key-file=
...

3-2-5. 部署Scheduler(主節點)

3-2-5-1. 簡介

kube-scheduler負責分配排程Pod到叢集內的節點上,它監聽kube-apiserver,查詢還未分配Node的Pod,然後根據排程策略為這些Pod分配節點。我們前面講到的kubernetes的各種排程策略就是它實現的。

3-2-5-2. 部署
$ cp target/master-node/kube-scheduler.service /lib/systemd/system/
$ systemctl enable kube-scheduler.service
$ service kube-scheduler start
$ journalctl -f -u kube-scheduler
3-2-5-3. 配置
[Unit]
Description=Kubernetes Scheduler
...
[Service]
ExecStart=/home/michael/bin/kube-scheduler \
#對外服務的監聽地址,這裡表示只有本機的程式可以訪問它
--address=127.0.0.1 \
#apiserver的url
--master=http://127.0.0.1:8080 \
...

3-2-6. 部署Calico