Pulumi实战 | 一款架构即代码的开源产品
新钛云服已累计为您分享741篇技术干货
一、Pulumi 诞生
(一)诞生原因
1、技术演化:
2、Pulumi 登场
1、组件(Cli端)
1)语言宿主(Language Hosts)
a、语言执行器
b、Runtime(运行时/语言运行器)
2)部署引擎(Deplayment Engine)
3)资源提供商(云商)
a、资源插件
b、SDK
2、组件(Service 端)
1)官方 Service 端
2)其它存储方式充当 Service 端
pulumi login file://D:\Lang-Python\Data\Pulumi-Service
二、为什么使用 Pulumi?
三、怎么使用Pulumi?
Pulumi 初体验
1、Pulumi 结构
2、安装 Pulumi
Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
choco install pulumi
3、开始使用 Pulumi
pulumi new alicloud-python
pulumi up
pulumi destroy
四、其它特点
目前支持这几种云商转换成 Pulumi 程序。
五、搭建 Navos 和 SpringBoot 环境:
1、编写 Shell 脚本
#!/bin/bash
export LC_ALL=en_US.UTF-8
# 当前目录
BASE_DIR=$(pwd)
# 环境变量保存目录
PROFILE_ENVS="/etc/profile.envs"
export BASE_DIR
export PROFILE_ENVS
# ****************************** JAVA CONFIG****************************** #
# Java 安装路径
JAVA_INSTALL="/usr/local/java11"
# Java 安装包
JAVA_FOLDER_NAME="jdk-11.0.16"
JAVA_PACKAGE="${JAVA_FOLDER_NAME}_linux-x64_bin.tar.gz"
# JAVA_HOME_PATH
JAVA_HOME_PATH="${JAVA_INSTALL}/${JAVA_FOLDER_NAME}"
export JAVA_INSTALL
export JAVA_FOLDER_NAME
export JAVA_PACKAGE
export JAVA_HOME_PATH
# ****************************** Maven CONFIG****************************** #
# Maven 安装路径
MAVEN_INSTALL="/usr/local/maven386"
# Maven 安装包
MAVEN_FOLDER_NAME="apache-maven-3.8.6"
MAVEN_PACKAGE="${MAVEN_FOLDER_NAME}-bin.tar.gz"
# MAVEN_HOME_PATH
MAVEN_HOME_PATH="${MAVEN_INSTALL}/${MAVEN_FOLDER_NAME}"
export MAVEN_INSTALL
export MAVEN_FOLDER_NAME
export MAVEN_PACKAGE
export MAVEN_HOME_PATH
# ****************************** Nacos CONFIG****************************** #
# Nacos 安装路径
NACOS_INSTALL="/usr/local/nacos211"
# Nacos 安装包
NACOS_FOLDER_NAME="nacos"
NACOS_PACKAGE="nacos-server-2.1.1.tar.gz"
# NACOS_HOME_PATH
NACOS_HOME_PATH="${NACOS_INSTALL}/${NACOS_FOLDER_NAME}"
export NACOS_INSTALL
export NACOS_FOLDER_NAME
export NACOS_PACKAGE
export NACOS_HOME_PATH
# ****************************** SpringBoot Boot CONFIG****************************** #
# SpringBoot Boot 工作台
SPRING_BOOT_WORKFLOW="/opt/boot_workflow"
SPRING_BOOT_GROUP_ID="com.gavin"
SPRING_BOOT_ARTIFACT_ID="na-boot"
SPRING_BOOT_VERSION="0.0.1-snapshot"
export SPRING_BOOT_WORKFLOW
export SPRING_BOOT_GROUP_ID
export SPRING_BOOT_ARTIFACT_ID
export SPRING_BOOT_VERSION
add_dir() {
dirs=$*
log "mkdir -p ${dirs}"
mkdir -p "${dirs}" >/dev/null 2>&1
LAST_INFO=$?
if [[ ${LAST_INFO} -eq 0 ]]; then
ok "Folder \"${dirs}\" Create Success"
else
err "Folder \"${dirs}\" Create Failed"
fi
}
1)安装 Java
function install_java() {
cd "${BASE_DIR}" || return 1
# 添加安装目录
add_dir "${JAVA_INSTALL}"
# 解压缩到安装目录
tar -zxf "packages/${JAVA_PACKAGE}" -C "${JAVA_INSTALL}/"
# 添加环境变量
cat >"${PROFILE_ENVS}/java.sh" <<EOF
export JAVA_HOME=${JAVA_HOME_PATH}
export JRE_HOME=${JAVA_HOME}/jre
export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
export PATH=${JAVA_HOME}/bin:$PATH
EOF
echo "source ${PROFILE_ENVS}/java.sh" >>/etc/profile
source /etc/profile
# 添加软链接
ln -s "${JAVA_HOME_PATH}/bin/java" /usr/bin/java
ln -s "${JAVA_HOME_PATH}/bin/javac" /usr/bin/javac
}
2)安装 Maven
function install_maven() {
cd "${BASE_DIR}" || return 1
# 添加安装目录
add_dir "${MAVEN_INSTALL}"
# 解压缩到安装目录
tar -zxf "packages/${MAVEN_PACKAGE}" -C "${MAVEN_INSTALL}/"
# 新建仓库目录
add_dir "${MAVEN_HOME_PATH}/repository"
# 更换阿里云镜像并设置maven仓库位置
cat >"${MAVEN_HOME_PATH}/conf/settings.xml" <<EOF
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.2.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.2.0 https://maven.apache.org/xsd/settings-1.2.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<servers>
</servers>
<mirrors>
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
</mirrors>
<localRepository>${MAVEN_HOME_PATH}/repository</localRepository>
<profiles>
</profiles>
</settings>
EOF
# 添加环境变量
cat >"${PROFILE_ENVS}/maven.sh" <<EOF
export MAVEN_HOME=${MAVEN_HOME_PATH}
export PATH=$PATH:$MAVEN_HOME/bin
EOF
echo "source ${PROFILE_ENVS}/maven.sh" >>/etc/profile
source /etc/profile
# 添加软链接
ln -s "${MAVEN_HOME_PATH}/bin/mvn" /usr/bin/mvn
}
3)安装 Navos
function start_nacos() {
# 单机模式运行 nacos
bash "${NACOS_HOME_PATH}/bin/startup.sh" -m standalone
}
function install_nacos() {
cd "${BASE_DIR}" || return 1
# 添加安装目录
add_dir "${NACOS_INSTALL}"
# 解压缩到安装目录
tar -zxf "packages/${NACOS_PACKAGE}" -C "${NACOS_INSTALL}/"
# 启动 nacos
start_nacos
}
4)创建 Spring Boot
function create_spring_boot() {
# 添加项目目录
add_dir "${SPRING_BOOT_WORKFLOW}"
# 进入项目目录
cd "${SPRING_BOOT_WORKFLOW}" || return 1
# maven 创建项目
echo "y" | mvn archetype:generate -DgroupId="${SPRING_BOOT_GROUP_ID}" \
-DartifactId="${SPRING_BOOT_ARTIFACT_ID}" \
-DarchetypeArtifactId=maven-archetype-quickstart \
-Dversion="${SPRING_BOOT_VERSION}"
# 编译 SpringBoot Boot
compile_spring_boot
# 启动 SpringBoot Boot
start_spring_boot
}
function compile_spring_boot() {
cd "${SPRING_BOOT_WORKFLOW}/${SPRING_BOOT_ARTIFACT_ID}/" || return 1
mvn compile
}
function start_spring_boot() {
cd "${SPRING_BOOT_WORKFLOW}/${SPRING_BOOT_ARTIFACT_ID}/" || return 1
# 启动
nohup mvn spring-boot:run -Dspring-boot.run.profiles=prod &
}
5)主执行函数
#!/bin/bash
export LC_ALL=en_US.UTF-8
source ./config.sh
source ./functions.sh
source ./scripts/java.sh
source ./scripts/maven.sh
source ./scripts/nacos.sh
source ./scripts/spring_boot.sh
# 前置处理
pre_deal() {
yum_install_pkg "rsync"
yum_install_pkg "tree"
yum_install_pkg "lsof"
yum_install_pkg "lrzsz"
}
# 前置处理
pre_deal
# 添加 profile 环境文件夹
add_dir "${PROFILE_ENVS}"
# 安装 Java
install_java
# 安装 Maven
install_maven
# 安装 Nacos
install_nacos
# 创建 SpringBoot Boot 项目
create_spring_boot
package com.gavin;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
@EnableAutoConfiguration // 作用: 开启自动配置 初始化spring环境 springmvc环境
@ComponentScan // 作用: 用来扫描相关注解 扫描范围 当前入口类所在的包及子包(com.gavin及其子包)
public class App {
public static void main(String[] args) {
// springApplication: spring应用类 作用: 用来启动springboot应用
// 参数1: 传入入口类 类对象 参数2: main函数的参数
SpringApplication.run(App.class, args);
}
}
package com.gavin.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/test")
public class helloController {
@GetMapping("/hello")
public String hello() {
System.out.println("Hello SpringBoot!!!");
return "Hello SpringBoot";
}
}
2、生成阿里云镜像
1)Packer介绍
2)安装 Packer
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo https://rpm.releases.hashicorp.com/RHEL/hashicorp.repo
sudo yum -y install packer
https://releases.hashicorp.com/packer/1.8.3/packer_1.8.3_windows_386.zip
https://releases.hashicorp.com/packer/1.8.3/packer_1.8.3_windows_amd64.zip
3)打包镜像脚本
SpringBoot.zip
4)创建打包镜像配置
{
"variables": {
"access_key": "ACCESS_KEY",
"secret_key": "SECRET_KEY"
},
"builders": [{
"type":"alicloud-ecs",
"access_key":"ACCESS_KEY",
"secret_key":"SECRET_KEY",
"region":"cn-hangzhou",
"image_name":"packer_spring_boot_image",
"source_image":"centos_7_03_64_20G_alibase_20170818.vhd",
"ssh_username":"root",
"instance_type":"ecs.n2.small",
"internet_charge_type":"PayByTraffic",
"io_optimized":"true"
}],
"provisioners": [{
"type": "file",
"source": "SpringBoot.zip",
"destination": "/tmp/"
},{
"type": "shell",
"inline": [
"sleep 30",
"cd /tmp",
"yum install -y unzip",
"unzip SpringBoot.zip",
"cd SpringBoot",
"sudo chmod 755 main.sh",
"./main.sh"
]
}]
}
5)执行镜像打包(Windows)
F:\PackerSoftware\packer\packer.exe validate spring_boot.json
F:\PackerSoftware\packer\packer.exe build spring_boot.json
3、编写 Pulumi 代码(Python)
1)创建 VPC
import pulumi_alicloud as alicloud
def create_network(pre_name):
# VPC
vpc_name = "{}_vpc".format(pre_name)
vpc = alicloud.vpc.Network(vpc_name, cidr_block="172.16.0.0/12")
return vpc
2)创建 Switch
import pulumi_alicloud as alicloud
def create_switch(pre_name, az, vpc):
# 交换机
vswitch_name = "{}_vswitch".format(pre_name)
vswitch = alicloud.vpc.Switch(vswitch_name, zone_id=az, cidr_block="172.16.1.0/24", vpc_id=vpc.id)
return vswitch
3)创建安全组以及安全规则
import pulumi
import pulumi_alicloud as alicloud
def create_security_group(pre_name, vpc):
# 安全组
sg_name = "{}_sg".format(pre_name)
sg_description = "{} security groups".format(pre_name)
sg = alicloud.ecs.SecurityGroup(sg_name, description=sg_description, vpc_id=vpc.id)
return sg
def create_security_group_rule(pre_name, sg, port_range):
# 安全组规则
sg_rule_name = "{}_sg_rule".format(pre_name)
sg_rule = alicloud.ecs.SecurityGroupRule(
sg_rule_name,
security_group_id=sg.id,
ip_protocol="tcp",
type="ingress",
nic_type="intranet",
port_range=port_range,
cidr_ip="0.0.0.0/0"
)
return sg_rule
4)创建 ECS
import pulumi
import pulumi_alicloud as alicloud
def create_instance(
pre_name,
availability_zone=None,
vswitch=None,
sg=None,
password=None,
user_data=None,
instance_type=None,
image_id=None
):
# 实例
sg_ids = [sg.id]
instance_name = "{}-instance".format(pre_name)
instance = alicloud.ecs.Instance(
instance_name,
availability_zone=availability_zone,
instance_type=instance_type,
security_groups=sg_ids,
image_id=image_id,
instance_name=instance_name,
vswitch_id=vswitch.id,
internet_max_bandwidth_out=10,
password=password,
user_data=user_data
)
pulumi.export("{}-IP".format(instance_name), instance.public_ip)
return instance
4、创建实例
1) pulumi up
pulumi up
2)查看结果
3) pulumi destroy
5、总结
六、后记
推荐阅读
了解新钛云服
微信扫码关注该文公众号作者
戳这里提交新闻线索和高质量文章给我们。
来源: qq
点击查看作者最近其他文章