Skip to content

实战-terraform方式部署k8s集群-20240531(测试成功)

实战:terraform方式部署k8s集群-2024.5.31(测试成功)

参考文档

《00-devops8基础环境配置(重要)》 https:# terraform用到的providersterraform{required_providers{kind={source="tehcyx/kind"version="0.0.12"}null={source="hashicorp/null"version="3.1.1"}}}provider"kind"{}# 此变量指定kubeconfig的文件输出路径variable"kind_cluster_config_path"{type=stringdefault="~/.kube/config"}# 此输出会在控制台打印kubeconfig内容output"kubeconfig"{value=kind_cluster.default.kubeconfig}# 定义k8s集群resource"kind_cluster""default"{name="devopscluster"# 集群名称node_image="kindest/node:v1.24.0"# kind镜像kubeconfig_path=pathexpand(var.kind_cluster_config_path) # kubeconfig路径wait_for_ready=true# 等待集群节点ready# kind配置文件kind_config{kind="Cluster"api_version="kind.x-k8s.io/v1alpha4"# Control节点配置node{role="control-plane"kubeadm_config_patches=[<<-EOTkind:InitConfigurationimageRepository:registry.aliyuncs.com/google_containersnetworking:serviceSubnet:10.0.0.0/16apiServerAddress:"0.0.0.0"nodeRegistration:kubeletExtraArgs:node-labels:"ingress-ready=true"---kind:KubeletConfigurationcgroupDriver:systemdcgroupRoot:/kubeletfailSwapOn:falseEOT]extra_port_mappings {container_port =80host_port =80}extra_port_mappings {container_port =443host_port =443}extra_port_mappings {container_port =6443host_port =6443}}# worker 节点1node {role ="worker"}# worker 节点2node {role ="worker"}}}# null_resource 用于执行shell命令# 此步骤用于加载ingress镜像并部署ingressresource "null_resource""wait_for_instatll_ingress"{triggers ={key =uuid()}provisioner "local-exec"{command =<<EOFsleep 5 kind load docker-image k8s.gcr.io/ingress-nginx/controller:v1.2.0 --name devopsclusterkind load docker-image k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.1.1 --name devopsclusterkubectl create ns ingress-nginxkubectl apply -f ingress.yaml -n ingress-nginxprintf "\nWaiting for the nginx ingress controller...\n"kubectl wait --namespace ingress-nginx \--for=condition=ready pod \--selector=app.kubernetes.io/component=controller \--timeout=90s #这里等待了90sEOF}depends_on =[kind_cluster.default]}

结果

bash
[root@devops8 ~]#kind get clustersdevopscluster[root@devops8 ~]#kubectl get nodeNAMESTATUSROLESAGEVERSIONdevopscluster-control-planeReadycontrol-plane7h14mv1.24.0devopscluster-workerReady<none>7h14mv1.24.0devopscluster-worker2Ready<none>7h14mv1.24.0[root@devops8 ~]#kubectl get po -ANAMESPACENAMEREADYSTATUSRESTARTSAGEingress-nginxingress-nginx-admission-create-kv69f0/1Completed07h14mingress-nginxingress-nginx-admission-patch-2swjx0/1Completed07h14mingress-nginxingress-nginx-controller-59c96b9cb7-95rd61/1Running07h14mkube-systemcoredns-6d4b75cb6d-czwnm1/1Running07h14mkube-systemcoredns-6d4b75cb6d-scvnz1/1Running07h14mkube-systemetcd-devopscluster-control-plane1/1Running07h15mkube-systemkindnet-7dmf61/1Running07h14mkube-systemkindnet-d2x641/1Running07h14mkube-systemkindnet-mc4qm1/1Running07h14mkube-systemkube-apiserver-devopscluster-control-plane1/1Running07h15mkube-systemkube-controller-manager-devopscluster-control-plane1/1Running07h15mkube-systemkube-proxy-bvf7d1/1Running07h14mkube-systemkube-proxy-lbt7f1/1Running07h14mkube-systemkube-proxy-s2lsg1/1Running07h14mkube-systemkube-scheduler-devopscluster-control-plane1/1Running07h15mkube-systemnfs-subdir-external-provisioner-5846f745b4-8sx2n1/1Running07h13mlocal-path-storagelocal-path-provisioner-9cd9bd544-s2grg1/1Running07h14m[root@devops8 ~]#
最近更新