【CI/CD】Rancher CD过程--20230906

gitlab设定CICD的变量

【CI/CD】Rancher CD过程--20230906_第1张图片

  1. HARBOR_PASSWORD:密码
  2. HARBOR_USER:工号
  3. K8S_TOKEN:Bearer + rancher key
    【CI/CD】Rancher CD过程--20230906_第2张图片
  4. K8S_WORKLOAD_URL:选择【View in API】的URL,并非workload的URL。
    如果是新版rancher,则使用/g回去旧版界面。
    【CI/CD】Rancher CD过程--20230906_第3张图片
    【CI/CD】Rancher CD过程--20230906_第4张图片

准备json.txt方法1(W公司SOP,但是可能失效)

  1. 选择workload,进入【View in API】
    【CI/CD】Rancher CD过程--20230906_第5张图片

  2. right panel >> click edit
    【CI/CD】Rancher CD过程--20230906_第6张图片

  3. Move to buttom , and click “Show Request”
    【CI/CD】Rancher CD过程--20230906_第7张图片

  4. Copy the highlight area from “-d” parameter content
    【CI/CD】Rancher CD过程--20230906_第8张图片

  5. Create json.txt file and paste the content
    【CI/CD】Rancher CD过程--20230906_第9张图片

  6. 记得修改image tag和 “cattle.io/timestamp”: “CHANGEME”。

{
    "actions": {
        "pause": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-django?action=pause",
        "redeploy": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-django?action=redeploy",
        "resume": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-django?action=resume",
        "rollback": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-django?action=rollback"
    },
    "annotations": {
        "cattle.io/timestamp": "CHANGEME"
    },
    "baseType": "workload",
    "containers": [
        {
            "allowPrivilegeEscalation": false,
            "image": "harbor-k8s.wzs.wistron.com.cn/season/training_platform_django:0.0.8-build36",
            "imagePullPolicy": "IfNotPresent",
            "initContainer": false,
            "name": "training-platform-django",
            "ports": [
                {
                    "containerPort": 8000,
                    "dnsName": "training-platform-django-nodeport",
                    "hostPort": 0,
                    "kind": "NodePort",
                    "name": "8000tcl",
                    "protocol": "TCP",
                    "sourcePort": 0,
                    "type": "/v3/project/schemas/containerPort"
                }
            ],
            "privileged": false,
            "readOnly": false,
            "resources": {
                "limits": {
                    "cpu": "1",
                    "memory": "1000Mi"
                },
                "requests": {
                    "cpu": "1",
                    "memory": "1000Mi"
                },
                "type": "/v3/project/schemas/resourceRequirements"
            },
            "restartCount": 0,
            "runAsNonRoot": false,
            "stdin": true,
            "stdinOnce": false,
            "terminationMessagePath": "/dev/termination-log",
            "terminationMessagePolicy": "File",
            "tty": true,
            "type": "/v3/project/schemas/container"
        }
    ],
    "created": "2023-06-27T13:13:37Z",
    "createdTS": 1687871617000,
    "creatorId": null,
    "deploymentConfig": {
        "maxSurge": 1,
        "maxUnavailable": 0,
        "minReadySeconds": 0,
        "progressDeadlineSeconds": 600,
        "revisionHistoryLimit": 10,
        "strategy": "RollingUpdate"
    },
    "deploymentStatus": {
        "availableReplicas": 1,
        "conditions": [
            {
                "lastTransitionTime": "2023-06-27T13:13:37Z",
                "lastTransitionTimeTS": 1687871617000,
                "lastUpdateTime": "2023-08-24T05:46:46Z",
                "lastUpdateTimeTS": 1692856006000,
                "message": "ReplicaSet \"training-platform-django-5dc8b9dc9c\" has successfully progressed.",
                "reason": "NewReplicaSetAvailable",
                "status": "True",
                "type": "Progressing"
            },
            {
                "lastTransitionTime": "2023-09-05T09:48:01Z",
                "lastTransitionTimeTS": 1693907281000,
                "lastUpdateTime": "2023-09-05T09:48:01Z",
                "lastUpdateTimeTS": 1693907281000,
                "message": "Deployment has minimum availability.",
                "reason": "MinimumReplicasAvailable",
                "status": "True",
                "type": "Available"
            }
        ],
        "observedGeneration": 67,
        "readyReplicas": 1,
        "replicas": 1,
        "type": "/v3/project/schemas/deploymentStatus",
        "unavailableReplicas": 0,
        "updatedReplicas": 1
    },
    "dnsConfig": {
        "type": "/v3/project/schemas/podDNSConfig"
    },
    "dnsPolicy": "ClusterFirst",
    "hostIPC": false,
    "hostNetwork": false,
    "hostPID": false,
    "id": "deployment:season:training-platform-django",
    "labels": {
        "workload.user.cattle.io/workloadselector": "deployment-season-training-platform-django"
    },
    "links": {
        "remove": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-django",
        "revisions": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-django/revisions",
        "self": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-django",
        "update": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-django",
        "yaml": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-django/yaml"
    },
    "name": "training-platform-django",
    "namespaceId": "season",
    "paused": false,
    "projectId": "local:p-vhqv2",
    "publicEndpoints": [
        {
            "addresses": [
                "10.41.241.111",
                "10.41.241.112",
                "10.41.241.113",
                "10.41.241.114",
                "10.41.241.115",
                "10.41.241.116"
            ],
            "allNodes": false,
            "hostname": "training-platform-django.k8sprd-wzs.k8s.wistron.com.cn",
            "ingressId": "season:training-platform-django-ingress",
            "nodeId": null,
            "podId": null,
            "port": 80,
            "protocol": "HTTP",
            "serviceId": "season:ingress-053ebdc075a82cf5efbe1ac16b822ab4"
        },
        {
            "addresses": [
                "10.41.241.111"
            ],
            "allNodes": true,
            "ingressId": null,
            "nodeId": null,
            "podId": null,
            "port": 30481,
            "protocol": "TCP",
            "serviceId": "season:training-platform-django-nodeport"
        }
    ],
    "restartPolicy": "Always",
    "scale": 1,
    "scheduling": {
        "scheduler": "default-scheduler"
    },
    "selector": {
        "matchLabels": {
            "workload.user.cattle.io/workloadselector": "deployment-season-training-platform-django"
        },
        "type": "/v3/project/schemas/labelSelector"
    },
    "state": "active",
    "terminationGracePeriodSeconds": 30,
    "transitioning": "no",
    "transitioningMessage": "",
    "type": "deployment",
    "uuid": "ab30eb79-f594-4de1-a615-b042b011b02f",
    "workloadAnnotations": {
        "deployment.kubernetes.io/revision": "12",
        "field.cattle.io/creatorId": "u-mp27e6gufb"
    },
    "workloadLabels": {
        "cattle.io/creator": "norman",
        "workload.user.cattle.io/workloadselector": "deployment-season-training-platform-django"
    }
}

准备json.txt方法2(Vincent Wen方法)

1.获取token
【CI/CD】Rancher CD过程--20230906_第10张图片

输入curl -H "Authorization: Bearer token-jgch5:wvcrwwpglhk5jxz2fxt2h2jwmd2f7znjbjmzg5gwg4pnrc2qp5h5r9" -H "Content-Type:application/json" -X GET https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist

相当于是 curl -H "Authorization:${K8S_TOKEN}" -H "Content-Type:application/json" -X GET ${K8S_WORKLOAD_URL}
【CI/CD】Rancher CD过程--20230906_第11张图片
2. 复制并写入json.txt中。

{
	"actions": {
		"pause": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist?action=pause",
		"redeploy": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist?action=redeploy",
		"resume": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist?action=resume",
		"rollback": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist?action=rollback"
	},
	"annotations": {
		"cattle.io/timestamp": "1693990188"
	},
	"baseType": "workload",
	"containers": [{
		"allowPrivilegeEscalation": false,
		"image": "harbor-k8s.wzs.wistron.com.cn/season/training_platform_vue_dist:0.0.0-build30",
		"imagePullPolicy": "IfNotPresent",
		"initContainer": false,
		"name": "training-platform-vue-dist",
		"ports": [{
			"containerPort": 8080,
			"dnsName": "training-platform-vue-dist-nodeport",
			"hostPort": 0,
			"kind": "NodePort",
			"name": "8080tcl",
			"protocol": "TCP",
			"sourcePort": 0,
			"type": "/v3/project/schemas/containerPort"
		}],
		"privileged": false,
		"readOnly": false,
		"resources": {
			"limits": {
				"cpu": "300m",
				"memory": "300Mi"
			},
			"type": "/v3/project/schemas/resourceRequirements"
		},
		"restartCount": 0,
		"runAsNonRoot": false,
		"stdin": true,
		"stdinOnce": false,
		"terminationMessagePath": "/dev/termination-log",
		"terminationMessagePolicy": "File",
		"tty": true,
		"type": "/v3/project/schemas/container"
	}],
	"created": "2023-06-27T14:18:09Z",
	"createdTS": 1687875489000,
	"creatorId": null,
	"deploymentConfig": {
		"maxSurge": 1,
		"maxUnavailable": 0,
		"minReadySeconds": 0,
		"progressDeadlineSeconds": 600,
		"revisionHistoryLimit": 10,
		"strategy": "RollingUpdate"
	},
	"deploymentStatus": {
		"availableReplicas": 1,
		"conditions": [{
			"lastTransitionTime": "2023-09-07T02:04:59Z",
			"lastTransitionTimeTS": 1694052299000,
			"lastUpdateTime": "2023-09-07T02:04:59Z",
			"lastUpdateTimeTS": 1694052299000,
			"message": "ReplicaSet \"training-platform-vue-dist-7db7c6498f\" has successfully progressed.",
			"reason": "NewReplicaSetAvailable",
			"status": "True",
			"type": "Progressing"
		}, {
			"lastTransitionTime": "2023-09-07T02:05:23Z",
			"lastTransitionTimeTS": 1694052323000,
			"lastUpdateTime": "2023-09-07T02:05:23Z",
			"lastUpdateTimeTS": 1694052323000,
			"message": "Deployment has minimum availability.",
			"reason": "MinimumReplicasAvailable",
			"status": "True",
			"type": "Available"
		}],
		"observedGeneration": 74,
		"readyReplicas": 1,
		"replicas": 1,
		"type": "/v3/project/schemas/deploymentStatus",
		"unavailableReplicas": 0,
		"updatedReplicas": 1
	},
	"dnsPolicy": "ClusterFirst",
	"hostIPC": false,
	"hostNetwork": false,
	"hostPID": false,
	"id": "deployment:season:training-platform-vue-dist",
	"labels": {
		"workload.user.cattle.io/workloadselector": "deployment-season-training-platform-vue-dist"
	},
	"links": {
		"remove": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist",
		"revisions": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist/revisions",
		"self": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist",
		"update": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist",
		"yaml": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist/yaml"
	},
	"name": "training-platform-vue-dist",
	"namespaceId": "season",
	"paused": false,
	"projectId": "local:p-vhqv2",
	"publicEndpoints": [{
		"addresses": ["10.41.241.111", "10.41.241.112", "10.41.241.113", "10.41.241.114", "10.41.241.115", "10.41.241.116"],
		"allNodes": false,
		"hostname": "training-platform.k8sprd-wzs.k8s.wistron.com.cn",
		"ingressId": "season:training-platform-vue-dist-ingress",
		"nodeId": null,
		"podId": null,
		"port": 80,
		"protocol": "HTTP",
		"serviceId": "season:ingress-9932efa3ab00c77c4cfb44b758c758b2"
	}, {
		"addresses": ["10.41.241.111"],
		"allNodes": true,
		"ingressId": null,
		"nodeId": null,
		"podId": null,
		"port": 30396,
		"protocol": "TCP",
		"serviceId": "season:training-platform-vue-dist-nodeport"
	}],
	"restartPolicy": "Always",
	"scale": 1,
	"scheduling": {
		"scheduler": "default-scheduler"
	},
	"selector": {
		"matchLabels": {
			"workload.user.cattle.io/workloadselector": "deployment-season-training-platform-vue-dist"
		},
		"type": "/v3/project/schemas/labelSelector"
	},
	"state": "active",
	"terminationGracePeriodSeconds": 30,
	"transitioning": "no",
	"transitioningMessage": "",
	"type": "deployment",
	"uuid": "d63402fa-b097-4f01-9a6e-1db4e2b87912",
	"workloadAnnotations": {
		"deployment.kubernetes.io/revision": "21",
		"field.cattle.io/creatorId": "u-mp27e6gufb"
	},
	"workloadLabels": {
		"cattle.io/creator": "norman",
		"workload.user.cattle.io/workloadselector": "deployment-season-training-platform-vue-dist"
	}
}
  1. 记得修改json.txt中image tag和 “cattle.io/timestamp”: “CHANGEME”。
{
	"actions": {
		"pause": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist?action=pause",
		"redeploy": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist?action=redeploy",
		"resume": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist?action=resume",
		"rollback": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist?action=rollback"
	},
	"annotations": {
		"cattle.io/timestamp": "CHANGEME"
	},
	"baseType": "workload",
	"containers": [{
		"allowPrivilegeEscalation": false,
		"image": "harbor-k8s.wzs.wistron.com.cn/season/training_platform_vue_dist:0.0.0-buildCI_PIPELINE_IID", #(image需要修订版本号)
		"imagePullPolicy": "IfNotPresent",
		"initContainer": false,
		"name": "training-platform-vue-dist",
		"ports": [{
			"containerPort": 8080,
			"dnsName": "training-platform-vue-dist-nodeport",
			"hostPort": 0,
			"kind": "NodePort",
			"name": "8080tcl",
			"protocol": "TCP",
			"sourcePort": 0,
			"type": "/v3/project/schemas/containerPort"
		}],
		"privileged": false,
		"readOnly": false,
		"resources": {
			"limits": {
				"cpu": "300m",
				"memory": "300Mi"
			},
			"type": "/v3/project/schemas/resourceRequirements"
		},
		"restartCount": 0,
		"runAsNonRoot": false,
		"stdin": true,
		"stdinOnce": false,
		"terminationMessagePath": "/dev/termination-log",
		"terminationMessagePolicy": "File",
		"tty": true,
		"type": "/v3/project/schemas/container"
	}],
	"created": "2023-06-27T14:18:09Z",
	"createdTS": 1687875489000,
	"creatorId": null,
	"deploymentConfig": {
		"maxSurge": 1,
		"maxUnavailable": 0,
		"minReadySeconds": 0,
		"progressDeadlineSeconds": 600,
		"revisionHistoryLimit": 10,
		"strategy": "RollingUpdate"
	},
	"deploymentStatus": {
		"availableReplicas": 1,
		"conditions": [{
			"lastTransitionTime": "2023-09-07T02:04:59Z",
			"lastTransitionTimeTS": 1694052299000,
			"lastUpdateTime": "2023-09-07T02:04:59Z",
			"lastUpdateTimeTS": 1694052299000,
			"message": "ReplicaSet \"training-platform-vue-dist-7db7c6498f\" has successfully progressed.",
			"reason": "NewReplicaSetAvailable",
			"status": "True",
			"type": "Progressing"
		}, {
			"lastTransitionTime": "2023-09-07T02:05:23Z",
			"lastTransitionTimeTS": 1694052323000,
			"lastUpdateTime": "2023-09-07T02:05:23Z",
			"lastUpdateTimeTS": 1694052323000,
			"message": "Deployment has minimum availability.",
			"reason": "MinimumReplicasAvailable",
			"status": "True",
			"type": "Available"
		}],
		"observedGeneration": 74,
		"readyReplicas": 1,
		"replicas": 1,
		"type": "/v3/project/schemas/deploymentStatus",
		"unavailableReplicas": 0,
		"updatedReplicas": 1
	},
	"dnsPolicy": "ClusterFirst",
	"hostIPC": false,
	"hostNetwork": false,
	"hostPID": false,
	"id": "deployment:season:training-platform-vue-dist",
	"labels": {
		"workload.user.cattle.io/workloadselector": "deployment-season-training-platform-vue-dist"
	},
	"links": {
		"remove": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist",
		"revisions": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist/revisions",
		"self": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist",
		"update": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist",
		"yaml": "https://rancher-wzsarm.wistron.com/v3/project/local:p-vhqv2/workloads/deployment:season:training-platform-vue-dist/yaml"
	},
	"name": "training-platform-vue-dist",
	"namespaceId": "season",
	"paused": false,
	"projectId": "local:p-vhqv2",
	"publicEndpoints": [{
		"addresses": ["10.41.241.111", "10.41.241.112", "10.41.241.113", "10.41.241.114", "10.41.241.115", "10.41.241.116"],
		"allNodes": false,
		"hostname": "training-platform.k8sprd-wzs.k8s.wistron.com.cn",
		"ingressId": "season:training-platform-vue-dist-ingress",
		"nodeId": null,
		"podId": null,
		"port": 80,
		"protocol": "HTTP",
		"serviceId": "season:ingress-9932efa3ab00c77c4cfb44b758c758b2"
	}, {
		"addresses": ["10.41.241.111"],
		"allNodes": true,
		"ingressId": null,
		"nodeId": null,
		"podId": null,
		"port": 30396,
		"protocol": "TCP",
		"serviceId": "season:training-platform-vue-dist-nodeport"
	}],
	"restartPolicy": "Always",
	"scale": 1,
	"scheduling": {
		"scheduler": "default-scheduler"
	},
	"selector": {
		"matchLabels": {
			"workload.user.cattle.io/workloadselector": "deployment-season-training-platform-vue-dist"
		},
		"type": "/v3/project/schemas/labelSelector"
	},
	"state": "active",
	"terminationGracePeriodSeconds": 30,
	"transitioning": "no",
	"transitioningMessage": "",
	"type": "deployment",
	"uuid": "d63402fa-b097-4f01-9a6e-1db4e2b87912",
	"workloadAnnotations": {
		"deployment.kubernetes.io/revision": "21",
		"field.cattle.io/creatorId": "u-mp27e6gufb"
	},
	"workloadLabels": {
		"cattle.io/creator": "norman",
		"workload.user.cattle.io/workloadselector": "deployment-season-training-platform-vue-dist"
	}
}

修改.gitlab-ci.yml

format

update:
  stage: deploy
  image: harbor.wistron.com/base_image/alpine:3.10
  before_script:
    - apk add curl
    - export CI_JOB_TIMESTAMP=$(date +"%s")
  tags:
    - prd-runner02  # select your runner
  script:
    - MOVE TO YOUR JSON.TXT PATH
    - sed -i s/CHANGEME/$CI_JOB_TIMESTAMP/g json.txt
    - sed -i s/CI_PIPELINE_IID/$CI_PIPELINE_IID/g json.txt
    - curl -H "Authorization:${K8S_TOKEN}" -H "Content-Type:application/json" -d "@json.txt" -X PUT ${K8S_WORKLOAD_URL}

我的代码.gitlab-ci.yml

variables:
  DOCKER_IMAGE_NAME: training_platform_django
  DOCKER_IMAGE_TAG: 0.0.8

 
  
services:
  - name: harbor-k8s.wzs.wistron.com.cn/base_image/docker:stable
    entrypoint: []
    command: ["--insecure-registry=harbor-k8s.wistron.com.cn"]

stages:
  - docker_build
  - docker-update

docker-build:
  image: harbor-k8s.wzs.wistron.com.cn/base_image/docker:stable
  stage: docker_build
  tags: 
    - wzs-runner01
  script:
    - docker info
    - echo $DOCKER_IMAGE_NAME
    - echo $DOCKER_IMAGE_TAG
    - docker build -t $DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG .
    - docker tag $DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG harbor-k8s.wzs.wistron.com.cn/season/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG-build$CI_PIPELINE_IID
    - echo "$HARBOR_PASSWORD" | docker login -u "$HARBOR_USER"  --password "$HARBOR_PASSWORD" harbor-k8s.wzs.wistron.com.cn
    - docker push harbor-k8s.wzs.wistron.com.cn/season/$DOCKER_IMAGE_NAME:$DOCKER_IMAGE_TAG-build$CI_PIPELINE_IID



docker-update:
  stage: docker-update
  image: harbor.wistron.com/base_image/alpine:3.10
  before_script:
    - apk add curl
    - export CI_JOB_TIMESTAMP=$(date +"%s")
  tags:
    - wzs-runner01
  script:
    - sed -i s/CHANGEME/$CI_JOB_TIMESTAMP/g json.txt
    - sed -i s/CI_PIPELINE_IID/$CI_PIPELINE_IID/g json.txt
    - curl -H "Authorization:${K8S_TOKEN}" -H "Content-Type:application/json" -d "@json.txt" -X PUT ${K8S_WORKLOAD_URL}

gitlab pipeline

显示成功
【CI/CD】Rancher CD过程--20230906_第12张图片
自动更新
【CI/CD】Rancher CD过程--20230906_第13张图片
【CI/CD】Rancher CD过程--20230906_第14张图片

你可能感兴趣的:(CICD,ci/cd,rancher,kubernetes)