咱们将部署三个节点的 Kubernetes Cluster。node
master 是 Master,node1 和 node2 是 Node。全部节点的操做系统均为 Ubuntu 18.04,固然其余 Linux 也是能够的。官方安装文档能够参考 https://kubernetes.io/docs/setup/independent/install-kubeadm/linux
注意:Kubernetes 几乎全部的安装组件和 Docker 镜像都放在 goolge 本身的网站上,这对国内的同窗多是个不小的障碍。建议是:网络障碍都必须想办法克服,否则连 Kubernetes 的门都进不了。docker
全部节点都须要安装 Docker。参考:https://docs.docker.com/engine/install/ubuntu/数据库
# step 1: 安装必要的一些系统工具,安装包,容许 apt 命令 HTTPS 访问 Docker 源。 sudo apt-get update sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common # step 2: 安装GPG证书 curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add - # Step 3: 写入软件源信息,将 Docker 的源添加到 /etc/apt/sources.list sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" # Step 4: 更新并安装Docker-CE sudo apt-get -y update && sudo apt-get install docker-ce docker-ce-cli containerd.io
在全部节点上安装 kubelet、kubeadm 和 kubectl。bootstrap
kubelet 运行在 Cluster 全部节点上,负责启动 Pod 和容器。ubuntu
kubeadm 用于初始化 Cluster。api
kubectl 是 Kubernetes 命令行工具。经过 kubectl 能够部署和管理应用,查看各类资源,建立、删除和更新各类组件。bash
apt-get update && apt-get install -y apt-transport-https curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add - cat <<EOF >/etc/apt/sources.list.d/kubernetes.list deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main EOF apt-get update apt-get install -y kubelet kubeadm kubectl
完整的官方文档能够参考 https://kubernetes.io/docs/setup/independent/create-cluster-kubeadm/网络
控制平面节点是运行控制平面组件的机器, 包括 etcd(集群数据库) 和 API Server(命令行工具 kubectl与之通讯)。app
1.(推荐)若是计划将单个控制平面 kubeadm 集群升级成高可用, 你应该指定 --control-plane-endpoint 为全部控制平面节点设置共享端点。 端点能够是负载均衡器的 DNS 名称或 IP 地址。 2.选择一个Pod网络插件,并验证是否须要为 kubeadm init 传递参数。 根据你选择的第三方网络插件,你可能须要设置 --pod-network-cidr 的值。 请参阅 安装Pod网络附加组件。 3.(可选)从版本1.14开始,kubeadm 尝试使用一系列众所周知的域套接字路径来检测 Linux 上的容器运行时。 要使用不一样的容器运行时, 或者若是在预配置的节点上安装了多个容器,请为 kubeadm init 指定 --cri-socket 参数。 请参阅安装运行时。 4.(可选)除非另有说明,不然 kubeadm 使用与默认网关关联的网络接口来设置此控制平面节点 API server 的广播地址。 要使用其余网络接口,请为 kubeadm init 设置 --apiserver-advertise-address=<ip-address> 参数。 要部署使用 IPv6 地址的 Kubernetes 集群, 必须指定一个 IPv6 地址,例如 --apiserver-advertise-address=fd00::101 5.(可选)在 kubeadm init 以前运行 kubeadm config images pull,以验证与 gcr.io 容器镜像仓库的连通性。
关于 apiserver-advertise-address 和 ControlPlaneEndpoint 的注意事项:
--apiserver-advertise-address
可用于为控制平面节点的 API server 设置广播地址, --control-plane-endpoint
可用于为全部控制平面节点设置共享端点。
--control-plane-endpoint
容许 IP 地址和能够映射到 IP 地址的 DNS 名称。 请与你的网络管理员联系,以评估有关此类映射的可能解决方案。
这是一个示例映射:
192.168.0.102 cluster-endpoint
其中 192.168.0.102
是此节点的 IP 地址,cluster-endpoint
是映射到该 IP 的自定义 DNS 名称。 这将容许你将 --control-plane-endpoint=cluster-endpoint
传递给 kubeadm init
,并将相同的 DNS 名称传递给 kubeadm join
。 稍后你能够修改 cluster-endpoint
以指向高可用性方案中的负载均衡器的地址。
kubeadm 不支持将没有 --control-plane-endpoint
参数的单个控制平面集群转换为高可用性集群。
关闭 SELinux:
# 临时禁用selinux # 永久关闭 修改/etc/sysconfig/selinux文件设置 sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux # 这里按回车,下面是第二条命令 setenforce 0
在 Master 上执行以下命令:
关闭swap: swapoff -a sed -ri 's/.*swap.*/#&/' /etc/fstab 全部节点,kubeadm初始化警告”cgroupfs“解决 vi /lib/systemd/system/docker.service ExecStart=/usr/bin/dockerd -H fd:// --containerd=/run/containerd/containerd.sock --exec-opt native.cgroupdriver=systemd systemctl daemon-reload systemctl restart docker 初始化master: kubeadm init --image-repository registry.aliyuncs.com/google_containers --apiserver-advertise-address 10.0.0.41 --pod-network-cidr=10.244.0.0/16
--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。在后面的实践中咱们会切换到其余网络方案,好比 Canal。
root@cuiyongchao:~# kubeadm init --image-repository registry.aliyuncs.com/google_containers --apiserver-advertise-address 10.0.0.41 --pod-network-cidr=10.244.0.0/16 W1101 09:18:28.676350 26460 configset.go:348] WARNING: kubeadm cannot validate component configs for API groups [kubelet.config.k8s.io kubeproxy.config.k8s.io] [init] Using Kubernetes version: v1.19.3 [preflight] Running pre-flight checks ---① [WARNING IsDockerSystemdCheck]: detected "cgroupfs" as the Docker cgroup driver. The recommended driver is "systemd". Please follow the guide at https://kubernetes.io/docs/setup/cri/ [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' [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 [cuiyongchao kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 10.0.0.41] [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/server" certificate and key [certs] etcd/server serving cert is signed for DNS names [cuiyongchao localhost] and IPs [10.0.0.41 127.0.0.1 ::1] [certs] Generating "etcd/peer" certificate and key [certs] etcd/peer serving cert is signed for DNS names [cuiyongchao localhost] and IPs [10.0.0.41 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 [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] Starting the kubelet [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 33.004269 seconds ----④ [upload-config] Storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace [kubelet] Creating a ConfigMap "kubelet-config-1.19" in namespace kube-system with the configuration for the kubelets in the cluster [upload-certs] Skipping phase. Please see --upload-certs [mark-control-plane] Marking the node cuiyongchao as control-plane by adding the label "node-role.kubernetes.io/master=''" [mark-control-plane] Marking the node cuiyongchao as control-plane by adding the taints [node-role.kubernetes.io/master:NoSchedule] [bootstrap-token] Using token: krsig9.fnxqz4724vkrlevz [bootstrap-token] Configuring bootstrap tokens, cluster-info ConfigMap, RBAC Roles [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to get nodes [bootstrap-token] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials [bootstrap-token] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token [bootstrap-token] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster [bootstrap-token] Creating the "cluster-info" ConfigMap in the "kube-public" namespace [kubelet-finalize] Updating "/etc/kubernetes/kubelet.conf" to point to a rotatable kubelet client certificate and key [addons] Applied essential addon: CoreDNS [addons] Applied essential addon: kube-proxy ---⑤ Your Kubernetes control-plane 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/ Then you can join any number of worker nodes by running the following on each as root: kubeadm join 10.0.0.41:6443 --token krsig9.fnxqz4724vkrlevz \ ---⑨ --discovery-token-ca-cert-hash sha256:cf41916f790097ac0619a837626caefb0ff5d926ea8e5cdedf5dbc1c80292fd1 root@cuiyongchao:~#
① kubeadm 执行初始化前的检查。
② 生成 token 和证书。
③ 生成 KubeConfig 文件,kubelet 须要这个文件与 Master 通讯。
④ 安装 Master 组件,会从 goolge 的 Registry 下载组件的 Docker 镜像,这一步可能会花一些时间,主要取决于网络质量。
⑤ 安装附加组件 kube-proxy 和 kube-dns。
⑥ Kubernetes Master 初始化成功。
⑦ 提示如何配置 kubectl,后面会实践。
⑧ 提示如何安装 Pod 网络,后面会实践。
⑨ 提示如何注册其余节点到 Cluster,后面会实践。
kubectl 是管理 Kubernetes Cluster 的命令行工具,前面咱们已经在全部的节点安装了 kubectl。Master 初始化完成后须要作一些配置工做,而后 kubectl 就能使用了。依照 kubeadm init
输出的第 ⑦ 步提示,推荐用 Linux 普通用户执行 kubectl(root 会有一些问题)。
咱们为 ubuntu 用户配置 kubectl:
su - ubuntu rm -rf $HOME/.kube sudo mkdir -p $HOME/.kube sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config sudo chown $(id -u):$(id -g) $HOME/.kube/config 为了使用更便捷,启用 kubectl 命令的自动补全功能。 echo "source <(kubectl completion bash)" >> ~/.bashrc 这样 ubuntu 用户就能够使用 kubectl 了。
这样 ubuntu 用户就能够使用 kubectl 了。