面向初學者的 Kubernetes Kops 簡介
已發表: 2021-06-13讓我們了解一下 Kubernetes 運維工具 Kops。
Kubernetes 是最流行的 DevOps 工具之一,因為它具有驚人的容器編排系統和功能。 但是,儘管 Kubernetes 提供瞭如此多的功能,但從頭開始設置 Kubernetes 集群是很痛苦的。 這就是 Kops 發揮作用的地方。
使用 Kops,在 AWS、Google Cloud 等雲提供商上創建 Kubernetes 集群輕而易舉。它使設置 Kubernetes 集群變得輕鬆,在本文中,我將討論這個很棒的工具。
什麼是科普斯?
Kops,也稱為 Kubernetes 操作,是一個開源的官方 Kubernetes 項目,它允許您創建、維護、升級和銷毀高可用的生產級 Kubernetes 集群。 它還根據需求配置雲基礎架構。 Kops 的開發人員將其描述為 Kubernetes 集群的 kubectl。
Kops 主要用於部署 AWS 和 GCE Kubernetes 集群。 Kops 官方僅支持 AWS,其他雲提供商如 DigitalOcean、GCE 和 OpenStack 處於 beta 階段。
如果你以前在 kubectl 上工作過,你會覺得在 Kops 上工作很舒服。 Kops 提供了創建、獲取、更新、刪除集群的命令。 此外,Kops 知道如何在現有集群中應用更改,因為它使用聲明性配置。 使用 Kops,您還可以擴展和縮減 Kubernetes 集群。
以下是 Kops 的特點:
- 部署具有高可用性的 Kubernetes 主服務器
- 支持滾動集群更新
- 自動配置 AWS 和 GCE Kubernetes 集群
- 管理集群插件
- 在命令行中自動完成命令
- 生成 CloudFormation 和 Terraform 配置
- 支持用於空運行和自動冪等性的狀態同步模型
- 創建實例組以支持異構集群
安裝 Kops
以下是在 Linux 環境中安裝 Kops 的簡單步驟。 我正在使用 Ubuntu 20.x。
首先,從發布包下載 Kops。 下面的命令下載最新的 Kops 包。
geekflare@ip-170-11-26-119:~$ curl -Lo kops https://github.com/kubernetes/kops/releases/download/$(curl -s https://api.github.com/repos/kubernetes/kops/releases/latest | grep tag_name | cut -d '"' -f 4)/kops-linux-amd64 Saving to: 'kops-linux-amd64' 100%[=========================================================================================================================================================================>] 81,964,000 8.37MB/s in 7.1s 2021-06-10 16:23:19 (7.84 MB/s) - 'kops-linux-amd64' saved [81964000/81964000]
您需要為下載的 kops 文件提供可執行權限並將其移動到 /usr/local/bin/ 目錄。
geekflare@ip-170-11-26-119:~$ sudo chmod +x kops-linux-amd64 geekflare@ip-170-11-26-119:~$ sudo mv kops-linux-amd64 /usr/local/bin/kops
安裝完成。 現在您可以運行 kops 命令來驗證安裝。
geekflare@ip-170-11-26-119:~$ kops kops is Kubernetes ops. kops is the easiest way to get a production grade Kubernetes cluster up and running. We like to think of it as kubectl for clusters. kops helps you create, destroy, upgrade and maintain production-grade, highly available, Kubernetes clusters from the command line. AWS (Amazon Web Services) is currently officially supported, with GCE and VMware vSphere in alpha support. Usage: kops [command] Available Commands: completion Output shell completion code for the given shell (bash or zsh). create Create a resource by command line, filename or stdin. delete Delete clusters,instancegroups, or secrets. describe Describe a resource. edit Edit clusters and other resources. export Export configuration. get Get one or many resources. import Import a cluster. replace Replace cluster resources. rolling-update Rolling update a cluster. toolbox Misc infrequently used commands. update Update a cluster. upgrade Upgrade a kubernetes cluster. validate Validate a kops cluster. version Print the kops version information. Flags: --alsologtostderr log to standard error as well as files --config string config file (default is $HOME/.kops.yaml) -h, --help help for kops --log_backtrace_at traceLocation when logging hits line file:N, emit a stack trace (default :0) --log_dir string If non-empty, write log files in this directory --logtostderr log to standard error instead of files (default false) --name string Name of cluster --state string Location of state storage --stderrthreshold severity logs at or above this threshold go to stderr (default 2) -v, --v Level log level for V logs --vmodule moduleSpec comma-separated list of pattern=N settings for file-filtered logging Use "kops [command] --help" for more information about a command.
檢查 Kops 版本以確保正確安裝 Kops。
geekflare@ip-170-11-26-119:~$ kops version Version 1.20.1 (git-5a27dad)
現在讓我們看看管理員廣泛用於執行 Kubernetes 操作的幾個重要的 Kops 命令。
Kops 命令
以下是您必須知道的廣泛使用的 Kops 命令。
kops 創建
kops create 命令用於註冊集群。
語法: kops create cluster <clustername>
除了默認命令之外,您還可以添加許多其他參數,例如區域、區域、實例類型、節點數等。
kops 更新
kops update 命令用於使用指定的集群規範更新集群。
語法: kops update cluster –name <clustername>
您可以在預覽模式下運行此命令以更安全,一旦預覽輸出符合您的預期,您可以運行帶有 –yes 標誌的命令以將更改應用到集群。
kops 得到
kops get 命令用於列出所有集群。
語法: kops 獲取集群
kops 刪除
kops delete 命令用於從註冊表中刪除特定集群以及分配給該集群的所有云資源。
語法: kops delete cluster –name <clustername>
就像更新一樣,您也可以在預覽模式下運行此命令。
kops 滾動更新
kops rolling-update 命令用於更新 Kubernetes 集群以匹配雲和 kops 規範。
語法: kops rolling-update cluster –name <clustername>
就像更新一樣,您也可以在預覽模式下運行此命令。
kops 驗證
kops validate 命令驗證您創建的集群是否啟動。 例如,如果 pod 和節點處於掛起狀態,則 validate 命令將返回集群尚不健康。
語法: kops validate cluster –wait <specified_time>
此命令將等待並驗證集群指定的時間。 因此,如果您想驗證集群 5 分鐘,請以 5m 作為指定時間運行命令。
以上就是 Kops 的基礎知識,現在讓我向您展示如何使用 Kops 在 AWS 上創建 Kubernetes 集群。
使用 Kops 在 AWS 上設置 Kubernetes
在開始執行下面提到的步驟之前,有一些先決條件:
- Ubuntu 20.04
- AWS CLI
- AWS 配置
安裝 kubectl
首先,我將安裝 kubectl。
Kubectl 用於在 Kubernetes 集群上運行命令行命令。 下載一個 kubectl 包。
geekflare@ip-170-11-26-119:~$ curl -LO https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 39.6M 100 39.6M 0 0 6988k 0 0:00:07 0:00:07 --:--:-- 6988k
您需要為下載的文件提供可執行權限並將其移動到 /usr/local/bin/ 目錄。
geekflare@ip-170-11-26-119:~$ chmod +x ./kubectl geekflare@ip-170-11-26-119:~$ sudo mv ./kubectl /usr/local/bin/kubectl
創建 S3 存儲桶
在 Linux 機器中安裝和配置 AWS CLI 後,您將能夠運行aws
命令。 我在我的 Ubuntu 系統上安裝了 aws cli,所以讓我運行一個簡單的命令,它將列出 S3 中的所有存儲桶。
geekflare@ip-170-11-26-119:~$ aws s3 ls
它將是空的,因為我現在沒有任何 s3 存儲桶。 讓我檢查是否有任何 ec2 實例正在運行。
geekflare@ip-170-11-26-119:~$ aws ec2 describe-instances { "Reservations": [] }
這意味著目前沒有任何 ec2 實例正在運行。
現在您需要創建一個 s3 存儲桶,Kops 將在其中保存所有集群的狀態信息。 在這裡,我在 us-west-2 區域創建了一個名為 geekkops-bucket-1132 的 s3 存儲桶。 您可以使用 LocationConstraint 來避免該區域出現任何錯誤。
geekflare@ip-170-11-26-119:~$ aws s3api create-bucket --bucket geekkops-bucket-1132 --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2 { "Location": "http://geekkops-bucket-1132.s3.amazonaws.com/" }
如果我再次列出aws s3 bucket
,我將得到我剛剛創建的存儲桶。
geekflare@ip-170-11-26-119:~$ aws s3 ls 2021-06-10 16:30:13 geekkops-bucket-1132
運行以下命令以啟用 s3 存儲桶的版本。
geekflare@ip-170-11-26-119:~$ aws s3api put-bucket-versioning --bucket geekkops-bucket-1132 --versioning-configuration Status=Enabled
生成密鑰
生成 Kops 將用於集群登錄和密碼生成的 ssh 密鑰。
geekflare@ip-170-11-26-119:~$ ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/home/ubuntu/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/ubuntu/.ssh/id_rsa. Your public key has been saved in /home/ubuntu/.ssh/id_rsa.pub. The key fingerprint is: SHA256:fH4JCBXMNRqzk1hmoK+cXmwSFaeBsuGBA5IWMkNuvq0 geekflare@ip-170-11-26-119 The key's randomart image is: +---[RSA 2048]----+ |O=. .++Xoo | |B++ .. @o* . | |.= =. = = | |ooooo | | . . . S o | | o. = o . . | | . .= + . o | | .. + . | | E . | +----[SHA256]-----+
導出環境變量
將集群名稱和 s3 存儲桶公開為環境變量。 這僅適用於當前會話。 我使用後綴“.k8s.local”,因為我沒有使用任何預配置的 DNS。
geekflare@ip-170-11-26-119:~$ export KOPS_CLUSTER_NAME=geekdemo1.k8s.local geekflare@ip-170-11-26-119:~$ export KOPS_STATE_STORE=s3://geekkops-bucket-1132
創建集群
使用 kops create 命令創建集群。 以下是我使用 Kops 在 AWS 上創建 Kubernetes 集群的參數:
-
--cloud
告訴我正在使用的雲提供商 --zones
是將部署集群實例的區域--node-count
是要在 Kubernetes 集群中部署的節點數--node-size
和--master-size
是 ec2 實例類型,我使用的是微型實例--name
是集群名稱
geekflare@ip-170-11-26-119:~$ kops create cluster --cloud=aws --zones=eu-central-1a --node-count=1 --node-size=t2.micro --master-size=t2.micro --name=${KOPS_CLUSTER_NAME} I0216 16:35:24.225238 4326 subnets.go:180] Assigned CIDR 172.20.32.0/19 to subnet eu-central-1a I0216 16:35:24.068088 4326 create_cluster.go:717] Using SSH public key: /home/ubuntu/.ssh/id_rsa.pub Previewing changes that will be made: I0216 16:35:24.332590 4326 apply_cluster.go:465] Gossip DNS: skipping DNS validation I0216 16:35:24.392712 4326 executor.go:111] Tasks: 0 done / 83 total; 42 can run W0216 16:35:24.792113 4326 vfs_castore.go:604] CA private key was not found I0216 16:35:24.938057 4326 executor.go:111] Tasks: 42 done / 83 total; 17 can run I0216 16:35:25.436407 4326 executor.go:111] Tasks: 59 done / 83 total; 18 can run I0216 16:35:25.822395 4326 executor.go:111] Tasks: 77 done / 83 total; 2 can run I0216 16:35:25.823088 4326 executor.go:111] Tasks: 79 done / 83 total; 2 can run I0216 16:35:26.406919 4326 executor.go:111] Tasks: 81 done / 83 total; 2 can run I0216 16:35:27.842148 4326 executor.go:111] Tasks: 83 done / 83 total; 0 can run LaunchTemplate/master-eu-central-1a.masters.geekdemo1.k8s.local AssociatePublicIP true HTTPPutResponseHopLimit 1 HTTPTokens optional IAMInstanceProfile name:masters.geekdemo1.k8s.local id:masters.geekdemo1.k8s.local ImageID 099720109477/ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-20210119.1 InstanceType t2.micro RootVolumeSize 64 RootVolumeType gp2 RootVolumeEncryption false RootVolumeKmsKey SSHKey name:kubernetes.geekdemo1.k8s.local-3e:19:92:ca:dd:64:d5:cf:ff:ed:3a:92:0f:40:d4:e8 id:kubernetes.geekdemo1.k8s.local-3e:19:92:ca:dd:64:d5:cf:ff:ed:3a:92:0f:40:d4:e8 SecurityGroups [name:masters.geekdemo1.k8s.local] SpotPrice Tags {k8s.io/cluster-autoscaler/node-template/label/kubernetes.io/role: master, k8s.io/cluster-autoscaler/node-template/label/kops.k8s.io/instancegroup: master-eu-central-1a, k8s.io/role/master: 1, kops.k8s.io/instancegroup: master-eu-central-1a, Name: master-eu-central-1a.masters.geekdemo1.k8s.local, KubernetesCluster: geekdemo1.k8s.local, kubernetes.io/cluster/geekdemo1.k8s.local: owned, k8s.io/cluster-autoscaler/node-template/label/node-role.kubernetes.io/master: } Subnet/eu-central-1a.geekdemo1.k8s.local ShortName eu-central-1a VPC name:geekdemo1.k8s.local AvailabilityZone eu-central-1a CIDR 172.20.32.0/19 Shared false Tags {KubernetesCluster: geekdemo1.k8s.local, kubernetes.io/cluster/geekdemo1.k8s.local: owned, SubnetType: Public, kubernetes.io/role/elb: 1, Name: eu-central-1a.geekdemo1.k8s.local} VPC/geekdemo1.k8s.local CIDR 172.20.0.0/16 EnableDNSHostnames true EnableDNSSupport true Shared false Tags {kubernetes.io/cluster/geekdemo1.k8s.local: owned, Name: geekdemo1.k8s.local, KubernetesCluster: geekdemo1.k8s.local} VPCDHCPOptionsAssociation/geekdemo1.k8s.local VPC name:geekdemo1.k8s.local DHCPOptions name:geekdemo1.k8s.local Must specify --yes to apply changes Cluster configuration has been created. Suggestions: * list clusters with: kops get cluster * edit this cluster with: kops edit cluster geekdemo1.k8s.local * edit your node instance group: kops edit ig --name=geekdemo1.k8s.local nodes-eu-central-1a * edit your master instance group: kops edit ig --name=geekdemo1.k8s.local master-eu-central-1a Finally configure your cluster with: kops update cluster --name geekdemo1.k8s.local --yes –admin
運行 kops get 命令以查看集群是否已創建。

geekflare@ip-170-11-26-119:~$ kops get cluster NAME CLOUD ZONES geekdemo1.k8s.local aws eu-central-1a
更新集群
要將指定的集群規範應用於集群,請運行 kops update 命令。
geekflare@ip-170-11-26-119:~$ kops update cluster --name geekdemo1.k8s.local --yes --admin I0216 16:38:16.800767 4344 apply_cluster.go:465] Gossip DNS: skipping DNS validation I0216 16:38:16.919282 4344 executor.go:111] Tasks: 0 done / 83 total; 42 can run W0216 16:38:17.343336 4344 vfs_castore.go:604] CA private key was not found I0216 16:38:18.421652 4344 keypair.go:195] Issuing new certificate: "etcd-clients-ca" I0216 16:38:18.450699 4344 keypair.go:195] Issuing new certificate: "etcd-peers-ca-main" I0216 16:38:19.470785 4344 keypair.go:195] Issuing new certificate: "etcd-manager-ca-main" I0216 16:38:19.531852 4344 keypair.go:195] Issuing new certificate: "etcd-peers-ca-events" I0216 16:38:19.551601 4344 keypair.go:195] Issuing new certificate: "apiserver-aggregator-ca" I0216 16:38:19.571834 4344 keypair.go:195] Issuing new certificate: "etcd-manager-ca-events" I0216 16:38:19.592090 4344 keypair.go:195] Issuing new certificate: "master" W0216 16:38:19.652894 4344 vfs_castore.go:604] CA private key was not found I0216 16:38:19.653013 4344 keypair.go:195] Issuing new certificate: "ca" I0216 16:38:24.344075 4344 executor.go:111] Tasks: 42 done / 83 total; 17 can run I0216 16:38:24.306125 4344 executor.go:111] Tasks: 59 done / 83 total; 18 can run I0216 16:38:26.189798 4344 executor.go:111] Tasks: 77 done / 83 total; 2 can run I0216 16:38:26.190464 4344 executor.go:111] Tasks: 79 done / 83 total; 2 can run I0216 16:38:26.738600 4344 executor.go:111] Tasks: 81 done / 83 total; 2 can run I0216 16:38:28.810100 4344 executor.go:111] Tasks: 83 done / 83 total; 0 can run I0216 16:38:29.904257 4344 update_cluster.go:313] Exporting kubecfg for cluster kops has set your kubectl context to geekdemo1.k8s.local Cluster is starting. It should be ready in a few minutes. Suggestions: * validate cluster: kops validate cluster --wait 10m * list nodes: kubectl get nodes --show-labels * ssh to the master: ssh -i ~/.ssh/id_rsa [email protected] * the ubuntu user is specific to Ubuntu. If not using Ubuntu please use the appropriate user based on your OS. * read about installing addons at: https://kops.sigs.k8s.io/operations/addons.
如果您立即檢查 Kubernetes 節點是否正在運行,您將收到錯誤消息。 您需要耐心等待幾分鐘 (5-10) 直到創建集群。
geekflare@ip-170-11-26-119:~$ kubectl get nodes Unable to connect to the server: dial tcp: lookup api-geekdemo1-k8s-local-dason2-1001342368.eu-central-1.elb.amazonaws.com on 127.0.0.53:53: no such host
驗證集群
我正在運行 validate 命令 5 分鐘以檢查集群是否正常運行。 在驗證輸出中,您將能夠看到節點啟動時的詳細信息。
geekflare@ip-170-11-26-119:~$ kops validate cluster --wait 5m Validating cluster geekdemo1.k8s.local INSTANCE GROUPS NAME ROLE MACHINETYPE MIN MAX SUBNETS master-eu-central-1a Master t2.micro 1 1 eu-central-1a nodes-eu-central-1a Node t2.micro 1 1 eu-central-1a
列出節點和 Pod
現在運行以下命令以檢查所有節點是否已準備好並正在運行。 您可以看到主節點和節點都處於就緒狀態。
geekflare@ip-170-11-26-119:~$ kubectl get nodes NAME STATUS ROLES AGE VERSION ip-173-19-35-156.eu-central-1.compute.internal Ready master 10m v1.20.1 ip-172-36-23-149.eu-central-1.compute.internal Ready node 5m38s v1.20.1
您可以檢查 Kubernetes 集群中運行的所有 Pod。
geekflare@ip-170-11-26-119:~$ kubectl get pods --all-namespaces NAMESPACE NAME READY STATUS RESTARTS AGE kube-system dns-controller-8d8889c4b-xp9dl 1/1 Running 0 8m26s kube-system etcd-manager-events-ip-173-19-35-156.eu-central-1.compute.internal 1/1 Running 0 10m kube-system etcd-manager-main-ip-173-19-35-156.eu-central-1.compute.internal 1/1 Running 0 10m kube-system kops-controller-9skdk 1/1 Running 3 6m51s kube-system kube-apiserver-ip-173-19-35-156.eu-central-1.compute.internal 2/2 Running 0 10m kube-system kube-controller-manager-ip-173-19-35-156.eu-central-1.compute.internal 1/1 Running 6 10m kube-system kube-dns-696cb84c7-g8nhb 3/3 Running 0 4m27s kube-system kube-dns-autoscaler-55f8f75459-zlxbr 1/1 Running 0 7m18s kube-system kube-proxy-ip-173-19-35-156.eu-central-1.compute.internal 1/1 Running 0 10m kube-system kube-proxy-ip-172-36-23-149.eu-central-1.compute.internal 1/1 Running 0 7m2s kube-system kube-scheduler-ip-173-19-35-156.eu-central-1.compute.internal 1/1 Running 5 10m
刪除集群
就像創建 Kubernetes 集群一樣,使用 Kops 刪除 Kubernetes 集群非常簡單。 此 kops delete 命令將刪除集群的所有云資源和集群註冊表本身。
geekflare@ip-170-11-26-119:~$ kops delete cluster --name geekdemo1.k8s.local --yes TYPE NAME ID autoscaling-config master-eu-central-1a.masters.geekdemo1.k8s.local lt-0cc11aec1943204e4 autoscaling-config nodes-eu-central-1a.geekdemo1.k8s.local lt-0da65d2eaf6de9f5c autoscaling-group master-eu-central-1a.masters.geekdemo1.k8s.local master-eu-central-1a.masters.geekdemo1.k8s.local autoscaling-group nodes-eu-central-1a.geekdemo1.k8s.local nodes-eu-central-1a.geekdemo1.k8s.local dhcp-options geekdemo1.k8s.local dopt-0403a0cbbfbc0c72b iam-instance-profile masters.geekdemo1.k8s.local masters.geekdemo1.k8s.local iam-instance-profile nodes.geekdemo1.k8s.local nodes.geekdemo1.k8s.local iam-role masters.geekdemo1.k8s.local masters.geekdemo1.k8s.local iam-role nodes.geekdemo1.k8s.local nodes.geekdemo1.k8s.local instance master-eu-central-1a.masters.geekdemo1.k8s.local i-069c73f2c23eb502a instance nodes-eu-central-1a.geekdemo1.k8s.local i-0401d6b0d4fc11e77 iam-instance-profile:nodes.geekdemo1.k8s.local ok load-balancer:api-geekdemo1-k8s-local-dason2 ok iam-instance-profile:masters.geekdemo1.k8s.local ok iam-role:masters.geekdemo1.k8s.local ok instance:i-069c73f2c23eb502a ok autoscaling-group:nodes-eu-central-1a.geekdemo1.k8s.local ok iam-role:nodes.geekdemo1.k8s.local ok instance:i-0401d6b0d4fc11e77 ok autoscaling-config:lt-0cc11aec1943204e4 ok autoscaling-config:lt-0da65d2eaf6de9f5c ok autoscaling-group:master-eu-central-1a.masters.geekdemo1.k8s.local ok keypair:key-0d82g920j421b89dn ok Deleted kubectl config for geekdemo1.k8s.local Deleted cluster: "geekdemo1.k8s.local"
結論
我希望這篇關於 Kops 的文章對您有所幫助,並且您今天必須學習一些新東西。 Kops 是在雲上與 Kubernetes 一起工作的絕佳工具。 因此,繼續嘗試本文中提到的步驟,並使用 Kops 在 AWS 上設置您的 Kubernetes 集群。