- 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.
- Linux Mint System Administrator’s Beginner's Guide
- 傳感器技術實驗教程
- 影視后期制作(Avid Media Composer 5.0)
- Expert AWS Development
- 最簡數據挖掘
- 水晶石精粹:3ds max & ZBrush三維數字靜幀藝術
- 精通數據科學算法
- Visual Basic項目開發案例精粹
- Hands-On DevOps
- Practical Network Automation
- 大數據:從基礎理論到最佳實踐
- Photoshop CS6白金手冊
- 數據庫技術:Access 2003計算機網絡技術
- GAN實戰
- Hands-On Automated Machine Learning