kubespray部署k8s 1.26集群安装指南

news/2024/12/5 4:40:23/

Kubespray 是一个自由开源的工具,它提供了 Ansible 剧本(playbook) 来部署和管理 Kubernetes 集群。它旨在简化跨多个节点的 Kubernetes 集群的安装过程,允许用户快速轻松地部署和管理生产就绪的 Kubernetes 集群。

它支持一系列操作系统,包括 Ubuntu、CentOS、Rocky Linux 和 Red Hat Enterprise Linux(RHEL),它可以在各种平台上部署 Kubernetes,包括裸机、公共云和私有云。

一、K8S集群节点准备

1.1 主机列表

在这里插入图片描述

1.2 主机名解析

hostnamectl set-hostname k8s-master01 && bash  #k8s-master01上执行
hostnamectl set-hostname k8s-master02 && bash  #k8s-master02上执行
hostnamectl set-hostname k8s-master03 && bash #k8s-master03上执行
hostnamectl set-hostname k8s-worker01 && bash  #k8s-worker01上执行
hostnamectl set-hostname k8s-worker02 && bash  #k8s-worker02上执行
hostnamectl set-hostname kubespray && bash    #kubespray上执行cat >> /etc/hosts << EOF
192.192.168.0.2 k8s-master01
192.168.0.15 k8s-master02
192.168.0.183 k8s-master03
192.168.0.136 k8s-worker01
192.168.0.71 k8s-worker02
192.168.0.223 kubespray
EOF

1.3 kubespray节点python3准备(所有操作都在kubespray上)

yum install -y ncurses-devel gdbm-devel xz-devel sqlite-devel tk-devel uuid-devel readline-devel bzip2-devel libffi-devel curl wget
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
yum install -y openssl-devel openssl11 openssl11-devel[root@kubespray ~]# openssl11 version
OpenSSL 1.1.1k  FIPS 25 Mar 2021

1.3.2 安装python 3.10.4

mkdir -p /doc/temp && cd /doc/temp
wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tgz

编译主要需要注意的问题是设置编译FLAG,以便使用最新的openssl库。

export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)
echo $CFLAGS
#显示结果 -I/usr/include/openssl11
echo $LDFLAGS
#显示结果  -L/usr/lib64/openssl11 -lssl -lcrypto
tar xf Python-3.10.4.tgz
cd Python-3.10.4/
./configure --enable-optimizations && make altinstall
python3.10 --version #显示如下版本
#Python 3.10.4
pip3.10 --version #显示如下版本
#pip 22.0.4 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)ln -sf /usr/local/bin/python3.10 /usr/bin/python3
ln -sf /usr/local/bin/pip3.10  /usr/bin/pip3

1.4 kubespray源文件获取

[root@kubespray ~]# git clone https://github.com/kubernetes-sigs/kubespray.git
[root@kubespray ~]# ls
kubespray 
[root@kubespray ~]# cd kubespray/
[root@kubespray kubespray]# ls
ansible.cfg         CONTRIBUTING.md  inventory  OWNERS_ALIASES             RELEASE.md             roles              setup.py
cluster.yml         Dockerfile       library    pipeline.Dockerfile        remove-node.yml        run.rc             test-infra
CNAME               docs             LICENSE    playbooks                  requirements-2.11.txt  scale.yml          tests
code-of-conduct.md  extra_playbooks  logo       plugins                    requirements-2.12.txt  scripts            upgrade-cluster.yml
_config.yml         galaxy.yml       Makefile   README.md                  requirements.txt       SECURITY_CONTACTS  Vagrantfile
contrib             index.html       OWNERS     recover-control-plane.yml  reset.yml              setup.cfg

1.5 kubespray环境准备

[root@kubespray kubespray]# pip3 install -r requirements.txt
[root@kubespray kubespray]# ansible --version
ansible [core 2.12.5]config file = /root/kubespray/ansible.cfgconfigured module search path = ['/root/kubespray/library']ansible python module location = /usr/local/lib/python3.10/site-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/local/bin/ansiblepython version = 3.10.4 (main, Apr 27 2023, 10:58:46) [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)]jinja version = 3.1.2libyaml = True

1.6 创建主机清单

[root@kubespray kubespray]# ls inventory/
local  sample
[root@kubespray kubespray]# cp -rfp inventory/sample inventory/mycluster
[root@kubespray kubespray]# ls inventory/
local  mycluster  sample
[root@kubespray kubespray]# declare -a IPS=(192.168.0.2 192.168.0.15 192.168.0.183 192.168.0.136 192.168.0.71)
[root@kubespray kubespray]# ls inventory/mycluster/
group_vars  inventory.ini  patches[root@kubespray kubespray]# CONFIG_FILE=inventory/mycluster/hosts.yaml python3 contrib/inventory_builder/inventory.py ${IPS[@]}DEBUG: Adding group all
DEBUG: Adding group kube_control_plane
DEBUG: Adding group kube_node
DEBUG: Adding group etcd
DEBUG: Adding group k8s_cluster
DEBUG: Adding group calico_rr
DEBUG: adding host node1 to group all
DEBUG: adding host node2 to group all
DEBUG: adding host node3 to group all
DEBUG: adding host node4 to group all
DEBUG: adding host node5 to group all
DEBUG: adding host node1 to group etcd
DEBUG: adding host node2 to group etcd
DEBUG: adding host node3 to group etcd
DEBUG: adding host node1 to group kube_control_plane
DEBUG: adding host node2 to group kube_control_plane
DEBUG: adding host node1 to group kube_node
DEBUG: adding host node2 to group kube_node
DEBUG: adding host node3 to group kube_node
DEBUG: adding host node4 to group kube_node
DEBUG: adding host node5 to group kube_node
[root@kubespray kubespray]# ls inventory/mycluster/
group_vars  hosts.yaml  inventory.ini  patches
[root@kubespray kubespray]# cat inventory/mycluster/hosts.yaml
all:hosts:node1:ansible_host: 192.168.0.2ip: 192.168.0.2access_ip: 192.168.0.2node2:ansible_host: 192.168.0.15ip: 192.168.0.15access_ip: 192.168.0.15node3:ansible_host: 192.168.0.183ip: 192.168.0.183access_ip: 192.168.0.183node4:ansible_host: 192.168.0.136ip: 192.168.0.136access_ip: 192.168.0.136node5:ansible_host: 192.168.0.71ip: 192.168.0.71access_ip: 192.168.0.71children:kube_control_plane:hosts:node1:node2:kube_node:hosts:node1:node2:node3:node4:node5:etcd:hosts:node1:node2:node3:k8s_cluster:children:kube_control_plane:kube_node:calico_rr:hosts: {}修改为:添加了一个master,删除了二个node
[root@kubespray kubespray]# cat inventory/mycluster/hosts.yaml
all:hosts:node1:ansible_host: 192.168.0.2ip: 192.168.0.2access_ip: 192.168.0.2node2:ansible_host: 192.168.0.15ip: 192.168.0.15access_ip: 192.168.0.15node3:ansible_host: 192.168.0.183ip: 192.168.0.183access_ip: 192.168.0.183node4:ansible_host: 192.168.0.136ip: 192.168.0.136access_ip: 192.168.0.136node5:ansible_host: 192.168.0.71ip: 192.168.0.71access_ip: 192.168.0.71children:kube_control_plane:hosts:node1:node2:node3:kube_node:hosts:node4:node5:etcd:hosts:node1:node2:node3:k8s_cluster:children:kube_control_plane:kube_node:calico_rr:hosts: {}

1.7 准备K8S集群配置文件

[root@kubespray kubespray]# cat inventory/mycluster/group_vars/k8s_cluster/k8s-cluster.yml
---
# Kubernetes configuration dirs and system namespace.
# Those are where all the additional config stuff goes
# the kubernetes normally puts in /srv/kubernetes.
# This puts them in a sane location and namespace.
# Editing those values will almost surely break something.
kube_config_dir: /etc/kubernetes
kube_script_dir: "{{ bin_dir }}/kubernetes-scripts"
kube_manifest_dir: "{{ kube_config_dir }}/manifests"# This is where all the cert scripts and certs will be located
kube_cert_dir: "{{ kube_config_dir }}/ssl"# This is where all of the bearer tokens will be stored
kube_token_dir: "{{ kube_config_dir }}/tokens"kube_api_anonymous_auth: true## Change this to use another Kubernetes version, e.g. a current beta release
kube_version: v1.26.3# Where the binaries will be downloaded.
# Note: ensure that you've enough disk space (about 1G)
local_release_dir: "/tmp/releases"
# Random shifts for retrying failed ops like pushing/downloading
retry_stagger: 5# This is the user that owns tha cluster installation.
kube_owner: kube修改:重点观察20、70、76、81、160行等
默认可以不用修改。

1.8 准备k8s集群插件文件

要启用 Kuberenetes 仪表板和入口控制器等插件,请在文件inventory/mycluster/group_vars/k8s_cluster/addons.yml 中将参数设置为已启用:

根据自身业务需要开启对应的服务即可。例如:
[root@kubespray kubespray]# vim inventory/mycluster/group_vars/k8s_cluster/addons.yml
1 ---2 # Kubernetes dashboard3 # RBAC required. see docs/getting-started.md for access details.4 dashboard_enabled: true56 # Helm deployment7 helm_enabled: false89 # Registry deployment10 registry_enabled: false11 # registry_namespace: kube-system12 # registry_storage_class: ""13 # registry_disk_size: "10Gi"1415 # Metrics Server deployment16 metrics_server_enabled: false

1.9 准备ssh密钥

1.9.1 在kubespray主机生成ssh密钥

[root@kubespray ~]# ssh-keygen

1.9.2 使用ssh-copy-id复制ssh密钥到k8s集群节点主机

[root@kubespray ~]# ssh-copy-id root@192.168.0.2
[root@kubespray ~]# ssh-copy-id root@192.168.0.15
[root@kubespray ~]# ssh-copy-id root@192.168.0.183
[root@kubespray ~]# ssh-copy-id root@192.168.0.136
[root@kubespray ~]# ssh-copy-id root@192.168.0.71

1.9.3 在K8S集群节点添加sysops用户指行授权

所有的k8s集群节点

echo "sysops ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/sysops

1.10 k8s集群主机安全设置

[root@kubespray ~]# cd kubespray/[root@kubespray kubespray]# ansible all -i inventory/mycluster/hosts.yaml -m shell -a "systemctl stop firewalld && systemctl disable firewalld"

1.11 k8s集群主机路由转发设置

[root@kubespray kubespray]# ansible all -i inventory/mycluster/hosts.yaml -m shell -a "echo 'net.ipv4.ip_forward=1' | tee -a /etc/sysctl.conf"

1.12 禁用swap分区

[root@kubespray kubespray]# ansible all -i inventory/mycluster/hosts.yaml -m shell -a "sed -i '/ swap / s/^\(.*\)$/#\1/g' /etc/fstab &&  swapoff -a"

二、k8s集群部署及可用性验证

[root@kubespray ~]# cd kubespray/
[root@kubespray kubespray]# ansible-playbook -i inventory/mycluster/hosts.yaml --become --become-user=root cluster.yml
#如果没有执行成功,可以多次执行。
[root@k8s-master01 ~]# kubectl create deployment demo-nginx-kubespray --image=nginx --replicas=2
deployment.apps/demo-nginx-kubespray created[root@k8s-master01 ~]# kubectl get pods
NAME                                   READY   STATUS              RESTARTS   AGE
demo-nginx-kubespray-b65cf84cd-jzkzf   1/1     Running             0          16sdemo-nginx-kubespray-b65cf84cd-v2nv4   0/1     ContainerCreating   0          16s
[root@k8s-master01 ~]# kubectl expose deployment demo-nginx-kubespray --type NodePort --port=80
service/demo-nginx-kubespray exposed[root@k8s-master01 ~]# kubectl get svc
NAME                   TYPE        CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
demo-nginx-kubespray   NodePort    10.233.7.87   <none>        80:30532/TCP   4s
kubernetes             ClusterIP   10.233.0.1    <none>        443/TCP        16m[root@k8s-master01 ~]# kubectl get  deployments.apps
NAME                   READY   UP-TO-DATE   AVAILABLE   AGE
demo-nginx-kubespray   2/2     2            2           116s[root@k8s-master01 ~]# kubectl get pods
NAME                                   READY   STATUS    RESTARTS   AGE
demo-nginx-kubespray-b65cf84cd-jzkzf   1/1     Running   0          44s
demo-nginx-kubespray-b65cf84cd-v2nv4   1/1     Running   0          44s[root@k8s-master01 ~]# kubectl get svc demo-nginx-kubespray
NAME                   TYPE       CLUSTER-IP    EXTERNAL-IP   PORT(S)        AGE
demo-nginx-kubespray   NodePort   10.233.7.87   <none>        80:30532/TCP   17s

三、移除节点

不用修改hosts.yaml文件

[root@kubespray kubespray]# ansible-playbook -i inventory/mycluster/hosts.yaml --become --become-user=root remove-node.yml -v -b --extra-vars "node=node5"

四、增加节点

需要修改hosts.yaml文件,在inventory/mycluster/hosts.yaml中添加新增节点信息

[root@kubespray kubespray]# cat inventory/mycluster/hosts.yaml
all:hosts:node1:ansible_host: 192.168.0.2ip: 192.168.0.2access_ip: 192.168.0.2node2:ansible_host: 192.168.0.15ip: 192.168.0.15access_ip: 192.168.0.15node3:ansible_host: 192.168.0.183ip: 192.168.0.183access_ip: 192.168.0.183node4:  添加ansible_host: x.x.x.xip: x.x.x.xaccess_ip: x.x.x.xnode5:  添加ansible_host: xx.xx.xx.xxip: xx.xx.xx.xxaccess_ip: xx.xx.xx.xxchildren:kube_control_plane:hosts:node1:node2:node3:kube_node:hosts:node4:  x.x.x.x #添加node5:  xx.xx.xx.xx #添加etcd:hosts:node1:node2:node3:k8s_cluster:children:kube_control_plane:kube_node:calico_rr:hosts: {}
[root@kubespray kubespray]# ansible-playbook -i inventory/mycluster/hosts.yaml --become --become-user=root scale.yml -v -b

五、清理k8s集群

[root@kubespray ~]# cd kubespray/
[root@kubespray kubespray]# ansible-playbook -i inventory/mycluster/hosts.yaml  --become --become-user=root reset.yml

六、修改默认容器镜像仓库

[root@kubespray ~]# cd kubespray/[root@kubespray kubespray]# vim extra_playbooks/roles/download/defaults/main.yml
#把第 93行:kube_image_repo: "registry.k8s.io"里面的registry.k8s.io
#修改为:registry.aliyuncs.com/google_containers

http://www.ppmy.cn/news/66566.html

相关文章

力扣 139. 单词拆分

一、题目描述 给你一个字符串 s 和一个字符串列表 word_dict 作为字典。请你判断是否可以利用字典中出现的单词拼接出 s。 注意&#xff1a;不要求字典中出现的单词全部都使用&#xff0c;并且字典中的单词可以重复使用。 示例 1&#xff1a; 输入: s "leetcode"…

Appium环境搭建及元素定位

Appium简介 Appium是一个开源测试自动化框架&#xff0c;可用于原生&#xff0c;混合和移动Web应用程序测试。它使用WebDriver 协议驱动iOS&#xff0c;Android和Windows应用程序。 01 环境搭建步骤 Appium环境安装&#xff1a; 第一步 安装 appium 桌面版客户端 Appium-1…

【动态代理】JDK动态代理与cglib动态代理源码解析

JDK动态代理 demo展示 UserService&#xff0c;接口类 public interface UserService {void addUser(); }UserServiceImpl&#xff0c;实现类 public class UserServiceImpl implements UserService {Overridepublic void addUser() {System.out.println("register al…

Liunx telnet 命令详解

文章目录 telnet补充说明语法选项参数实例 telnet 登录远程主机和管理(测试ip端口是否连通) 补充说明 telnet命令 用于登录远程主机&#xff0c;对远程主机进行管理。telnet因为采用明文传送报文&#xff0c;安全性不好&#xff0c;很多Linux服务器都不开放telnet服务&#…

【Java多线程编程】线程的六种状态

前言&#xff1a; 在我们进行多线程编程&#xff0c;脑海里会想到线程运行的状态到底是什么&#xff1f;因此我整理出这线程的状态这篇博文。线程的状态分为六种&#xff1a;新建状态&#xff08;NEW&#xff09;、就绪状态&#xff08;RUNNABLE&#xff09;、阻塞状态&#xf…

黑马头条(学习笔记)

​ 目录 一. 项目概述 二、项目初始化 移动端 REM 适配&#xff1a; 关于 PostCSS 配置文件&#xff1a; Autoprefixer 插件的配置 &#xff1a; postcss-pxtorem 插件的配置&#xff1a; 关于字体图标: 配置路由&#xff1a; 封装请求模块: 三&#xff1a;登录注册&…

lua实战(1)

目录 IDELua中的名称Lua是一种区分大小写的语言 Lua 是一个小巧的脚本语言。它是巴西里约热内卢天主教大学(Pontifical Catholic University of Rio de Janeiro)里的一个由Roberto Ierusalimschy、Waldemar Celes 和 Luiz Henrique de Figueiredo三人所组成的研究小组于1993年开…

ISO9001是什么?ISO9000和ISO9001有何关系?

ISO 9000和ISO 9001是质量管理领域的两个重要标准。它们被用来确保组织能够提供符合客户要求的产品和服务&#xff0c;同时不断提高其业务效率和质量水平。本文将探讨ISO 9000和ISO 9001之间的关系&#xff0c;解释它们的区别以及为什么对企业非常重要。 什么是ISO9000和ISO90…