kubernetes V1.9安装(附离线安装包和离线镜像)

简介

环境信息(采用一个master节点+两个node节点)
master 192.168.2.110
node-1 192.168.2.112
node-2 192.168.2.113

软件版本
kubernetes v1.9
docker:17.03
kubeadm:v1.9.0
kube-apiserver:v1.9.0
kube-controller-manager:v1.9.0
kube-scheduler:v1.9.0
k8s-dns-sidecar:1.14.7
k8s-dns-kube-dns:1.14.7
k8s-dns-dnsmasq-nanny:1.14.7
kube-proxy:v1.9.0
etcd:3.1.10
pause :3.0
flannel:v0.9.1
kubernetes-dashboard:v1.8.1

采用kubeadm安装

kubeadm为kubernetes官方推荐的自动化部署工具,他将kubernetes的组件以pod的形式部署在master和node节点上,并自动完成证书认证等操作。
因为kubeadm默认要从google的镜像仓库下载镜像,但目前国内无法访问google镜像仓库,所以这里我提交将镜像下好了,只需要将离线包的镜像导入到节点中就可以了。

开始安装

所有节点操作

下载
链接: https://pan.baidu.com/s/1c2O1gIW 密码: 9s92
比对md5解压离线包

MD5 (k8s_images.tar.bz2) = b60ad6a638eda472b8ddcfa9006315ee

解压下载下来的离线包

1
tar -xjvf k8s_images.tar.bz2

安装docker-ce17.03(kubeadmv1.9最大支持docker-ce17.03)

1
2
rpm -ihv docker-ce-selinux-17.03.2.ce-1.el7.centos.noarch.rpm
rpm -ivh docker-ce-17.03.2.ce-1.el7.centos.x86_64.rpm

修改docker的镜像源为国内的daocloud的。

1
curl -sSL https://get.daocloud.io/daotools/set_mirror.sh | sh -s http://a58c8480.m.daocloud.io

启动docker-ce

1
systemctl start docker && systemctl enable docker

绑定hosts
将master和node-1、node-2绑定hosts

master节点与node节点做互信

1
2
3
[root@master ~]# ssh-keygen
[root@master ~]# ssh-copy-id node-1
[root@master ~]# ssh-copy-id node-2

关闭防火墙和selinux

1
systemctl stop firewalld  && systemctl disable firewalld

vim /etc/selinux/config
SELINUX=disabled

1
setenforce 0

配置系统路由参数,防止kubeadm报路由警告

echo "
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
" >> /etc/sysctl.conf
sysctl -p
1
2
3

导入镜像

docker load < /root/k8s_images/docker_images/etcd-amd64_v3.1.10.tar docker load </root/k8s_images/docker_images/flannel\:v0.9.1-amd64.tar docker load </root/k8s_images/docker_images/k8s-dns-dnsmasq-nanny-amd64_v1.14.7.tar docker load </root/k8s_images/docker_images/k8s-dns-kube-dns-amd64_1.14.7.tar docker load </root/k8s_images/docker_images/k8s-dns-sidecar-amd64_1.14.7.tar docker load </root/k8s_images/docker_images/kube-apiserver-amd64_v1.9.0.tar docker load </root/k8s_images/docker_images/kube-controller-manager-amd64_v1.9.0.tar docker load </root/k8s_images/docker_images/kube-scheduler-amd64_v1.9.0.tar docker load < /root/k8s_images/docker_images/kube-proxy-amd64_v1.9.0.tar docker load </root/k8s_images/docker_images/pause-amd64_3.0.tar docker load < /root/k8s_images/docker_images/kubernetes-dashboard_v1.8.1.tar
1
2

安装安装kubelet kubeadm kubectl包
rpm -ivh socat-1.7.3.2-2.el7.x86_64.rpm rpm -ivh kubernetes-cni-0.6.0-0.x86_64.rpm kubelet-1.9.9-9.x86_64.rpm kubectl-1.9.0-0.x86_64.rpm rpm -ivh kubectl-1.9.0-0.x86_64.rpm rpm -ivh kubeadm-1.9.0-0.x86_64.rpm
1
2
3
4


### master节点操作
启动kubelete
systemctl enable kubelet && sudo systemctl start kubelet
1
开始初始化master
kubeadm init --kubernetes-version=v1.9.0 --pod-network-cidr=10.244.0.0/16
1
2
3
4
5
6
7
8
9
10
11
12
kubernetes默认支持多重网络插件如flannel、weave、calico,这里使用flanne,就必须要设置--pod-network-cidr参数,10.244.0.0/16是kube-flannel.yml里面配置的默认网段,如果需要修改的话,需要把kubeadm init的--pod-network-cidr参数和后面的kube-flannel.yml里面修改成一样的网段就可以了。

![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_1.png)
![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_2.png)

发现kubelet启动不了
查看日志/var/log/message

![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_4.png)

发现原来是kubelet默认的cgroup的driver和docker的不一样,docker默认的cgroupfs,kubelet默认为systemd
修改
vim /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
1
2
3
![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_5.png)

重启reload
systemctl daemon-reload && systemctl restart kubelet
1
2
3
4
5
6
7
查看状态

![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_6.png)


此时记得将环境reset一下
执行
kubeadm reset
1
在重新执行
kubeadm init --kubernetes-version=v1.9.0 --pod-network-cidr=10.244.0.0/16
1
2
3
4
5
6
7
8
9
10
11
![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_7.png)

将kubeadm join xxx保存下来,等下node节点需要使用
如果忘记了,可以在master上通过kubeadmin token list得到

![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_8.png)

![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_9.png)


默认token 24小时就会过期,后续的机器要加入集群需要重新生成token
kubeadm token create
1
然后在执行   
kubeadm join --token xxx master_ip:6443
1
2
按照上面提示,此时root用户还不能使用kubelet控制集群需要,配置下环境变量     
对于非root用户
mkdir -p $HOME/.kube cp -i /etc/kubernetes/admin.conf $HOME/.kube/config chown $(id -u):$(id -g) $HOME/.kube/config
1
对于root用户
export KUBECONFIG=/etc/kubernetes/admin.conf
1
也可以直接放到~/.bash_profile
echo "export KUBECONFIG=/etc/kubernetes/admin.conf" >> ~/.bash_profile
1
source一下环境变量
source ~/.bash_profile
1
2
3
4
5
6
kubectl version测试
![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_10.png)

安装网络,可以使用flannel、calico、weave、macvlan这里我们用flannel。

下载此文件
wget https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml
1
2
3
4
5

或直接使用离线包里面的
若要修改网段,需要kubeadm --pod-network-cidr=和这里同步
vim kube-flannel.yml
修改network项
"Network": "10.244.0.0/16",
1
2
3

执行

kubectl create -f kube-flannel.yml
1
2
3
4
5
6


### node节点操作
修改kubelet配置文件根上面有一将cgroup的driver由systemd改为cgroupfs
vim /etc/systemd/system/kubelet.service.d/10-kubeadm.conf
Environment="KUBELET_CGROUP_ARGS=--cgroup-driver=cgroupfs"
systemctl daemon-reload
1
2
```
systemctl enable kubelet&&systemctl restart kubelet
使用刚刚执行kubeadm后的kubeadm join --xxx
1
kubeadm join --token 361c68.fbafaa96a5381651 192.168.2.110:6443 --discovery-token-ca-cert-hash sha256:e5e392f4ce66117635431f76512d96824b88816dfdf0178dc497972cf8631a98
多次加入报错查看/var/log/message日志. 这个错是因为没有配置前面sysctl的router的环境变量 ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_26.png) 发现node节点启动不了flannel容器,查看容器log发现是host上没有默认路由,在网卡配置文件里面设置好默认路由。 ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_11.png) 加入成功 在master节点上check一下 ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_27.png) kubernetes会在每个node节点创建flannel和kube-proxy的pod ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_14.png) ### 测试集群 在master节点上发起个创建应用请求 这里我们创建个名为httpd-app的应用,镜像为httpd,有两个副本pod
1
kubectl run httpd-app --image=httpd --replicas=2
![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_15.png) 检查pod 可以看见pod分布在node-1和node-2上 ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_16.png) 因为创建的资源不是service所以不会调用kube-proxy 直接访问测试 ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_17.png) 至此kubernetes基本集群安装完成。 ### 部署kubernetes-dashboard kubernetes-dashboard是可选组件,因为,实在不好用,功能太弱了。 建议在部署master时一起把kubernetes-dashboard一起部署了,不然在node节点加入集群后,kubernetes-dashboard会被kube-scheduler调度node节点上,这样根kube-apiserver通信需要额外配置。 直接使用离线包里面的kubernetes-dashboard.yaml 修改kubernetes-dashboard.yaml 如果需要让外面访问需要修改这个yaml文件端口类型为NodePort默认为clusterport外部访问不了, ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_18.png) nodeport默认端口范围30000-32767 32666就是我的映射端口,根docker run -d xxx:xxx差不多 创建kubernetes-dashboard
1
kubectl create -f kubernetes-dashboard.yaml
访问
1
https://master_ip:NodePort
![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_19.png) 默认验证方式有kubeconfig和token,这里我们都不用。 这里我们使用basicauth的方式进行apiserver的验证 创建/etc/kubernetes/pki/basic_auth_file 用于存放用户名和密码 #user,password,userid admin,admin,2 给kube-apiserver添加basic_auth验证
1
vim /etc/kubernetes/manifests/kube-apiserver.yaml
![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_20.png) 加上这行 更新kube-apiserver容器
1
kubectl  apply -f kube-apiserver.yaml
授权 k8s1.6后版本都采用RBAC授权模型 给admin授权 默认cluster-admin是拥有全部权限的,将admin和cluster-admin bind这样admin就有cluster-admin的权限。 ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_21.png) 那我们将admin和cluster-admin bind在一起这样admin也拥用cluster-admin的权限了
1
kubectl create clusterrolebinding login-on-dashboard-with-cluster-admin --clusterrole=cluster-admin --user=admin
查看
1
kubectl get clusterrolebinding/login-on-dashboard-with-cluster-admin -o yaml
![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_22.png) 在此访问https://master:32666 ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_28.png) 选基本,就可以通过用户名和密码访问了 ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_23.png) 创建个应用测试 ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_24.png) 部署成功 ![](https://image-1251900790.cos.ap-chengdu.myqcloud.com/image/kubernetes_install_25.png) 参考链接: https://kubernetes.io/docs/setup/independent/install-kubeadm/ http://tonybai.com/2017/07/20/fix-cannot-access-dashboard-in-k8s-1-6-4/