k8s all-in-one 部署过程记录
更新Ubuntu18.04的源
备份:cp /etc/apt/sources.list /etc/apt/sources.list.bak
查看系统代号:lsb_release -c
修改成阿里源(https://opsx.alibaba.com/mirror?lang=zh-cn):vi /etc/apt/sources
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse
更新软件列表
apt-get update
修改k8s的源为阿里源,安装kubelet kubeadm kubectl
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
systemctl enable kubelet && systemctl start kubelet
禁掉swap分区
sudo swapoff -a
#要永久禁掉swap分区,打开如下文件注释掉swap那一行
sudo vi /etc/fstab
可以得到机器的MAC和product_uuid
gemfield@sl:~$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp8s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 60:eb:69:af:b2:76 brd ff:ff:ff:ff:ff:ff
3: wlp5s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DORMANT group default qlen 1000
link/ether 5c:ac:4c:bf:6d:12 brd ff:ff:ff:ff:ff:ff
4: docker0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN mode DEFAULT group default
link/ether 02:42:d2:2f:a7:0b brd ff:ff:ff:ff:ff:ff
gemfield@sl:~$ sudo cat /sys/class/dmi/id/product_uuid
20FD5881-4A7E-11CB-A0FC-C6E1CB03C2A6
确保Kubunetes能正确使用机器的网卡设备,在多网卡的环境下,有必要的时候你要设置路由。
另外,Master Node上需要以下TCP端口:
6443*,Kubernetes API server
2379-2380, etcd server client API
10250, Kubelet API
10251,kube-scheduler
10252,kube-controller-manager
10255,Read-only Kubelet API
而worker node上需要以下TCP端口:
10250,Kubelet API
10255,Read-only Kubelet API
30000-32767,NodePort Services**
下载Kubernetes的相关镜像
因为连不上Google,使用github上别人下载好的镜像。
下载脚本:
#!/bin/bash
images=(kube-proxy-amd64:v1.11.1
kube-scheduler-amd64:v1.11.1
kube-controller-manager-amd64:v1.11.1
kube-apiserver-amd64:v1.11.1
etcd-amd64:3.2.18
coredns:1.1.3
pause:3.1
pause-amd64:3.1
kubernetes-dashboard-amd64:v1.8.3
k8s-dns-sidecar-amd64:1.14.8
k8s-dns-kube-dns-amd64:1.14.8
k8s-dns-dnsmasq-nanny-amd64:1.14.8
)
for image in ${images[@]};do
echo $image
docker pull quxf2012/$image
#docker tag quxf2012/$image gcr.io/google_containers/$image
#k8s.gcr.io in use,创建标签成功后删除,临时标签
docker tag quxf2012/$image k8s.gcr.io/$image && docker rmi quxf2012/$image
done
#down quay.io/coreos/flannel:v0.10.0-amd64
docker pull quxf2012/kube-flannel:v0.10.0-amd64
docker tag quxf2012/kube-flannel:v0.10.0-amd64 quay.io/coreos/flannel:v0.10.0-amd64 && docker rmi quxf2012/kube-flannel:v0.10.0-amd64
安装docker
在每台机器上安装docker,在今天(2018年4月27日)这个时刻,这些docker版本是验证过的:v1.12、 v1.11、v1.13和17.03。使用下面的命令来安装docker。
gemfield@sl:~$ sudo apt-get update
gemfield@sl:~$ sudo apt-get install -y docker.io
(注:docker版本未验证的也不一定不能用,现在在Ubuntu18.04上使用的是17.12.1-ce的版本)
初始化master node
kubeadm init --kubernetes-version=v1.11.1 --pod-network-cidr=192.168.111.0/24 --apiserver-advertise-address=192.168.211.129
(注:选项--kubernetes-version=v1.11.1是必须的,这样就能使用下载好的服务镜像,否则会因为访问google网站被墙而无法执行命令。这里使用v1.11.1版本,与上面下载的相关容器镜像的版本有关。)
上面的输出信息建议保存一份,后续添加工作节点还要用到(我们都装在master节点)。
Kubernetes Master初始化成功后,按提示执行以下操作:
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
root@ubuntu:~/image# kubectl get nodes
NAME STATUS ROLES AGE VERSION
ubuntu NotReady master 3m v1.11.1
root@ubuntu:~# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-78fcdf6894-k9xlj 0/1 Pending 0 3m
kube-system coredns-78fcdf6894-rn2xt 0/1 Pending 0 3m
kube-system etcd-ubuntu 1/1 Running 0 2m
kube-system kube-apiserver-ubuntu 1/1 Running 0 3m
kube-system kube-controller-manager-ubuntu 1/1 Running 0 2m
kube-system kube-proxy-g6q9z 1/1 Running 0 3m
kube-system kube-scheduler-ubuntu 1/1 Running 0 3m
(至此完成了Master节点上k8s软件的安装,但集群内还没有可用的工作Node,也缺少容器网络的配置。查看pods状态信息,可以看到还有coredns-78fcdf6894-k9xlj和coredns-78fcdf6894-rn2xt的pod处于Pending状态,这是受缺少容器网络支持的影响而造成的。查看nodes状态信息,看到ubuntu节点的状态为NotReady 。)
安装网络插件
再详细看一下Master节点初始化时输出的提示信息,包括了网络插件的安装建议:
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/
这里是选择安装weave插件,在Master节点上执行:
root@ubuntu:~# kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
serviceaccount/weave-net created
clusterrole.rbac.authorization.k8s.io/weave-net created
clusterrolebinding.rbac.authorization.k8s.io/weave-net created
role.rbac.authorization.k8s.io/weave-net created
rolebinding.rbac.authorization.k8s.io/weave-net created
daemonset.extensions/weave-net created
过一会再看,dns变成running:
root@ubuntu:~# kubectl get pods --all-namespaces
NAMESPACE NAME READY STATUS RESTARTS AGE
kube-system coredns-78fcdf6894-k9xlj 1/1 Running 0 6m
kube-system coredns-78fcdf6894-rn2xt 1/1 Running 0 6m
kube-system etcd-ubuntu 1/1 Running 0 6m
kube-system kube-apiserver-ubuntu 1/1 Running 0 6m
kube-system kube-controller-manager-ubuntu 1/1 Running 0 6m
kube-system kube-proxy-g6q9z 1/1 Running 0 6m
kube-system kube-scheduler-ubuntu 1/1 Running 0 6m
kube-system weave-net-b2x9p 2/2 Running 0 1m
root@ubuntu:~# kubectl get nodes
NAME STATUS ROLES AGE VERSION
ubuntu Ready master 8m v1.11.1
安装一个weave网络管理工具:
root@ubuntu:~# curl -L git.io/weave -o /usr/local/bin/weave
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- 0:00:01 --:--:-- 0
0 0 0 0 0 0 0 0 --:--:-- 0:00:02 --:--:-- 0
100 595 0 595 0 0 152 0 --:--:-- 0:00:03 --:--:-- 426
100 50382 100 50382 0 0 6462 0 0:00:07 0:00:07 --:--:-- 21097
root@ubuntu:~# chmod a+x /usr/local/bin/weave
root@ubuntu:~# weave status
Unable to find image 'weaveworks/weaveexec:2.3.0' locally
2.3.0: Pulling from weaveworks/weaveexec
88286f41530e: Already exists
ad4e50ed2c08: Already exists
b3f4c952e7c2: Already exists
5e27cb7f1c2b: Already exists
f9dfb03c1d7b: Already exists
21771db04786: Pull complete
5fbda086495f: Pull complete
80427f885b22: Pull complete
0c4698905755: Pull complete
Digest: sha256:eb8eb1d83fc58716b20621d397d63737a18f86cbed1fedb1d71671cfc486517b
Status: Downloaded newer image for weaveworks/weaveexec:2.3.0
Version: 2.3.0 (failed to check latest version - see logs; next check at 2018/07/24 09:57:58)
Service: router
Protocol: weave 1..2
Name: 8e:49:65:cb:32:0f(ubuntu)
Encryption: disabled
PeerDiscovery: enabled
Targets: 1
Connections: 1 (1 failed)
Peers: 1
TrustedSubnets: none
Service: ipam
Status: ready
Range: 10.32.0.0/12
DefaultSubnet: 10.32.0.0/12
安装Node并加入集群
因为是all-in-one,所以需要让master节点可以创建pod
kubectl taint nodes --all node-role.kubernetes.io/master-
若另有计算节点,将计算节点加入集群
kubeadm join 192.168.211.129:6443 --token 52aacm.pek7by4gime8pqsy --discovery-token-ca-cert-hash sha256:e319af0fca7bd8934a530e87f554dd4d40d51ac06c8a9e5322e2d7f8b51b009e
token过期处理:
kubeadm token create 创建新的token
root@ubuntu:~# openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
c6838c102e8006690894c5dedba904fb76f212c6b02d6ba7a6bcb6a8d6b558d9
加入集群:
kubeadm join 192.168.211.132:6443 --token r9qkfx.qz68w0dttjq6hiae --discovery-token-ca-cert-hash sha256:c6838c102e8006690894c5dedba904fb76f212c6b02d6ba7a6bcb6a8d6b558d9