本教程介绍如何使用Terraform一键部署OpenShift平台。

前提条件

在开始之前,请您确保完成以下操作:

背景信息

OpenShift是一个开源的容器云平台,底层基于容器资源编排系统Kubernetes和Docker引擎,企业可以基于此平台搭建内部PaaS(Platform as a Service,平台即服务)平台,贯穿CI/CD流程,提高企业IT效率,拥抱DevOps和敏捷开发。您可以使用Terraform Module (OpenShift)在阿里云上一键部署OpenShift平台。

操作步骤

  1. 编写Terraform脚本代码。本教程以Cloud Shell中运行terraform为例。
    1. 登录阿里云Cloud Shell
    2. 生成当前用户的密钥对。
      ssh-keygen
    3. main.tf中引用Module。
      vim main.tf

      按下i键进入vim的编辑模式,新增以下内容:

      module "openshift" {
        source  = "zzxwill/openshift/alicloud"
        version = "0.0.4"
        // 地域
        region = "cn-hangzhou"
        // 可用区
        availability_zone = "cn-hangzhou-i"
        // 请替换为您的本地公钥
        public_key = "ssh-rsa xxx"
      }

      其中public_key为本地公钥,可通过下面的命令查看。

      cat ~/.ssh/id_rsa.pub
    4. 编写OKD安装脚本。
      vim okd.sh

      按下i键进入vim的编辑模式,新增以下内容:

      #!/usr/bin/env bash
      
      set -x
      cd /opt/
      wget https://github.com/openshift/origin/releases/download/v3.11.0/openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit.tar.gz
      tar -zxvf openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit.tar.gz
      cd openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit
       export PATH=/opt/openshift-origin-client-tools-v3.11.0-0cbc58b-linux-64bit:$PATH
      
      yum install -y docker
      service docker start
      
      oc cluster up --skip-registry-check=true  --public-hostname=$1
  2. 运行terraform init初始化。
    terraform init

    命令输出结果类似如下:

    Initializing modules...
    Downloading zzxwill/openshift/alicloud 0.0.4 for openshift...
    - openshift in .terraform/modules/openshift
    
    Initializing the backend...
    
    Initializing provider plugins...
    - Checking for available provider plugins...
    - Downloading plugin for provider "null" (hashicorp/null) 2.1.2...
    - Downloading plugin for provider "alicloud" (hashicorp/alicloud) 1.71.0...
    
    The following providers do not have any version constraints in configuration,
    so the latest version was installed.
    
    To prevent automatic upgrades to new major versions that may contain breaking
    changes, it is recommended to add version = "..." constraints to the
    corresponding provider blocks in configuration, with the constraint strings
    suggested below.
    
    * provider.alicloud: version = "~> 1.71"
    * provider.null: version = "~> 2.1"
    
    Terraform has been successfully initialized!
    
    You may now begin working with Terraform. Try running "terraform plan" to see
    any changes that are required for your infrastructure. All Terraform commands
    should now work.
    
    If you ever set or change modules or backend configuration for Terraform,
    rerun this command to reinitialize your working directory. If you forget, other
    commands will detect it and remind you to do so if necessary.
  3. 运行terraform apply开始创建。
    terraform apply

    看到类似下面的输出结果,说明创建完成。

    Apply complete! Resources: 11 added, 0 changed, 0 destroyed.
    
    Outputs:
    
    OpenShift = https://x.x.x.x:8443/console
    Password = 123456
    Username = admin
  4. 登录OpenShift平台。
    1. 完成创建后在浏览器输入https://x.x.x.x:8443/console(替换为自己的IP地址),就可以看到OpenShift登录页面。
      okd_login
    2. 在登录界面输入Uername=admin,Password=123456,登录成功后如下图所示:
      okd_console