Kubernetes 作业和部署
Kubernetes job and deployment
我可以 运行 在单个配置中进行作业和部署 file/action
部署将在哪里等待作业完成并检查它是否成功以便继续部署?
根据您提供的信息,我相信您可以使用名为 InitContainer:
的 Kubernetes 功能实现您的目标
Init containers are exactly like regular containers, except:
- Init containers always run to completion.
- Each init container must complete successfully before the next one starts.
If a Pod’s init container fails, Kubernetes repeatedly restarts the Pod until the init container succeeds. However, if the Pod has a restartPolicy
of Never, Kubernetes does not restart the Pod.
- 我将创建一个
initContainer
,其中包含一个 busybox
到 运行 一个命令 linux 以等待服务 mydb
变为 运行ning 在继续部署之前。
重现步骤:
- 创建一个带有 initContainer
的部署,它将 运行 在执行部署之前需要完成的工作:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: my-app
name: my-app
spec:
replicas: 2
selector:
matchLabels:
run: my-app
template:
metadata:
labels:
run: my-app
spec:
restartPolicy: Always
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-mydb
image: busybox:1.28
command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
在这个领域可以使用多种命令,你只需要select一个包含你需要的二进制文件的docker图像(包括你的sequelize
工作)
- 现在让我们应用它看看部署状态:
$ kubectl apply -f my-app.yaml
deployment.apps/my-app created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
my-app-6b4fb4958f-44ds7 0/1 Init:0/1 0 4s
my-app-6b4fb4958f-s7wmr 0/1 Init:0/1 0 4s
pods 处于Init:0/1
状态等待初始化容器完成。
- 现在让我们创建 initcontainer 在完成任务之前等待 运行ning 的服务:
apiVersion: v1
kind: Service
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377
- 我们将应用它并监控 pods 中的变化:
$ kubectl apply -f mydb-svc.yaml
service/mydb created
$ kubectl get pods -w
NAME READY STATUS RESTARTS AGE
my-app-6b4fb4958f-44ds7 0/1 Init:0/1 0 91s
my-app-6b4fb4958f-s7wmr 0/1 Init:0/1 0 91s
my-app-6b4fb4958f-s7wmr 0/1 PodInitializing 0 93s
my-app-6b4fb4958f-44ds7 0/1 PodInitializing 0 94s
my-app-6b4fb4958f-s7wmr 1/1 Running 0 94s
my-app-6b4fb4958f-44ds7 1/1 Running 0 95s
^C
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-app-6b4fb4958f-44ds7 1/1 Running 0 99s
pod/my-app-6b4fb4958f-s7wmr 1/1 Running 0 99s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mydb ClusterIP 10.100.106.67 <none> 80/TCP 14s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/my-app 2/2 2 2 99s
NAME DESIRED CURRENT READY AGE
replicaset.apps/my-app-6b4fb4958f 2 2 2 99s
如果您需要帮助将此应用于您的环境,请告诉我。
尽管 initContainers 是此解决方案的一个可行选项,但如果您使用 helm 来管理和部署到您的集群,还有另一个选项。
Helm has chart hooks 允许您在 helm chart 中的其他安装发生之前 运行 a Job
。您提到这是针对服务部署之前的数据库迁移。完成此操作的一些示例 helm 配置可能是...
apiVersion: batch/v1
kind: Job
metadata:
name: api-migration-job
namespace: default
labels:
app: api-migration-job
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-weight": "-1"
"helm.sh/hook-delete-policy": before-hook-creation
spec:
template:
spec:
containers:
- name: platform-migration
...
这将 运行 作业完成,然后再进入 helm chart 中的安装/升级阶段。您可以看到有一个 'hook-weight' 变量允许您根据需要订购这些挂钩。
在我看来,这是一个比 init 容器更优雅的解决方案,并且可以更好地控制。
我可以 运行 在单个配置中进行作业和部署 file/action 部署将在哪里等待作业完成并检查它是否成功以便继续部署?
根据您提供的信息,我相信您可以使用名为 InitContainer:
的 Kubernetes 功能实现您的目标Init containers are exactly like regular containers, except:
- Init containers always run to completion.
- Each init container must complete successfully before the next one starts.
If a Pod’s init container fails, Kubernetes repeatedly restarts the Pod until the init container succeeds. However, if the Pod has a
restartPolicy
of Never, Kubernetes does not restart the Pod.
- 我将创建一个
initContainer
,其中包含一个busybox
到 运行 一个命令 linux 以等待服务mydb
变为 运行ning 在继续部署之前。
重现步骤:
- 创建一个带有 initContainer
的部署,它将 运行 在执行部署之前需要完成的工作:
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: my-app
name: my-app
spec:
replicas: 2
selector:
matchLabels:
run: my-app
template:
metadata:
labels:
run: my-app
spec:
restartPolicy: Always
containers:
- name: myapp-container
image: busybox:1.28
command: ['sh', '-c', 'echo The app is running! && sleep 3600']
initContainers:
- name: init-mydb
image: busybox:1.28
command: ['sh', '-c', "until nslookup mydb.$(cat /var/run/secrets/kubernetes.io/serviceaccount/namespace).svc.cluster.local; do echo waiting for mydb; sleep 2; done"]
在这个领域可以使用多种命令,你只需要select一个包含你需要的二进制文件的docker图像(包括你的sequelize
工作)
- 现在让我们应用它看看部署状态:
$ kubectl apply -f my-app.yaml
deployment.apps/my-app created
$ kubectl get pods
NAME READY STATUS RESTARTS AGE
my-app-6b4fb4958f-44ds7 0/1 Init:0/1 0 4s
my-app-6b4fb4958f-s7wmr 0/1 Init:0/1 0 4s
pods 处于Init:0/1
状态等待初始化容器完成。
- 现在让我们创建 initcontainer 在完成任务之前等待 运行ning 的服务:
apiVersion: v1
kind: Service
metadata:
name: mydb
spec:
ports:
- protocol: TCP
port: 80
targetPort: 9377
- 我们将应用它并监控 pods 中的变化:
$ kubectl apply -f mydb-svc.yaml
service/mydb created
$ kubectl get pods -w
NAME READY STATUS RESTARTS AGE
my-app-6b4fb4958f-44ds7 0/1 Init:0/1 0 91s
my-app-6b4fb4958f-s7wmr 0/1 Init:0/1 0 91s
my-app-6b4fb4958f-s7wmr 0/1 PodInitializing 0 93s
my-app-6b4fb4958f-44ds7 0/1 PodInitializing 0 94s
my-app-6b4fb4958f-s7wmr 1/1 Running 0 94s
my-app-6b4fb4958f-44ds7 1/1 Running 0 95s
^C
$ kubectl get all
NAME READY STATUS RESTARTS AGE
pod/my-app-6b4fb4958f-44ds7 1/1 Running 0 99s
pod/my-app-6b4fb4958f-s7wmr 1/1 Running 0 99s
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
service/mydb ClusterIP 10.100.106.67 <none> 80/TCP 14s
NAME READY UP-TO-DATE AVAILABLE AGE
deployment.apps/my-app 2/2 2 2 99s
NAME DESIRED CURRENT READY AGE
replicaset.apps/my-app-6b4fb4958f 2 2 2 99s
如果您需要帮助将此应用于您的环境,请告诉我。
尽管 initContainers 是此解决方案的一个可行选项,但如果您使用 helm 来管理和部署到您的集群,还有另一个选项。
Helm has chart hooks 允许您在 helm chart 中的其他安装发生之前 运行 a Job
。您提到这是针对服务部署之前的数据库迁移。完成此操作的一些示例 helm 配置可能是...
apiVersion: batch/v1
kind: Job
metadata:
name: api-migration-job
namespace: default
labels:
app: api-migration-job
annotations:
"helm.sh/hook": pre-install,pre-upgrade
"helm.sh/hook-weight": "-1"
"helm.sh/hook-delete-policy": before-hook-creation
spec:
template:
spec:
containers:
- name: platform-migration
...
这将 运行 作业完成,然后再进入 helm chart 中的安装/升级阶段。您可以看到有一个 'hook-weight' 变量允许您根据需要订购这些挂钩。
在我看来,这是一个比 init 容器更优雅的解决方案,并且可以更好地控制。