本文是通过ansible-playbook
的roles功能实现二进制批量自动安装部署Kubernetes
集群服务。本想做成离线版本,但由于coredns,ingress,dashboard插件需要拉取镜像,(这里把flannel做成非容器安装版)如需容器版去https://github.com/flannel-io/flannel中获取yaml文件
目录结构:
[root@ceph01 ~]$tree -L 2 k8s/
k8s/
├── ansible.cfg
├── group_vars
│ └── all.yml
├── hosts
├── roles
│ ├── addons # 部署k8s插件目录
│ ├── common # 系统初始化目录
│ ├── docker # docker安装
│ ├── etcd # etcd安装
│ ├── master # master节点
│ ├── nginx # ingrees代理Nginx
│ ├── node # node节点
│ └── tls # 证书生成
└── single.yml
代码GitHub下载:https://github.com/5279314/ansible-k8s
[root@ansible ~/k8s]$cat group_vars/all.yml
# 安装目录
software_dir: '/server/tools' # 所需软件存放目录
k8s_work_dir: '/opt/kubernetes' # k8s的安装目录
etcd_work_dir: '/opt/etcd' # etcd的安装目录
tmp_dir: '/tmp/k8s' # 其他文件存放目录
# 集群网络
service_cidr: '192.168.0.0/16'
localhost_dns: '10.1.1.2' # 本地dns
cluster_dns: '192.168.0.2' # 与roles/addons/files/coredns.yaml中IP一致,并且是service_cidr中的IP;
pod_cidr: "172.7.0.0/16" # 容器所需IP段
service_nodeport_range: '30000-32767' # 暴露端口范围
cluster_domain: 'cluster.local'
# 自签证书可信任IP列表,为方便扩展,可添加多个预留IP
cert_hosts:
# 包含所有LB、VIP、Master IP和service_cidr的第一个IP
k8s:
- 127.0.0.1
- 192.168.0.1
- 10.1.1.50
- 10.1.1.60
- 10.1.1.100
- 10.1.1.110
- 10.1.1.120
- 10.1.1.130
- 10.1.1.150
# 包含所有etcd节点IP
etcd:
- 10.1.1.100
- 10.1.1.130
- 10.1.1.120
# k8s插件镜像
coredns_images: 'coredns/coredns:1.8.0'
dashboard_images: 'kubernetesui/dashboard:v2.0.4'
traefik_images: 'traefik:v1.7.26'
[root@ansible ~/k8s]$ls /server/tools/
cfssl docker-20.10.1.tgz
cfssl-certinfo etcd-v3.3.22-linux-amd64.tar.gz
cfssljson flannel-v0.11.0-linux-amd64.tar.gz
cfssl.tar.gz kubernetes-1.20.11.tar.gz
cni-plugins-linux-amd64-v0.8.7.tgz
[root@k8s-master1 ~]# kubectl get pod -A -o wide
NAMESPACE NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
kube-system coredns-5b5b4cb755-w7gvf 1/1 Running 0 67m 172.7.3.2 k8s-node1 <none> <none>
kube-system kubernetes-dashboard-757b689f8b-vbv7f 1/1 Running 0 67m 172.7.61.2 k8s-node2 <none> <none>
kube-system traefik-ingress-controller-c88t2 1/1 Running 0 67m 172.7.3.3 k8s-node1 <none> <none>
kube-system traefik-ingress-controller-d7lpk 1/1 Running 0 67m 172.7.61.3 k8s-node2 <none> <none>