主机名 | IP | 服务 | 角色 |
---|---|---|---|
rocky01.examp.io | 192.168.36.151 | apiserver/scheduler/controller-manager/kubelet/kube-proxy | 主节点 |
rocky01.examp.io | 192.168.36.152 | apiserver/scheduler/controller-manager/kubelet/kube-proxy | 主节点 |
rocky01.examp.io | 192.168.36.153 | etcd/kubelet/kube-proxy | 工作节点 |
rocky01.examp.io | 192.168.36.154 | etcd/kubelet/kube-proxy | 工作节点 |
rocky01.examp.io | 192.168.36.155 | etcd/kubelet/kube-proxy | 工作节点 |
docker.examp.io | 192.168.36.128 | kubectl/dnsmasq/ansible | 客户端 |
网段选择:
完成前面的主机和网络规划,现在可以开始进行部署了
首先是Clone ansible脚本仓库
Ansible 脚本仓库(订阅专栏后私信我开通权限) https://gitee.com/yxydde/ansible_k8s.git
# 在客户端节点安装 ansible (CentOS需要安装对应版本的epel源)
~]# yum install ansible -y
# Clone Ansible 脚本仓库(里面有联系方式,有疑问可以私信我) https://gitee.com/yxydde/ansible_k8s.git
~]# git clone https://gitee.com/yxydde/ansible_k8s.git
~]# cd ansible_k8s
# 配置主机清单文件
~]# vi example/hosts.multi-node
[etcd]
192.168.36.153 NODE_NAME=etcd1
192.168.36.154 NODE_NAME=etcd2
192.168.36.155 NODE_NAME=etcd3
[kube_master]
192.168.36.151
192.168.36.152
[kube_node]
192.168.36.151
192.168.36.152
192.168.36.153
192.168.36.154
192.168.36.155
[ingress_node]
192.168.36.153
192.168.36.154
192.168.36.155
[kube_client]
localhost
# 可选,使用 haproxy + keepalive 高可用方案是需要进行配置
[ext_lb]
192.168.36.151 ROLE=MASTER PRIORITY=160
192.168.36.151 ROLE=BACKUP PRIORITY=110
[etcd:vars]
# Etcd服务内存配置,默认2G,建议调整到8G
ETCD_QUOTA_BACKEND_BYTES="2147483648"
[all:vars]
# 下载的containerd的二进制包路径
CONTAINERD_PKG="/root/cri-containerd-cni-1.7.11-linux-amd64.tar.gz"
# ingress nginx 日志路径的 hostPath
NGINX_LOG_HOST_PATH="/var/log/nginx/"
# 使用 haproxy + keepalive 高可用方案是需要进行配置为高可用的虚拟IP
# 选用本地代理时使用127.0.0.1即可
APISERVER_LB_IP="127.0.0.1"
# apiserver HTTPS 端口
APISERVER_SECURE_PORT=6443
# apiserver 负载均衡器端口,避免和 APISERVER_SECURE_PORT 冲突
APISERVER_LB_PORT=5443
K8S_LOG_DIR="/var/log/kubernetes"
KUBELET_ROOT_DIR="/var/lib/kubelet"
CONTAINERD_ROOT_DIR="/var/lib/containerd"
# 生成CA证书文件的本地路径
PKI_GEN_DIR="{{ inventory_dir }}/pki"
YAML_GEN_DIR="{{ inventory_dir }}/yaml"
# K8S 集群名称
CLUSTER_NAME="kubernetes"
# K8S 日志级别
DEFAULT_LOG_LEVEL=1
# Service IP 网段,默认 10.96.0.0/12
SERVICE_CLUSTER_IP_RANGE="172.20.0.0/16"
# SERVICE_CLUSTER_IP_RANGE 的第1个IP
MASTER_CLUSTER_IP="172.20.0.1"
# SERVICE_CLUSTER_IP_RANGE 的第10个IP
DNS_SERVER_IP="172.20.0.10"
# Calico CIDR default 192.168.0.0/16
# Flannel CIDR default 10.244.0.0/16
CLUSTER_CIDR="10.244.0.0/16"
# PAUSE POD 镜像地址
POD_INFRA_CONTAINER_IMAGE="registry.cn-beijing.aliyuncs.com/kube-mirrors/pause:3.9"
# https://kubernetes.io/docs/reference/access-authn-authz/bootstrap-tokens/
BOOTSTRAP_TOKEN_ID="07401b"
BOOTSTRAP_TOKEN_SECRET="rany0t9iuijdk42b"
# dashboard 域名
DASHBOARD_DOMAIN="das.example.io"
LONGHORN_UI_DOMAIN="longhorn.example.io"
# 私有镜像仓库地址 https://github.com/containerd/containerd/blob/main/docs/hosts.md
PRIVATE_REGISTRY="http://harbor.example.io"
# ansible节点到其它节点的ssh用户名密码,如果配置了ssh免密,则不需要配置
ansible_ssh_user = root
ansible_ssh_pass = root
安装的第一步是对操作系统进行基础的设置主要包括以下内容:
task的具体脚本路径 https://gitee.com/yxydde/ansible_k8s/tree/main/roles/prepare/tasks
# 执行 01.prepare.yml 脚本
ansible_k8s]# ansible-playbook -i example/hosts.multi-node 01.prepare.yml
PLAY [kube_master,kube_node,etcd] ********************************************************************************************************************************************
TASK [Gathering Facts] ********************************************************************************************************************************************
ok: [192.168.36.151]
ok: [192.168.36.152]
ok: [192.168.36.154]
ok: [192.168.36.155]
ok: [192.168.36.153]
TASK [prepare : show hosts information] ********************************************************************************************************************************************
ok: [192.168.36.153] => {
"msg": "Rocky-9-Blue Onyx"
}
ok: [192.168.36.154] => {
"msg": "Rocky-9-Blue Onyx"
}
ok: [192.168.36.152] => {
"msg": "Rocky-9-Blue Onyx"
}
ok: [192.168.36.151] => {
"msg": "Rocky-9-Blue Onyx"
}
ok: [192.168.36.155] => {
"msg": "Rocky-9-Blue Onyx"
}
TASK [prepare : yum install required tools] ********************************************************************************************************************************************
ok: [192.168.36.151]
ok: [192.168.36.152]
ok: [192.168.36.153]
ok: [192.168.36.154]
ok: [192.168.36.155]
TASK [prepare : stop firewalld.service] ********************************************************************************************************************************************
ok: [192.168.36.151]
ok: [192.168.36.152]
ok: [192.168.36.154]
ok: [192.168.36.153]
ok: [192.168.36.155]
TASK [prepare : disable selinux] ********************************************************************************************************************************************
ok: [192.168.36.151]
ok: [192.168.36.152]
ok: [192.168.36.154]
ok: [192.168.36.155]
ok: [192.168.36.153]
TASK [prepare : setenforce 0] ********************************************************************************************************************************************
skipping: [192.168.36.151]
skipping: [192.168.36.152]
skipping: [192.168.36.153]
skipping: [192.168.36.154]
skipping: [192.168.36.155]
TASK [prepare : selinux status] ********************************************************************************************************************************************
ok: [192.168.36.151] => {
"msg": "SElinux: disabled"
}
ok: [192.168.36.152] => {
"msg": "SElinux: disabled"
}
ok: [192.168.36.153] => {
"msg": "SElinux: disabled"
}
ok: [192.168.36.154] => {
"msg": "SElinux: disabled"
}
ok: [192.168.36.155] => {
"msg": "SElinux: disabled"
}
TASK [prepare : apt install required tools] ********************************************************************************************************************************************
skipping: [192.168.36.151]
skipping: [192.168.36.152]
skipping: [192.168.36.153]
skipping: [192.168.36.154]
skipping: [192.168.36.155]
TASK [prepare : Disable firewall with UFW] ********************************************************************************************************************************************
skipping: [192.168.36.151]
skipping: [192.168.36.152]
skipping: [192.168.36.153]
skipping: [192.168.36.154]
skipping: [192.168.36.155]
TASK [prepare : enable chronyd.service] ********************************************************************************************************************************************
ok: [192.168.36.151]
ok: [192.168.36.152]
ok: [192.168.36.154]
ok: [192.168.36.153]
ok: [192.168.36.155]
TASK [prepare : Set TimeZone to Asia/Shanghai] ********************************************************************************************************************************************
ok: [192.168.36.151]
ok: [192.168.36.153]
ok: [192.168.36.152]
ok: [192.168.36.154]
ok: [192.168.36.155]
TASK [prepare : Remove swapfile from /etc/fstab] ********************************************************************************************************************************************
ok: [192.168.36.151] => (item=swap)
ok: [192.168.36.152] => (item=swap)
ok: [192.168.36.154] => (item=swap)
ok: [192.168.36.153] => (item=swap)
ok: [192.168.36.155] => (item=swap)
ok: [192.168.36.151] => (item=none)
ok: [192.168.36.152] => (item=none)
ok: [192.168.36.154] => (item=none)
ok: [192.168.36.153] => (item=none)
ok: [192.168.36.155] => (item=none)
TASK [prepare : disable swap] ********************************************************************************************************************************************
ok: [192.168.36.152]
ok: [192.168.36.151]
ok: [192.168.36.153]
ok: [192.168.36.155]
ok: [192.168.36.154]
PLAY RECAP ********************************************************************************************************************************************
192.168.36.151 : ok=10 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
192.168.36.152 : ok=10 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
192.168.36.153 : ok=10 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
192.168.36.154 : ok=10 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0
192.168.36.155 : ok=10 changed=0 unreachable=0 failed=0 skipped=3 rescued=0 ignored=0