注意:
- 此教程需要访问 Oracle Cloud。要注册免费账户,请参阅开始使用 Oracle Cloud Infrastructure Free Tier 。
- 它使用 Oracle Cloud Infrastructure 身份证明、租户和区间示例值。完成实验室时,请将这些值替换为特定于云环境的值。
在 Oracle Cloud Infrastructure 中使用 Pulumi 部署计算实例和 VCN
简介
在本教程中,我们将使用 Pulumi 部署计算实例和虚拟云网络 (VCN)。借助 Pulumi 的简单性和 Oracle Cloud Infrastructure (OCI) 强大的基础设施,您将学会轻松自动化部署流程。您将学习如何轻松部署和管理 OCI 资源,释放云部署的新可能性。
目标
- 在 OCI 中使用 Pulumi Infrastructure 作为代码 (IaC) 工具部署计算实例和 VCN 资源。
先决条件
- 安装和配置 OCI 提供商 Pulumi 并设置 OCI 的环境变量。有关详细信息,请参阅在 Oracle Cloud Infrastructure 上安装和配置 Pulumi Infrastructure as Code 。
任务 1:创建 VCN
使用 Pulumi 部署 VCN。
-
为 Pulumi 项目创建新目录。
-
打开终端或命令提示符。
-
导航到要创建 Pulumi 项目的目录。可以使用
cd
命令更改目录。 -
为项目创建新目录。
例如:
mkdir my-oci-project cd my-oci-project
-
-
运行以下命令以使用 OCI Python 模板创建新的 Pulumi 项目。在这里,我们使用 python 作为 pulumi 的编程语言,但您可以使用您选择的任何支持的编程语言。
pulumi new oci-python
-
Pulumi 会要求您配置项目。输入必需的信息,例如项目名称、项目说明和堆栈名称,然后单击输入。
例如:
Project name: my-oci-project Project description: This is an example of OCI Compute and VCN deployment Stack name: Dev
-
复制并定制代码。
-
Pulumi 将在项目目录中创建名为
__main__.py
的基本 Python 文件。 -
使用文本编辑器或 IDE VS Code 打开此文件。
-
使用 Pulumi 的 Python SDK 编写 OCI 基础设施代码,并定义 VCN、子网、安全组等。例如,将提供的 Python 代码复制到名为
__main__.py
的新文件中,并通过使用实际的 OCI 区间 ID 和显示名称替换占位符值来定制代码。这些更改可确保您的资源在正确的区间中创建并具有有意义的名称。"""A OCI Python Pulumi program""" import pulumi import pulumi_oci as oci from pulumi_oci import core from pulumi_oci import identity # Create a New VCN test_vcn = oci.core.Vcn("test_vcn", compartment_id="ocid1.compartment.oc1...xx...xxx", cidr_block="10.0.0.0/16", display_name="ocinetwork", dns_label="ocinetwork" ) #Code block for InternetGateway test_internet_gateway = oci.core.InternetGateway("test_internet_gateway", compartment_id="ocid1.compartment.oc1...xx...xxx", vcn_id=test_vcn.id, enabled="true", display_name="internet_gateway") #Code block for route table and attach to IG test_route_table = oci.core.RouteTable("test_route_table", compartment_id="ocid1.compartment.oc1...xx...xxx", vcn_id=test_vcn.id, display_name="route_table_ig", route_rules=[oci.core.RouteTableRouteRuleArgs( network_entity_id=test_internet_gateway.id, destination="0.0.0.0/0", destination_type="CIDR_BLOCK", )]) #Code block for security list security_list_resource = oci.core.SecurityList("securityListResource", compartment_id="ocid1.compartment.oc1...xx...xxx", vcn_id=test_vcn.id, display_name="sc_list", egress_security_rules=[oci.core.SecurityListEgressSecurityRuleArgs( destination="0.0.0.0/0", protocol="all", destination_type="CIDR_BLOCK", stateless=False, )], ingress_security_rules=[ oci.core.SecurityListIngressSecurityRuleArgs( protocol="6", source="0.0.0.0/0", description="Sor SSH", tcp_options=oci.core.SecurityListIngressSecurityRuleTcpOptionsArgs( max=22, min=22, ), source_type="CIDR_BLOCK", stateless=False, ), oci.core.SecurityListIngressSecurityRuleArgs( protocol="1", source="0.0.0.0/0", description="ICMP", icmp_options=oci.core.SecurityListIngressSecurityRuleIcmpOptionsArgs( type=3, code=4, ), source_type="CIDR_BLOCK", stateless=False, ) ] ) # Create a Public Subnet test_public_subnet = oci.core.Subnet("test_public_subnet", cidr_block="10.0.0.0/24", compartment_id="ocid1.compartment.oc1...xx...xxx", vcn_id=test_vcn.id , display_name="public_subnet", dns_label="publicsubnet", prohibit_internet_ingress="False", prohibit_public_ip_on_vnic="False", route_table_id=test_route_table.id, security_list_ids=[security_list_resource.id]) # Create a Private Subnet test_Private_subnet = oci.core.Subnet("test_private_subnet", cidr_block="10.0.1.0/24", compartment_id="ocid1.compartment.oc1...xx...xxx", vcn_id=test_vcn.id , display_name="private_subnet", dns_label="privatesubnet", prohibit_internet_ingress="true", prohibit_public_ip_on_vnic="true")
此代码在 OCI 中预配 VCN 及其关联资源。它创建 Internet 网关,将路由表附加到该网关进行路由,并设置用于控制网络流量的安全列表。此外,它还在 VCN 中创建公共子网和专用子网,每个子网都有特定的网络配置,例如 CIDR 块和 DNS 标签。它可确保专用子网禁止互联网访问和向实例分配公共 IP,从而增强网络安全性。
-
-
部署 VCN 代码。
-
运行以下命令以部署 VCN 资源。
pulumi up
-
Pulumi 将检测代码中定义的资源并提示您确认部署。查看更改,并在出现提示时输入
yes
以继续。
-
-
部署完成后,验证您的 VCN 和其他资源是否已在 OCI 中预配。您可以检查 OCI 控制台或使用 Oracle Cloud Infrastructure 命令行界面 (OCI CLI) 列出区间中的资源。
任务 2:创建计算实例
让我们通过使用 Pulumi 将 OCI 计算实例添加到现有 VCN 来扩展 OCI 部署。
-
要访问 Pulumi 项目,请导航到包含 Pulumi 项目的目录并打开
__main__.py
文件。 -
复制以下代码并将其粘贴到
__main__.py
文件中,以便在现有 VCN 部署代码之后进行更新。# Create a Compute Instance compartment_id = "ocid1.compartment.oc1..xxxxxx" test_availability_domains = oci.identity.get_availability_domains(compartment_id="ocid1.compartment.oc1.xxx") public_ssh_key = "Copy and Paste the Public SSH Key" instance = oci.core.Instance("my-vm-instance", # Replace with your desired availability domain. availability_domain=test_availability_domains.availability_domains[0].name, # Replace with your compartment ID. compartment_id="ocid1.compartment.oc1..xxxxxxxx", # Using VM.Standard.E4.Flex shape. shape="VM.Standard.E4.Flex", # Replace with your subnet ID. create_vnic_details=oci.core.InstanceCreateVnicDetailsArgs( assign_private_dns_record=True, assign_public_ip="true", display_name="pulumideminst", subnet_id=test_public_subnet.id, ), # Metadata for the instance, including SSH keys for access. metadata={ "ssh_authorized_keys": public_ssh_key #"ssh_authorized_keys": std.file_output(input=public_ssh_key).apply(lambda invoke: invoke.result) }, # Use an Oracle-provided image or your own custom image. source_details=oci.core.InstanceSourceDetailsArgs( source_type="image", # Replace with the image OCID. source_id="ocid1.image.oc1.iad.aaaaaaaalbjc2slze7i3rbpho3p4ict6u4k2l6r2r3igvvkopbfd4xt2wwla", ), # Specifying the OCPU and memory configurations. shape_config=oci.core.InstanceShapeConfigArgs( ocpus=2.0, # Number of OCPUs. memory_in_gbs=8.0, # Amount of RAM in GBs. ), # Additional arguments like display name, can be specified here. display_name="pulumidemo_instance",) # Exporting the public IP of the instance. pulumi.export('instance_public_ip', instance.public_ip), pulumi.export('create_shape', instance.shape) pulumi.export('create_vnic', instance.create_vnic_details) pulumi.export('create_vnic', instance.create_vnic_details["display_name"])
将
ocid1.compartment.oc1.xxxxxxx
替换为区间 ID,将VM.Standard.E4.Flex
替换为所需的实例配置,将ocid1.image.oc1.iad.xxxxxxx
替换为所需的映像 ID,将YOUR_SSH_PUBLIC_KEY
替换为 SSH 公共密钥。 -
保存对
__main__.py
的更改并使用 Pulumi 命令行界面部署更新的堆栈。pulumi up
-
部署后,验证是否已在 VCN 中成功预配了计算实例。您可以检查 OCI 控制台,也可以使用 OCI CLI 在区间中列出实例。
-
现在,您可以通过 SSH 访问 OCI 计算实例。确保配置了访问所必需的安全组和规则。
相关链接
确认
- Authors -Akarsha I K(Cloud Architect)、Maninder Flora(Cloud Architect)
更多学习资源
浏览 docs.oracle.com/learn 上的其他实验室,或者通过 Oracle Learning YouTube 频道访问更多免费学习内容。此外,请访问 education.oracle.com/learning-explorer 以成为 Oracle Learning Explorer。
有关产品文档,请访问 Oracle 帮助中心。
Deploy Compute Instance and VCN using Pulumi in Oracle Cloud Infrastructure
F99046-01
May 2024