使用 ARC 自托管运行器一步构建镜像并将其部署到 Kubernetes 集群

问题描述 投票:0回答:1

我正在编写一个 Github 操作来构建 docker 镜像并将其部署到 k8s 集群,目前必须构建、将镜像推送到存储库,然后

kubectl apply
以标准方式部署它。

我已经使用 ARC (actions-runner-controller) 设置了一个自托管的 GitHub actions runner 在我的 k8s 集群上运行。由于执行工作的运行程序本身位于集群上,因此有什么方法可以构建映像,然后立即将其部署到集群,而不必推送和拉取到存储库以返回到构建映像的位置第一名?

docker kubernetes github-actions google-kubernetes-engine github-actions-runners
1个回答
0
投票

您想要做的事情在下面

  1. 在容器内构建容器镜像

  2. 无需推拉过程,使用构建的镜像来更改或部署到k8s集群

要做到这一点,你需要了解

1。 DIND(Docker 中的 Docker) <- to build image inside a container Need privileged options to run dind

services:
  docker:
    image: docker:dind
    options: --privileged

2。 RBAC(用于在集群上使用 kubectl 部署或更改映像)

您的 github 操作运行程序需要角色才能部署在您的 kubernetes 上 创建服务帐户、角色(或集群角色)并将其绑定到您的跑步者

示例

服务帐户

apiVersion: v1
kind: ServiceAccount
metadata:
  name: your-service-account-name
  namespace: your-namespace

角色(或 ClusterRole)

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: your-namespace
  name: runner-deployment-role
rules:
- apiGroups: ["", "apps"]
  resources: ["deployments", "services"]
  verbs: ["get", "list", "watch", "create", "update", "patch", "delete"]
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: runner-deployment-role-binding
  namespace: your-namespace
subjects:
- kind: ServiceAccount
  name: your-service-account-name
  namespace: default
roleRef:
  kind: Role
  name: runner-deployment-role
  apiGroup: rbac.authorization.k8s.io

3.正确的工作流程设置和github操作上的图像名称

您还需要 kubeconfig 才能使用 kubectl

示例

jobs:
  build-and-deploy:
    runs-on: self-hosted
    services:
      docker:
        image: docker:dind
        options: --privileged
    steps:
    - name: check code
      uses: actions/checkout@v2

    - name: build image by inside dind
      run: |
        docker build -t my-app:latest .

    - name: deploy on your cluster
      run: |
        kubectl --kubeconfig /path/to/kubeconfig apply -f k8s-deployment.yaml

注意事项:请注意,使用 DinD 和 上传 kubeconfig 进行使用可能会带来安全风险。它是 了解这些影响并进行彻底研究很重要 在您的设置中实施它们之前

© www.soinside.com 2019 - 2024. All rights reserved.