前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用Kubespray安装k8s集群

使用Kubespray安装k8s集群

作者头像
颇忒脱
发布2018-10-19 14:35:12
2.6K0
发布2018-10-19 14:35:12
举报
文章被收录于专栏:颇忒脱的技术博客

GitHub: https://github.com/chanjarste...

使用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,比如:

代码语言:javascript
复制
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,比如下面这种:

代码语言:javascript
复制
[all]
node1 ansible_host=172.50.10.2 ip=192.168.1.4
node2 ansible_host=172.50.10.13 ip=192.168.1.8
node3 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: v1 kind: ServiceAccount metadata: 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/v1 kind: ClusterRoleBinding metadata: name: admin-user roleRef: apiGroup: rbac.authorization.k8s.io kind: ClusterRole name: cluster-admin subjects: - 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}')

代码语言:javascript
复制
[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
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 使用Kubespray安装k8s集群
    • 准备好target nodes
      • 执行Kubespray
        • Troubleshooting
      • 用kubectl访问
        • 访问Dashboard
        相关产品与服务
        容器服务
        腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档