使用kubeadm安装Kubernetes 1.7
2017-07-04
kubeadm是Kubernetes官方提供的快速安装和初始化Kubernetes集群的工具,目前的还处于孵化开发状态,伴随Kubernetes每个版本的发布都会同步更新。 当然,目前的kubeadm是不能用于生产环境的。 但伴随着Kubernetes每次版本升级,kubeadm都会对集群配置方面的一些实践做调整,通过实验kubeadm我们可以学习到Kubernetes官方在集群配置上一些新的最佳实践。 例如从Kubernetes 1.6开始kubeadm的目标就是可以快速初始化一个更加安全的Kubernetes集群,Kubernetes 1.6进入beat的RBAC,在这版的kubeadm就有了集成。 目前,我们一套Kubernetes 1.6环境的部署,就是先根据kubeadm中的配置实践整理,再编写成ansible role部署的:
下面回到正题中来,Kubernetes 1.7已经发布了,为了跟上官方更新的脚步,接下来体验一下Kubernetes 1.7中的kubeadm。
0.更新 #
- 2017/07/04 首次使用kubeadm 1.7.0遇到问题,向集群中添加新的Node时报Token不合法的问题(issue 335 和PR 48480),按照issue 335中描述的通过手动创建role和rolebinding解决。
- 2017/07/15 重新使用kubeadm 1.7.1体验安装流程,向集群中添加新的Node时报
[preflight] WARNING: hostname "" could not be reached
,无法完成安装。 stackoverflow上有人遇到了相同的问题。 - 2017/07/25 kubernetes 1.7.2已经发布了,这回体验kubeadm 1.7.2的安装流程,以上问题依旧,无法完成安装。 github上有这个issuekubeadm cannot work with machines that cannot resolve their own host。看来需要继续等kubeadm 1.7.3了。
- 2017/08/03 kubernetes 1.7.3已经发布,这回体验kubeadm 1.7.3的安装流程。
1.准备 #
1.1系统配置 #
在安装之前,需要先做如下准备。两台CentOS 7主机如下:
1cat /etc/hosts
2192.168.61.91 k8s-node1
3192.168.61.92 k8s-node2
如果各个主机启用了防火墙,需要开放Kubernetes各个组件所需要的端口,可以查看Installing kubeadm中的"Check required ports"一节。 这里简单起见在各节点禁用防火墙:
1systemctl stop firewalld
2systemctl disable firewalld
创建/etc/sysctl.d/k8s.conf文件,添加如下内容:
1net.bridge.bridge-nf-call-ip6tables = 1
2net.bridge.bridge-nf-call-iptables = 1
执行sysctl -p /etc/sysctl.d/k8s.conf使修改生效。
禁用SELINUX:
1setenforce 0
1vi /etc/selinux/config
2SELINUX=disabled
1.2安装Docker #
Kubernetes官方还没有针对docker 1.13和docker 17.03以上版本做测试和验证,但由于我们线上环境的Kubernetes 1.6集群中已经使用了docker 17.03,所以这里决定体验一下Docker的最新版本。 接下来在各节点安装最新版本的Docker。
1yum install -y yum-utils device-mapper-persistent-data lvm2
2yum-config-manager \
3 --add-repo \
4 https://download.docker.com/linux/centos/docker-ce.repo
查看最新的Docker版本:
1yum list docker-ce.x86_64 --showduplicates |sort -r
2docker-ce.x86_64 17.06.0.ce-1.el7.centos docker-ce-stable
3docker-ce.x86_64 17.03.2.ce-1.el7.centos docker-ce-stable
4docker-ce.x86_64 17.03.1.ce-1.el7.centos docker-ce-stable
5docker-ce.x86_64 17.03.0.ce-1.el7.centos docker-ce-stable
可以看出最新版本已经是docker 17.06了,安装这个版本:
1yum makecache fast
2yum install docker-ce-17.06.0.ce-1.el7.centos
3
4systemctl start docker
5systemctl enable docker
Docker从1.13版本开始调整了默认的防火墙规则,禁用了iptables filter表中FOWARD链,这样会引起Kubernetes集群中跨Node的Pod无法通信,在各个Docker节点执行下面的命令:
1iptables -P FORWARD ACCEPT
2.安装kubeadm和kubelet #
下面在各节点安装kubeadm和kubelet:
1cat <<EOF > /etc/yum.repos.d/kubernetes.repo
2[kubernetes]
3name=Kubernetes
4baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
5enabled=1
6gpgcheck=1
7repo_gpgcheck=1
8gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg
9 https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
10EOF
测试地址https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64是否可用,如果不可用需要科学上网。
1curl https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
注意kubernetes yum repo的地址对比使用kubeadm安装Kubernetes 1.6时发生了变化
查看kubeadm, kubelet, kubectl, kubernets-cni的最新版本:
1yum list kubeadm --showduplicates |sort -r
2kubeadm.x86_64 1.7.3-1 kubernetes
3kubeadm.x86_64 1.7.2-0 kubernetes
4kubeadm.x86_64 1.7.2-0 @kubernetes
5kubeadm.x86_64 1.7.1-0 kubernetes
6kubeadm.x86_64 1.7.0-0 kubernetes
7kubeadm.x86_64 1.6.7-0 kubernetes
8...
9
10
11yum list kubelet --showduplicates |sort -r
12kubelet.x86_64 1.7.3-1 kubernetes
13kubelet.x86_64 1.7.2-0 kubernetes
14kubelet.x86_64 1.7.2-0 @kubernetes
15kubelet.x86_64 1.7.1-0 kubernetes
16kubelet.x86_64 1.7.0-0 kubernetes
17kubelet.x86_64 1.6.7-0 kubernetes
18...
19
20
21yum list kubectl --showduplicates |sort -r
22kubectl.x86_64 1.7.3-1 kubernetes
23kubectl.x86_64 1.7.2-0 kubernetes
24kubectl.x86_64 1.7.2-0 @kubernetes
25kubectl.x86_64 1.7.1-0 kubernetes
26kubectl.x86_64 1.7.0-0 kubernetes
27kubectl.x86_64 1.6.7-0 kubernetes
28...
kubeadm, kubelet, kubectl已经是1.7.3,就是我们要安装的版本,直接安装即可:
1yum makecache fast
2yum install -y kubelet kubeadm kubectl
3
4...
5Installed:
6 kubeadm.x86_64 0:1.7.3-1 kubectl.x86_64 0:1.7.3-1 kubelet.x86_64 0:1.7.3-1
7
8Dependency Installed:
9 kubernetes-cni.x86_64 0:0.5.1-0 socat.x86_64 0:1.7.2.2-5.el7
- 从安装结果可以看出还安装了kubernetes-cni和socat两个依赖: * 可以看出官方Kubernetes 1.7依赖的cni还是0.5.1版本 * socat是kubelet的依赖
- 我们之前在Kubernetes 1.6 高可用集群部署中手动安装这两个依赖的
在各节点开机启动kubelet服务:
1systemctl enable kubelet.service
3.使用kubeadm init初始化集群 #
接下来使用kubeadm初始化集群,选择k8s-node1作为Master Node,在k8s-node1上执行下面的命令:
1kubeadm init \
2 --kubernetes-version=v1.7.3 \
3 --pod-network-cidr=10.244.0.0/16 \
4 --apiserver-advertise-address=192.168.61.91
因为我们选择flannel作为Pod网络插件,所以上面的命令指定–pod-network-cidr=10.244.0.0/16。
第一次运行上面的init命令输出卡在了:
1...
2[apiclient] Created API client, waiting for the control plane to become ready
通过查看/var/log/message发现如下日志:
1error: failed to run Kubelet: failed to create kubelet:
2misconfiguration: kubelet cgroup driver: "systemd" is different from docker cgroup driver: "cgroupfs"
使用docker info
打印docker信息:
1docker info
2......
3Server Version: 17.06.0-ce
4......
5Cgroup Driver: cgroupfs
可以看出docker 17.06使用的Cgroup Driver为cgroupfs。 查看kubelet的 /etc/systemd/system/kubelet.service.d/10-kubeadm.conf文件,其中包含如下内容:
1Environment="KUBELET_CGROUP_ARGS=--cgroup-driver=systemd"
查看了一下kubelet的启动参数:
1--cgroup-driver string Driver that the kubelet uses to manipulate cgroups on the host.
2 Possible values: 'cgroupfs', 'systemd' (default "cgroupfs")
默认值为cgroupfs,但是我们注意到yum安装kubelet,kubeadm时生成10-kubeadm.conf文件中将这个参数值改成了systemd。
于是修改各节点docker的cgroup driver使其和kubelet一致,即修改或创建/etc/docker/daemon.json,加入下面的内容:
1{
2 "exec-opts": ["native.cgroupdriver=systemd"]
3}
重启docker:
1systemctl restart docker
2systemctl status docker
前面集群初始化遇到问题,使用下面的命令进行清理:
1kubeadm reset
2ifconfig cni0 down
3ip link delete cni0
4ifconfig flannel.1 down
5ip link delete flannel.1
6rm -rf /var/lib/cni/
清理完成后,再重新初始化:
1kubeadm init \
2> --kubernetes-version=v1.7.3 \
3> --pod-network-cidr=10.244.0.0/16 \
4> --apiserver-advertise-address=192.168.61.91
5[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.
6[init] Using Kubernetes version: v1.7.3
7[init] Using Authorization modes: [Node RBAC]
8[preflight] Running pre-flight checks
9[preflight] WARNING: docker version is greater than the most recently validated version. Docker version: 17.06.0-ce. Max validated version: 1.12
10[kubeadm] WARNING: starting in 1.8, tokens expire after 24 hours by default (if you require a non-expiring token use --token-ttl 0)
11[certificates] Generated CA certificate and key.
12[certificates] Generated API server certificate and key.
13[certificates] API Server serving cert is signed for DNS names [k8s-node1 kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.61.91]
14[certificates] Generated API server kubelet client certificate and key.
15[certificates] Generated service account token signing key and public key.
16[certificates] Generated front-proxy CA certificate and key.
17[certificates] Generated front-proxy client certificate and key.
18[certificates] Valid certificates and keys now exist in "/etc/kubernetes/pki"
19[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf"
20[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
21[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/controller-manager.conf"
22[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/scheduler.conf"
23[apiclient] Created API client, waiting for the control plane to become ready
24[apiclient] All control plane components are healthy after 490.016471 seconds
25[token] Using token: 78bf97.8b83ab4d9933076f
26[apiconfig] Created RBAC rules
27[addons] Applied essential addon: kube-proxy
28[addons] Applied essential addon: kube-dns
29
30Your Kubernetes master has initialized successfully!
31
32To start using your cluster, you need to run (as a regular user):
33
34 mkdir -p $HOME/.kube
35 sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
36 sudo chown $(id -u):$(id -g) $HOME/.kube/config
37
38You should now deploy a pod network to the cluster.
39Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
40 http://kubernetes.io/docs/admin/addons/
41
42You can now join any number of machines by running the following on each node
43as root:
44
45 kubeadm join --token 78bf97.8b83ab4d9933076f 192.168.61.91:6443
这次初始化成功了,上面记录了完成的初始化输出的内容。
其中由以下关键内容:
- kubeadm 1.7当前还处于beta状态,还不能用于生产环境。目前来看这东西安装的etcd和apiserver都是单节点,当然不能用于生产环境。
- kubeadm 1.7仍然启用了集群安全的RBAC机制
- 输出内容中还给我们报了Docker版本过高的警告,当然这里只是体验。Kubernetes官方还没有针对docker 1.13和docker 17.03以上版本做测试和验证,所以还是应该官方推荐的Docker 1.12版本。ps:我们目前有一套环境使用的docker 17.03。
- 接下来是生成证书和相关的kubeconfig文件,这个目前我们在Kubernetes 1.6 高可用集群部署也是这么做的,目前没看出有什么新东西
- 生成token记录下来,后边使用
kubeadm join
往集群中添加节点时会用到 - 另外注意kubeadm还报了
starting in 1.8, tokens expire after 24 hours by default (if you require a non-expiring token use --token-ttl 0)
的警告 - 下面的命令是配置常规用户如何使用kubectl访问集群:
1mkdir -p $HOME/.kube 2sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config 3sudo chown $(id -u):$(id -g) $HOME/.kube/config
- 最后给出了将节点加入集群的命令
kubeadm join --token d3c777.2556db2a398bc913 192.168.61.91:6443
查看一下集群状态:
1kubectl get cs
2NAME STATUS MESSAGE ERROR
3scheduler Healthy ok
4controller-manager Healthy ok
5etcd-0 Healthy {"health": "true"}
确认个组件都处于healthy状态。
4.安装Pod Network #
接下来安装flannel network add-on:
1mkdir -p ~/k8s/
2wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel-rbac.yml
3wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
4kubectl create -f kube-flannel-rbac.yml
5kubectl apply -f kube-flannel.yml
6serviceaccount "flannel" created
7configmap "kube-flannel-cfg" created
8daemonset "kube-flannel-ds" created
这里注意flannel 0.8.0已经发布,kube-flannel.yml这个文件里的flannel的镜像是0.8.0,quay.io/coreos/flannel:v0.8.0-amd64
如果Node有多个网卡的话,参考flannel issues 39701,目前需要在kube-flannel.yml中使用--iface
参数指定集群主机内网网卡的名称,否则可能会出现dns无法解析。需要将kube-flannel.yml下载到本地,flanneld启动参数加上--iface=<iface-name>
1......
2apiVersion: extensions/v1beta1
3kind: DaemonSet
4metadata:
5 name: kube-flannel-ds
6......
7containers:
8 - name: kube-flannel
9 image: quay.io/coreos/flannel:v0.8.0-amd64
10 command: [ "/opt/bin/flanneld", "--ip-masq", "--kube-subnet-mgr", "--iface=eth1" ]
11......
使用kubectl get pod --all-namespaces -o wide
确保所有的Pod都处于Running状态。
1kubectl get pod --all-namespaces -o wide
5.master node参与工作负载 #
使用kubeadm初始化的集群,出于安全考虑Pod不会被调度到Master Node上,也就是说Master Node不参与工作负载。
这里搭建的是测试环境可以使用下面的命令使Master Node参与工作负载:
1kubectl taint nodes k8s-node1 node-role.kubernetes.io/master-
2node "k8s-node1" untainted
6.测试DNS #
1kubectl run curl --image=radial/busyboxplus:curl -i --tty
2If you don't see a command prompt, try pressing enter.
3[ root@curl-2716574283-xr8zd:/ ]$
进入后执行nslookup kubernetes.default确认解析正常:
1nslookup kubernetes.default
2Server: 10.96.0.10
3Address 1: 10.96.0.10 kube-dns.kube-system.svc.cluster.local
4
5Name: kubernetes.default
6Address 1: 10.96.0.1 kubernetes.default.svc.cluster.local
7.向Kubernetes集群添加Node #
下面我们将k8s-node2这个主机添加到Kubernetes集群中,在k8s-node2上执行:
1kubeadm join --token 78bf97.8b83ab4d9933076f 192.168.61.91:6443
2[kubeadm] WARNING: kubeadm is in beta, please do not use it for production clusters.
3[preflight] Running pre-flight checks
4[preflight] WARNING: docker version is greater than the most recently validated version. Docker version: 17.06.0-ce. Max validated version: 1.12
5[discovery] Trying to connect to API Server "192.168.61.91:6443"
6[discovery] Created cluster-info discovery client, requesting info from "https://192.168.61.91:6443"
7[discovery] Cluster info signature and contents are valid, will use API Server "https://192.168.61.91:6443"
8[discovery] Successfully established connection with API Server "192.168.61.91:6443"
9[bootstrap] Detected server version: v1.7.3
10[bootstrap] The server supports the Certificates API (certificates.k8s.io/v1beta1)
11[csr] Created API client to obtain unique certificate for this node, generating keys and certificate signing request
12[csr] Received signed certificate from the API server, generating KubeConfig...
13[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
14
15Node join complete:
16* Certificate signing request sent to master and response
17 received.
18* Kubelet informed of new secure connection details.
19
20Run 'kubectl get nodes' on the master to see this machine join.
这次很是顺利,下面在master节点上执行命令查看集群中的节点:
1kubectl get nodes
2NAME STATUS AGE VERSION
3k8s-node1 Ready 16m v1.7.3
4k8s-node2 Ready 5m v1.7.3
如何从集群中移除Node #
如果需要从集群中移除k8s-node2这个Node执行下面的命令:
在master节点上执行:
1kubectl drain k8s-node2 --delete-local-data --force --ignore-daemonsets
2kubectl delete node k8s-node2
在k8s-node2上执行:
1kubeadm reset
2ifconfig cni0 down
3ip link delete cni0
4ifconfig flannel.1 down
5ip link delete flannel.1
6rm -rf /var/lib/cni/
8.dashboard插件部署 #
1mkdir -p ~/k8s/
2wget https://raw.githubusercontent.com/kubernetes/dashboard/master/src/deploy/kubernetes-dashboard.yaml
3kubectl create -f kubernetes-dashboard.yaml
注意当前dashboard的版本已经是1.6.3了
9.heapster插件部署 #
下面安装Heapster为集群添加使用统计和监控功能,为Dashboard添加仪表盘。 使用InfluxDB做为Heapster的后端存储,开始部署:
1mkdir -p ~/k8s/heapster
2cd ~/k8s/heapster
3wget https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/grafana.yaml
4wget https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/rbac/heapster-rbac.yaml
5wget https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/heapster.yaml
6wget https://raw.githubusercontent.com/kubernetes/heapster/master/deploy/kube-config/influxdb/influxdb.yaml
7
8kubectl create -f ./
最后确认所有的pod都处于running状态,打开Dashboard,集群的使用统计会以仪表盘的形式显示出来。
本次安装涉及到的Docker镜像:
1gcr.io/google_containers/kube-apiserver-amd64:v1.7.3
2gcr.io/google_containers/kube-controller-manager-amd64:v1.7.3
3gcr.io/google_containers/kube-scheduler-amd64:v1.7.3
4gcr.io/google_containers/kube-proxy-amd64:v1.7.3
5quay.io/coreos/flannel:v0.8.0-amd64
6gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.4
7gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.4
8gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.4
9gcr.io/google_containers/etcd-amd64:3.0.17
10gcr.io/google_containers/pause-amd64:3.0
11gcr.io/google_containers/kube-proxy-amd64:v1.7.3
12gcr.io/google_containers/kubernetes-dashboard-amd64:v1.6.3
13gcr.io/google_containers/heapster-amd64:v1.3.0
14gcr.io/google_containers/heapster-grafana-amd64:v4.4.1
15gcr.io/google_containers/heapster-influxdb-amd64:v1.1.1