- 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.
- Clojure Data Analysis Cookbook
- Ansible Configuration Management
- 零起步輕松學(xué)單片機(jī)技術(shù)(第2版)
- Hands-On Intelligent Agents with OpenAI Gym
- 會(huì)聲會(huì)影X5視頻剪輯高手速成
- Learning Social Media Analytics with R
- WordPress Theme Development Beginner's Guide(Third Edition)
- Windows內(nèi)核原理與實(shí)現(xiàn)
- Cloudera Administration Handbook
- 從零開(kāi)始學(xué)C++
- 精通數(shù)據(jù)科學(xué):從線性回歸到深度學(xué)習(xí)
- 單片機(jī)原理實(shí)用教程
- 21天學(xué)通Linux嵌入式開(kāi)發(fā)
- Building Google Cloud Platform Solutions
- 30天學(xué)通Java Web項(xiàng)目案例開(kāi)發(fā)