安装Ansible
代码语言:javascript复制# 更换清华源
sudo sed -e 's|^mirrorlist=|#mirrorlist=|g'
-e 's|^#baseurl=http://mirror.centos.org|baseurl=https://mirrors.tuna.tsinghua.edu.cn|g'
-i.bak
/etc/yum.repos.d/CentOS-*.repo
# 更换阿里epel
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum -y install vim wget ansible
管理机配置host
代码语言:javascript复制vim /etc/hosts
172.16.87.100 kubernetes-master
172.16.87.101 kubernetes-node1
172.16.87.102 kubernetes-node2
Ansible添加主机节点
代码语言:javascript复制vim /etc/ansible/hosts
[k8s_all]
172.16.87.100
172.16.87.101
172.16.87.102
[k8s_master]
172.16.87.100
[k8s_node]
172.16.87.101
172.16.87.102
更改其他主机host
代码语言:javascript复制vim alter_hosts.yaml
- hosts: k8s_node
remote_user: root
tasks:
- name: backup /etc/hosts
shell: mv /etc/hosts /etc/host_bak
- name: copy localhosts file to remote
copy: src=/etc/hosts dest=/etc/ owner=root group=root mode=0644
ansible-playbook alter_hosts.yaml # 执行命令
更改完成后请重启主机
安装Docker
代码语言:javascript复制vim install_docker.yaml
- hosts: k8s_all
remote_user: root
tasks:
- name: install dependencies
yum: state=installed
args:
name: ['ntp','yum-utils','device-mapper-persistent-data','lvm2']
- name: Add docker-repo
shell: yum-config-manager --add-repo https://mirrors.ustc.edu.cn/docker-ce/linux/centos/docker-ce.repo
- name: Install docker
yum: name=docker-ce
- name: Start docker
shell: systemctl start docker;systemctl enable docker
info.sh
代码语言:javascript复制#!/bin/bash
# 防火墙
systemctl disable firewalld
systemctl stop firewalld
setenforce 0
sed -i 's/^SELINUX=enforcing$/SELINUX=disabled/' /etc/selinux/config
#禁用swap
swapoff -a
echo "vm.swappiness =0" >> /etc/sysctl.conf
#修改内核参数
modprobe br_netfilter
cat <<EOF > /etc/sysctl.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF
#重新加载配置文件
sysctl -p
#配置阿里k8s yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
#更新缓存
yum clean all -y && yum makecache -y && yum repolist -y
# 部署镜像
docker pull bluersw/kube-apiserver:v1.16.2
docker pull bluersw/kube-controller-manager:v1.16.2
docker pull bluersw/kube-scheduler:v1.16.2
docker pull bluersw/kube-proxy:v1.16.2
docker pull bluersw/pause:3.1
docker pull bluersw/etcd:3.3.15-0
docker pull bluersw/coredns:1.6.2
docker pull bluersw/flannel:v0.11.0-amd64
# 还原
docker tag bluersw/kube-apiserver:v1.16.2 k8s.gcr.io/kube-apiserver:v1.16.2
docker tag bluersw/kube-controller-manager:v1.16.2 k8s.gcr.io/kube-controller-manager:v1.16.2
docker tag bluersw/kube-scheduler:v1.16.2 k8s.gcr.io/kube-scheduler:v1.16.2
docker tag bluersw/kube-proxy:v1.16.2 k8s.gcr.io/kube-proxy:v1.16.2
docker tag bluersw/pause:3.1 k8s.gcr.io/pause:3.1
docker tag bluersw/etcd:3.3.15-0 k8s.gcr.io/etcd:3.3.15-0
docker tag bluersw/coredns:1.6.2 k8s.gcr.io/coredns:1.6.2
docker tag bluersw/flannel:v0.11.0-amd64 quay.io/coreos/flannel:v0.11.0-amd64
docker images | grep bluersw | awk '{print "docker rmi ",$1":"$2}' | sh -x
deploy_master.yaml
代码语言:javascript复制- hosts: k8s_master
remote_user: root
vars:
kube_version: 1.16.2
k8s_version: v1.16.2
k8s_master: 172.16.87.100
tasks:
- name: Exec Info
script: ./info.sh
- name: install Kubernetes
yum: name={{item}} state=present
with_items:
- kubectl-{{kube_version}}
- kubeadm-{{kube_version}}
- kubelet-{{kube_version}}
- name: Init Kubernetes
shell: kubeadm init --kubernetes-version={{k8s_version}} --apiserver-advertise-address={{k8s_master}} --pod-network-cidr=10.244.0.0/16 --service-cidr=10.1.0.0/16 --token-ttl 0
- name: config kube
shell: mkdir -p $HOME/.kube && cp -i /etc/kubernetes/admin.conf $HOME/.kube/config && chown $(id -u):$(id -g) $HOME/.kube/config
- name: Apply flannel
shell: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
- name: get join command
shell: kubeadm token create --print-join-command
register: join_command
- name: show join command
debug: var=join_command verbosity=0
deploy_node.yaml
代码语言:javascript复制- hosts: k8s_node
remote_user: root
vars:
kube_version: 1.16.2
tasks:
- name: Exec Info
script: ./info.sh
- name: install Kubernetes
yum: name={{item}} state=present
with_items:
- kubeadm-{{kube_version}}
- kubelet-{{kube_version}}
- name: start kubelet
shell: systemctl enable kubelet && systemctl start kubelet
- name: join cluster
shell: kubeadm join 172.16.87.100:6443 --token vwsx61.rxzjukcfdnd2zob2 --discovery-token-ca-cert-hash sha256:cd53a381768b059f822f4d85f21041c73bf9c67e5b4f61eafbdfe6abb9ac65cb
- Token:需要到Master查看Ansible返回的执行信息
验证集群
代码语言:javascript复制[root@kubernetes-master ~]# kubectl get node
NAME STATUS ROLES AGE VERSION
kubernetes-master Ready master 14m v1.16.2
kubernetes-node1 Ready <none> 5m1s v1.16.2
kubernetes-node2 Ready <none> 6m47s v1.16.2