经简单了解,k8s集群部署的方式有不少:node
本文主要包括如下五个步骤:docker
step0.准备工做bootstrap
两台服务器的准备api
step1.每台机器上安装runtime安全
docker的安装bash
step2.在每台机器上安装kubeadm,kubelet和kubectl服务器
基本组件安装网络
step3.建立集群app
在master节点,建立k8s集群dom
step4加入子节点
在子节点操做,加入到master的集群中
step0.准备工做
为了方便学习和实践k8s,须要搭建一套本身的k8s环境,所以在阿里云买了两台机器,信息以下:
地域:美国(弗吉尼亚)
CPU:2核(共享型)
内存:2GB
存储:40GB
宽带:100Mbps按流量计费(0.5/GB)
数量:*2
时间:一个月
总额:183.8元
(ps:k8s官方声明的最低配置是2u2g,因此先买一个月试试,后面若是不须要的话就不续费了)
step1.每台机器上安装runtime
k8s官方从v1.6.0起开始支持CRI,即容器运行时接口。官方提到的CRI有:Docker、CRI-O、Containerd、fracti等等。不过目前Docker几乎成为业界主流的容器引擎,且k8s默认支持它,因此咱们选择使用Docker。
k8s发行说明中跟踪最新验证的Docker版本,所以咱们安装最新的docker 19.03.1-ce
Docker官方出品了一件安装脚本,脚本会自动区分不一样的操做系统。
$ sudo wget -qO- https://get.docker.com/ | bash $ # 若是上面的不行,执行下面两句 $ curl -fsSL https://get.docker.com -o get-docker.sh $ sudo sh get-docker.sh $ # 安装成功执行下面语句,若是有相似回显,说明安装成功 $ docker --version Docker version 18.06.1-ce, build e68fc7a
安装成功截图:
参考:https://zhuanlan.zhihu.com/p/54147784
step2.在每台机器上安装kubeadm,kubelet和kubectl
kubeadm: 用来初始化集群的指令。
kubelet: 在集群中的每一个节点上用来启动 pod 和 container 等。
kubectl: 用来与集群通讯的命令行工具。
官方文档提示,kubeadm不能帮忙管理kubelet或kubectl,所以他们之间的版本必定要一致,否则会出问题。
接下来根据官方文档的提示一步一步操做。
参考: https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/install-kubeadm/
apt-get update && apt-get install -y apt-transport-https curl
curl -s https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list deb https://apt.kubernetes.io/ kubernetes-xenial main EOF
apt-get update
apt-get install -y kubelet kubeadm kubectl
。。。。。。
apt-mark hold kubelet kubeadm kubectl
kubelet 如今每隔几秒就会重启,由于它陷入了一个等待 kubeadm 指令的死循环。
版本信息以下:
若是安装的CRI不是Docker的话,这里还要加一个步骤,在master节点上配置kubelet所需的cgroup驱动。
因为咱们用的是docker因此跳过此步。
step3.建立集群
在master节点使用kubeadm init进行初始化。
这个操做包括了如下11个大步骤
kubeadm init这个命令的参数很是多,官方给出的所有参数以下:
kubeadm init [flags] --apiserver-advertise-address string The IP address the API Server will advertise it's listening on. If not set the default network interface will be used. --apiserver-bind-port int32 Port for the API Server to bind to. (default 6443) --apiserver-cert-extra-sans strings Optional extra Subject Alternative Names (SANs) to use for the API Server serving certificate. Can be both IP addresses and DNS names. --cert-dir string The path where to save and store the certificates. (default "/etc/kubernetes/pki") --certificate-key string Key used to encrypt the control-plane certificates in the kubeadm-certs Secret. --config string Path to a kubeadm configuration file. --cri-socket string Path to the CRI socket to connect. If empty kubeadm will try to auto-detect this value; use this option only if you have more than one CRI installed or if you have non-standard CRI socket. --dry-run Don't apply any changes; just output what would be done. --feature-gates string A set of key=value pairs that describe feature gates for various features. No feature gates are available in this release. -h, --help help for init --ignore-preflight-errors strings A list of checks whose errors will be shown as warnings. Example: 'IsPrivilegedUser,Swap'. Value 'all' ignores errors from all checks. --image-repository string Choose a container registry to pull control plane images from (default "k8s.gcr.io") --kubernetes-version string Choose a specific Kubernetes version for the control plane. (default "stable-1") --node-name string Specify the node name. --pod-network-cidr string Specify range of IP addresses for the pod network. If set, the control plane will automatically allocate CIDRs for every node. --service-cidr string Use alternative range of IP address for service VIPs. (default "10.96.0.0/12") --service-dns-domain string Use alternative domain for services, e.g. "myorg.internal". (default "cluster.local") --skip-certificate-key-print Don't print the key used to encrypt the control-plane certificates. --skip-phases strings List of phases to be skipped --skip-token-print Skip printing of the default bootstrap token generated by 'kubeadm init'. --token string The token to use for establishing bidirectional trust between nodes and control-plane nodes. The format is [a-z0-9]{6}\.[a-z0-9]{16} - e.g. abcdef.0123456789abcdef --token-ttl duration The duration before the token is automatically deleted (e.g. 1s, 2m, 3h). If set to '0', the token will never expire (default 24h0m0s) --upload-certs Upload control-plane certificates to the kubeadm-certs Secret.
这里咱们只选择几个基本参数尝试如下初始化
kubeadm init --apiserver-advertise-address=0.0.0.0 --pod-network-cidr=10.244.0.0/16
获得结果:
显示建立成功,只是在preflight阶段有一个warning,先无论他。
记录下这里的信息,加入worker节点时候会用到。
下一步, api认证。
由于我准备只用root因此运行下面的命令
export KUBECONFIG=/etc/kubernetes/admin.conf
执行了这一步操做后,kubectl已经能够开始工做了,也能够查看一下node的状况。
能够查看node,不过STATUS依然是NotReady状态。
官方文档上面在加入子节点前还给了两个操做选项:
网络pod插件的功能是让pod之间可以进行网络通讯。官方提供了不少的pod网络组件,而咱们在kubeadm init的时候已经选择了Canal网络组件。
如今须要执行这个命令配置net-port
kubectl apply -f https://docs.projectcalico.org/v3.8/manifests/canal.yaml
如今node的status状态也是正常Ready状态了
而控制面节点隔离操做是这样的,本来k8s不在master节点上安排pod编排,而执行控制面隔离后后,master节点上也讲安排pod,这是默认不容许的,也是不经常使用的,由于会引发安全问题。
虽然我如今只有一个机器作子节点,我也不想作这个操做,因此跳过。
整个step3中若是出现了什么错误能够用如下命令充值kubeadm,而后从kubeinit操做开始从新来。
sudo kubeadm reset
如今的namespace和pod状态以下:看时间都是50m左右,所以都是再kubeadm init操做后产生的。
canal是pod网络有关组件,是刚配置好的,因此是新的。这里不能再墨迹了。
step4加入子节点
若是忘记了刚才master节点给出的hash信息,能够执行如下命令从新获取
kubeadm token create --print-join-command
复制获得的命令,在子节点执行
kubeadm token create --print-join-command
成功加入集群
再到master节点查看结果。成功!!!
本文可能有不少描述不清楚或不许确的地方,你们请谅解。
记录k8s安装过程也是为了深刻理解它,之后发现哪里能够补充的,我会再回来补充。
参考:
https://kubernetes.io/zh/docs/setup/independent/install-kubeadm/#%E5%AE%89%E8%A3%85-runtime
https://kubernetes.io/docs/setup/production-environment/tools/kubeadm/create-cluster-kubeadm/
https://blog.csdn.net/weixin_38070561/article/details/82982710