使用Kubespray部署k8s集群

一、准备工作

1.规划集群

集群节点规划

10.254.4.1

10.254.4.2

10.254.4.3

  1. 关闭swap
swapoff -a
  1. 节点互信

本文以第一个节点 10.254.4.1作为操作节点,所以需要做从10.254.4.1 到其他节点的互信,在4.1上执行如下命令:

ssh-keygen -t rsa

ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.254.4.1

ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.254.4.2

ssh-copy-id -i /root/.ssh/id_rsa.pub root@10.254.4.3

4.软件安装

安装以下可能会用到的软件:

//可能python使用yum会装不到  
yum install -y epel-release python-pip python34 python34-pip ansible    
pip install netaddr    
pip install --upgrade pip    
// 这个组件版本不对,会导致kubespray安装失败,需要更新  
pip install --upgrade jinja2

二、使用kubespray部署集群

1. 下载kubespray

wget https://github.com/kubernetes-incubator/kubespray/archive/v2.6.0.tar.gz

2. 节点规划配置

tar -xvf v2.6.0.tar.gz
cd kubespray-2.6.0/   
cp -rfp inventory/sample inventory/mycluster   
declare -a IPS=(10.254.4.1 10.254.4.2 10.254.4.3)   
CONFIG_FILE=inventory/mycluster/hosts.ini python3 contrib/inventory_builder/inventory.py ${IPS[@]}

3. 修改国内源

 安装过程中会用到google的镜像,此处替换为使用国内源,执行:

cd kubespray/   
sed -i  's/gcr\.io\/google_containers/mirrorgooglecontainers/g' roles/download/defaults/main.yml  
sed -i 's/gcr\.io\/google-containers/mirrorgooglecontainers/g' roles/download/defaults/main.yml  
sed -i 's/gcr\.io\/google_containers/mirrorgooglecontainers/g' roles/kubernetes-apps/ansible/defaults/main.yml

4. 安装
ansible-playbook -i inventory/mycluster/hosts.ini cluster.yml

5. 安装成功

=============================================================================== 
download : contain--------------------------------- 190.31s
network_plugin/calico : Calico | Copy cni plugins from hyperkube -------------------------------------------------- 182.77s
download : container_download | Download containers if pull is required or told to always pull (all nodes) ------------------------------------------------------------------------ 141.82s
download : container_download ---------------------- 92.60s
kubernetes/preinstall : Update package management cache (YUM) ------------------------------------------------------ 36.63s
network_plugin/calico : Calico | Copy cni plugins from calico/cni container ---------------------------------------- 33.25s
download : container_download | Download ccntainers if pull is required or told to always pull (all nodes)---------- 30.09s
download : container_download | Download containers if pull is required or told to always pull (all nodes) --------- 27.44s
gather facts from all instances -------------------- 24.79s
kubernetes/master : Master | wait for the apiserver to be running -------------------------------------------------------------------------------------------------                  22.32s
kubernetes/preinstall : Install packages requirements -------------------------------------------------------------- 22.266
download : container_download | Download containers if pull is required or told to always pull (all nodes) ----------------------------------------------------------------          20.79s
ettd : Configure | Join member(s) to etcd cluster one at a time ---------------------------------------------------- 20.11s
etcd : Configure | Join member(s) to etcd-events cluster one at a timee------------------------------------------------------------------------------------------------------------  20.11s
download : container_download | Download containers if pull is required or told to always pull (all nodes) ------------------------------------------------------------------------ 19.888s
download : connainer_download | Download containers if pull is required or told to always pull (all nodes) ------------------------------------------------------------------------- 12.43s
download : container_download | Download containers if pull is required or told to always pull (all nodes) ------------------------------------------------------------------------- 11.20s
etcd : reload etcd --------------------------------- 10.76s
docker : Docker | pause while Docker restarts -------------------------------------------------------------------------------------------------------------------------------------- 10.17s
download : container_download | Download containers if pull is required or told to always pull (all nodes) -------------------------------------------------------------------------- 9.08s

6. 验证

[root@test kubespray-2.5.0]# kubectl get nodes
NAME      STATUS     ROLES         AGE       VERSION
node1     Ready      master,node   9m        v1.9.5
node2     Ready      master,node   9m        v1.9.5
node3     Ready      node          9m        v1.9.5
[root@test kubespray-2.5.0]# kubectl get pods --all-namespaces
NAMESPACE     NAME                                    READY     STATUS     RESTARTS   AGE
kube-system   calico-node-6qvg2                       1/1       Running    0          5m
kube-system   calico-node-hjw47                       1/1       Running    0          5m
kube-system   calico-node-kcwxf                       1/1       Running    0          5m
kube-system   kube-apiserver-node1                    1/1       Running    0          9m
kube-system   kube-apiserver-node2                    1/1       Running    0          9m
kube-system   kube-controller-manager-node1           1/1       Running    0          10m
kube-system   kube-controller-manager-node2           1/1       Running    0          10m
kube-system   kube-dns-5dbd8b996b-6g89g               3/3       Running    0          5m
kube-system   kube-dns-5dbd8b996b-nd98t               3/3       Running    0          5m
kube-system   kube-proxy-node1                        1/1       Running    0          9m
kube-system   kube-proxy-node2                        1/1       Running    0          9m
kube-system   kube-scheduler-node1                    1/1       Running    0          10m
kube-system   kube-scheduler-node2                    1/1       Running    0          10m
kube-system   kubedns-autoscaler-d76d58748-gql8t      1/1       Running    0          5m
kube-system   kubernetes-dashboard-6c96b87867-tk557   1/1       Running    0          5m

至此,集群安装成功,由于kubespray安装默认自带了dashboard,可以直接使用了

验证:部署一个NGINX

# 启动一个单节点nginx
kubectl run nginx --image =nginx:1.7.9 --port=80
# 为“nginx”服务暴露端口
kubectl expose deployment nginx  -- type = NodePort
# 查看nginx服务详情
kubectl  get  svc nginx
NAME      TYPE       CLUSTER - IP     EXTERNAL-IP   PORT ( S )         AGE 
nginx      NodePort  10.233 . 29.96   <none>        80 : 32345 / TCP   15s
# 访问测试,如果能够正常返回NGINX首页,说明正常
curl localhost : 32345

卸载:

ansible - playbook  - i inventory / mycluster / hosts . ini reset . yml

文章作者: Ciwei
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Ciwei !
 上一篇
ssh免密登录 ssh免密登录
跳板机就是一台机器作为ssh登录的机器 可以使用crt登录 其他都是内网登录 修改hosts: 10.254.4.1 mysql 10.254.4.2 redis 10.254.4.3 mongodb比如我们现在有一台机器 10.254
2018-09-16
下一篇 
推荐一款所有系统能用的牛B播放器 推荐一款所有系统能用的牛B播放器
Listen 1可以搜索和播放来自网易云音乐,虾米,QQ音乐,酷狗音乐,酷我音乐网站的歌曲,让你的曲库更全面 document.querySelectorAll('.github-emoji')
2018-09-15
  目录