admin 管理员组

文章数量: 887021

[kubernetes]

导语:kubernetes打算通过监测job的状态来触发其余容器。资料找起来不容器记录一下。

需求是用job类型执行mysql初始化,业务容器要确保job类型执行完成再启动。即用initcontainer去检测pod的状态。

一般命令行可以这样操作

kubectl wait --for=condition=complete job/myjob
kubectl wait --for=condition=complete --timeout=30s job/myjob
until kubectl get jobs myjob -o jsonpath='{.status.conditions[? (@.type=="Complete")].status}' | grep True ; do sleep 1 ; done

找到一个可以在容器内实现需求的方式

创建Role 和RoleBinding

kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:namespace: deepwisename: wait-for-role
rules:
- apiGroups: [""]resources: ["pods"]verbs: ["get", "list", "watch"]
- apiGroups: ["extensions", "apps"]resources: ["deployments"]verbs: ["get", "watch", "list"]
- apiGroups: ["batch","","apps"]resources: ["jobs"]verbs: ["get", "list", "watch"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:name: wait-for-rolebindingnamespace: deepwise
roleRef:apiGroup: rbac.authorization.k8s.iokind: Rolename: wait-for-role
subjects:- kind: ServiceAccountname: wait-for-operatornamespace: deepwise

创建sa

apiVersion: v1
kind: ServiceAccount
metadata:namespace: deepwisename: wait-for-operator

创建pod测试

apiVersion: apps/v1
kind: Deployment
metadata:name: wait-for-dpnamespace: deepwise
spec:replicas: 1selector:matchLabels:app: wait-forenv: deepwisetemplate:metadata:labels:app: wait-forenv: deepwisespec:hostNetwork: trueserviceAccountName: wait-for-operatordnsPolicy: ClusterFirstWithHostNetrestartPolicy: Alwayscontainers:- name: k8s-wait-forimage: groundnuty/k8s-wait-for:v1.3#kubectl run   k8s-wait-for --rm -it --image groundnuty/k8s-wait-for:v1.3 -n deepwise --restart Never --command /bin/shocommand:- /sbin/init

进入容器测试

kubectl  exec -it wait-for-dp-6f7d89c95-q8vtb sh -n deepwise
# 执行命令测试
wait_for.sh pod -lapp=develop-volume-gluster-krakow
wait_for.sh pod -lapp=aiserver
wait_for.sh job mysql-init
wait_for.sh job mysql-init333

贴一个官方demo

kind: StatefulSet
metadata:name: develop-oneprovider-krakowlabels:app: develop-oneprovider-krakowchart: oneprovider-krakowrelease: developheritage: Tillercomponent: oneproviderannotations:version: "0.2.17"
spec:selector:matchLabels:app: develop-oneprovider-krakowchart: oneprovider-krakowrelease: developheritage: Tillercomponent: "oneprovider"serviceName: develop-oneprovider-krakowtemplate:metadata:labels:app: develop-oneprovider-krakowchart: oneprovider-krakowrelease: developheritage: Tillercomponent: "oneprovider"annotations:version: "0.2.17"spec:initContainers:- name: wait-for-onezoneimage: groundnuty/k8s-wait-for:v1.3imagePullPolicy: Alwaysargs:- "job"- "develop-onezone-ready-check"- name: wait-for-volume-cephimage: groundnuty/k8s-wait-for:v1.3imagePullPolicy: Alwaysargs:- "pod"- "-lapp=develop-volume-ceph-krakow"- name: wait-for-volume-glusterimage: groundnuty/k8s-wait-for:v1.3imagePullPolicy: Alwaysargs:- "pod"- "-lapp=develop-volume-gluster-krakow"containers:- name: oneproviderimage: docker.onedata.org/oneprovider:ID-a3a9ff0d78imagePullPolicy: Always

参考

.md

本文标签: kubernetes