使用Restful api更改 rancher 镜像版本

300 阅读1分钟

环境

rancher 版本 2.6.3

申请api Token

  1. 登录rancher,点击头像,选择accout & api keys image.png
  2. 点击create API Key,直接点确定,不要选择scrope,否则调用接口会提示没有权限 image.png
  3. 保存 api key相关信息,这些信息只在创建时显示,所以自己保存下来 image.png

查找需要的api

  1. 在浏览器输入rancher的地址 后面加v3,会列出rancher所有的信息
https://<Rancher-Server-IP>/v3
  1. 搜索project image.png

  2. 找到对应namespace下面的deployments image.png

  3. 选择redeploy image.png

  4. 点击edit image.png

  5. 点击showRequest image.png

  6. 可以看到完整的request信息 image.png

  7. 点击sendRequest 复制请求体数据 image.png image.png

  8. 复制请求体数据,我这里通过axios发的请求,看需求,也可以按照文档提示使用curl

axios
  .request({
      method: 'put',
      maxBodyLength: Infinity,
      url: 'https://${<Rancher-Server-IP>}/v3/project/c-m-hqfgrdxw:p-g84dz/workloads/deployment:web-cloud:scm-saas',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
        Authorization: 'Bearer ${你申请的token}',
      },
      data: JSON.stringify(...你的请求体数据),
  })
  .then((response) => {
    console.log(JSON.stringify(response.data));
  })
  .catch((error) => {
    console.log(error);
  });

常见报错

  1. unable to verify the first certificate image.png 目前我采用的方案是关闭认证,会有一定的安全隐患,内部使用,问题不大

    process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0;
    
  2. Unauthorized 401: must authenticate image.png 这是因为在申请token的时候选择了scope,解决方案:不选择scope,重新申请key

参考文档

docs.rancher.cn/docs/ranche… github.com/rancher/api…