给我写一个部署k8s集群的ansible的role
时间: 2023-02-08 14:02:10 浏览: 162
K8s集群搭建ansible部署脚本
5星 · 资源好评率100%
在这里我给你一个示例的Ansible Role来部署K8s集群:
```
- name: Deploy K8s Cluster
hosts: k8s-masters
become: yes
vars:
k8s_version: 1.20.5
kubeadm_version: 1.20.5-00
tasks:
- name: Install kubeadm, kubelet, and kubectl
apt:
name:
- kubeadm={{ kubeadm_version }}
- kubelet={{ k8s_version }}-00
- kubectl={{ k8s_version }}-00
state: present
update_cache: true
- name: Initialize K8s Cluster
command: kubeadm init --kubernetes-version={{ k8s_version }}
creates: /etc/kubernetes/admin.conf
- name: Copy K8s Configuration
copy:
src: /etc/kubernetes/admin.conf
dest: /etc/kubernetes/kubeconfig
owner: root
group: root
mode: 0644
- name: Deploy Networking
kubectl:
cmd: apply
resource:
- svc
- deployment
- replicaset
- pod
namespace: kube-system
src: /etc/kubernetes/manifests
- name: Join Nodes
command: kubeadm join --token={{ kubeadm_token }} {{ ansible_host }}
- name: Deploy Addons
kubectl:
cmd: create
resource:
- svc
- deployment
- replicaset
- pod
namespace: kube-system
src: /etc/kubernetes/addons
```
这个示例的Ansible Role会在k8s-masters上安装kubeadm, kubelet, kubectl,初始化k8s集群,复制k8s配置,部署网络,加入新节点并部署插件。
请注意,这只是一个示例,您需要根据您自己的环境进行定制。
阅读全文