使用Kubeadm(1.13+)快速搭建Kubernetes集群
Kubeadm是管理集群生命周期的重要工具丧失,從創(chuàng)建到配置再到升級,Kubeadm處理現(xiàn)有硬件上的生產(chǎn)集群的引導(dǎo)蛮浑,并以最佳實踐方式配置核心Kubernetes組件,以便為新節(jié)點提供安全而簡單的連接流程并支持輕松升級淳地。隨著Kubernetes 1.13 的發(fā)布著拭,現(xiàn)在Kubeadm正式成為GA。
準(zhǔn)備
首先準(zhǔn)備2臺虛擬機(jī)(CPU最少2核)衡怀,我是使用Hyper-V創(chuàng)建的2臺Ubuntu18.04虛擬機(jī)棍矛,IP和機(jī)器名如下:
172.17.20.210 master
172.17.20.211 node1
禁用Swap
Kubernetes 1.8開始要求必須禁用Swap,如果不關(guān)閉抛杨,默認(rèn)配置下kubelet將無法啟動够委。
編輯/etc/fstab
文件:
sudo vim /etc/fstab
UUID=8be04efd-f7c5-11e8-be8b-00155d000500 / ext4 defaults 0 0
UUID=C0E3-6A72 /boot/efi vfat defaults 0 0
#/swap.img none swap sw 0 0
如上,將/swap.img
所在的行注釋掉怖现,然后運行:
sudo swapoff -a
(可選)DNS配置
在Ubuntu18.04+版本中茁帽,DNS由systemd
全面接管,接口監(jiān)聽在127.0.0.53:53
屈嗤,配置文件在/etc/systemd/resolved.conf
中潘拨。
有時候會導(dǎo)致無法解析域名的問題,可使用如下2種方式來解決:
1.最簡單的就是關(guān)閉systemd-resolvd服務(wù)
sudo systemctl stop systemd-resolved
sudo systemctl disable systemd-resolved
然后手動修改/etc/resolv.conf
文件就可以了饶号。
2.更加推薦的做法是修改systemd-resolv的設(shè)置:
sudo vim /etc/systemd/resolved.conf
# 修改為如下
[Resolve]
DNS=1.1.1.1 1.0.0.1
#FallbackDNS=
#Domains=
LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes
DNS=設(shè)置的是域名解析服務(wù)器的IP地址铁追,這里分別設(shè)為1.1.1.1和1.0.0.1
LLMNR=設(shè)置的是禁止運行LLMNR(Link-Local Multicast Name Resolution),否則systemd-resolve會監(jiān)聽5535端口讨韭。
安裝Docker
Kubernetes從1.6開始使用CRI(Container Runtime Interface)容器運行時接口脂信。默認(rèn)的容器運行時仍然是Docker,是使用kubelet中內(nèi)置dockershim CRI來實現(xiàn)的透硝。
Docker的安裝可以參考之前的博客:Docker初體驗狰闪。
需要注意的是,Kubernetes 1.13已經(jīng)針對Docker的1.11.1, 1.12.1, 1.13.1, 17.03, 17.06, 17.09, 18.06等版本做了驗證濒生,最低支持的Docker版本是1.11.1埋泵,最高支持是18.06,而Docker最新版本已經(jīng)是
18.09
了罪治,故我們安裝時需要指定版本為18.06.1-ce
:sudo apt install docker-ce=18.06.1~ce~3-0~ubuntu
安裝kubeadm, kubelet 和 kubectl
部署之前丽声,我們需要安裝三個包:
kubeadm: 引導(dǎo)啟動k8s集群的命令行工具。
kubelet: 在群集中所有節(jié)點上運行的核心組件, 用來執(zhí)行如啟動pods和containers等操作觉义。
kubectl: 操作集群的命令行工具雁社。
首先添加apt-key:
sudo apt update && sudo apt install -y apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -
添加kubernetes源:
sudo vim /etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
安裝:
sudo apt update
sudo apt install -y kubelet kubeadm kubectl
sudo apt-mark hold kubelet kubeadm kubectl
使用kubeadm創(chuàng)建一個單Master集群
初始化Master節(jié)點
K8s的控制面板組件運行在Master節(jié)點上,包括etcd和API server(Kubectl便是通過API server與k8s通信)晒骇。
在執(zhí)行初始化之前霉撵,我們還有一下3點需要注意:
1.選擇一個網(wǎng)絡(luò)插件磺浙,并檢查它是否需要在初始化Master時指定一些參數(shù),比如我們可能需要根據(jù)選擇的插件來設(shè)置--pod-network-cidr
參數(shù)徒坡。參考:Installing a pod network add-on撕氧。
2.kubeadm使用eth0的默認(rèn)網(wǎng)絡(luò)接口(通常是內(nèi)網(wǎng)IP)做為Master節(jié)點的advertise address,如果我們想使用不同的網(wǎng)絡(luò)接口喇完,可以使用--apiserver-advertise-address=<ip-address>
參數(shù)來設(shè)置伦泥。如果適應(yīng)IPv6,則必須使用IPv6d的地址锦溪,如:--apiserver-advertise-address=fd00::101
不脯。
3.使用kubeadm config images pull
來預(yù)先拉取初始化需要用到的鏡像,用來檢查是否能連接到Kubenetes的Registries海洼。
Kubenetes默認(rèn)Registries地址是k8s.gcr.io
跨新,很明顯,在國內(nèi)并不能訪問gcr.io坏逢,因此在kubeadm v1.13之前的版本域帐,安裝起來非常麻煩,但是在1.13
版本中終于解決了國內(nèi)的痛點是整,其增加了一個--image-repository
參數(shù)肖揣,默認(rèn)值是k8s.gcr.io
,我們將其指定為國內(nèi)鏡像地址:registry.aliyuncs.com/google_containers
浮入,其它的就可以完全按照官方文檔來愉快的玩耍了龙优。
其次,我們還需要指定--kubernetes-version
參數(shù)事秀,因為它的默認(rèn)值是stable-1
彤断,會導(dǎo)致從https://dl.k8s.io/release/stable-1.txt
下載最新的版本號,我們可以將其指定為固定版本(最新版:v1.13.1)來跳過網(wǎng)絡(luò)請求易迹。
現(xiàn)在宰衙,我們就來試一下:
# 使用calico網(wǎng)絡(luò) --pod-network-cidr=192.168.0.0/16
sudo kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.13.1 --pod-network-cidr=192.168.0.0/16
# 輸出
[init] Using Kubernetes version: v1.13.1
[preflight] Running pre-flight checks
[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 "ca" certificate and key
[certs] Generating "apiserver" certificate and key
[certs] apiserver serving cert is signed for DNS names [master kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 172.17.20.210]
[certs] Generating "apiserver-kubelet-client" certificate and key
[certs] Generating "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [master localhost] and IPs [172.17.20.210 127.0.0.1 ::1]
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [master localhost] and IPs [172.17.20.210 127.0.0.1 ::1]
[certs] Generating "etcd/healthcheck-client" certificate and key
[certs] Generating "apiserver-etcd-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
[kubelet-check] Initial timeout of 40s passed.
[apiclient] All control plane components are healthy after 42.003645 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 "master" as an annotation
[mark-control-plane] Marking the node master as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node master as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: 6pkrlg.8glf2fqpuf3i489m
[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.17.20.210:6443 --token 6pkrlg.8glf2fqpuf3i489m --discovery-token-ca-cert-hash sha256:eebfe256113bee397b218ba832f412273ae734bd4686241fb910885d26efd222
這次非常順利的就部署成功了,如果我們想使用非root用戶操作kubectl
睹欲,可以使用以下命令供炼,這也是kubeadm init
輸出的一部分:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
安裝網(wǎng)絡(luò)插件
為了讓Pods間可以相互通信,我們必須安裝一個網(wǎng)絡(luò)插件窘疮,并且必須在部署任何應(yīng)用之前安裝袋哼,CoreDNS也是在網(wǎng)絡(luò)插件安裝之后才會啟動的。
網(wǎng)絡(luò)的插件完整列表闸衫,請參考 Networking and Network Policy涛贯。
在安裝之前,我們先查看一下當(dāng)前Pods的狀態(tài):
kubectl get pods --all-namespaces
# 輸出
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-78d4cf999f-6pgfr 0/1 Pending 0 87s
kube-system coredns-78d4cf999f-m9kgs 0/1 Pending 0 87s
kube-system etcd-master 1/1 Running 0 47s
kube-system kube-apiserver-master 1/1 Running 0 38s
kube-system kube-controller-manager-master 1/1 Running 0 55s
kube-system kube-proxy-mkg24 1/1 Running 0 87s
kube-system kube-scheduler-master 1/1 Running 0 41s
如上蔚出,可以看到CoreDND的狀態(tài)是Pending
疫蔓,這是因為我們還沒有安裝網(wǎng)絡(luò)插件含懊。
Calico是一個純?nèi)龑拥奶摂M網(wǎng)絡(luò)方案,Calico 為每個容器分配一個 IP衅胀,每個 host 都是 router,把不同 host 的容器連接起來酥筝。與 VxLAN 不同的是滚躯,Calico 不對數(shù)據(jù)包做額外封裝,不需要 NAT 和端口映射嘿歌,擴(kuò)展性和性能都很好掸掏。
默認(rèn)情況下,Calico網(wǎng)絡(luò)插件使用的的網(wǎng)段是192.168.0.0/16
宙帝,在init
的時候丧凤,我們已經(jīng)通過--pod-network-cidr=192.168.0.0/16
來適配Calico,當(dāng)然你也可以修改calico.yml
文件來指定不同的網(wǎng)段步脓。
可以使用如下命令命令來安裝Canal
插件:
kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/rbac-kdd.yaml
kubectl apply -f https://docs.projectcalico.org/v3.3/getting-started/kubernetes/installation/hosted/kubernetes-datastore/calico-networking/1.7/calico.yaml
# 上面的calico.yaml會去quay.io拉取鏡像愿待,如果無法拉取,可使用下面的國內(nèi)鏡像
kubectl apply -f http://mirror.faasx.com/k8s/calico/v3.3.2/rbac-kdd.yaml
kubectl apply -f http://mirror.faasx.com/k8s/calico/v3.3.2/calico.yaml
關(guān)于更多Canal
的信息可以查看Calico官方文檔:kubeadm quickstart靴患。
稍等片刻仍侥,再使用kubectl get pods --all-namespaces
命令來查看網(wǎng)絡(luò)插件的安裝情況:
kubectl get pods --all-namespaces
# 輸出
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system calico-node-x96gn 2/2 Running 0 47s
kube-system coredns-78d4cf999f-6pgfr 1/1 Running 0 54m
kube-system coredns-78d4cf999f-m9kgs 1/1 Running 0 54m
kube-system etcd-master 1/1 Running 3 53m
kube-system kube-apiserver-master 1/1 Running 3 53m
kube-system kube-controller-manager-master 1/1 Running 3 53m
kube-system kube-proxy-mkg24 1/1 Running 2 54m
kube-system kube-scheduler-master 1/1 Running 3 53m
如上,STATUS全部變?yōu)榱?code>Running鸳君,表示安裝成功农渊,接下來就可以加入其他節(jié)點以及部署應(yīng)用了。
Master隔離
默認(rèn)情況下或颊,由于安全原因砸紊,集群并不會將pods部署在Master節(jié)點上。但是在開發(fā)環(huán)境下囱挑,我們可能就只有一個Master節(jié)點醉顽,這時可以使用下面的命令來解除這個限制:
kubectl taint nodes --all node-role.kubernetes.io/master-
## 輸出
node/master untainted
加入工作節(jié)點
要為群集添加工作節(jié)點,需要為每臺計算機(jī)執(zhí)行以下操作:
- SSH到機(jī)器
- 成為root用戶看铆,(如: sudo su -)
- 運行上面的
kubeadm init
命令輸出的:kubeadm join --token <token> <master-ip>:<master-port> --discovery-token-ca-cert-hash sha256:<hash>
如果我們忘記了Master節(jié)點的加入token徽鼎,可以使用如下命令來查看:
kubeadm token list
# 輸出
TOKEN TTL EXPIRES USAGES DESCRIPTION EXTRA GROUPS
6pkrlg.8glf2fqpuf3i489m 22h 2018-12-07T13:46:33Z authentication,signing The default bootstrap token generated by 'kubeadm init'. system:bootstrappers:kubeadm:default-node-token
默認(rèn)情況下,token的有效期是24小時弹惦,如果我們的token已經(jīng)過期的話否淤,可以使用以下命令重新生成:
kubeadm token create
# 輸出
u2mt59.tyqpo0v5wf05lx2q
如果我們也沒有--discovery-token-ca-cert-hash
的值,可以使用以下命令生成:
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
# 輸出
eebfe256113bee397b218ba832f412273ae734bd4686241fb910885d26efd222
現(xiàn)在棠隐,我們登錄到工作節(jié)點服務(wù)器石抡,然后運行如下命令加入集群(這也是上面init
輸出的一部分):
sudo kubeadm join 172.17.20.210:6443 --token 6pkrlg.8glf2fqpuf3i489m --discovery-token-ca-cert-hash sha256:eebfe256113bee397b218ba832f412273ae734bd4686241fb910885d26efd222
# 輸出
[sudo] password for raining:
[preflight] Running pre-flight checks
[discovery] Trying to connect to API Server "172.17.20.210:6443"
[discovery] Created cluster-info discovery client, requesting info from "https://172.17.20.210:6443"
[discovery] Requesting info from "https://172.17.20.210: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.17.20.210:6443"
[discovery] Successfully established connection with API Server "172.17.20.210: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 "node1" 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.
等待一會,我們可以在Master節(jié)點上使用kubectl get nodes
命令來查看節(jié)點的狀態(tài):
kubectl get nodes
# 輸出
NAME STATUS ROLES AGE VERSION
master Ready master 17m v1.13.1
node1 Ready <none> 15m v1.13.1
如上全部Ready
助泽,大功告成啰扛,我們可以運行一些命令來測試一下集群是否正常嚎京。
測試
首先驗證kube-apiserver, kube-controller-manager, kube-scheduler, pod network 是否正常:
# 部署一個 Nginx Deployment,包含兩個Pod
# https://kubernetes.io/docs/concepts/workloads/controllers/deployment/
kubectl create deployment nginx --image=nginx:alpine
kubectl scale deployment nginx --replicas=2
# 驗證Nginx Pod是否正確運行隐解,并且會分配192.168.開頭的集群IP
kubectl get pods -l app=nginx -o wide
# 輸出如下:
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
nginx-54458cd494-p8jzs 1/1 Running 0 31s 192.168.1.2 node1 <none> <none>
nginx-54458cd494-v2m4b 1/1 Running 0 24s 192.168.1.3 node1 <none> <none>
再驗證一下kube-proxy
是否正常:
# 以 NodePort 方式對外提供服務(wù) https://kubernetes.io/docs/concepts/services-networking/connect-applications-service/
kubectl expose deployment nginx --port=80 --type=NodePort
# 查看集群外可訪問的Port
kubectl get services nginx
# 輸出
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx NodePort 10.110.49.49 <none> 80:31899/TCP 4s
# 可以通過任意 NodeIP:Port 在集群外部訪問這個服務(wù)鞍帝,本示例中部署的2臺集群IP分別是172.17.20.210和172.17.20.211
curl http://172.17.20.210:31899
curl http://172.17.20.211:31899
最后驗證一下dns, pod network是否正常:
# 運行Busybox并進(jìn)入交互模式
kubectl run -it curl --image=radial/busyboxplus:curl
# 輸入`nslookup nginx`查看是否可以正確解析出集群內(nèi)的IP,已驗證DNS是否正常
[ root@curl-66959f6557-6sfqh:/ ]$ nslookup nginx
# 輸出
Server: 10.96.0.10
Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
Name: nginx
Address 1: 10.110.49.49 nginx.default.svc.cluster.local
# 通過服務(wù)名進(jìn)行訪問煞茫,驗證kube-proxy是否正常
[ root@curl-66959f6557-6sfqh:/ ]$ curl http://nginx/
# 輸出如下:
# <!DOCTYPE html> ---省略
# 分別訪問一下2個Pod的內(nèi)網(wǎng)IP帕涌,驗證跨Node的網(wǎng)絡(luò)通信是否正常
[ root@curl-66959f6557-6sfqh:/ ]$ curl http://192.168.1.2/
[ root@curl-66959f6557-6sfqh:/ ]$ curl http://192.168.1.3/
驗證通過,集群搭建成功续徽,接下來我們就可以參考官方文檔來部署其他服務(wù)蚓曼,愉快的玩耍了。
卸載集群
想要撤銷kubeadm執(zhí)行的操作钦扭,首先要排除節(jié)點纫版,并確保該節(jié)點為空, 然后再將其關(guān)閉。
在Master節(jié)點上運行:
kubectl drain <node name> --delete-local-data --force --ignore-daemonsets
kubectl delete node <node name>
然后在需要移除的節(jié)點上客情,重置kubeadm的安裝狀態(tài):
sudo kubeadm reset
如果你想重新配置集群其弊,使用新的參數(shù)重新運行kubeadm init
或者kubeadm join
即可。
參考資料
Memo
本文轉(zhuǎn)載至 https://www.cnblogs.com/RainingNight/p/using-kubeadm-to-create-a-cluster-1-13.html