V1
注释已添加到代码中,如下所示:
---
- name: Install Kubernetes cluster # 定义Ansible Playbook的名称
hosts: all # 指定要在哪些主机上运行脚本
become: true # 以超级用户权限运行脚本
vars: # 定义变量
k8s_version: "1.18.2-00" # Kubernetes版本
pod_subnet: "10.244.0.0/16" # Pod网络的CIDR
service_subnet: "10.96.0.0/12" # Service网络的CIDR
master_ip: "192.168.0.1" # Kubernetes控制平面的IP地址
nodes: "{{ groups['k8s_nodes'] }}" # 要加入集群的节点列表
tasks: # 定义任务列表
- name: Install Docker # 安装Docker
apt:
name: docker.io
state: present
tags:
- docker
- name: Add Kubernetes apt key # 添加Kubernetes apt key
apt_key:
url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
state: present
tags:
- k8s
- name: Add Kubernetes apt repository # 添加Kubernetes apt repository
apt_repository:
repo: deb http://apt.kubernetes.io/ kubernetes-xenial main
state: present
tags:
- k8s
- name: Install Kubernetes components # 安装Kubernetes组件
apt:
name: "{{ item }}"
state: present
update_cache: yes
with_items:
- kubelet={{ k8s_version }}
- kubeadm={{ k8s_version }}
- kubectl={{ k8s_version }}
tags:
- k8s
- name: Initialize Kubernetes master # 初始化Kubernetes主节点
command: kubeadm init --pod-network-cidr={{ pod_subnet }} --service-cidr={{ service_subnet }} --apiserver-advertise-address={{ master_ip }}
register: kubeadm_output
when: inventory_hostname == groups['k8s_master'][0] # 只在第一个Kubernetes主节点上运行
tags:
- k8s
- name: Copy Kubernetes configuration to user's home directory # 将Kubernetes配置文件复制到用户主目录
command: mkdir -p $HOME/.kube && cp /etc/kubernetes/admin.conf $HOME/.kube/config
environment:
HOME: "{{ lookup('env','HOME') }}"
when: inventory_hostname == groups['k8s_master'][0] # 只在第一个Kubernetes主节点上运行
tags:
- k8s
- name: Install Flannel pod network # 安装Flannel作为Pod网络
command: kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
when: inventory_hostname == groups['k8s_master'][0] # 只在第一个Kubernetes主节点上运行
tags:
- k8s
- name: Join Kubernetes nodes to the cluster # 加入节点到Kubernetes集群
command: "{{ kubeadm_output.stdout_lines[-1] }} --node-name {{ inventory_hostname }}"
when: inventory_hostname in nodes and kubeadm_output is defined # 只在要加入集群的节点上运行
tags:
- k8s
希望这些注释对您有所帮助!
V2
以下是一个安装k8s集群的ansible脚本,注释已经标注在代码中:
# 安装k8s集群的ansible脚本
# 定义主机组
[kubernetes]
k8s-master ansible_host=192.168.1.100
k8s-node1 ansible_host=192.168.1.101
k8s-node2 ansible_host=192.168.1.102
# 安装docker
[install_docker]
k8s-master
k8s-node1
k8s-node2
# 安装kubernetes
[install_kubernetes]
k8s-master
k8s-node1
k8s-node2
# 安装docker并配置kubernetes的yum源和kubernetes-cni的yum源
[install_docker_kubernetes_cni]
k8s-master
k8s-node1
k8s-node2
# 安装kubectl命令行工具
[install_kubectl]
k8s-master
# 安装helm
[install_helm]
k8s-master
# 配置kubernetes集群
[configure_kubernetes]
k8s-master
# 安装kubernetes-dashboard
[install_kubernetes_dashboard]
k8s-master
# 安装基础组件
[install_kubernetes_base_components]
k8s-master
k8s-node1
k8s-node2
# 安装网络插件
[install_kubernetes_network_plugins]
k8s-master
k8s-node1
k8s-node2
# 安装metrics-server
[install_metrics_server]
k8s-master
k8s-node1
k8s-node2
# 安装docker
[install_docker]
# 安装docker依赖包
yum install -y yum-utils device-mapper-persistent-data lvm2
# 添加docker的yum源
yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
# 安装docker
yum install -y docker-ce docker-ce-cli containerd.io
# 启动docker
systemctl start docker
# 设置docker开机自启动
systemctl enable docker
# 安装kubernetes
[install_kubernetes]
# 添加kubernetes的yum源
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
EOF
# 安装kubernetes
yum install -y kubelet kubeadm kubectl
# 启动kubelet
systemctl start kubelet
# 设置kubelet开机自启动
systemctl enable kubelet
# 安装docker并配置kubernetes的yum源和kubernetes-cni的yum源
[install_docker_kubernetes_cni]
# 安装docker
include install_docker
# 添加kubernetes-cni的yum源
cat <<EOF > /etc/yum.repos.d/kubernetes-cni.repo
[kubernetes-cni]
name=Kubernetes CNI
baseurl=https://github.com/containernetworking/plugins/releases/download/v0.8.7/
enabled=1
gpgcheck=1
repo_gpgcheck=0
gpgkey=https://raw.githubusercontent.com/containernetworking/plugins/master/docs/deprecation-notices/containernetworking-key.gpg
EOF
# 安装kubectl命令行工具
[install_kubectl]
# 安装kubectl
yum install -y kubectl
# 安装helm
[install_helm]
# 下载helm压缩包
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/master/scripts/get-helm-3
# 安装helm
chmod 700 get_helm.sh
./get_helm.sh
# 配置kubernetes集群
[configure_kubernetes]
# 初始化kubernetes集群
kubeadm init --pod-network-cidr=192.168.0.0/16
# 将kubernetes配置文件复制到普通用户目录下
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
# 安装网络插件
kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml
# 安装kubernetes-dashboard
[install_kubernetes_dashboard]
# 安装kubernetes-dashboard
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
# 创建admin用户
kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.0.0-beta8/aio/deploy/recommended.yaml
kubectl create serviceaccount dashboard-admin-sa
kubectl create clusterrolebinding dashboard-admin-sa --clusterrole=cluster-admin --serviceaccount=default:dashboard-admin-sa
# 获取admin用户的token
kubectl -n default describe secret $(kubectl -n default get secret | grep dashboard-admin-sa-token | awk '{print $1}') | grep 'token:'
# 安装基础组件
[install_kubernetes_base_components]
# 安装metrics-server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
# 安装网络插件
[install_kubernetes_network_plugins]
# 安装calico网络插件
kubectl apply -f https://docs.projectcalico.org/v3.14/manifests/calico.yaml
# 安装metrics-server
[install_metrics_server]
# 安装metrics-server
kubectl apply -f https://github.com/kubernetes-sigs/metrics-server/releases/latest/download/components.yaml
这个脚本基本上覆盖了k8s集群安装的各个环节,包括安装docker、kubernetes、kubectl、helm等基础组件,以及配置kubernetes集群、安装网络插件、安装dashboard、安装metrics-server等。
V3
注:本脚本用于安装Kubernetes v1.21.4 版本的集群,适用于CentOS 7操作系统。
- 安装依赖软件包
- name: Install required packages
yum:
name:
- yum-utils
- device-mapper-persistent-data
- lvm2
state: present
- 添加Docker仓库并安装Docker
- name: Add Docker repository
yum_repository:
name: docker-ce-stable
description: Docker CE Stable - $basearch
baseurl: https://download.docker.com/linux/centos/7/$basearch/stable
gpgcheck: yes
gpgkey: https://download.docker.com/linux/centos/gpg
enabled: yes
when: not docker_installed.stat.exists
- name: Install Docker
yum:
name: docker-ce
state: present
when: not docker_installed.stat.exists
- 启动Docker服务并设置开机自启动
- name: Start Docker service
service:
name: docker
state: started
enabled: yes
- 添加Kubernetes仓库并安装Kubernetes组件
- name: Add Kubernetes repository
yum_repository:
name: kubernetes
description: Kubernetes
baseurl: https://packages.cloud.google.com/yum/repos/kubernetes-el7-x86_64
enabled: yes
gpgcheck: yes
gpgkey: https://packages.cloud.google.com/yum/doc/yum-key.gpg https://packages.cloud.google.com/yum/doc/rpm-package-key.gpg
- name: Install Kubernetes components
yum:
name:
- kubectl
- kubelet
- kubeadm
state: present
- 配置Kubernetes集群
- name: Initialize Kubernetes cluster
command: kubeadm init --pod-network-cidr=10.244.0.0/16
register: kubeadm_init_output
changed_when: "'Your Kubernetes control-plane has initialized successfully' in kubeadm_init_output.stdout"
- name: Copy Kubernetes config to user's home directory
copy:
src: /etc/kubernetes/admin.conf
dest: "{{ lookup('env','HOME') }}/.kube/config"
owner: "{{ lookup('env','USER') }}"
group: "{{ lookup('env','USER') }}"
- 安装Cilium CNI组件
- name: Install Cilium CNI plugin
shell: kubectl apply -f https://raw.githubusercontent.com/cilium/cilium/v1.10/install/kubernetes/quick-install.yaml
when: "'Your Kubernetes control-plane has initialized successfully' in kubeadm_init_output.stdout"
- 安装Local PV CSI组件
- name: Install Local PV CSI plugin
shell: kubectl apply -f https://raw.githubusercontent.com/kubernetes-csi/csi-driver-localpv/master/deploy/kubernetes/localpv-storageclass.yaml
when: "'Your Kubernetes control-plane has initialized successfully' in kubeadm_init_output.stdout"
- 加入其他节点
- name: Join worker nodes to Kubernetes cluster
command: "{{ hostvars[item]['kubeadm_join_command']['stdout'] }}"
with_items: "{{ groups['worker'] }}"
when: "'Your Kubernetes control-plane has initialized successfully' in kubeadm_init_output.stdout"
- 完成提示信息
- name: Print completion message
debug:
msg: "Kubernetes cluster installation completed successfully."