ansible部署k8s

准备工作

机器列表

节点ip地址
ansible192.168.200.200
master192.168.200.201
node1192.168.200.202
node2192.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 root@192.168.200.201
ssh-copy-id root@192.168.200.202
ssh-copy-id root@192.168.200.203

/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

在这里插入图片描述
https://github.com/coreos/flannel/releases 地址下载对应版本
在这里插入图片描述

修改kube-flannel.yml
在这里插入图片描述

部署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


  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无痕Miss

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值