ubuntu安装k8s二进制文件
# 更新包列表并安装 HTTPS 支持
sudo apt-get update && sudo apt-get install -y apt-transport-https
# 创建 keyrings 目录(若已存在不会报错)
sudo mkdir -p /etc/apt/keyrings
# 下载并添加阿里云 Kubernetes 镜像的 GPG 密钥
curl -fsSL https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/deb/Release.key | sudo gpg --dearmor -o /etc/apt/keyrings/kubernetes-apt-keyring.gpg
# 添加 Kubernetes 阿里云镜像源
echo "deb [signed-by=/etc/apt/keyrings/kubernetes-apt-keyring.gpg] https://mirrors.aliyun.com/kubernetes-new/core/stable/v1.28/deb/ /" | sudo tee /etc/apt/sources.list.d/kubernetes.list
# 再次更新包列表以从新添加的 Kubernetes 源中获取信息
sudo apt-get update
# 安装 kubelet、kubeadm 和 kubectl
sudo apt-get install -y kubelet kubeadm kubectl
# 锁定版本,防止自动升级
sudo apt-mark hold kubelet kubeadm kubectl
镜像镜像转移到阿里云
image.sh
kubeadm config images list | grep k8s | while read image; do
# 从 image 中提取镜像名称和标签
image_name=$(echo $image | sed -E 's#(.*)/([^:]+):(.*)#\2:\3#')
tag=$(echo $image | sed -E 's#(.*)/([^:]+):(.*)#\3#')
# 拼接新的镜像地址
new_image="registry.cn-hangzhou.aliyuncs.com/k8s-a/$image_name"
# 检查该镜像是否已经存在本地
if ! docker images | grep -q "$new_image"; then
echo "Pushing image: $new_image"
docker push "$new_image"
else
echo "Image $new_image already exists locally, skipping push."
fi
done
sh image.sh
从阿里云同步镜像到harbor
- 点击仓库管理,点击新建目标,
- 提供者,选择Alibaba ACR,目标名,k8s-a(自己定义),目标URL,https://registry.cn-hangzhou.aliyuncs.com,访问ID,访问密码,对应阿里云的RAM访问的AccessKey
- 点击测试连接
- 点击复制管理,点击新建规则,
- 名称,k8s-a,复制模式,选择Pull-based,源仓库,k8s-a-https://registry.cn-hangzhou.aliyuncs.com,触发模式,手动,点击保存
- 选中k8s-a规则左侧圆点,点击复制,再次点击复制,任务开始执行
- 点击下方任务id,查看复制任务信息,查看成功失败信息,失败的可以查看失败日志
下载镜像和二进制文件
roles/download/tasks/main.yml
同步二进制文件到本地
cat roles/download/tasks/main.yml
---
- name: Download | Prepare working directories and variables
import_tasks: prep_download.yml
when:
- not skip_downloads | default(false)
tags:
- download
- upload
- name: Download | Get kubeadm binary and list of required images
include_tasks: prep_kubeadm_images.yml
when:
- not skip_downloads | default(false)
- inventory_hostname in groups['kube_control_plane']
tags:
- download
- upload
- name: Print combined download variable
debug:
msg: |
Combined Download: {{ downloads | combine(kubeadm_images) | dict2items }}
vars:
download: "{{ download_defaults | combine(item.value) }}"
tags:
- download
- upload
- name: Print combined download variable
debug:
msg: "nerdctl pull {{ download.repo }}: {{ download.tag }}"
loop: "{{ downloads | combine(kubeadm_images) | dict2items }}"
vars:
download: "{{ download_defaults | combine(item.value) }}"
include_file: "download_{% if download.container %}container{% else %}file{% endif %}.yml"
tags:
- download
- upload
- name: Write combined download commands to a file
blockinfile:
path: /tmp/output_file.sh
marker: "# {mark} ANSIBLE GENERATED BLOCK"
block: |
{% for item in downloads | combine(kubeadm_images) | dict2items %}
{% set download = download_defaults | combine(item.value) %}
nerdctl pull {{ download.repo }}:{{ download.tag }}
{% endfor %}
vars:
download_defaults: # 设置默认值
repo: ""
tag: ""
tags:
- download
- upload
- name: Pause for manual intervention
pause:
prompt: "Press Enter to continue or Ctrl+C to stop the playbook"
- name: Download | Download files / images
include_tasks: "{{ include_file }}"
loop: "{{ downloads | combine(kubeadm_images) | dict2items }}"
vars:
download: "{{ download_defaults | combine(item.value) }}"
include_file: "download_{% if download.container %}container{% else %}file{% endif %}.yml"
when:
- not skip_downloads | default(false)
- download.enabled
- item.value.enabled
- (not (item.value.container | default(false))) or (item.value.container and download_container)
- (download_run_once and inventory_hostname == download_delegate) or (group_names | intersect(download.groups) | length)
初始化k8s
#没有配置好,想办法下载一个指定版本的git代码包
git clone --branch v1.29.0 --depth 1 https://github.com/kubernetes/kubernetes.git kubernetesv1.29.0
#安装ansible
pip install ansible-core==2.17 -i https://pypi.tuna.tsinghua.edu.cn/simple/
#安装开发包
yum install python3-pip python3-devel -y
#设置全局python版本
pyenv global 3.12.0
#安装k8s集群
ansible-playbook -i inventory/mycluster/inventory.ini --become cluster.yml
安装集群报错