ansible部署k8s

使用ansible部署k8s

  • 准备工作
    • 机器列表
  • ansible配置
    • ansible安装
    • 免密登录
    • /etc/hosts文件中添加服务器节点信息【所有节点】
    • ansible添加/etc/ansible/hosts文件中添加服务器节点信息
  • 安装Docker
    • 创建yml文件,并且执行
      • docker_playbook.yml
      • 修改ansible.cfg
      • 执行yml
  • 部署k8s master节点
    • 初始化内容
    • 安装kubectl,kubeadm,kubelet,flannel
    • 修改/etc/ansible/ansible.cfg
    • 执行yml
    • 查看docker
    • 查看k8s 情况
    • 可能会遇到的错误
      • flannel-ds-amd64-fxnpl 为backoff
  • 部署k8s node
    • node节点部署
    • 查看是否已经部署好

准备工作

机器列表

节点 ip地址
ansible 192.168.200.200
master 192.168.200.201
node1 192.168.200.202
node2 192.168.200.203

ansible配置

ansible安装

cd /etc/yum.repos.d/
mv CentOS-Base.repo CentOS-Base.repo.backup
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
yum clean all
yum makecache
yum update
#安装ansible
yum -y install ansible-2.9.7-1.el7.ans.noarch.rpm / yum -y install ansible

免密登录

#生成公私钥
ssh-keygen
ssh-copy-id [email protected]
ssh-copy-id [email protected]
ssh-copy-id [email protected]

/etc/hosts文件中添加服务器节点信息【所有节点】

192.168.200.201 master
192.168.200.202 node1
192.168.200.203 node2

ansible添加/etc/ansible/hosts文件中添加服务器节点信息

[k8s-all]
192.168.200.201
192.168.200.202
192.168.200.203
[master]
192.168.200.201
[nodes]
192.168.200.202
192.168.200.203

安装Docker

创建yml文件,并且执行

docker_playbook.yml

- hosts: k8s-all
  remote_user: root
  vars: 
    docker_version: 18.09.2

  tasks:
    - name: install dependencies
      shell:  yum install -y yum-utils device-mapper-persistent-data lvm2
    - name: 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-{{docker_version}} state=present
    - name: start docker
      shell: systemctl start docker && systemctl enable docker

修改ansible.cfg

/etc/ansible/ansible.cfg

deprecation_warnings = false

执行yml

ansible-playbook docker_playbook.yml

部署k8s master节点

初始化内容

firstop.sh

#!/bin/bash
#防火墙
systemctl disable firewalld
systemctl stop firewalld
setenforce 0

#禁用swap,临时关闭,重启失效
swapoff -a
#永久关闭,重启后也生效
cp -p /etc/fstab /etc/fstab.bak$(date '+%Y%m%d%H%M%S')
sed -i "s/\/dev\/mapper\/centos-swap/\#\/dev\/mapper\/centos-swap/g" /etc/fstab

#修改内核参数
cat <<EOF >  /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
EOF

#重新加载配置文件
sysctl --system

#配置阿里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

安装kubectl,kubeadm,kubelet,flannel

master_playbook.yml

- hosts: master
  remote_user: root
  vars:
    kube_version: 1.16.0-0
    k8s_version: v1.16.0
    k8s_master: 192.168.200.201
  tasks: 
    - name: firstop
      script: ./firstop.sh
    - name: install kube***
      yum: name={{item}} state=present
      with_items:
        - kubectl-{{kube_version}}
        - kubeadm-{{kube_version}}
        - kubelet-{{kube_version}}
    - name: init k8s
      shell: kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version {{k8s_version}} --apiserver-advertise-address {{k8s_master}}  --pod-network-cidr=10.244.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: copy flannel yaml file
      copy: src=/root/kube-flannel.yml dest=/tmp/kube-flannel.yml
    - name: install flannel
      shell: kubectl apply -f /tmp/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

修改/etc/ansible/ansible.cfg

command_warnings = False

执行yml

ansible-playbook master_playbook.yml

查看docker

docker  images

查看k8s 情况

kubectl get pods --all-namespaces

kubectl get nodes

可能会遇到的错误

flannel-ds-amd64-fxnpl 为backoff

ansible部署k8s_第1张图片
https://github.com/coreos/flannel/releases 地址下载对应版本
ansible部署k8s_第2张图片

修改kube-flannel.yml
ansible部署k8s_第3张图片

部署k8s node

node节点部署

nodes_playbook.yml

- hosts: nodes
  remote_user: root
  vars:
    kube_version: 1.16.0-0
  tasks:
    - name: firstop
      script: ./firstop.sh
    - name: install kube***
      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 192.168.200.201:6443 --token 2pwoo0.aqf9rs5udye8894a     --discovery-token-ca-cert-hash sha256:e60517875d435847f741d3650f6506e2f9b9e823a66165874d3a135878b85b44

【备注】:此处的token自己在master节点进行生成

kubeadm token create --print-join-command

查看是否已经部署好

#master下执行
kubectl get nodes


你可能感兴趣的:(kubernetes,kubernetes)