Cearc no an t-ubh : splitting IaC

Cearc no an t-ubh : splitting IaC
Dè thàinig an toiseach - an cearc neo an ugh? Tòisich gu math neònach airson artaigil mu Bhun-structar-as-Code, nach e?

Dè th' ann an ugh?

Mar as trice, tha Bun-structair-mar-Chòd (IaC) na dhòigh dearbhach airson bun-structar a riochdachadh. Ann tha sinn a’ toirt cunntas air an staid a tha sinn airson a choileanadh, a’ tòiseachadh bhon phàirt bathar-cruaidh agus a’ crìochnachadh le rèiteachadh bathar-bog. Mar sin tha IaC air a chleachdadh airson:

  1. Solarachadh Ghoireasan. Is iad sin VMan, S3, VPC, msaa. Innealan bunaiteach airson obair: Terraform и Cruthachadh Cloud.
  2. Rèiteachadh bathar-bog. Innealan bunaiteach: Freagair, còcaire, etc.

Tha còd sam bith ann an stòran git. Agus nas luaithe no nas fhaide bidh ceannard na sgioba co-dhùnadh gum feum iad a bhith air an cur ann an òrdugh. Agus bheir e ath-bheothachadh. Agus cruthaichidh e beagan structar. Agus chì e gu bheil seo math.

Tha e math cuideachd gu bheil e ann mu thràth GitLab и GitHub-provider airson Terraform (agus is e seo Configuration Bathar-bog). Le an cuideachadh, faodaidh tu am pròiseact gu lèir a riaghladh: buill sgioba, CI / CD, git-flow, msaa.

Cò às a thàinig an t-ugh?

Mar sin tha sinn mean air mhean a 'tighinn faisg air a' phrìomh cheist.

An toiseach, feumaidh tu tòiseachadh le stòr a bheir cunntas air structar stòran eile, thu fhèin nam measg. Agus gu dearbh, mar phàirt de GitOps, feumaidh tu CI a chuir ris gus an tèid atharrachaidhean a chuir gu bàs gu fèin-ghluasadach.

Mura h-eil Git air a chruthachadh fhathast?

  1. Ciamar a stòradh ann an Git?
  2. Ciamar a stàladh CI?
  3. Ma chleachdas sinn Gitlab cuideachd a’ cleachdadh IaC, agus eadhon ann an Kubernetes?
  4. Agus GitLab Runner cuideachd ann an Kubernetes?
  5. Dè mu dheidhinn Kubernetes anns an t-solaraiche sgòthan?

Dè a thàinig an toiseach: an GitLab far an luchdaich mi suas mo chòd, no an còd a mhìnicheas dè an seòrsa GitLab a tha a dhìth orm?

Cearc le uighean

«Oyakodon3 le dineosaur" [SRC]

Feuchaidh sinn ri mias a chòcaireachd le bhith a’ cleachdadh mar sholaraiche sgòthan Stiùirich Kubernetes Selectel.

TL; DR

A bheil e comasach a dhol còmhla ri aon sgioba aig an aon àm?

$ export MY_SELECTEL_TOKEN=<token>
$ curl https://gitlab.com/chicken-or-egg/mks/make/-/snippets/2002106/raw | bash

Ingredients:

  • Cunntas bho my.selectel.ru;
  • Comharra cunntais;
  • Sgilean Kubernetes;
  • Sgilean Helm;
  • Sgilean Terraform;
  • Clàr Helm GitLab;
  • Cairt Helm Runner GitLab.

Rèit:

  1. Faigh MY_SELECTEL_TOKEN on phannal mo.selectel.ru.
  2. Cruthaich cruinneachadh Kubernetes le bhith a’ gluasad tòcan cunntais thuige.
  3. Faigh KUBECONFIG bhon bhuidheann a chaidh a chruthachadh.
  4. Stàlaich GitLab air Kubernetes.
  5. Faigh GitLab-token bho GitLab air a chruthachadh airson neach-cleachdaidh freumh.
  6. Cruthaich structar pròiseict ann an GitLab a’ cleachdadh GitLab-token.
  7. Brùth an còd gnàthaichte gu GitLab.
  8. ???
  9. Prothaid!

ceum 1. Gheibhear an tòcan anns an earrainn Iuchraichean API.

Cearc no an t-ubh : splitting IaCceum 2. Bidh sinn ag ullachadh ar Terraform airson “bèicearachd” cruinneachadh de 2 nodan. Ma tha thu cinnteach gu bheil goireasan gu leòr agad airson a h-uile càil, faodaidh tu cuotathan fèin-ghluasadach a chomasachadh:

provider "selectel" {
 token = var.my_selectel_token
}

variable "my_selectel_token" {}
variable "username" {}
variable "region" {}


resource "selectel_vpc_project_v2" "my-k8s" {
 name = "my-k8s-cluster"
 theme = {
   color = "269926"
 }
 quotas {
   resource_name = "compute_cores"
   resource_quotas {
     region = var.region
     zone = "${var.region}a"
     value = 16
   }
 }
 quotas {
   resource_name = "network_floatingips"
   resource_quotas {
     region = var.region
     value = 1
   }
 }
 quotas {
   resource_name = "load_balancers"
   resource_quotas {
     region = var.region
     value = 1
   }
 }
 quotas {
   resource_name = "compute_ram"
   resource_quotas {
     region = var.region
     zone = "${var.region}a"
     value = 32768
   }
 }
 quotas {
   resource_name = "volume_gigabytes_fast"
   resource_quotas {
     region = var.region
     zone = "${var.region}a"
     # (20 * 2) + 50 + (8 * 3 + 10)
     value = 130
   }
 }
}

resource "selectel_mks_cluster_v1" "k8s-cluster" {
 name         = "k8s-cluster"
 project_id   = selectel_vpc_project_v2.my-k8s.id
 region       = var.region
 kube_version = "1.17.9"
}

resource "selectel_mks_nodegroup_v1" "nodegroup_1" {
 cluster_id        = selectel_mks_cluster_v1.k8s-cluster.id
 project_id        = selectel_mks_cluster_v1.k8s-cluster.project_id
 region            = selectel_mks_cluster_v1.k8s-cluster.region
 availability_zone = "${var.region}a"
 nodes_count       = 2
 cpus              = 8
 ram_mb            = 16384
 volume_gb         = 15
 volume_type       = "fast.${var.region}a"
 labels            = {
   "project": "my",
 }
}

Cuir cleachdaiche ris a’ phròiseact:

resource "random_password" "my-k8s-user-pass" {
 length = 16
 special = true
 override_special = "_%@"
}

resource "selectel_vpc_user_v2" "my-k8s-user" {
 password = random_password.my-k8s-user-pass.result
 name = var.username
 enabled  = true
}

resource "selectel_vpc_keypair_v2" "my-k8s-user-ssh" {
 public_key = file("~/.ssh/id_rsa.pub")
 user_id    = selectel_vpc_user_v2.my-k8s-user.id
 name = var.username
}

resource "selectel_vpc_role_v2" "my-k8s-role" {
 project_id = selectel_vpc_project_v2.my-k8s.id
 user_id    = selectel_vpc_user_v2.my-k8s-user.id
}

Toradh:

output "project_id" {
 value = selectel_vpc_project_v2.my-k8s.id
}

output "k8s_id" {
 value = selectel_mks_cluster_v1.k8s-cluster.id
}

output "user_name" {
 value = selectel_vpc_user_v2.my-k8s-user.name
}

output "user_pass" {
 value = selectel_vpc_user_v2.my-k8s-user.password
}

Nach cuir sinn air bhog:

$ env 
TF_VAR_region=ru-3 
TF_VAR_username=diamon 
TF_VAR_my_selectel_token=<token> 
terraform plan -out planfile

$ terraform apply -input=false -auto-approve planfile

Cearc no an t-ubh : splitting IaC
ceum 3. Gheibh sinn an cubeconfig.

Gus KUBECONFIG a luchdachadh sìos gu prògramach, feumaidh tu comharra fhaighinn bho OpenStack:

openstack token issue -c id -f value > token

Agus leis a’ chomharra seo dèan iarrtas gu API Managed Kubernetes Selectel. k8s_id cùisean terraform:

curl -XGET -H "x-auth-token: $(cat token)" "https://ru-3.mks.selcloud.ru/v1/clusters/$(cat k8s_id)/kubeconfig" -o kubeConfig.yaml

Faodar faighinn gu Cupconfig tron ​​​​phannal cuideachd.

Cearc no an t-ubh : splitting IaC
ceum 4. Às deidh a’ bhraisle a bhith air a bhèicearachd agus cothrom fhaighinn air, is urrainn dhuinn yaml a chuir ris airson blas.

Is fheàrr leam a chur ris:

  • ainm-àite
  • clas stòraidh
  • poileasaidh tèarainteachd pod agus mar sin air adhart.

Clas stòraidh airson Selectel faodar a thoirt bho ionad-tasgaidh oifigeil.

Bhon toiseach thagh mi cruinneachadh san raon ru-3a, an uairsin feumaidh mi an Clas Stòraidh bhon raon seo.

kind: StorageClass
apiVersion: storage.k8s.io/v1
metadata:
 name: fast.ru-3a
 annotations:
   storageclass.kubernetes.io/is-default-class: "true"
provisioner: cinder.csi.openstack.org
parameters:
 type: fast.ru-3a
 availability: ru-3a
allowVolumeExpansion: true

ceum 5. Stàlaich cothromachadh luchdan.

Cleachdaidh sinn am fear àbhaisteach airson mòran nginx-steach. Tha stiùireadh gu leòr ann mu thràth airson a stàladh, agus mar sin cha bhith sinn a’ fuireach air.

$ helm repo add nginx-stable https://helm.nginx.com/stable
$ helm upgrade nginx-ingress nginx-stable/nginx-ingress -n ingress --install -f ../internal/K8S-cluster/ingress/values.yml

Bidh sinn a’ feitheamh gus am faigh e IP taobh a-muigh airson timcheall air 3-4 mionaidean:

Cearc no an t-ubh : splitting IaC
Fhuair sinn IP bhon taobh a-muigh:

Cearc no an t-ubh : splitting IaC
ceum 6. Stàlaich GitLab.

$ helm repo add gitlab https://charts.gitlab.io
$ helm upgrade gitlab gitlab/gitlab -n gitlab  --install -f gitlab/values.yml --set "global.hosts.domain=gitlab.$EXTERNAL_IP.nip.io"

A-rithist tha sinn a 'feitheamh gus an èirich na pods gu lèir.

kubectl get po -n gitlab
NAME                                      	READY   STATUS  	RESTARTS   AGE
gitlab-gitaly-0                           	0/1 	Pending 	0      	0s
gitlab-gitlab-exporter-88f6cc8c4-fl52d    	0/1 	Pending 	0      	0s
gitlab-gitlab-runner-6b6867c5cf-hd9dp     	0/1 	Pending 	0      	0s
gitlab-gitlab-shell-55cb6ccdb-h5g8x       	0/1 	Init:0/2	0      	0s
gitlab-migrations.1-2cg6n                 	0/1 	Pending 	0      	0s
gitlab-minio-6dd7d96ddb-zd9j6             	0/1 	Pending 	0      	0s
gitlab-minio-create-buckets.1-bncdp       	0/1 	Pending 	0      	0s
gitlab-postgresql-0                       	0/2 	Pending 	0      	0s
gitlab-prometheus-server-6cfb57f575-v8k6j 	0/2 	Pending 	0      	0s
gitlab-redis-master-0                     	0/2 	Pending 	0      	0s
gitlab-registry-6bd77b4b8c-pb9v9          	0/1 	Pending 	0      	0s
gitlab-registry-6bd77b4b8c-zgb6r          	0/1 	Init:0/2	0      	0s
gitlab-shared-secrets.1-pc7-5jgq4         	0/1 	Completed   0      	20s
gitlab-sidekiq-all-in-1-v1-54dbcf7f5f-qbq67   0/1 	Pending 	0      	0s
gitlab-task-runner-6fd6857db7-9x567       	0/1 	Pending 	0      	0s
gitlab-webservice-d9d4fcff8-hp8wl         	0/2 	Pending 	0      	0s
Waiting gitlab
./wait_gitlab.sh ../internal/gitlab/gitlab/.pods
waiting for pod...
waiting for pod...
waiting for pod...

Dh’ èirich na pods:

Cearc no an t-ubh : splitting IaC
ceum 7. Gheibh sinn GitLab-token.

An toiseach, faigh a-mach am facal-faire logadh a-steach:

kubectl get secret -n gitlab gitlab-gitlab-initial-root-password -o jsonpath='{.data.password}' | base64 --decode

A-nis leig dhuinn logadh a-steach agus faigh tòcan:

python3 get_gitlab_token.py root $GITLAB_PASSWORD http://gitlab.gitlab.$EXTERNAL_IP.nip.io

ceum 8. A’ toirt tasgaidhean Git chun rangachd cheart a’ cleachdadh an Solaraiche Gitlab.

cd ../internal/gitlab/hierarchy && terraform apply -input=false -auto-approve planfile

Gu mì-fhortanach, tha solaraiche terraform GitLab air bhog buga. An uairsin feumaidh tu na pròiseactan connspaideach a dhubhadh às le làimh gus an tèid tf.state a chàradh. An uairsin ath-ruith an àithne `$ dèan uile`

ceum 9. Bidh sinn a’ gluasad stòran ionadail chun t-seirbheisiche.

$ make push

[master (root-commit) b61d977]  Initial commit
 3 files changed, 46 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 values.yml
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 8 threads
Compressing objects: 100% (5/5), done.
Writing objects: 100% (5/5), 770 bytes | 770.00 KiB/s, done.
Total 5 (delta 0), reused 0 (delta 0)

Dèanta:

Cearc no an t-ubh : splitting IaC
Cearc no an t-ubh : splitting IaC
Cearc no an t-ubh : splitting IaC

co-dhùnadh

Tha sinn air a choileanadh gun urrainn dhuinn a h-uile càil a riaghladh gu dearbhach bhon inneal ionadail againn. A-nis tha mi airson na gnìomhan sin uile a ghluasad gu CI agus dìreach brùth putanan. Gus seo a dhèanamh, feumaidh sinn na stàitean ionadail againn (stàit Terraform) a ghluasad gu CI. Tha mar a nì thu seo anns an ath phàirt.

Subscribe to our блогgus nach caill thu foillseachadh artaigilean ùra!

Source: www.habr.com

Cuir beachd ann