按顺序启动服务



[root@k8s-master ~]# cat rc-sequence.yaml apiVersion: v1 kind: Pod metadata: name: helloworld-service labels: weblogic-app: helloworld1 annotations: pod.beta.kubernetes.io/init-containers: '[ { "name": "wait-weblogic", "image": "docker.io/giantswarm/tiny-tools:latest", "imagePullPolicy": "IfNotPresent", "command": ["sh","-c","until [ $(echo $(curl -s http://192.168.0.105:30005/console | grep Temporarily | wc -l)) -eq 1 ];do echo "waiting...";sleep 5;done"], "args": ["monitoring", "helloworldsvc"] } ]' spec: containers: - name: weblogichelloworld image: 1213-helloworld:v2 ports: - containerPort: 7001 --- apiVersion: v1 kind: Service metadata: name: helloworldsvc1 labels: weblogic-app: helloworld1 spec: type: NodePort ports: - port: 7001 protocol: TCP targetPort: 7001 name: http nodePort: 30006 selector: weblogic-app: helloworld1 sessionAffinity: ClientIP --- apiVersion: v1 kind: ReplicationController metadata: name: helloworld-service spec: replicas: 1 template: metadata: labels: weblogic-app: "helloworld" version: "0.1" spec: containers: - name: weblogichelloworld image: 1213-helloworld:v2 ports: - containerPort: 7001 --- apiVersion: v1 kind: Service metadata: name: helloworldsvc labels: weblogic-app: helloworld spec: type: NodePort ports: - port: 7001 protocol: TCP targetPort: 7001 name: http nodePort: 30005 selector: weblogic-app: helloworld sessionAffinity: ClientIP

一言不合先贴一段代码:)

先需要下载一个giantswarm/tiny-tools的小工具镜像,然后让他运行脚本

sh -c "until [ $(echo $(curl http://192.168.0.105:30005/console | grep "Temporarily" | wc -l)) -eq 1 ]; do echo "waiting...";sleep 3;done"

通过了才让继续,否则sleep然后再sleep,这段脚本踩了无数坑。

原来是这样的

sh -c "until [ $(echo $(curl http://192.168.0.105:30005/console | grep 'Moved Temporarily' | wc -l)) -eq 1 ]; do echo "waiting...";sleep 3;done"

再pod内部运行没问题,但写到yaml文件死活不过,然后换成

sh -c "until [ $(echo $(curl http://192.168.0.105:30005/console | grep "Moved Temporarily" | wc -l)) -eq 1 ]; do echo "waiting...";sleep 3;done"

yaml文件格式通过,但死活运行不出来

后来发现grep的词语不能有空格,最后变成这样就通过了

sh -c "until [ $(echo $(curl http://192.168.0.105:30005/console | grep Temporarily | wc -l)) -eq 1 ]; do echo "waiting...";sleep 3;done"

如果不是一个pod,而是一个deployment需要依赖服务启动,yaml文件如下

[root@k8s-master ~]# cat rc-sequence.yaml 
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: helloworld-service
  labels:
        weblogic-app: helloworld1
spec:
   replicas: 2
   template:
     metadata:
      labels:
        weblogic-app: helloworld1
      annotations:
        pod.beta.kubernetes.io/init-containers: '[
                {
                        "name": "wait-weblogic",
                        "image": "docker.io/giantswarm/tiny-tools:latest",
                        "imagePullPolicy": "IfNotPresent",
                        "command": ["sh","-c","until [ $(echo $(curl -s  http://192.168.0.105:30005/console | grep Temporarily | wc -l)) -eq 1 ];do echo "waiting...";sleep 5;done"],
                        "args": ["monitoring", "helloworldsvc"]
                }
        ]'
     spec:
      containers:
      - name: weblogichelloworld
        image: 1213-helloworld:v2
        ports:
        - containerPort: 7001
---
apiVersion: v1
kind: Service
metadata:
  name: helloworldsvc1
  labels:
    weblogic-app: helloworld1
spec:
  type: NodePort
  ports:
  - port: 7001
    protocol: TCP
    targetPort: 7001
    name: http
    nodePort: 30006
  selector:
    weblogic-app: helloworld1
  sessionAffinity: ClientIP
---
apiVersion: v1
kind: ReplicationController
metadata:
  name: helloworld-service
spec:
  replicas: 1 
  template:
    metadata:
      labels:
        weblogic-app: "helloworld"
        version: "0.1"
    spec:
      containers:
      - name: weblogichelloworld
        image: 1213-helloworld:v2
        ports:
        - containerPort: 7001
---
apiVersion: v1
kind: Service
metadata:
  name: helloworldsvc
  labels:
    weblogic-app: helloworld
spec:
  type: NodePort
  ports:
  - port: 7001
    protocol: TCP
    targetPort: 7001
    name: http
    nodePort: 30005
  selector:
    weblogic-app: helloworld
  sessionAffinity: ClientIP
原文地址:https://www.cnblogs.com/ericnie/p/8085816.html