任何CI / CD管道的基本步骤都是部署。 如果管道与Docker容器一起运行并部署到K8s集群,则部署步骤的目标是将特定的Docker映像(存储在某些容器注册表中)部署到特定的K8s集群。 假设有一个正在执行此部署步骤的VM。 将该VM用作可部署到Kuberenetes的计算机之前,需要完成几件事:
- 安装kubectl (K8s CLI)
- 配置对我们将要部署的K8s集群的访问权限
配置虚拟机后,部署步骤将执行以下操作:
# kubeconfig file contains access configuration to all K8s clusters we need
# each configuration is called "context"
export KUBECONFIG=kubeconfig
# switch to "google-cloud-k8s-dev" context (K8s cluster on Google Cloud for Dev)
# so all subsequent kubectl commands are applied to that K8s cluster
kubectl config use-context google-cloud-k8s-dev
# actually deploy by applying k8s-deployment.yaml file
# containing instructions on what image should be deployed and how
kubectl apply -f k8s-deployment.yaml
在本文中,我将展示我们如何创建一个预配置的Docker容器,该容器能够将Docker映像部署到K8s集群。 因此,基本上,它将用作具有两个参数的函数:泊坞窗映像,K8s上下文。 因此,我们将基于此“部署程序”容器在Fn Project中创建一个功能,并通过在http上调用该功能将其部署到K8s。
部署程序容器将由具有以下内容的Dockerfile构建:
FROM ubuntu
# install kubectl
ADD https://storage.googleapis.com/kubernetes-release/release/v1.6.4/bin/linux/amd64/kubectl /usr/local/bin/kubectl
ENV HOME=/config
RUN chmod +x /usr/local/bin/kubectl
RUN export PATH=$PATH:/usr/local/bin
# install rpl
RUN apt-get update
RUN apt-get install rpl -y
# copy into container k8s configuration file with access to all K8s clusters
COPY kubeconfig kubeconfig
# copy into container yaml file template with IMAGE_NAME placeholder
# and an instruction on how to deploy the container to K8s cluster
COPY k8s-deployment.yaml k8s-deployment.yaml
# copy into container a shell script performing the deployment
COPY deploy.sh /usr/local/bin/deploy.sh
RUN chmod +x /usr/local/bin/deploy.sh
ENTRYPOINT ["xargs","/usr/local/bin/deploy.sh"]
值得看看k8s-deployment.yaml文件。 它包含IMAGE_NAME占位符,将在部署时替换为确切的Docker映像名称:
apiVersion: extensions/v1beta1
kind: Deployment
...
spec:
containers:
- image: IMAGE_NAME
imagePullPolicy: Always
...
启动容器后将被调用的deploy.sh脚本具有以下内容:
#!/bin/bash
# replace IMAGE_NAME placeholder in yaml file with the first shell parameter
rpl IMAGE_NAME $1 k8s-deployment.yaml
export KUBECONFIG=kubeconfig
# switch to K8s context specified in the second shell parameter
kubectl config use-context $2
# deploy to K8s cluster
kubectl apply -f k8s-deployment.yaml
因此,我们将通过调用以下docker命令从Dockerfile构建docker映像:
docker build -t efedorenko/k8sdeployer:1.0 .
假设有FN项目并(因为它是在这个例如描述K8S集群上运行的某个地方后 ),我们可以创建一个FN应用程序:
fn apps create k8sdeployerapp
然后创建一个到k8sdeployer容器的路由:
fn routes create k8sdeployerapp /deploy efedorenko/k8sdeployer:1.0
我们创建了一个功能部署码头工人形象的K8S集群。 可以通过http调用此函数,如下所示:
curl http://35.225.120.28:80/r/k8sdeployer -d "google-cloud-k8s-dev efedorenko/happyeaster:latest"
该调用会将efedorenko / happyeaster:latest Docker映像部署到Google Cloud Platform上的K8s集群。
而已!
翻译自: https://www.javacodegeeks.com/2018/04/deploying-to-k8s-cluster-with-fn-function.html