kubeadm是Kubernetes官方提供的用于快速安装Kubernetes集群的工具,伴随Kubernetes每个版本的发布都会同步更新,kubeadm会对集群配置方面的一些实践做调整,通过实验kubeadm可以学习到Kubernetes官方在集群配置上一些新的最佳实践。

在Kubernetes的文档Using kubeadm to Create a Cluster中已经给出了目前kubeadm的主要特性已经处于beta状态了,在2018年将进入GA状态,说明kubeadm离可以在生产环境中使用的距离越来越近了。

当然我们线上稳定运行的Kubernetes集群是使用ansible以二进制形式的部署的高可用集群,这里体验Kubernetes 1.11中的kubeadm是为了跟随官方对集群初始化和配置方面的最佳实践,进一步完善我们的ansible部署脚本。

1.准备

1.1系统配置

在安装之前,需要先做如下准备。两台CentOS 7.4主机如下:

1cat /etc/hosts
2192.168.61.11 node1
3192.168.61.12 node2

如果各个主机启用了防火墙,需要开放Kubernetes各个组件所需要的端口,可以查看Installing kubeadm中的"Check required ports"一节。 这里简单起见在各节点禁用防火墙:

1systemctl stop firewalld
2systemctl disable firewalld

禁用SELINUX:

1setenforce 0
1vi /etc/selinux/config
2SELINUX=disabled

创建/etc/sysctl.d/k8s.conf文件,添加如下内容:

1net.bridge.bridge-nf-call-ip6tables = 1
2net.bridge.bridge-nf-call-iptables = 1
3net.ipv4.ip_forward = 1

执行命令使修改生效。

1modprobe br_netfilter
2sysctl -p /etc/sysctl.d/k8s.conf

1.2安装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            18.06.0.ce-3.el7                    docker-ce-stable
 3docker-ce.x86_64            18.03.1.ce-1.el7.centos             docker-ce-stable
 4docker-ce.x86_64            18.03.0.ce-1.el7.centos             docker-ce-stable
 5docker-ce.x86_64            17.12.1.ce-1.el7.centos             docker-ce-stable
 6docker-ce.x86_64            17.12.0.ce-1.el7.centos             docker-ce-stable
 7docker-ce.x86_64            17.09.1.ce-1.el7.centos             docker-ce-stable
 8docker-ce.x86_64            17.09.0.ce-1.el7.centos             docker-ce-stable
 9docker-ce.x86_64            17.06.2.ce-1.el7.centos             docker-ce-stable
10docker-ce.x86_64            17.06.1.ce-1.el7.centos             docker-ce-stable
11docker-ce.x86_64            17.06.0.ce-1.el7.centos             docker-ce-stable
12docker-ce.x86_64            17.03.2.ce-1.el7.centos             docker-ce-stable
13docker-ce.x86_64            17.03.1.ce-1.el7.centos             docker-ce-stable
14docker-ce.x86_64            17.03.0.ce-1.el7.centos             docker-ce-stable

Kubernetes 1.10已经针对Docker的1.11, 1.12, 1.13.1和17.03等版本做了验证,需要注意Kubernetes 1.10最低支持的Docker版本是1.11。 我们这里在各节点安装docker的17.03.2版本。

1yum makecache fast
2
3yum install -y --setopt=obsoletes=0 \
4  docker-ce-17.03.2.ce-1.el7.centos \
5  docker-ce-selinux-17.03.2.ce-1.el7.centos
6
7systemctl start docker
8systemctl enable docker

Docker从1.13版本开始调整了默认的防火墙规则,禁用了iptables filter表中FOWARD链,这样会引起Kubernetes集群中跨Node的Pod无法通信,在各个Docker节点执行下面的命令:

1iptables -P FORWARD ACCEPT

可在docker的systemd unit文件中以ExecStartPost加入上面的命令:

1ExecStartPost=/usr/sbin/iptables -P FORWARD ACCEPT
1systemctl daemon-reload
2systemctl restart docker

2.使用kubeadm部署Kubernetes

2.1 安装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
1yum makecache fast
2yum install -y kubelet kubeadm kubectl
3
4... 
5Installed:
6  kubeadm.x86_64 0:1.11.1-0    kubectl.x86_64 0:1.11.1-0    kubelet.x86_64 0:1.11.1-0
7
8Dependency Installed:
9  cri-tools.x86_64 0:1.11.0-0  kubernetes-cni.x86_64 0:0.6.0-0  socat.x86_64 0:1.7.3.2-2.el7
  • 从安装结果可以看出还安装了cri-tools, kubernetes-cni, socat三个依赖: * 官方从Kubernetes 1.9开始就将cni依赖升级到了0.6.0版本,在当前1.11中仍然是这个版本 * socat是kubelet的依赖
  • 可以看出从Kubernetes 1.10开始安装了cri-tools,这是CRI(Container Runtime Interface)容器运行时接口的命令行工具

运行kubelet --help可以看到原来kubelet的绝大多数命令行flag参数都被DEPRECATED了,如:

1......
2--address 0.0.0.0   The IP address for the Kubelet to serve on (set to 0.0.0.0 for all IPv4 interfaces and `::` for all IPv6 interfaces) (default 0.0.0.0) (DEPRECATED: This parameter should be set via the config file specified by the Kubelet's --config flag. See https://kubernetes.io/docs/tasks/administer-cluster/kubelet-config-file/ for more information.)
3......

而官方推荐我们使用--config指定配置文件,并在配置文件中指定原来这些flag所配置的内容。具体内容可以查看这里Set Kubelet parameters via a config file。这也是Kubernetes为了支持动态Kubelet配置(Dynamic Kubelet Configuration)才这么做的,参考Reconfigure a Node’s Kubelet in a Live Cluster

kubelet的配置文件必须是json或yaml格式,具体可查看这里

Kubernetes 1.8开始要求关闭系统的Swap,如果不关闭,默认配置下kubelet将无法启动。

关闭系统的Swap方法如下:

1swapoff -a

修改 /etc/fstab 文件,注释掉 SWAP 的自动挂载,使用free -m确认swap已经关闭。 swappiness参数调整,修改/etc/sysctl.d/k8s.conf添加下面一行:

1vm.swappiness=0

执行sysctl -p /etc/sysctl.d/k8s.conf使修改生效。

因为这里本次用于测试两台主机上还运行其他服务,关闭swap可能会对其他服务产生影响,所以这里修改kubelet的配置去掉这个限制。 之前的Kubernetes版本我们都是通过kubelet的启动参数--fail-swap-on=false去掉这个限制的。前面已经分析了Kubernetes不再推荐使用启动参数,而推荐使用配置文件。 所以这里我们改成配置文件配置的形式。

查看/etc/systemd/system/kubelet.service.d/10-kubeadm.conf,看到了下面的内容:

 1# Note: This dropin only works with kubeadm and kubelet v1.11+
 2[Service]
 3Environment="KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf"
 4Environment="KUBELET_CONFIG_ARGS=--config=/var/lib/kubelet/config.yaml"
 5# This is a file that "kubeadm init" and "kubeadm join" generates at runtime, populating the KUBELET_KUBEADM_ARGS variable dynamically
 6EnvironmentFile=-/var/lib/kubelet/kubeadm-flags.env
 7# This is a file that the user can use for overrides of the kubelet args as a last resort. Preferably, the user should use
 8# the .NodeRegistration.KubeletExtraArgs object in the configuration files instead. KUBELET_EXTRA_ARGS should be sourced from this file.
 9EnvironmentFile=-/etc/sysconfig/kubelet
10ExecStart=
11ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_CONFIG_ARGS $KUBELET_KUBEADM_ARGS $KUBELET_EXTRA_ARGS

上面显示kubeadm部署的kubelet的配置文件--config=/var/lib/kubelet/config.yaml,实际去查看/var/lib/kubelet和这个config.yaml的配置文件都没有被创建。 可以猜想肯定是运行kubeadm初始化集群时会自动生成这个配置文件,而如果我们不关闭Swap的话,第一次初始化集群肯定会失败的。

1kind: KubeletConfiguration
2apiVersion: kubelet.config.k8s.io/v1beta1
3failSwapOn: false

所以还是老老实实的回到使用kubelet的启动参数--fail-swap-on=false去掉必须关闭Swap的限制。 修改/etc/sysconfig/kubelet,加入:

1KUBELET_EXTRA_ARGS=--fail-swap-on=false

2.2 使用kubeadm init初始化集群

在各节点开机启动kubelet服务:

1systemctl enable kubelet.service

接下来使用kubeadm初始化集群,选择node1作为Master Node,在node1上执行下面的命令:

1kubeadm init \
2  --kubernetes-version=v1.11.1 \
3  --pod-network-cidr=10.244.0.0/16 \
4  --apiserver-advertise-address=192.168.61.11

因为我们选择flannel作为Pod网络插件,所以上面的命令指定–pod-network-cidr=10.244.0.0/16。

执行时报了下面的错误:

1[init] using Kubernetes version: v1.11.1
2[preflight] running pre-flight checks
3I0808 22:43:43.985513   10870 kernel_validator.go:81] Validating kernel version
4I0808 22:43:43.985694   10870 kernel_validator.go:96] Validating kernel config
5[preflight] Some fatal errors occurred:
6        [ERROR Swap]: running with swap on is not supported. Please disable swap
7[preflight] If you know what you are doing, you can make a check non-fatal with `--ignore-preflight-errors=...`

有一个错误信息是running with swap on is not supported. Please disable swap。因为我们决定配置failSwapOn: false,所以重新添加–ignore-preflight-errors=Swap参数忽略这个错误,重新运行。

 1kubeadm init \
 2   --kubernetes-version=v1.11.1 \
 3   --pod-network-cidr=10.244.0.0/16 \
 4   --apiserver-advertise-address=192.168.61.11 \
 5   --ignore-preflight-errors=Swap
 6
 7[init] using Kubernetes version: v1.11.1
 8[preflight] running pre-flight checks
 9        [WARNING Swap]: running with swap on is not supported. Please disable swap
10I0808 19:09:53.569206    1866 kernel_validator.go:81] Validating kernel version
11I0808 19:09:53.569323    1866 kernel_validator.go:96] Validating kernel config
12[preflight/images] Pulling images required for setting up a Kubernetes cluster
13[preflight/images] This might take a minute or two, depending on the speed of your internet connection
14[preflight/images] You can also perform this action in beforehand using 'kubeadm config images pull'
15[kubelet] Writing kubelet environment file with flags to file "/var/lib/kubelet/                                                      kubeadm-flags.env"
16[kubelet] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
17[preflight] Activating the kubelet service
18[certificates] Generated ca certificate and key.
19[certificates] Generated apiserver certificate and key.
20[certificates] apiserver serving cert is signed for DNS names [node1 kubernetes                                                       kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.61.11]
21[certificates] Generated apiserver-kubelet-client certificate and key.
22[certificates] Generated sa key and public key.
23[certificates] Generated front-proxy-ca certificate and key.
24[certificates] Generated front-proxy-client certificate and key.
25[certificates] Generated etcd/ca certificate and key.
26[certificates] Generated etcd/server certificate and key.
27[certificates] etcd/server serving cert is signed for DNS names [node1 localhost] and IPs [127.0.0.1 ::1]
28[certificates] Generated etcd/peer certificate and key.
29[certificates] etcd/peer serving cert is signed for DNS names [node1 localhost] and IPs [192.168.61.11 127.0.0.1 ::1]
30[certificates] Generated etcd/healthcheck-client certificate and key.
31[certificates] Generated apiserver-etcd-client certificate and key.
32[certificates] valid certificates and keys now exist in "/etc/kubernetes/pki"
33[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/admin.conf"
34[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/kubelet.conf"
35[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/controller-manager.conf"
36[kubeconfig] Wrote KubeConfig file to disk: "/etc/kubernetes/scheduler.conf"
37[controlplane] wrote Static Pod manifest for component kube-apiserver to "/etc/kubernetes/manifests/kube-apiserver.yaml"
38[controlplane] wrote Static Pod manifest for component kube-controller-manager to "/etc/kubernetes/manifests/kube-controller-manager.yaml"
39[controlplane] wrote Static Pod manifest for component kube-scheduler to "/etc/kubernetes/manifests/kube-scheduler.yaml"
40[etcd] Wrote Static Pod manifest for a local etcd instance to "/etc/kubernetes/manifests/etcd.yaml"
41[init] waiting for the kubelet to boot up the control plane as Static Pods from directory "/etc/kubernetes/manifests"
42[init] this might take a minute or longer if the control plane images have to be pulled
43[apiclient] All control plane components are healthy after 44.502604 seconds
44[uploadconfig] storing the configuration used in ConfigMap "kubeadm-config" in the "kube-system" Namespace
45[kubelet] Creating a ConfigMap "kubelet-config-1.11" in namespace kube-system with the configuration for the kubelets in the cluster
46[markmaster] Marking the node node1 as master by adding the label "node-role.kubernetes.io/master=''"
47[markmaster] Marking the node node1 as master by adding the taints [node-role.kubernetes.io/master:NoSchedule]
48[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "node1" as an annotation
49[bootstraptoken] using token: 1v7mpt.52vss1lhp4v6eqke
50[bootstraptoken] configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
51[bootstraptoken] configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
52[bootstraptoken] configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
53[bootstraptoken] creating the "cluster-info" ConfigMap in the "kube-public" namespace
54[addons] Applied essential addon: CoreDNS
55[addons] Applied essential addon: kube-proxy
56
57Your Kubernetes master has initialized successfully!
58
59To start using your cluster, you need to run the following as a regular user:
60
61  mkdir -p $HOME/.kube
62  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
63  sudo chown $(id -u):$(id -g) $HOME/.kube/config
64
65You should now deploy a pod network to the cluster.
66Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
67  https://kubernetes.io/docs/concepts/cluster-administration/addons/
68
69You can now join any number of machines by running the following on each node
70as root:
71
72  kubeadm join 192.168.61.11:6443 --token 1v7mpt.52vss1lhp4v6eqke --discovery-token-ca-cert-hash sha256:b7161b219514e5943cf4dfd5e20851fb1dc98185b94bf600356cd2a3a4a91782

上面记录了完成的初始化输出的内容,根据输出的内容基本上可以看出手动初始化安装一个Kubernetes集群所需要的关键步骤。

其中有以下关键内容:

  • [kubelet] 生成kubelet的配置文件"/var/lib/kubelet/config.yaml"
  • [certificates]生成相关的各种证书
  • [kubeconfig]接下来是生成证书和相关的kubeconfig文件,这个目前我们在Kubernetes 1.6 高可用集群部署也是这么做的
  • [bootstraptoken]生成token记录下来,后边使用kubeadm join往集群中添加节点时会用到
  • 下面的命令是配置常规用户如何使用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 192.168.61.11:6443 --token 1v7mpt.52vss1lhp4v6eqke --discovery-token-ca-cert-hash sha256:b7161b219514e5943cf4dfd5e20851fb1dc98185b94bf600356cd2a3a4a91782

查看一下集群状态:

1NAME                 STATUS    MESSAGE              ERROR
2scheduler            Healthy   ok
3controller-manager   Healthy   ok
4etcd-0               Healthy   {"health": "true"}

确认个组件都处于healthy状态。

集群初始化如果遇到问题,可以使用下面的命令进行清理:

1kubeadm reset
2ifconfig cni0 down
3ip link delete cni0
4ifconfig flannel.1 down
5ip link delete flannel.1
6rm -rf /var/lib/cni/

2.3 安装Pod Network

接下来安装flannel network add-on:

 1mkdir -p ~/k8s/
 2cd ~/k8s
 3wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
 4kubectl apply -f  kube-flannel.yml
 5
 6clusterrole.rbac.authorization.k8s.io/flannel created
 7clusterrolebinding.rbac.authorization.k8s.io/flannel created
 8serviceaccount/flannel created
 9configmap/kube-flannel-cfg created
10daemonset.extensions/kube-flannel-ds-amd64 created
11daemonset.extensions/kube-flannel-ds-arm64 created
12daemonset.extensions/kube-flannel-ds-arm created
13daemonset.extensions/kube-flannel-ds-ppc64le created
14daemonset.extensions/kube-flannel-ds-s390x created

这里注意kube-flannel.yml这个文件里的flannel的镜像是0.10.0,quay.io/coreos/flannel:v0.10.0-amd64

如果Node有多个网卡的话,参考flannel issues 39701,目前需要在kube-flannel.yml中使用--iface参数指定集群主机内网网卡的名称,否则可能会出现dns无法解析。需要将kube-flannel.yml下载到本地,flanneld启动参数加上--iface=<iface-name>

 1......
 2containers:
 3      - name: kube-flannel
 4        image: quay.io/coreos/flannel:v0.10.0-amd64
 5        command:
 6        - /opt/bin/flanneld
 7        args:
 8        - --ip-masq
 9        - --kube-subnet-mgr
10        - --iface=eth1
11......

使用kubectl get pod --all-namespaces -o wide确保所有的Pod都处于Running状态。

 1kubectl get pod --all-namespaces -o wide
 2NAMESPACE     NAME                            READY     STATUS    RESTARTS   AGE       IP              NODE
 3kube-system   coredns-78fcdf6894-46nwh        1/1       Running   0          12m       10.244.0.2      node1
 4kube-system   coredns-78fcdf6894-vf85c        1/1       Running   0          12m       10.244.0.3      node1
 5kube-system   etcd-node1                      1/1       Running   0          12m       192.168.61.11   node1
 6kube-system   kube-apiserver-node1            1/1       Running   0          12m       192.168.61.11   node1
 7kube-system   kube-controller-manager-node1   1/1       Running   0          12m       192.168.61.11   node1
 8kube-system   kube-flannel-ds-amd64-2fjp6     1/1       Running   0          2m        192.168.61.11   node1
 9kube-system   kube-proxy-gms8k                1/1       Running   0          12m       192.168.61.11   node1
10kube-system   kube-scheduler-node1            1/1       Running   0          11m       192.168.61.11   node1

可以看到Kubernetes 1.11已经使用coredns替换了kube-dns

2.4 master node参与工作负载

使用kubeadm初始化的集群,出于安全考虑Pod不会被调度到Master Node上,也就是说Master Node不参与工作负载。这是因为当前的master节点node1被打上了node-role.kubernetes.io/master:NoSchedule的污点:

1kubectl describe node node1 | grep Taint
2Taints:             node-role.kubernetes.io/master:NoSchedule

因为这里搭建的是测试环境,去掉这个污点使node1参与工作负载:

1kubectl taint nodes node1 node-role.kubernetes.io/master-
2node "node1" untainted

2.5 测试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

2.6 向Kubernetes集群中添加Node节点

下面我们将node2这个主机添加到Kubernetes集群中,因为我们同样在node2上的kubelet的启动参数中去掉了必须关闭swap的限制,所以同样需要--ignore-preflight-errors=Swap这个参数。 在node2上执行:

 1kubeadm join 192.168.61.11:6443 --token 1v7mpt.52vss1lhp4v6eqke --discovery-token-ca-cert-hash sha256:b7161b219514e5943cf4dfd5e20851fb1dc98185b94bf600356cd2a3a4a91782 \
 2 --ignore-preflight-errors=Swap
 3
 4......
 5[discovery] Trying to connect to API Server "192.168.61.11:6443"
 6[discovery] Created cluster-info discovery client, requesting info from "https://192.168.61.11:6443"
 7[discovery] Requesting info from "https://192.168.61.11:6443" again to validate TLS against the pinned public key
 8[discovery] Cluster info signature and contents are valid and TLS certificate validates against pinned roots, will use API Server "192.168.61.11:6443"
 9[discovery] Successfully established connection with API Server "192.168.61.11:6443"
10[kubelet] Downloading configuration for the kubelet from the "kubelet-config-1.11" ConfigMap in the kube-system namespace
11[kubelet] Writing kubelet configuration to file "/var/lib/kubelet/config.yaml"
12[kubelet] Writing kubelet environment file with flags to file "/var/lib/kubelet/kubeadm-flags.env"
13[preflight] Activating the kubelet service
14[tlsbootstrap] Waiting for the kubelet to perform the TLS Bootstrap...
15[patchnode] Uploading the CRI Socket information "/var/run/dockershim.sock" to the Node API object "node2" as an annotation
16
17This node has joined the cluster:
18* Certificate signing request was sent to master and a response
19  was received.
20* The Kubelet was informed of the new secure connection details.
21
22Run 'kubectl get nodes' on the master to see this node join the cluster.

node2加入集群很是顺利,下面在master节点上执行命令查看集群中的节点:

1kubectl get nodes
2NAME      STATUS    ROLES     AGE       VERSION
3node1     Ready     master    26m       v1.11.1
4node2     Ready     <none>    2m        v1.11.1

如何从集群中移除Node

如果需要从集群中移除node2这个Node执行下面的命令:

在master节点上执行:

1kubectl drain node2 --delete-local-data --force --ignore-daemonsets
2kubectl delete node node2

在node2上执行:

1kubeadm reset
2ifconfig cni0 down
3ip link delete cni0
4ifconfig flannel.1 down
5ip link delete flannel.1
6rm -rf /var/lib/cni/

3.Kubernetes常用组件部署

越来越多的公司和团队开始使用Helm这个Kubernetes的包管理器,我们也将使用Helm安装Kubernetes的常用组件。

3.1 Helm的安装

Helm由客户端命helm令行工具和服务端tiller组成,Helm的安装十分简单。 下载helm命令行工具到master节点node1的/usr/local/bin下,这里下载的2.9.1版本:

1wget https://storage.googleapis.com/kubernetes-helm/helm-v2.9.1-linux-amd64.tar.gz
2tar -zxvf helm-v2.9.1-linux-amd64.tar.gz
3cd linux-amd64/
4cp helm /usr/local/bin/

为了安装服务端tiller,还需要在这台机器上配置好kubectl工具和kubeconfig文件,确保kubectl工具可以在这台机器上访问apiserver且正常使用。 这里的node1节点以及配置好了kubectl。

因为Kubernetes APIServer开启了RBAC访问控制,所以需要创建tiller使用的service account: tiller并分配合适的角色给它。 详细内容可以查看helm文档中的Role-based Access Control。 这里简单起见直接分配cluster-admin这个集群内置的ClusterRole给它。创建rbac-config.yaml文件:

 1apiVersion: v1
 2kind: ServiceAccount
 3metadata:
 4  name: tiller
 5  namespace: kube-system
 6---
 7apiVersion: rbac.authorization.k8s.io/v1beta1
 8kind: ClusterRoleBinding
 9metadata:
10  name: tiller
11roleRef:
12  apiGroup: rbac.authorization.k8s.io
13  kind: ClusterRole
14  name: cluster-admin
15subjects:
16  - kind: ServiceAccount
17    name: tiller
18    namespace: kube-system
1kubectl create -f rbac-config.yaml
2serviceaccount/tiller created
3clusterrolebinding.rbac.authorization.k8s.io/tiller created

接下来使用helm部署tiller:

 1helm init --service-account tiller --skip-refresh
 2Creating /root/.helm
 3Creating /root/.helm/repository
 4Creating /root/.helm/repository/cache
 5Creating /root/.helm/repository/local
 6Creating /root/.helm/plugins
 7Creating /root/.helm/starters
 8Creating /root/.helm/cache/archive
 9Creating /root/.helm/repository/repositories.yaml
10Adding stable repo with URL: https://kubernetes-charts.storage.googleapis.com
11Adding local repo with URL: http://127.0.0.1:8879/charts
12$HELM_HOME has been configured at /root/.helm.
13
14Tiller (the Helm server-side component) has been installed into your Kubernetes Cluster.
15
16Please note: by default, Tiller is deployed with an insecure 'allow unauthenticated users' policy.
17For more information on securing your installation see: https://docs.helm.sh/using_helm/#securing-your-helm-installation
18Happy Helming!

tiller默认被部署在k8s集群中的kube-system这个namespace下:

1kubectl get pod -n kube-system -l app=helm
2NAME                            READY     STATUS    RESTARTS   AGE
3tiller-deploy-759cb9df9-fdg58   1/1       Running   0          2m
1helm version
2Client: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}
3Server: &version.Version{SemVer:"v2.9.1", GitCommit:"20adb27c7c5868466912eebdf6664e7390ebe710", GitTreeState:"clean"}

注意由于某些原因需要网络可以访问gcr.io和kubernetes-charts.storage.googleapis.com,如果无法访问可以通过helm init --service-account tiller --tiller-image <your-docker-registry>/tiller:2.9.1 --skip-refresh使用私有镜像仓库中的tiller镜像

3.2 使用Helm部署Nginx Ingress

为了便于将集群中的服务暴露到集群外部,从集群外部访问,接下来使用Helm将Nginx Ingress部署到Kubernetes上。

ingress-nginx.yaml:

1controller:
2  service:
3    externalIPs:
4      - 192.168.61.11
1helm repo update
2
3helm install stable/nginx-ingress \
4-n nginx-ingress \
5--namespace ingress-nginx  \
6-f ingress-nginx.yaml

如果访问http://192.168.61.11返回default backend,则部署完成:

1curl http://192.168.61.11/
2default backend - 404

3.2 将TLS证书配置到Kubernetes中

当使用Ingress将HTTPS的服务暴露到集群外部时,需要HTTPS证书,这里将*.frognew.com的证书和秘钥配置到Kubernetes中。

1kubectl create secret tls frognew-com-tls-secret --cert=fullchain.pem --key=privkey.pem -n kube-system
2secret/frognew-com-tls-secret created

后边部署在kube-system命名空间中的dashboard要使用这个证书,因此这里同样在kube-system中创建证书的secret

3.3 使用Helm部署dashboard

kubernetes-dashboard.yaml:

 1ingress:
 2  enabled: true
 3  hosts: 
 4    - k8s.frognew.com
 5  annotations:
 6    nginx.ingress.kubernetes.io/ssl-redirect: "true"
 7    nginx.ingress.kubernetes.io/secure-backends: "true"
 8  tls:
 9    - secretName: frognew-com-tls-secret
10      hosts:
11      - k8s.frognew.com
12rbac:
13  clusterAdminRole: true

注意因为Service kubernetes-dashboard启用了https,所以是secure backend,在创建Ingress时需要用annotation指定nginx.ingress.kubernetes.io/secure-backends: "true"。 (更新:ingress-nginx更新的版本中改用nginx.ingress.kubernetes.io/backend-protocol: "HTTPS"代替nginx.ingress.kubernetes.io/secure-backends: "true")

1helm install stable/kubernetes-dashboard \
2-n kubernetes-dashboard \
3--namespace kube-system  \
4-f kubernetes-dashboard.yaml
 1kubectl -n kube-system get secret | grep kubernetes-dashboard-token
 2kubernetes-dashboard-token-tjj25                 kubernetes.io/service-account-token   3         37s
 3
 4kubectl describe -n kube-system secret/kubernetes-dashboard-token-tjj25
 5Name:         kubernetes-dashboard-token-tjj25
 6Namespace:    kube-system
 7Labels:       <none>
 8Annotations:  kubernetes.io/service-account.name=kubernetes-dashboard
 9              kubernetes.io/service-account.uid=d19029f0-9cac-11e8-8d94-080027db403a
10
11Type:  kubernetes.io/service-account-token
12
13Data
14====
15namespace:  11 bytes
16token:      eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC10b2tlbi10amoyNSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6ImQxOTAyOWYwLTljYWMtMTFlOC04ZDk0LTA4MDAyN2RiNDAzYSIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTprdWJlcm5ldGVzLWRhc2hib2FyZCJ9.w1HZrtBOhANdqSRLNs22z8dQWd5IOCpEl9VyWQ6DUwhHfgpAlgdhEjTqH8TT0f4ftu_eSPnnUXWbsqTNDobnlxet6zVvZv1K-YmIO-o87yn2PGIrcRYWkb-ADWD6xUWzb0xOxu2834BFVC6T5p5_cKlyo5dwerdXGEMoz9OW0kYvRpKnx7E61lQmmacEeizq7hlIk9edP-ot5tCuIO_gxpf3ZaEHnspulceIRO_ltjxb8SvqnMglLfq6Bt54RpkUOFD1EKkgWuhlXJ8c9wJt_biHdglJWpu57tvOasXtNWaIzTfBaTiJ3AJdMB_n0bQt5CKAUnKBhK09NP3R0Qtqog

在dashboard的登录窗口使用上面的token登录。

dashboard

3.4 使用Helm部署heapster

heapster已经DEPRECATED,官方推荐使用metrics-server(https://github.com/kubernetes-incubator/metrics-server)。 关于metrics-server的部署可以查看使用kubeadm安装Kubernetes 1.12中的“使用Helm部署metrics-server”。

4.总结

本次安装涉及到的Docker镜像:

 1# kubernetes
 2k8s.gcr.io/kube-apiserver-amd64:v1.11.1
 3k8s.gcr.io/kube-scheduler-amd64:v1.11.1
 4k8s.gcr.io/kube-controller-manager-amd64:v1.11.1
 5k8s.gcr.io/kube-proxy-amd64:v1.11.1
 6k8s.gcr.io/etcd-amd64:3.2.18
 7k8s.gcr.io/pause:3.1
 8
 9# network and dns
10quay.io/coreos/flannel:v0.10.0-amd64
11k8s.gcr.io/coredns:1.1.3
12
13# helm and tiller
14gcr.io/kubernetes-helm/tiller:v2.9.1
15
16# nginx ingress
17quay.io/kubernetes-ingress-controller/nginx-ingress-controller:0.17.1
18k8s.gcr.io/defaultbackend:1.4
19
20# dashboard
21k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.3
  • pause镜像从Kubernetes 1.10开始就已经升级为3.1了
  • Kubernetes 1.11开始使用CoreDNS替换了Kubernetes插件kube-dns

参考