02.配置Kubernetes集群主机清单并对操作系统基本设置

2023-12-28 21:04:03

1、安装ansible、并编辑主机清单文件

主机名IP服务角色
rocky01.examp.io192.168.36.151apiserver/scheduler/controller-manager/kubelet/kube-proxy主节点
rocky01.examp.io192.168.36.152apiserver/scheduler/controller-manager/kubelet/kube-proxy主节点
rocky01.examp.io192.168.36.153etcd/kubelet/kube-proxy工作节点
rocky01.examp.io192.168.36.154etcd/kubelet/kube-proxy工作节点
rocky01.examp.io192.168.36.155etcd/kubelet/kube-proxy工作节点
docker.examp.io192.168.36.128kubectl/dnsmasq/ansible客户端

网段选择:

  • 虚拟机网段:192.168.36.0/34
  • Pod网络CIDR:10.244.0.0/16
  • Service CIDR:172.20.0.0/16

完成前面的主机和网络规划,现在可以开始进行部署了

首先是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

2、完成前面的基本准备以后,就可以开始进行安装操作了

安装的第一步是对操作系统进行基础的设置主要包括以下内容:

  • 禁用交换分区
  • 关闭防火墙
  • 禁用selinux
  • 安装必要的工具软件
  • 启用时间同步服务
  • 配置统一的时区

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   

文章来源:https://blog.csdn.net/yxydde/article/details/135220015
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。