k8s介紹
Kubernetes(k8s)是Google開源的容器集群管理系統(谷歌內部:Borg)。在Docker技術的基礎上,為容器化的應用提供部署運行、資源調度、服務發現和動態伸縮等一系列完整功能,提高了大規模容器集群管理的便捷性。
Kubernetes優勢:
– 容器編排
– 輕量級
– 開源
– 彈性伸縮
– 負載均衡
重要概念
1. cluster
cluster是 計算、存儲和網絡資源的集合,k8s利用這些資源運行各種基於容器的應用。
2.master
master是cluster的大腦,他的主要職責是調度,即決定將應用放在那里運行。master運行linux操作系統,可以是物理機或者虛擬機。為了實現高可用,可以運行多個master。
3.node
node的職責是運行容器應用。node由master管理,node負責監控並匯報容器的狀態,同時根據master的要求管理容器的生命周期。node運行在linux的操作系統上,可以是物理機或者是虛擬機。
4.pod
pod是k8s的最小工作單元。每個pod包含一個或者多個容器。pod中的容器會作為一個整體被master調度到一個node上運行。
5.controller
k8s通常不會直接創建pod,而是通過controller來管理pod的。controller中定義了pod的部署特性,比如有幾個劇本,在什么樣的node上運行等。為了滿足不同的業務場景,k8s提供了多種controller,包括deployment、replicaset、daemonset、statefulset、job等。
6.deployment
是最常用的controller。deployment可以管理pod的多個副本,並確保pod按照期望的狀態運行。
7.replicaset
實現了pod的多副本管理。使用deployment時會自動創建replicaset,也就是說deployment是通過replicaset來管理pod的多個副本的,我們通常不需要直接使用replicaset。
8.daemonset
用於每個node最多只運行一個pod副本的場景。正如其名稱所示的,daemonset通常用於運行daemon。
9.statefuleset
能夠保證pod的每個副本在整個生命周期中名稱是不變的,而其他controller不提供這個功能。當某個pod發生故障需要刪除並重新啟動時,pod的名稱會發生變化,同時statefulset會保證副本按照固定的順序啟動、更新或者刪除。、
10.job
用於運行結束就刪除的應用,而其他controller中的pod通常是長期持續運行的。
11.service
deployment可以部署多個副本,每個pod 都有自己的IP,外界如何訪問這些副本那?
答案是service
k8s的 service定義了外界訪問一組特定pod的方式。service有自己的IP和端口,service為pod提供了負載均衡。
k8s運行容器pod與訪問容器這兩項任務分別由controller和service執行。
12.namespace
可以將一個物理的cluster邏輯上划分成多個虛擬cluster,每個cluster就是一個namespace。不同的namespace里的資源是完全隔離的。
安裝 kubelet、kubeadm 和 kubectl
master: 172.20.10.2
node1: 172.20.10.7
node2: 172.20.10.9
官方安裝文檔可以參考 https://kubernetes.io/docs/setup/independent/install-kubeadm/
第一步:安裝docker
所有節點都需要安裝docker
每個節點都需要使docker開機自啟
[root@localhost yum.repos.d]# wget http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo [root@ken ~]# yum install docker-ce -y [root@ken ~]# mkdir /etc/docker [root@ken ~]# cat /etc/docker/daemon.json { "registry-mirrors": ["https://XXX.mirror.aliyuncs.com"] } [root@ken ~]# systemctl restart docker
[root@ken ~]# systemctl enable docker
第二步:配置k8s的yum文件
[k8s] name=k8s enabled=1 gpgcheck=0 baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
第三步:安裝 kubelet、kubeadm 和 kubectl(所有節點執行)
kubelet 運行在 Cluster 所有節點上,負責啟動 Pod 和容器。
kubeadm 用於初始化 Cluster。
kubectl 是 Kubernetes 命令行工具。通過 kubectl 可以部署和管理應用,查看各種資源,創建、刪除和更新各種組件。
[root@ken ~]# yum install kubelet kubeadm kubectl -y
第四步:啟動kubelet
此時,還不能啟動kubelet,因為此時配置還不能,現在僅僅可以設置開機自啟動
[root@ken ~]# systemctl enable kubelet
用 kubeadm 創建 Cluster
第一步:環境准備(各個節點都需要執行下面的操作master,node)
1.CPU數量至少兩個否則會報錯
2. 主機名必須解析
[root@ken ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 172.20.10.2 ken 172.20.10.7 host1 172.20.10.9 host2
3.要保證打開內置的橋功能,這個是借助於iptables來實現的
需要安裝docker才會成/proc/sys/net/bridge/bridge-nf-call-iptables
[root@ken ~]# echo "1" >/proc/sys/net/bridge/bridge-nf-call-iptables
4. 需要禁止各個節點啟用swap,如果啟用了swap,那么kubelet就無法啟動
[root@ken ~]# swapoff -a && sysctl -w vm.swappiness=0 vm.swappiness = 0 [root@ken ~]# free -m total used free shared buff/cache available Mem: 991 151 365 7 475 674 Swap: 0 0 0
5.關閉防火牆和selinux
第二步:初始化master
1.13.1版本可能太老了,在初始化的時候可以選擇更高的版本,例如:1.15.1
[root@ken ~]# kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.15.1 --apiserver-advertise-address 172.20.10.2 --pod-network-cidr=10.244.0.0/16
–image-repository string:這個用於指定從什么位置來拉取鏡像(1.13版本才有的),默認值是k8s.gcr.io,我們將其指定為國內鏡像地址:registry.aliyuncs.com/google_containers
–kubernetes-version string:指定kubenets版本號,默認值是stable-1,會導致從https://dl.k8s.io/release/stable-1.txt下載最新的版本號,我們可以將其指定為固定版本(v1.15.1)來跳過網絡請求。
–apiserver-advertise-address 指明用 Master 的哪個 interface 與 Cluster 的其他節點通信。如果 Master 有多個 interface,建議明確指定,如果不指定,kubeadm 會自動選擇有默認網關的 interface。
–pod-network-cidr指定 Pod 網絡的范圍。Kubernetes 支持多種網絡方案,而且不同網絡方案對 –pod-network-cidr有自己的要求,這里設置為10.244.0.0/16 是因為我們將使用 flannel 網絡方案,必須設置成這個 CIDR。
補充flannel網絡介紹
看到下面的輸出就表示你的集群創建成功了
[root@ken ~]# kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.15.1 --apiserver-advertise-address 172.20.10.2 --pod-network-cidr=10.244.0.0/16 [init] Using Kubernetes version: v1.15.1 [preflight] Running pre-flight checks [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.09.0. Latest validated version: 18.06 [preflight] Pulling images required for setting up a Kubernetes cluster [preflight] This might take a minute or two, depending on the speed of your internet connection [preflight] You can also perform this action in beforehand using 'kubeadm config images pull' [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" [kubelet-start] Activating the kubelet service [certs] Using certificateDir folder "/etc/kubernetes/pki" [certs] Generating "etcd/ca" certificate and key [certs] Generating "etcd/server" certificate and key [certs] etcd/server serving cert is signed for DNS names [ken localhost] and IPs [172.20.10.2 127.0.0.1 ::1] [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [ken localhost] and IPs [172.20.10.2 127.0.0.1 ::1] [certs] Generating "etcd/healthcheck-client" certificate and key [certs] Generating "apiserver-etcd-client" certificate and key [certs] Generating "front-proxy-ca" certificate and key [certs] Generating "front-proxy-client" certificate and key [certs] Generating "ca" certificate and key [certs] Generating "apiserver" certificate and key [certs] apiserver serving cert is signed for DNS names [ken kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.20.10.2] [certs] Generating "apiserver-kubelet-client" certificate and key [certs] Generating "sa" key and public key [kubeconfig] Using kubeconfig folder "/etc/kubernetes" [kubeconfig] Writing "admin.conf" kubeconfig file [kubeconfig] Writing "kubelet.conf" kubeconfig file [kubeconfig] Writing "controller-manager.conf" kubeconfig file [kubeconfig] Writing "scheduler.conf" kubeconfig file [control-plane] Using manifest folder "/etc/kubernetes/manifests" [control-plane] Creating static Pod manifest for "kube-apiserver" [control-plane] Creating static Pod manifest for "kube-controller-manager" [control-plane] Creating static Pod manifest for "kube-scheduler" [etcd] Creating static Pod manifest for local etcd in "/etc/kubernetes/manifests" [wait-control-plane] Waiting for the kubelet to boot up the control plane as static Pods from directory "/etc/kubernetes/manifests". This can take up to 4m0s [apiclient] All control plane components are healthy after 26.507041 seconds [uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.13" in namespace kube-system with the configuration for the kubelets in the cluster [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "ken" as an annotation [mark-control-plane] Marking the node ken as control-plane by adding the label "node-role.kubernetes.io/master=''" [mark-control-plane] Marking the node ken as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule] [bootstrap-token] Using token: rn816q.zj0crlasganmrzsr [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles [bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials [bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token [bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster [bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace [addons] Applied essential addon: CoreDNS [addons] Applied essential addon: kube-proxy Your Kubernetes master has initialized successfully! To start using your cluster, you need to run the following 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: https://kubernetes.io/docs/concepts/cluster-administration/addons/ You can now join any number of machines by running the following on each node as root: kubeadm join 172.20.10.2:6443 --token rn816q.zj0crlasganmrzsr --discovery-token-ca-cert-hash sha256:e339e4dbf6bd1323c13e794760fff3cbeb7a3f6f42b71d4cb3cffdde72179903
如果初始化失敗,請使用如下代碼清除后重新初始化
# kubeadm reset
# ifconfig cni0 down
# ip link delete cni0
# ifconfig flannel.1 down
# ip link delete flannel.1
# rm -rf /var/lib/cni/
# rm -rf /var/lib/etcd/*
docker初始化成功下載的鏡像
[root@ken ~]# docker image ls REPOSITORY TAG IMAGE ID CREATED SIZE registry.aliyuncs.com/google_containers/kube-proxy v1.13.1 fdb321fd30a0 6 weeks ago 80.2MB registry.aliyuncs.com/google_containers/kube-controller-manager v1.13.1 26e6f1db2a52 6 weeks ago 146MB registry.aliyuncs.com/google_containers/kube-apiserver v1.13.1 40a63db91ef8 6 weeks ago 181MB registry.aliyuncs.com/google_containers/kube-scheduler v1.13.1 ab81d7360408 6 weeks ago 79.6MB tomcat latest 48dd385504b1 7 weeks ago 475MB memcached latest 8230c836a4b3 2 months ago 62.2MB registry.aliyuncs.com/google_containers/coredns 1.2.6 f59dcacceff4 2 months ago 40MB busybox latest 59788edf1f3e 3 months ago 1.15MB registry.aliyuncs.com/google_containers/etcd 3.2.24 3cab8e1b9802 4 months ago 220MB registry.aliyuncs.com/google_containers/pause 3.1 da86e6ba6ca1 13 months ago 742kB
第三步:配置kubectl
kubectl 是管理 Kubernetes Cluster 的命令行工具,前面我們已經在所有的節點安裝了 kubectl。Master 初始化完成后需要做一些配置工作,然后 kubectl 就能使用了。
[root@ken ~]# mkdir -p $HOME/.kube [root@ken ~]# cp -i /etc/kubernetes/admin.conf $HOME/.kube/config [root@ken ~]# chown $(id -u):$(id -g) $HOME/.kube/config
為了使用更便捷,啟用 kubectl 命令的自動補全功能。
[root@ken ~]# echo "source <(kubectl completion bash)" >> ~/.bashrc
現在kubectl可以使用了
[root@ken ~]# kubectl get cs NAME STATUS MESSAGE ERROR scheduler Healthy ok controller-manager Healthy ok etcd-0 Healthy {"health": "true"}
第四步:安裝pod網絡
要讓 Kubernetes Cluster 能夠工作,必須安裝 Pod 網絡,否則 Pod 之間無法通信。
Kubernetes 支持多種網絡方案,這里我們先使用 flannel
[root@ken ~]# kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
每個節點啟動kubelet
[root@ken ~]# systemctl restart kubelet
等鏡像下載完成以后,看到node的狀態是ready了
[root@ken ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION ken Ready master 17m v1.13.2
此時,就可以看到pod信息了
[root@ken ~]# kubectl get pods -n kube-system NAME READY STATUS RESTARTS AGE coredns-78d4cf999f-dbxpc 1/1 Running 0 19m coredns-78d4cf999f-q9vq2 1/1 Running 0 19m etcd-ken 1/1 Running 0 18m kube-apiserver-ken 1/1 Running 0 18m kube-controller-manager-ken 1/1 Running 0 18m kube-flannel-ds-amd64-fd8mv 1/1 Running 0 3m26s kube-proxy-gwmr2 1/1 Running 0 19m kube-scheduler-ken 1/1 Running 0 18m
添加 k8s-node1 和 k8s-node2
第一步:環境准備
1.node節點關閉防火牆和selinux
2.禁用swap
3. 解析主機名
4.啟動內核功能
啟動kubeket
只需要設置為開機自啟動就可以了
[root@host1 ~]# systemctl enable kubelet
第二步:添加nodes
這里的–token 來自前面kubeadm init輸出提示,如果當時沒有記錄下來可以通過kubeadm token list 查看。
kubeadm join 172.20.10.2:6443 --token rn816q.zj0crlasganmrzsr --discovery-token-ca-cert-hash sha256:e339e4dbf6bd1323c13e794760fff3cbeb7a3f6f42b71d4cb3cffdde72179903
輸出如下的信息
[root@host2 ~]# kubeadm join 172.20.10.2:6443 --token rn816q.zj0crlasganmrzsr --discovery-token-ca-cert-hash sha256:e339e4dbf6bd1323c13e794760fff3cbeb7a3f6f42b71d4cb3cffdde72179903 [preflight] Running pre-flight checks [WARNING SystemVerification]: this Docker version is not on the list of validated versions: 18.09.1. Latest validated version: 18.06 [discovery] Trying to connect to API Server "172.20.10.2:6443" [discovery] Created cluster-info discovery client, requesting info from "https://172.20.10.2:6443" [discovery] Requesting info from "https://172.20.10.2:6443" again to validate TLS against the pinned public key [discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server "172.20.10.2:6443" [discovery] Successfully established connection with API Server "172.20.10.2:6443" [join] Reading configuration from the cluster... [join] FYI: You can look at this config file with 'kubectl -n kube-system get cm kubeadm-config -oyaml' [kubelet] Downloading configuration for the kubelet from the "kubelet-config-1.13" ConfigMap in the kube-system namespace [kubelet-start] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml" [kubelet-start] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env" [kubelet-start] Activating the kubelet service [tlsbootstrap] Waiting for the kubelet to perform the TLS Bootstrap... [patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "host2" as an annotation This node has joined the cluster: * Certificate signing request was sent to apiserver and a response was received. * The Kubelet was informed of the new secure connection details. Run 'kubectl get nodes' on the master to see this node join the cluster.
第三步:查看nodes
根據上面最后一行的輸出信息提示查看nodes
[root@ken ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION host1 NotReady <none> 2m54s v1.13.2 host2 NotReady <none> 2m16s v1.13.2 ken Ready master 38m v1.13.2
這里其實需要等一會,這個node1節點才會變成Ready狀態,因為node節點需要下載四個鏡像flannel coredns kube-proxy pause
過了一會查看節點狀態
[root@ken ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION host1 Ready <none> 4m15s v1.13.2 host2 Ready <none> 3m37s v1.13.2 ken Ready master 39m v1.13.2
補充:移除NODE節點的方法
第一步:先將節點設置為維護模式(host1是節點名稱)
[root@ken ~]# kubectl drain host1 --delete-local-data --force --ignore-daemonsets node/host1 cordoned WARNING: Ignoring DaemonSet-managed pods: kube-flannel-ds-amd64-ssqcl, kube-proxy-7cnsr node/host1 drained
第二步:然后刪除節點
[root@ken ~]# kubectl delete node host1 node "host1" deleted
第三步:查看節點
發現host1節點已經被刪除了
[root@ken ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION host2 Ready <none> 13m v1.13.2 ken Ready master 49m v1.13.2
如果這個時候再想添加進來這個node,需要執行兩步操作
第一步:停掉kubelet(需要添加進來的節點操作)
[root@host1 ~]# systemctl stop kubelet
第二步:刪除相關文件
[root@host1 ~]# rm -rf /etc/kubernetes/*
第三步:添加節點
[root@host1 ~]# kubeadm join 172.20.10.2:6443 --token rn816q.zj0crlasganmrzsr --discovery-token-ca-cert-hash sha256:e339e4dbf6bd1323c13e794760fff3cbeb7a3f6f42b71d4cb3cffdde72179903
第四步:查看節點
[root@ken ~]# kubectl get nodes NAME STATUS ROLES AGE VERSION host1 Ready <none> 13s v1.13.2 host2 Ready <none> 17m v1.13.2 ken Ready master 53m v1.13.2
忘掉token再次添加進k8s集群
第一步:主節點執行命令
獲取token
[root@ken-master ~]# kubeadm token list TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS ojxdod.fb7tqipat46yp8ti 10h 2019-05-06T04:55:42+08:00 authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
第二步: 獲取ca證書sha256編碼hash值
[root@ken-master ~]# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //' 2f8888cdb01191ff6dbca0edb02dbb21a14469028e4ff2598854a4544c5fa751
第三步:從節點執行如下的命令
[root@ken-node1 ~]# systemctl stop kubelet
第四步:刪除相關文件
[root@ken-node1 ~]# rm -rf /etc/kubernetes/*
第五步:加入集群
指定主節點IP,端口是6443
在生成的證書前有sha256:
[root@ken-node1 ~]# kubeadm join 192.168.64.10:6443 --token ojxdod.fb7tqipat46yp8ti --discovery-token-ca-cert-hash sha256