如何使用cloudformation在aws上创建和保存脚本?正在尝试启动kafka-zookeeper节点

vnjpjtjt  于 2021-06-04  发布在  Kafka
关注(0)|答案(1)|浏览(298)

在我开始告诉你之前,我对云形成和编码的整个过程都很陌生
我一直在尝试编写一个云形成模板,用一个zookeeper节点启动一个示例,并一步一步地进行。所以我写了我的用户数据来下载kafka tar并解压它。
现在不使用“bin/zookeeper-server-start.sh-daemon config/zookeeper.properties”来启动zookeeper,而是创建一个名为zookeeper的文件,然后在其上编写一个shell脚本并使其可执行如何在cloudformation中执行此操作??

DAEMON_PATH=/home/ubuntu/kafka/bin
 DAEMON_NAME=zookeeper

 PATH=$PATH:$DAEMON_PATH

 # See how we were called.
 case "$1" in
   start)
         # Start daemon.
         pid=`ps ax | grep -i 'org.apache.zookeeper' | grep -v grep | awk '{print $1}'`
         if [ -n "$pid" ]
           then
             echo "Zookeeper is already running";
         else
           echo "Starting $DAEMON_NAME";
           $DAEMON_PATH/zookeeper-server-start.sh -daemon /home/ubuntu/kafka/config/zookeeper.properties
         fi
         ;;
   stop)
         echo "Shutting down $DAEMON_NAME";
         $DAEMON_PATH/zookeeper-server-stop.sh
         ;;
   restart)
         $0 stop
         sleep 2
         $0 start
         ;;
   status)
         pid=`ps ax | grep -i 'org.apache.zookeeper' | grep -v grep | awk '{print $1}'`
         if [ -n "$pid" ]
           then
           echo "Zookeeper is Running as PID: $pid"
         else
           echo "Zookeeper is not Running"
         fi
         ;;
   *)
         echo "Usage: $0 {start|stop|restart|status}"
         exit 1
 esac

 exit 0

这是我试图写的脚本,所以我可以启动和停止使用sudo服务zookeeper启动/停止zookeeper
手动使用cli我做了下面的操作,然后将脚本粘贴到zookeeper中并保存它。

sudo nano /etc/init.d/zookeeper
sudo chmod +x /etc/init.d/zookeeper
sudo chown root:root /etc/init.d/zookeeper

谢谢,希望你们能理解我的问题。

polhcujo

polhcujo1#

有几种方法可以做到这一点:
将文件存储在s3中,并使用aws sdk下载到您的应用程序中 UserData .
直接在中创建文件 UserData 使用aws::cloudformation::init定义文件。
使用脚本创建预烘焙的ami,并使用ami启动示例
下面是一个例子 UserData 其中使用了heredoc bash 创建这样的文件。

UserData: 
        Fn::Base64: |
              #!/bin/bash -ex                            

              cat >/etc/init.d/zookeeper << 'EOL'

              DAEMON_PATH=/home/ubuntu/kafka/bin
              DAEMON_NAME=zookeeper

              PATH=$PATH:$DAEMON_PATH

              # See how we were called.
              case "$1" in
                start)
                      # Start daemon.
                      pid=`ps ax | grep -i 'org.apache.zookeeper' | grep -v grep | awk '{print $1}'`
                      if [ -n "$pid" ]
                        then
                          echo "Zookeeper is already running";
                      else
                        echo "Starting $DAEMON_NAME";
                        $DAEMON_PATH/zookeeper-server-start.sh -daemon /home/ubuntu/kafka/config/zookeeper.properties
                      fi
                      ;;
                stop)
                      echo "Shutting down $DAEMON_NAME";
                      $DAEMON_PATH/zookeeper-server-stop.sh
                      ;;
                restart)
                      $0 stop
                      sleep 2
                      $0 start
                      ;;
                status)
                      pid=`ps ax | grep -i 'org.apache.zookeeper' | grep -v grep | awk '{print $1}'`
                      if [ -n "$pid" ]
                        then
                        echo "Zookeeper is Running as PID: $pid"
                      else
                        echo "Zookeeper is not Running"
                      fi
                      ;;
                *)
                      echo "Usage: $0 {start|stop|restart|status}"
                      exit 1
              esac

              exit 0
              EOL

              chmod +x /etc/init.d/zookeeper
              chown root:root /etc/init.d/zookeeper

请不要,我还没有运行这个代码,因此我无法验证它是否可以在开箱即用。

相关问题