- Kubernetes on AWS
- Ed Robinson
- 519字
- 2021-06-10 18:41:28
Configuration as code
Throughout this chapter, we have interacted with Kubernetes by using commands provided by kubectl or the Kubernetes dashboard. In practice, I find that these tools are useful for quickly getting a container running in a cluster. When the configuration becomes more complex or I want to be able to deploy the same application to multiple environments, having a configuration file that I can submit to the cluster, and store in a version control system, is very useful.
kubectl and indeed the Kubernetes dashboard, will allow us to submit YAML or JSON formatted configurations for the resources we want to create on the cluster. We are going to take another look at how we would deploy the same Hello World application using YAML-formatted files rather than commands such as kubectl run.
Let's start by removing the configuration we created with kubectl so we have a clean state to reproduce the same configuration:
$ kubectl delete deployment/hello svc/hello deployment "hello" deleted service "hello" deleted
Let's define a deployment for version 1 of the hello service:
deployment.yaml apiVersion: apps/v1 kind: Deployment metadata: name: hello spec: replicas: 2 template: metadata: labels: app: hello spec: containers: - name: hello image: hello:v1 ports: - containerPort: 80
Now we can use kubectl to submit the deployment to Kubernetes:
$kubectl apply -f deployment.yaml deployment "hello" created
Next, let's do the same for a service:
service.yaml kind: Service apiVersion: v1 metadata: name: hello spec: selector: app: hello type: NodePort ports: - protocol: TCP port: 80 targetPort: 80
Submit the definition to Kubernetes with kubectl:
$ kubectl apply -f service.yaml service "hello" created
You can see that while we have sacrificed the speed and simplicity of just running a command to create a deployment, by explicitly specifying the resources we want to create, we gain greater control over exactly how our pods are configured, and we now have this definition in a form that we can check into version control and reliably update.
When it comes to updating a resource, we can make an edit to the file and then use the kubectl apply command to update the resource. kubectl detects that we are updating an existing resource and updates it to match our configuration. Try editing the image tag in deployment.yaml and then re submitting it to the cluster:
$ kubectl apply -f deployment.yaml deployment "hello" configured
If we are just making changes to the resource on our local cluster, we might just want to quickly change something without having to edit the file at all. Firstly, as in our previous example, you can use kubectl set to update a property. Kubernetes doesn't really care how we created the resource, so everything we did previously is still valid. The other method of making a quick change is with the kubectl edit command. Assuming you have the $EDITOR environment variable set up correctly with your favorite text editor, you should be able to open YAML for a resource, edit it, and then save while kubectl seamlessly updates the resource for you.
- 工業機器人虛擬仿真實例教程:KUKA.Sim Pro(全彩版)
- PostgreSQL 11 Server Side Programming Quick Start Guide
- Hands-On Machine Learning on Google Cloud Platform
- 計算機控制技術
- Zabbix Network Monitoring(Second Edition)
- STM32G4入門與電機控制實戰:基于X-CUBE-MCSDK的無刷直流電機與永磁同步電機控制實現
- 大數據安全與隱私保護
- 網絡綜合布線設計與施工技術
- 人工智能實踐錄
- 我也能做CTO之程序員職業規劃
- ASP.NET 2.0 Web開發入門指南
- 網絡脆弱性掃描產品原理及應用
- 人工智能:智能人機交互
- Win 7二十一
- Intel Edison Projects