二十五、02和03-使用Ansible一键部署kubernetes集群和部署其他组件(CentOS7.6)

使用Ansible搭建k8s1.15.0集群

一、主机配置

主机名 配置 运行服务 IP k8s角色
node01 2核4G ansible、docker 192.168.85.128 master
node02 1核2G docker 192.168.85.129 node
node03 1核2G docker 192.168.85.138 node

二、准备工作

  1. node01对node01、node02、node03做免密
# vim /etc/hosts
192.168.85.128 node01
192.168.85.129 node02
192.168.85.138 node03

# ssh-keygen
# ssh-copy-id root@node01
# ssh-copy-id root@node02
# ssh-copy-id root@node03
  1. node01安装ansible
# yum install -y ansible
  1. 配置/etc/ansible/hosts
# vim /etc/ansible/hosts
[docker]
node0[1:3]

[master]
node01

[node]
node0[2:3]

三、安装k8s1.15.0(包括基础服务、Dashboard、Ingress、Helm、Metrics-Server、监控)

role包地址:链接:https://pan.baidu.com/s/1fMRYCqy-zgGiw-BFGkjXrg
提取码:1duv

# vim k8s-1.15.0-role.yaml
- hosts: docker
  remote_user: root
  roles:
  - k8s-docker
- hosts: master
  remote_user: root
  roles:
  - k8s-master
- hosts: node
  remote_user: root
  roles:
  - k8s-node
- hosts: master
  remote_user: root
  roles:
  - k8s-add
[root@node01 ~]# ansible-playbook k8s-1.15.0.yaml
PLAY [docker] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [node01]
ok: [node03]
ok: [node02]
TASK [k8s-docker : config hosts] ****************************************************************************************************
changed: [node01]
changed: [node03]
changed: [node02]
TASK [k8s-docker : disable firewalld && selinux && swap] ****************************************************************************
changed: [node03]
changed: [node02]
changed: [node01]
TASK [k8s-docker : disable  selinux] ************************************************************************************************
skipping: [node01]
skipping: [node02]
skipping: [node03]
TASK [k8s-docker : system config] ***************************************************************************************************
changed: [node02]
changed: [node01]
changed: [node03]
TASK [k8s-docker : system config shell] *********************************************************************************************
changed: [node03]
changed: [node01]
changed: [node02]
TASK [k8s-docker : ipvs config] *****************************************************************************************************
changed: [node01]
changed: [node02]
changed: [node03]
TASK [k8s-docker : ipvs config shell] ***********************************************************************************************
 [WARNING]: Consider using file module with mode rather than running chmod
changed: [node02]
changed: [node03]
changed: [node01]
TASK [k8s-docker : install ipset ipvsadm] *******************************************************************************************
changed: [node01] => (item=[u'ipset', u'ipvsadm'])
changed: [node03] => (item=[u'ipset', u'ipvsadm'])
changed: [node02] => (item=[u'ipset', u'ipvsadm'])
TASK [k8s-docker : install docker pre] **********************************************************************************************
changed: [node01] => (item=[u'yum-utils', u'device-mapper-persistent-data', u'lvm2'])
changed: [node03] => (item=[u'yum-utils', u'device-mapper-persistent-data', u'lvm2'])
changed: [node02] => (item=[u'yum-utils', u'device-mapper-persistent-data', u'lvm2'])
TASK [k8s-docker : make k8s dir] ****************************************************************************************************
 [WARNING]: Consider using file module with state=directory rather than running mkdir
changed: [node01]
changed: [node02]
changed: [node03]
TASK [k8s-docker : copy k8s somethings] *********************************************************************************************
changed: [node01]
changed: [node02]
changed: [node03]
TASK [k8s-docker : tar k8s] *********************************************************************************************************
 [WARNING]: Consider using unarchive module rather than running tar
changed: [node03]
changed: [node02]
changed: [node01]
TASK [k8s-docker : remove k8s.tar.gz] ***********************************************************************************************
 [WARNING]: Consider using file module with state=absent rather than running rm
changed: [node01]
changed: [node03]
changed: [node02]
TASK [k8s-docker : command] *********************************************************************************************************
 [WARNING]: Consider using yum module rather than running yum
changed: [node01]
changed: [node03]
changed: [node02]
TASK [k8s-docker : start docker] ****************************************************************************************************
changed: [node02]
changed: [node01]
changed: [node03]

TASK [k8s-docker : loading cgroup driver == systemd] ********************************************************************************
changed: [node01]
changed: [node03]
changed: [node02]
RUNNING HANDLER [k8s-docker : restart docker] ***************************************************************************************
changed: [node02]
changed: [node03]
changed: [node01]
PLAY [master] ***********************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************
ok: [node01]
TASK [k8s-master : load k8s images] *************************************************************************************************
changed: [node01]

TASK [k8s-master : add kubeadm repo] ************************************************************************************************
changed: [node01]

TASK [k8s-master : enable kubelet] **************************************************************************************************
changed: [node01]

TASK [k8s-master : install initk8s conf] ********************************************************************************************
ok: [node01]

TASK [k8s-master : remove k8s] ******************************************************************************************************
changed: [node01]

TASK [k8s-master : init k8s] ********************************************************************************************************
changed: [node01]

TASK [k8s-master : copy kubeadm config] *********************************************************************************************
changed: [node01]

TASK [k8s-master : add k8s Network(flannel)] ****************************************************************************************
changed: [node01]

PLAY [node] *************************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************
ok: [node03]
ok: [node02]

TASK [k8s-node : load k8s images] ***************************************************************************************************
changed: [node02]
changed: [node03]

TASK [k8s-node : add kubeadm repo] **************************************************************************************************
changed: [node03]
changed: [node02]

TASK [k8s-node : enable kubelet] ****************************************************************************************************
changed: [node02]
changed: [node03]

PLAY [master] ***********************************************************************************************************************

TASK [Gathering Facts] **************************************************************************************************************
ok: [node01]

TASK [k8s-add : join k8s] ***********************************************************************************************************
changed: [node01]

TASK [k8s-add : sshbashk8s] *********************************************************************************************************
changed: [node01]

TASK [k8s-add : ssh] ****************************************************************************************************************
changed: [node01]

TASK [k8s-add : copy helm sh] *******************************************************************************************************
ok: [node01]

TASK [k8s-add : bash helm sh] *******************************************************************************************************
changed: [node01]

TASK [k8s-add : create ingress] *****************************************************************************************************
changed: [node01]

TASK [k8s-add : install ingress] ****************************************************************************************************
changed: [node01]

TASK [k8s-add : install dashboard] **************************************************************************************************
changed: [node01]

TASK [k8s-add : install metrics-server] *********************************************************************************************
changed: [node01]

TASK [k8s-add : install monitoring] *************************************************************************************************
changed: [node01]

PLAY RECAP **************************************************************************************************************************
node01                     : ok=37   changed=32   unreachable=0    failed=0
node02                     : ok=21   changed=19   unreachable=0    failed=0
node03                     : ok=21   changed=19   unreachable=0    failed=0

四、验证

[root@node01 ~]# kubectl get pod -n kube-system
NAME                                    READY   STATUS    RESTARTS   AGE
coredns-5c98db65d4-2r6sl                1/1     Running   0          12m
coredns-5c98db65d4-s9vl5                1/1     Running   0          12m
etcd-node01                             1/1     Running   0          12m
kube-apiserver-node01                   1/1     Running   0          11m
kube-controller-manager-node01          1/1     Running   0          12m
kube-flannel-ds-amd64-87464             1/1     Running   0          7m18s
kube-flannel-ds-amd64-jrbkj             1/1     Running   0          7m14s
kube-flannel-ds-amd64-jxjzg             1/1     Running   0          12m
kube-proxy-lgdzq                        1/1     Running   0          7m18s
kube-proxy-lvlks                        1/1     Running   0          7m14s
kube-proxy-nlhlg                        1/1     Running   0          12m
kube-scheduler-node01                   1/1     Running   0          12m
kubernetes-dashboard-848b8dd798-r4jqx   1/1     Running   0          6m4s
metrics-server-8456fb6676-vl7dh         1/1     Running   0          5m51s
tiller-deploy-7bf78cdbf7-r4tmx          1/1     Running   0          6m30s
[root@node01 ~]# kubectl get pod -n monitoring
NAME                                   READY   STATUS    RESTARTS   AGE
alertmanager-main-0                    1/2     Running   3          5m52s
grafana-6b687cd6c6-kxrsq               1/1     Running   0          5m54s
kube-state-metrics-5c4898c8f9-xwg6x    4/4     Running   0          5m39s
node-exporter-dshnf                    2/2     Running   0          5m57s
node-exporter-hmh66                    2/2     Running   0          5m57s
node-exporter-x6mww                    2/2     Running   0          5m57s
prometheus-k8s-0                       3/3     Running   1          5m45s
prometheus-k8s-1                       3/3     Running   1          5m31s
prometheus-operator-6c85479b4b-mxwtb   1/1     Running   0          5m58s

登陆Dashboard方法:
1、对node01做hosts解析(怎么添加windows的hosts解析,我就不教了)


image.png

2、查找登陆Dashboard的token

[root@node01 ~]# kubectl describe -n kube-system secret/$(kubectl -n kube-system get secret | grep kubernetes-dashboard-token |cut -d " " -f 1)
Name:         kubernetes-dashboard-token-kfnct
Namespace:    kube-system
Labels:       
Annotations:  kubernetes.io/service-account.name: kubernetes-dashboard
              kubernetes.io/service-account.uid: 3ee45978-beac-47e2-8b1b-d25451a6f016

Type:  kubernetes.io/service-account-token

Data
====
ca.crt:     1025 bytes
namespace:  11 bytes
token:      eyJhbGciOiJSUzI1NiIsImtpZCI6IiJ9.eyJpc3MiOiJrdWJlcm5ldGVzL3NlcnZpY2VhY2NvdW50Iiwia3ViZXJuZXRlcy5pby9zZXJ2aWNlYWNjb3VudC9uYW1lc3BhY2UiOiJrdWJlLXN5c3RlbSIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VjcmV0Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZC10b2tlbi1rZm5jdCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50Lm5hbWUiOiJrdWJlcm5ldGVzLWRhc2hib2FyZCIsImt1YmVybmV0ZXMuaW8vc2VydmljZWFjY291bnQvc2VydmljZS1hY2NvdW50LnVpZCI6IjNlZTQ1OTc4LWJlYWMtNDdlMi04YjFiLWQyNTQ1MWE2ZjAxNiIsInN1YiI6InN5c3RlbTpzZXJ2aWNlYWNjb3VudDprdWJlLXN5c3RlbTprdWJlcm5ldGVzLWRhc2hib2FyZCJ9.OjcdEur4cKR-19V0RvJoTUNiBun-zyXwCnkaIVTzipH0IExNI1atQPH9T9WftmFugR2xXWQ_pENaQk28xciJWgpgK6xQ70KA5Ptn1jv14bhywfwEU_M19rtF4z2G-EwtmOKdICJ3O7GeKTP4hNOVCl8yUdB3D-FVr-KEJebmg05-XsKogFaqw67Q0rHjZJ04aECsFqt5L6-i7Xu7lFpdEMQWE3-6y6gWO7J8NSh8kWcEPzqGJ9sI0VPxJ4dgmdfFcE9qpVykOfkw0K1AA9YlQ6M-zlfwG-uqqpfC0ENjxe3pBe1Tz-4UNmUioE50z7Kn7KWk-ra83laIthCtcuNttw

3、用token登陆


image.png

image.png

你可能感兴趣的:(二十五、02和03-使用Ansible一键部署kubernetes集群和部署其他组件(CentOS7.6))