官术网_书友最值得收藏!

  • 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.

This Kubernetes configuration is often referred to as a Manifest, and the YAML-or-JSON formatted files as Manifest files.

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.

主站蜘蛛池模板: 荆州市| 临泽县| 平顶山市| 平原县| 平利县| 阿拉尔市| 尚志市| 丹东市| 武平县| 团风县| 西平县| 平南县| 镇平县| 获嘉县| 新化县| 郑州市| 如东县| 长宁区| 武川县| 河北区| 虞城县| 乌拉特后旗| 松阳县| 汾阳市| 垫江县| 丹东市| 同仁县| 梧州市| 鄂尔多斯市| 两当县| 闽清县| 阿合奇县| 平昌县| 鄂托克前旗| 从化市| 城口县| 饶阳县| 新野县| 华坪县| 临城县| 莱阳市|