用kubeadm手动搭建Kubernetes集群

用kubespray基于Ansible自动化工具搭建集群是很方便,但是一旦遇到问题查起来经常找不到头绪,因为不知所以然.
所以回归本源,用官方提供的kubeadm一步步建立一个单主的集群,可以让我们更容易弄清它的部署架构.
这也是此文/这次实践的目的所在.

前提条件

禁用swap

sudo swapoff -a
# 永久性禁用
# 编辑/etc/fstab并注释掉swap那一行

保留端口

Master节点

组件 端口 注释
API server 6443 可重新定义
etcd 2379-2380
Kubelet API 10250
kube-scheduler 10251
kube-controller-manage 10252

注: 2 CPU+

Worker节点

组件 端口 注释
Kubelet API 6443
NodePort Services 30000-32767 可重新定义

Docker

容器运行时默认使用docker

安装说明

kubeadm, kubelet, kubectl

  • kubeadm: 集群引导命令行
  • kubelet: 集群每个节点都有(Node Agent),用于启动Pod/容器等
  • kubectl: 交互式集群管理命令行

注: kubelet的版本号不应该超过API server

# Ubuntu
apt-get update && apt-get install -y apt-transport-https curl
curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb http://mirrors.ustc.edu.cn/kubernetes/apt kubernetes-xenial main
EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
apt-mark hold kubelet kubeadm kubectl

kubeadm init

# 可以先运行下面命令检查到gcr.io的连接性
# kubeadm config images pull
# 解决etcd pull失败的问题
docker pull  xulz/coreos-etcd:v3.2.24
docker tag xulz/coreos-etcd:v3.2.24 gcr.mirrors.ustc.edu.cn/google-containers/etcd:3.2.24

kubeadm init --kubernetes-version=v1.13.1 --image-repository=gcr.mirrors.ustc.edu.cn/google-containers --pod-network-cidr=192.168.0.0/16

# --image-repository 指定镜像mirror,默认k8s.gcr.io
# 也可以通过配置文件的形式 imageRepository: <private-registry>

# root用户运行 
export KUBECONFIG=/etc/kubernetes/admin.conf

# 记录下关于 kubeadm join的输出用于添加节点

注: token用户master和加入节点间的双向认证,需保密. 也可用 kubeadm token命令管理token.

# kubelet环境变量
# /var/lib/kubelet/kubeadm-flags.env
# kubelet配置文件
# /var/lib/kubelet/config.yaml

安装Pod网络插件

pod networking provider

这里使用的CNI network plugin是Calico,它需要提供CIDR(默认使用192.168.0.0/16), 已经在上步的init提供.

# 先获取本地镜像并tag
docker pull xulz/calico-node:v3.3.2
docker tag xulz/calico-node:v3.3.2 quay.io/calico/node:v3.3.2

docker pull xulz/calico-cni:v3.3.2
docker tag xulz/calico-cni:v3.3.2 quay.io/calico/cni:v3.3.2

docker pull quay.mirrors.ustc.edu.cn/calico/typha:v3.3.2
docker tag quay.mirrors.ustc.edu.cn/calico/typha:v3.3.2 quay.io/calico/typha:v3.3.2

以Calico为例:

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


# 检查状态, CoreDNS pod应该已经运行
kubectl get pods --all-namespaces

添加节点

kubeadm join --token <token> <master-ip>:<master-port> --discovery-token-ca-cert-hash sha256:<hash>

# 获取token
kubeadm token list

# 获取 --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/^.* //'
   
# 检查状态
kubectl get nodes   

删除节点/清除

在Master节点执行

kubectl drain <node name> --delete-local-data --force --ignore-daemonsets
kubectl delete node <node name>

在要删除的节点执行:

# 重置状态
kubeadm reset

# 清理iptables
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X

参考

kubeadm 命令参考

# 输出默认配置
kubeadm config print

# 输出指定api对象
kubeadm config print-default --api-objects KubeletConfiguration


# 列出/获取kubeadm需要的容器镜像
kubeadm config images list
kubeadm config images pull

# 指定版本, 跳过该地址的检查 https://dl.k8s.io/release/stable-1.txt
--kubernetes-version=v1.13.1
# --config指定配置文件
kubeadm init --config kubeadm-config.yaml

kubelet配置

init阶段

  • /var/lib/kubelet/config.yaml : 同时上传至kubelet-config-1.X的ConfigMap
  • /etc/kubernetes/kubelet.conf : 存储客户端证书用于和API Server通讯
  • /var/lib/kubelet/kubeadm-flags.env : 实例特定的信息及cgroup driver,CRI runtime socket

join阶段

  • 读取ConfigMap并写入/var/lib/kubelet/config.yaml
  • /etc/kubernetes/bootstrap-kubelet.conf 包含CA 证书和Bootstrap Token.
  • 读取上步的文件生成/etc/kubernetes/kubelet.conf

systemd使用的配置

/etc/systemd/system/kubelet.service.d/10-kubeadm.conf

kubeadm init 输出日志

[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 "front-proxy-ca" certificate and key
[certs] Generating "front-proxy-client" certificate and key
[certs] Generating "etcd/ca" certificate and key
[certs] Generating "etcd/server" certificate and key
[certs] etcd/server serving cert is signed for DNS names [xulz-master1 localhost] and IPs [192.168.10.1 127.0.0.1 ::1]
[certs] Generating "etcd/peer" certificate and key
[certs] etcd/peer serving cert is signed for DNS names [xulz-master1 localhost] and IPs [192.168.10.1 127.0.0.1 ::1]
[certs] Generating "apiserver-etcd-client" certificate and key
[certs] Generating "etcd/healthcheck-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 [xulz-master1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.10.1]
[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 21.502262 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 "xulz-master1" as an annotation
[mark-control-plane] Marking the node xulz-master1 as control-plane by adding the label "node-role.kubernetes.io/master=''"
[mark-control-plane] Marking the node xulz-master1 as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule]
[bootstrap-token] Using token: r4p7.6fuglhjtitmb
[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 192.168.10.1:6443 --token r4p7.6fuglhjtitmb --discovery-token-ca-cert-hash sha256:12345660bf8a5ff727b3563a4be19abee27785fcc806bbf2e20f82a15a82be06

添加节点输出日志

[preflight] Running pre-flight checks
[discovery] Trying to connect to API Server "192.168.10.1:6443"
[discovery] Created cluster-info discovery client, requesting info from "https://192.168.10.1:6443"
[discovery] Requesting info from "https://192.168.10.1: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 "192.168.10.1:6443"
[discovery] Successfully established connection with API Server "192.168.10.1: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 "xulz-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.

文章列表