Coder Social home page Coder Social logo

example-nifcloud-k8s-cluster's Introduction

🚀Getting Started🚀

NIFCLOUD上にTerraform + kubesprayでKubernetes Clsuterを構築するやつ

概要図

overview.png

Requirements

Kubernetesのインフラ作成

準備

  • NIFCLOUDのアカウントを用意する
  • ACCESS_KEY_ID/SECRET_ACCESS_KEYを設定
    export NIFCLOUD_ACCESS_KEY_ID=<YOUR ACCESS KEY>
    export NIFCLOUD_SECRET_ACCESS_KEY=<YOUR SECRET ACCESS KEY>
  • Kubernets Clsuterを構築するRegion/Zoneを設定(変更してもよい)
    export TF_VAR_region=jp-west-1
    export TF_VAR_availability_zone=west-11 

インフラの作成

  • SSH Keyの生成とアップロード
    terraform -chdir=terraform/live/sshkey-uploder init
    terraform -chdir=terraform/live/sshkey-uploder apply
  • Elastic IPの作成
    terraform -chdir=terraform/live/elasticip/ init
    terraform -chdir=terraform/live/elasticip/ apply
  • Kubernetesのインフラストラクチャの作成
    export TF_VAR_working_server_ip=$(curl ifconfig.me)
    terraform -chdir=terraform/live/cluster/ init
    terraform -chdir=terraform/live/cluster/ apply

Kubernetesクラスターの構築

準備

  • 環境変数の設定
    KUBESPRAY_VERSION=v2.21.0
    export BASTION_IP=$(terraform -chdir=terraform/live/elasticip/ output -json | jq -r .bastion.value)
    export ANSIBLE_SSH_ARGS="-o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -o ProxyCommand=\"ssh root@${BASTION_IP} -W %h:%p\""
    export CP_LB_IP=$(terraform -chdir=terraform/live/cluster/ output -json | jq -r .control_plane_lb.value)
  • 構築したインフラの情報をファイルに保存
    • wk/cpの数変えてたらansible/mycluster/hosts.yamlの編集も必要
    EXTRA_VARS_FILE=ansible/extra-vars_cluster_info.yml
    echo "---" > ${EXTRA_VARS_FILE}
    terraform -chdir=terraform/live/cluster/ output -json | jq -r -c '.bastion_info.value | to_entries[] | .result = .key + ": " + .value.private_ip | .result' >> ${EXTRA_VARS_FILE}
    terraform -chdir=terraform/live/cluster/ output -json | jq -r -c '.egress_info.value | to_entries[] | .result = .key + ": " + .value.private_ip | .result' >> ${EXTRA_VARS_FILE}
    terraform -chdir=terraform/live/cluster/ output -json | jq -r -c '.worker_info.value | to_entries[] | .result = .key + ": " + .value.private_ip | .result' >> ${EXTRA_VARS_FILE}
    terraform -chdir=terraform/live/cluster/ output -json | jq -r -c '.control_plane_info.value | to_entries[] | .result = .key + ": " + .value.private_ip | .result' >> ${EXTRA_VARS_FILE}
  • 実行環境のbash取得
    docker run --rm -it -e CP_LB_IP -e ANSIBLE_SSH_ARGS -e BASTION_IP --mount type=bind,source="$(pwd)",dst=/wd  quay.io/kubespray/kubespray:${KUBESPRAY_VERSION} bash
  • 必要なパッケージ/Roleの取得
    # https://github.com/kubernetes-sigs/kubespray/issues/9695
    pip install jmespath==0.9.5
    ansible-galaxy install -r /wd/ansible/requirements.yml 
  • ssh keyの設定
    eval `ssh-agent`
    ssh-add /wd/out/key

Egressの構築

  • setup_egress.ymlを実行
    ansible-playbook -i /wd/ansible/mycluster/hosts.yaml -e @/wd/ansible/extra-vars_cluster_info.yml /wd/ansible/setup_egress.yml 

Kubernets clusterの構築

  • cluster.ymlを実行
    ansible-playbook -i /wd/ansible/mycluster/hosts.yaml -e cp_lb_ip=${CP_LB_IP} -e @/wd/ansible/extra-vars_cluster_info.yml  cluster.yml
    • だいたい1時間ちょっとかかる...😇

Bastionの構築

  • setup_bastion.ymlを実行
    cp /wd/ansible/setup_bastion.yml .
    ansible-playbook -i /wd/ansible/mycluster/hosts.yaml -e cp_lb_ip=${CP_LB_IP} -e @/wd/ansible/extra-vars_cluster_info.yml setup_bastion.yml 

接続確認

  • BastionにSSH接続
    ssh -i out/key root@${BASTION_IP}
  • kubectl実行
    kubectl get pod -A
    

さいごに

  • out/keyを誰にも内緒の秘密の場所に保存する🤫

example-nifcloud-k8s-cluster's People

Contributors

ystkfujii avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

norikmb

example-nifcloud-k8s-cluster's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.