Ansible通過kubernetes.core.k8s_info和kubernetes.core.k8s訪問OCP

文章目錄

  • 環境
    • OCP
    • Client(Ansible控制節點)
  • 步驟
    • 準備工作
      • 在client端配置ssh免密登錄OCP端
      • 在client端安裝Ansible
    • kubernetes.core.k8s_info
    • 第1次嘗試
    • 在OCP端安裝python和pip3
    • 在OCP端安裝kubernetes
    • 在OCP端安裝PyYAML
    • 第2次嘗試
    • 在OCP端配置config文件
    • 第3次嘗試
    • 其它問題
      • PyYAML
      • 安裝/更新kubectl(可選)
    • kubernetes.core.k8s
  • 參考

環境

OCP

  • api.kai1212.cp.fyre.ibm.com
    • Red Hat Enterprise Linux release 8.8 (Ootpa)

    • OpenShift 4.14.1

      • Red Hat Enterprise Linux CoreOS release 4.14
    • Python 3.6.8

    • pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

    • oc

      [root@api.kai1212.cp.fyre.ibm.com ~]# oc version
      Client Version: 4.14.1
      Kustomize Version: v5.0.1
      Server Version: 4.14.1
      Kubernetes Version: v1.27.6+f67aeb3
      
    • kubectl

      [root@api.kai1212.cp.fyre.ibm.com ~]# kubectl version
      WARNING: This version information is deprecated and will be replaced with the output from kubectl version --short.  Use --output=yaml|json to get the full version.
      Client Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.4", GitCommit:"0c63f9da2694c080257111616c60005f32a5bf47", GitTreeState:"clean", BuildDate:"2023-10-20T23:16:49Z", GoVersion:"go1.20.10 X:strictfipsruntime", Compiler:"gc", Platform:"linux/amd64"}
      Kustomize Version: v5.0.1
      Server Version: version.Info{Major:"1", Minor:"27", GitVersion:"v1.27.6+f67aeb3", GitCommit:"f3ec0ed759cde48849b6e3117c091b7db90c95fa", GitTreeState:"clean", BuildDate:"2023-10-20T22:20:44Z", GoVersion:"go1.20.10 X:strictfipsruntime", Compiler:"gc", Platform:"linux/amd64"}
      

Client(Ansible控制節點)

  • kairedhat91.fyre.ibm.com
    • Red Hat Enterprise Linux release 9.2 (Plow)
    • Ansible 2.14.9

步驟

準備工作

在client端配置ssh免密登錄OCP端

ssh-keygen -t rsa 命令生成密鑰對,然后把公鑰( /root/.ssh/id_rsa.pub )的內容添加到OCP端( api.kai1212.cp.fyre.ibm.com )的 /root/.ssh/authorized_keys 文件里。

測試一下ssh登錄,確保能夠免密登錄。

在client端安裝Ansible

在client端安裝Ansible:

yum install ansible

如果報錯:

No match for argument: ansible
Error: Unable to find a match: ansible

則需要配置repo源。修改 /etc/yum.repos.d/redhat.repo ,添加如下內容:

[epel]
name=epel
baseurl=https://mirrors.aliyun.com/epel/9/Everything/x86_64/
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-9

然后再安裝,就OK了。

......
......
Installed:ansible-1:7.7.0-1.el9.noarch           ansible-core-1:2.14.9-1.el9.x86_64      python3-cffi-1.14.5-5.el9.x86_64         python3-cryptography-36.0.1-4.el9.x86_64    python3-packaging-20.9-5.el9.noarch    python3-ply-3.11-14.el9.noarchpython3-pycparser-2.20-6.el9.noarch    python3-pyparsing-2.4.7-9.el9.noarch    python3-resolvelib-0.5.4-5.el9.noarch    sshpass-1.09-4.el9.x86_64Complete!

檢查Ansible版本:

[root@kairedhat91 ~]# ansible --version
ansible [core 2.14.9]config file = /etc/ansible/ansible.cfgconfigured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']ansible python module location = /usr/lib/python3.9/site-packages/ansibleansible collection location = /root/.ansible/collections:/usr/share/ansible/collectionsexecutable location = /usr/bin/ansiblepython version = 3.9.18 (main, Sep  7 2023, 00:00:00) [GCC 11.4.1 20230605 (Red Hat 11.4.1-2)] (/usr/bin/python3)jinja version = 3.1.2libyaml = True

編輯 /etc/ansible/hosts ,添加如下內容:

[myvm]
api.kai1212.cp.fyre.ibm.com

測試一下Ansible連通性:

[root@kairedhat91 ~]# ansible all -m ping
api.kai1212.cp.fyre.ibm.com | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/libexec/platform-python"},"changed": false,"ping": "pong"
}

kubernetes.core.k8s_info

在client端創建 test1.yml 如下:

---
- hosts: alltasks:- name: task1kubernetes.core.k8s_info:api_version: v1kind: Namespacename: myns1register: var1- name: task2debug:msg: "{{ var1 }}"

該腳本會嘗試讀取名為 myns1 namespace信息,當然,該namespace目前并不存在。

第1次嘗試

運行 ansible-playbook test1.yml ,報錯如下:

TASK [task1] **************************************************************************************************************************************************************************************************************************************
fatal: [api.kai1212.cp.fyre.ibm.com]: FAILED! => {"changed": false, "msg": "Failed to import the required Python library (kubernetes) on api.kai1212.cp.fyre.ibm.com's Python /usr/libexec/platform-python. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter"}

這是因為,按Ansible官網文檔所言,需要有以下先決條件:

  • python >= 3.6
  • kubernetes >= 12.0.0
  • PyYAML >= 3.11

在OCP端安裝python和pip3

在OCP端安裝python和pip3:

yum install python3

如果報錯說找不到,則需要配置repo源,參考上面的做法。

安裝成功,如下:

......
......
Installed:python3-pip-9.0.3-22.el8.noarch                                       python3-setuptools-39.2.0-7.el8.noarch                                       python36-3.6.8-38.module+el8.5.0+12207+5c5719bc.x86_64Complete!

安裝好以后,查看python3和pip3的版本:

[root@api.kai1211.cp.fyre.ibm.com ~]# python3 --version
Python 3.6.8
[root@api.kai1211.cp.fyre.ibm.com ~]# pip3 -V
pip 9.0.3 from /usr/lib/python3.6/site-packages (python 3.6)

注:如果已有python3,則可以下載安裝pip3。

下載 get-pip.py 文件:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py

安裝pip3:

python3 get-pip.py

在OCP端安裝kubernetes

在OCP端安裝kubernetes:

pip3 install kubernetes

注:如果報錯 AttributeError: module 'tarfile' has no attribute 'data_filter' ,則需升級pip3:

python3 -m pip install --upgrade pip

在OCP端安裝PyYAML

我沒有單獨做這一步,應該是在安裝kubernetes的同時,也安裝了滿足條件的PyYAML。

如果不滿足條件,則可能需要重新安裝kubernetes,詳見下面的“其它問題”。

第2次嘗試

再次運行 ansible-playbook test1.yml ,報錯如下:

TASK [task1] **************************************************************************************************************************************************************************************************************************************
An exception occurred during task execution. To see the full traceback, use -vvv. The error was: ansible_collections.kubernetes.core.plugins.module_utils.k8s.exceptions.CoreException: Could not create API client: Invalid kube-config file. No configuration found.
fatal: [api.kai1212.cp.fyre.ibm.com]: FAILED! => {"changed": false, "msg": "Could not create API client: Invalid kube-config file. No configuration found."}

這是因為我們沒有指定config文件。默認的config文件是 ~/.kube/config

在OCP端配置config文件

在OCP端,找到kube config文件,將其復制到 ~/.kube/ 目錄下:

cp /root/auth/kubeconfig ~/.kube/config

第3次嘗試

再次運行 ansible-playbook test1.yml ,這次終于成功了:

TASK [task1] **************************************************************************************************************************************************************************************************************************************
ok: [api.kai1212.cp.fyre.ibm.com]TASK [task2] **************************************************************************************************************************************************************************************************************************************
ok: [api.kai1212.cp.fyre.ibm.com] => {"msg": {"api_found": true,"changed": false,"failed": false,"resources": []}
}

運行成功,只不過沒找到 myns1 namespace,獲取到的信息為空。

其它問題

PyYAML

如果遇到如下報錯:

ERROR: Cannot uninstall 'PyYAML'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

解決方法為:

pip3 install --ignore-installed PyYAML

pip3 install kubernetes

安裝/更新kubectl(可選)

下載 kubectl

curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"

添加 x 屬性:

chmod +x kubectl

將其放到 /usr/bin/ 目錄下:

mv kubectl /usr/bin/

查看版本,比如:

[root@kai12101 ~]# kubectl version
Client Version: v1.28.4
Kustomize Version: v5.0.4-0.20230601165947-6ce0bf390ce3
The connection to the server localhost:8080 was refused - did you specify the right host or port?

kubernetes.core.k8s

前面通過 kubernetes.core.k8s_info 來讀取信息,接下來我們通過 kubernetes.core.k8s 來操作k8s,比如創建一個namespace。

創建文件 test2.yml 如下:

---
- hosts: alltasks:- name: task1kubernetes.core.k8s:name: myns1api_version: v1kind: Namespacestate: present

運行結果如下:

[root@kairedhat91 ~]# ansible-playbook test2.yml
......
......
TASK [task1] **************************************************************************************************************************************************************************************************************************************
changed: [api.kai1212.cp.fyre.ibm.com]

最后,我們再次運行 test1.yml ,獲取namespace myns1 的信息,結果如下:

TASK [task1] **************************************************************************************************************************************************************************************************************************************
ok: [api.kai1212.cp.fyre.ibm.com]TASK [task2] **************************************************************************************************************************************************************************************************************************************
ok: [api.kai1212.cp.fyre.ibm.com] => {"msg": {"api_found": true,"changed": false,"failed": false,"resources": [{"apiVersion": "v1","kind": "Namespace","metadata": {"annotations": {"openshift.io/sa.scc.mcs": "s0:c26,c15","openshift.io/sa.scc.supplemental-groups": "1000680000/10000","openshift.io/sa.scc.uid-range": "1000680000/10000"},"creationTimestamp": "2023-12-12T01:07:08Z","labels": {"kubernetes.io/metadata.name": "myns1","pod-security.kubernetes.io/audit": "restricted","pod-security.kubernetes.io/audit-version": "v1.24","pod-security.kubernetes.io/warn": "restricted","pod-security.kubernetes.io/warn-version": "v1.24"},"managedFields": [{"apiVersion": "v1","fieldsType": "FieldsV1","fieldsV1": {"f:metadata": {"f:labels": {"f:pod-security.kubernetes.io/audit": {},"f:pod-security.kubernetes.io/audit-version": {},"f:pod-security.kubernetes.io/warn": {},"f:pod-security.kubernetes.io/warn-version": {}}}},"manager": "pod-security-admission-label-synchronization-controller","operation": "Apply","time": "2023-12-12T01:07:08Z"},{"apiVersion": "v1","fieldsType": "FieldsV1","fieldsV1": {"f:metadata": {"f:labels": {".": {},"f:kubernetes.io/metadata.name": {}}}},"manager": "OpenAPI-Generator","operation": "Update","time": "2023-12-12T01:07:08Z"},{"apiVersion": "v1","fieldsType": "FieldsV1","fieldsV1": {"f:metadata": {"f:annotations": {".": {},"f:openshift.io/sa.scc.mcs": {},"f:openshift.io/sa.scc.supplemental-groups": {},"f:openshift.io/sa.scc.uid-range": {}}}},"manager": "cluster-policy-controller","operation": "Update","time": "2023-12-12T01:07:08Z"}],"name": "myns1","resourceVersion": "37555","uid": "7e4dcd8b-eae2-4f4a-8153-b229e279b0c4"},"spec": {"finalizers": ["kubernetes"]},"status": {"phase": "Active"}}]}
}

可見,成功獲取了namespace myns1 的信息。

參考

  • https://blog.csdn.net/qq_55977540/article/details/120235601
  • https://blog.csdn.net/weixin_41010198/article/details/103852838
  • https://blog.51cto.com/99cloud/2336420
  • https://docs.ansible.com/ansible/latest/collections/kubernetes/core/k8s_info_module.html
  • https://docs.ansible.com/ansible/latest/collections/kubernetes/core/k8s_module.html
  • https://www.runoob.com/w3cnote/python-pip-install-usage.html
  • https://mirrors.aliyun.com/epel/9/Everything/x86_64/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/215870.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/215870.shtml
英文地址,請注明出處:http://en.pswp.cn/news/215870.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

計算機循環神經網絡(RNN)

計算機循環神經網絡(RNN) 一、引言 循環神經網絡(RNN)是一種常見的深度學習模型,適用于處理序列數據,如文本、語音、時間序列等。RNN通過捕捉序列數據中的時間依賴關系和上下文信息,能夠解決很…

react Hooks之useId

當我們在編寫React組件時,有時需要為元素生成唯一的ID。這種情況經常出現在表單元素、標簽和用于無障礙性的目的上。React提供了一個名為useId的自定義Hook,它可以幫助我們生成唯一的ID。 1、作用: 用于生成一個唯一的 ID。這個 ID 可以用于…

CLIP的升級版Alpha-CLIP:區域感知創新與精細控制

為了增強CLIP在圖像理解和編輯方面的能力,上海交通大學、復旦大學、香港中文大學、上海人工智能實驗室、澳門大學以及MThreads Inc.等知名機構共同合作推出了Alpha-CLIP。這一創新性的突破旨在克服CLIP的局限性,通過賦予其識別特定區域(由點、…

Could not resolve all dependencies for configuration ‘:app:androidApis‘.

android studio出現Could not resolve all dependencies for configuration ‘:app:androidApis’. 試過很多種方法,但是都不好使,不管怎么樣都是提示如下報錯: Using insecure protocols with repositories, without explicit opt-in, is un…

丹麥市場開發攻略,帶你走進童話王國

說起安徒生,大家多多少少都知道,因為小時候讀的安徒生童話書真的太有名了,但是大家可能不知道安徒生是丹麥的。丹麥是高度發達的國家,奉行自由貿易政策,市場潛力是非常不錯的,而且中國是丹麥非常重要的貿易…

Python部分基礎知識入門學習,十分鐘快速上手

文章目錄 一、基礎語法二、變量類型三、運算符四、條件語句關于Python技術儲備一、Python所有方向的學習路線二、Python基礎學習視頻三、精品Python學習書籍四、Python工具包項目源碼合集①Python工具包②Python實戰案例③Python小游戲源碼五、面試資料六、Python兼職渠道 一、…

這家消金公司業務調整,暫停合作產品貸款服務

來源 | 鐳射財經(leishecaijing) 曾為金美信重要的線上自營渠道之一,錢多美宣告謝幕。 「鐳射財經」注意到,金美信消費金融近期發布一則關于錢多美的業務調整公告,提及2023年12月15日起,旗下“錢多美App”…

初識 WebGPU 以及遇到 WebGPU not supported 錯誤的解決方法

初識 WebGPU 以及遇到 WebGPU not supported 錯誤的解決方法 WebGPU學習資源初識WebGPU遇到并解決問題在線示例 因公司需求,開始接觸 WebGPU,偶然遇到問題,網上搜索無效,后來通過逐步判斷,終于定位到問題,這…

【WPF 按鈕點擊后異步上傳多文件code示例】

前言: WPF中按鈕點擊事件如何執行時間太長會導致整個UI線程卡頓,現象就是頁面刷新卡住,點擊其他按鈕無反饋。如下是進行異步執行命令,并遠程上傳文件的代碼。 ![異步上傳文件](https://img-blog.csdnimg.cn/direct/20c071929b004dcf9223dee2…

聽我的,日志還是得好好打!

日志這東西,平時看不出來什么,真要出了問題,那就是救命的稻草。這期就給大家分享一些日志相關的東西。 弄懂日志 SpringBoot項目啟動日志 什么是日志? 日志,維基百科中對其的定義是一個或多個由服務器自動創建和維護…

【數學建模】《實戰數學建模:例題與講解》第十一講-因子分析、聚類與主成分(含Matlab代碼)

【數學建模】《實戰數學建模:例題與講解》第十一講-因子分析、聚類與主成分(含Matlab代碼) 基本概念聚類分析Q型聚類分析R型聚類分析 主成分分析因子分析 習題10.11. 題目要求2.解題過程3.程序4.結果 習題10.21. 題目要求2.解題過程3.程序4.結…

Java網絡編程——安全網絡通信

在網絡上,信息在由源主機到目標主機的傳輸過程中會經過其他計算機。在一般情況下,中間的計算機不會監聽路過的信息。但在使用網上銀行或者進行信用卡交易時,網絡上的信息有可能被非法分子監聽,從而導致個人隱私的泄露。由于Intern…

request、limit資源配額

cpu/mem 的limit和request都是針對container來講的&#xff0c;不是針對pod。 0 < request < limit cpu cpu資源限制的單位m&#xff1a;CPU的計量單位叫毫核(m)。一個節點的CPU核心數量乘以1000&#xff0c;得到的就是節點總的CPU總數量。如&#xff0c;一個節點有兩個…

Rust做一個圖片服務器有多難?

今天我們將詳細探討如何使用Rust構建一個圖片服務器。Rust以其性能、安全性和并發處理能力而聞名&#xff0c;非常適合用于構建網絡服務。 一個圖片服務器需要處理圖片的上傳、存儲、訪問和處理&#xff0c;同時還要考慮安全性和性能。讓我們一步步了解如何用Rust來實現這一目…

使用kubeadm搭建高可用的K8s集群

—————————————————————————————————————————————— 博主介紹&#xff1a;Java領域優質創作者,博客之星城市賽道TOP20、專注于前端流行技術框架、Java后端技術領域、項目實戰運維以及GIS地理信息領域。 &#x1f345;文末獲取源碼…

ImmunityCanvas7.26安裝詳細教程

ImmunityCanvas7.26 大家想必都已經知道了Immunity Canvas7.26武器于2021年3月2日泄露了吧那我就廢話不多說了。 很多人已經有了這款工具不得不說這工具很nice如果要買的話一年的話3萬美金我的天我窮了。。 簡單介紹 Immunity Canvas是美國ImmunitySec出品的安全漏洞檢測工具…

數據庫產品層出不窮,金融行業應該怎么選?|飛輪科技聯合創始人連林江

眾所周知&#xff0c;金融行業對于數據有著極為嚴苛的標準和要求&#xff0c;尤其當在線化、實時化業務場景增多以后&#xff0c;金融行業也面臨著多重的挑戰&#xff1a;既要滿足實時數據分析的高性能、高效率需求&#xff0c;又要確保數據的安全性和完整性。基于此&#xff0…

[GPT]Andrej Karpathy微軟Build大會GPT演講(上)--GPT如何訓練

前言 OpenAI的創始人之一,大神Andrej Karpthy剛在微軟Build 2023開發者大會上做了專題演講:State of GPT(GPT的現狀)。 他詳細介紹了如何從GPT基礎模型一直訓練出ChatGPT這樣的助手模型(assistant model)。作者不曾在其他公開視頻里看過類似的內容,這或許是OpenAI官方…

產品經理在項目周期中扮演的角色Axure的安裝與基本使用

目錄 一.項目周期流程 二.Axure是什么 三.Axure安裝 3.1 一鍵式安裝 3.2 漢化 3.3 授權登錄 四.Axure的界面介紹及基本使用 4.1 菜單欄的使用 4.2 工具欄的使用 4.3 頁面概要的使用及組件的使用 4.4 組件的樣式設計 一.項目周期流程 在一般的項目周期中包含的工作內容有&…

2005-2021年全國各省資本存量測算數據(含原始數據+測算過程+計算結果)

2005-2021年全國各省資本存量測算數據&#xff08;含原始數據測算過程計算結果&#xff09; 1、時間&#xff1a;2005-2021年&#xff08;以2005年為基期&#xff09; 2、范圍&#xff1a;30個省市&#xff08;不含西藏&#xff09; 3、指標&#xff1a;固定資產形成總額、固…