博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用Kubespray安装k8s集群
阅读量:6940 次
发布时间:2019-06-27

本文共 4497 字,大约阅读时间需要 14 分钟。

GitHub:

使用Kubespray安装k8s集群

本文撰写时,Kubespray的master分支不稳定,请使用release版本来安装,具体来说就是切换到最新的tag上。

准备好target nodes

  1. 准备好几台服务器,假设你准备了3台。
  2. 给每个node安装好操作系统,Ubuntu 16.04 Server LTS或者CentOS 7,并且有一个可以sudo的用户或者直接有root用户。
  3. 确保每个node都安装了python 2.7
  4. 在每个node上开启IPv4 Forwarding,修改/etc/sysctl.conf,然后重启。可参考这篇文章

    net.ipv4.ip_forward = 1

执行Kubespray

找一台服务器,用来执行Kubespray,Kubespray的原理是通过ssh连接到各个target nodes执行命令安装k8s集群。

  1. 将服务器的.ssh/id_rsa.pub上传到target nodes:
    ssh-copy-id user@target-node-host
  2. 在Kubespray所在的机器上安装bash-git-prompt
  3. 到Kubespray项目仓库下载代码:
    git clone https://github.com/kubernetes-incubator/kubespray.git
  4. 使用中科大docker image mirror地址,运行以下命令:

    • find . -name '*.yml' | xargs -n1 -I{} sed -i 's/gcr\.io\/google-containers\//gcr\.mirrors\.ustc\.edu\.cn\/google-containers\//' {}
    • find . -name '*.yml' | xargs -n1 -I{} sed -i 's/gcr\.io\/google_containers\//gcr\.mirrors\.ustc\.edu\.cn\/google-containers\//' {}
    • find . -name '*.yml' | xargs -n1 -I{} sed -i 's/quay\.io/quay\.mirrors\.ustc\.edu\.cn/' {}

如果中科大地址不行,请更换anjia0532的地址

  • find . -name '*.yml' | xargs -n1 -I{} sed -i 's/gcr\.io\/google-containers\//anjia0532\/google-containers\./' {}
  • find . -name '*.yml' | xargs -n1 -I{} sed -i 's/gcr\.io\/google_containers\//anjia0532\/google-containers\./' {}
  1. 修改roles/docker/defaults/main.yml文件,设置docker仓库的镜像:

    docker_ubuntu_repo_base_url: "http://mirrors.aliyun.com/docker-ce/linux/ubuntu"docker_ubuntu_repo_gpgkey: "http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg"dockerproject_apt_repo_base_url: "https://mirrors.tuna.tsinghua.edu.cn/docker/apt/repo"dockerproject_apt_repo_gpgkey: "https://mirrors.tuna.tsinghua.edu.cn/docker/apt/gpg"
  2. 确保Kubespray所载机器上已经安装了pip
  3. 在执行ansible-playbook命令之前修改inventory/mycluster/group_vars/k8s-cluster.yml文件:

    • efk_enabled: true
    • ingress_nginx_enabled: true
    • kubeconfig_localhost: true
    • kubectl_localhost: true
  4. 修改roles/network_plugin/calico/defaults/main.yml文件里的calico_mtu参数,根据官方文档给每个服务器设置MTU。简单来说就是kubespray默认为calico启用了IP-in-IP模式,那么它的MTU应该是网卡MTU-20。
  5. 根据项目仓库的指南执行命令
  6. 安装完毕后,找到inventory/mycluster/artifacts/admin.conf文件,copy到~/.kube/config文件,然后你就可以在Kubespray机器上使用kubectl管理k8s了

Troubleshooting

提示Permission denied之类的错误

可能是执行Ansible playbook的时候,ssh到target node执行某些命令缺少root权限。

在教程的最后一步ansible-playbook -i inventory/mycluster/hosts.ini cluster.yml,根据情况添加-b --become-user --become-method等参数。

写本文时target node是ubuntu cloud image,所以只需添加-b参数就行了。其余情况请自行摸索。

同时也要记得添加-u 用户名参数。

提示unable to resolve host

这是因为每个target node有一个hostname,但是在/etc/hosts下没有配置造成的,修改每个target node的/etc/hosts,比如:

127.0.0.1 localhost kube-1

提示FAILED! ip in ansible_all_ipv4_addresses

这种错误出现在云环境中,target node有两个IP,一个是内部IP(外部不能访问),一个是外部IP(在OpenStack环境下就是一个是Project network IP,一个是Floating IP)。

这个时候需要修改inventory/mycluster/hosts.ini,把node的IP属性改成内部IP,比如下面这种:

[all]node1 ansible_host=172.50.10.2 ip=192.168.1.4node2 ansible_host=172.50.10.13 ip=192.168.1.8node3 ansible_host=172.50.10.15 ip=192.168.1.9...

用kubectl访问

在master-node下可以用kubectl来访问k8s,但这有点麻烦,实际上你能够在任意机器上安装kubectl然后远程访问。

方法很简单将之前提到的.kube/config放到你自己电脑(假设你用的是MAC或者Linux系统)的目录下,就可以了。

注意:在OpenStack环境下,每个node会被分配一个Floating IP,会导致你kubectl无法使用,这个时候需要你这样做:

  • 注释.clusters.cluster.certificate-authority-data
  • 添加.clusters.cluster.insecure-skip-tls-verify: true
  • 修改.clusters.cluster.server的IP地址到一个能够外部访问的IP地址(这种情况出现在OpenStack环境下的Floating IP)

访问Dashboard

如果都安装成功,那么你可以访问k8s dashboard来看看安装结果。打开浏览器,访问https://{某个master的IP}:6443/api/v1/namespaces/kube-system/services/https:kubernetes-dashboard:/proxy/,你会被要求登录,你得先创建一个用户并。

但是Kubespray并没有替你创建用户,所以请根据这篇guide来创建用户,然后获得Token,使用Token登录。

下面讲解主要步骤:

  1. 创建admin-user用户。

    1. 新建一个文件名字叫做admin-user.yaml,内容如下:

      apiVersion: v1kind: ServiceAccountmetadata:  name: admin-user  namespace: kube-system
    2. 在kube-master上运行kubectl create -f admin-user.yaml
  2. 绑定角色:

    1. 新建一个文件名字叫做admin-user-role.yaml,内容如下:

      apiVersion: rbac.authorization.k8s.io/v1kind: ClusterRoleBindingmetadata:  name: admin-userroleRef:  apiGroup: rbac.authorization.k8s.io  kind: ClusterRole  name: cluster-adminsubjects:- kind: ServiceAccount  name: admin-user
  3. 获得token:

kubectl -n kube-system describe secret $(kubectl -n kube-system get secret | grep admin-user | awk '{print $1}')

[calico-mtu]: https://docs.projectcalico.org/v2.2/usage/configuration/mtu[kubespray-repo]: https://github.com/kubernetes-incubator/kubespray[bash-git-prompt]: https://github.com/magicmonty/bash-git-prompt[enable-ipv4-forwarding]: http://www.ducea.com/2006/08/01/how-to-enable-ip-forwarding-in-linux/[k8s-dashboard-create-user]: https://github.com/kubernetes/dashboard/wiki/Creating-sample-user[anjia0532-mirror]: https://github.com/anjia0532/gcr.io_mirror

转载地址:http://zusnl.baihongyu.com/

你可能感兴趣的文章
Linux下安装jdk报Permission denied以及chmod详解
查看>>
Python搭建环境
查看>>
Solr环境搭建及IK分词的集成及solrJ的调用(二)
查看>>
No code, no money; No defect, no job.
查看>>
NO3
查看>>
LVS--DR模型
查看>>
微信扫码支付
查看>>
CentOS7.0 下sphinx搭建
查看>>
在三台不同的CentOS 7主机上用rpm包快速部署LAMP
查看>>
网页制作设计师如何能说服客户让网站落地
查看>>
浅谈JNDI导入两个同名不同路径jar包的先后次序影响程序运行结果的问题
查看>>
给服务器容天SCS4850配置校园网(附带:Xshell连接设置)
查看>>
ansible批处理
查看>>
Andorid截屏代码
查看>>
innobackupex 实现远程备份与备份恢复脚本
查看>>
PG字符:使用collation设置排序规则
查看>>
LINUX防火墙iptables基本命令
查看>>
Centos7 mariadb-galera-cluster-5.5+HAProxy+keepalived
查看>>
jsBOM对象
查看>>
Shell 脚本 100 例《一》
查看>>