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

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

主站蜘蛛池模板: 汽车| 仪陇县| 灯塔市| 兰州市| 岢岚县| 忻城县| 家居| 望江县| 贵溪市| 伊金霍洛旗| 西充县| 洪江市| 布拖县| 万源市| 乐至县| 波密县| 广水市| 柘荣县| 桦南县| 凌海市| 博乐市| 西乌珠穆沁旗| 兰州市| 新晃| 龙江县| 鹤山市| 马边| 霍州市| 方山县| 松原市| 融水| 大姚县| 互助| 交口县| 洞头县| 宁波市| 广南县| 柳州市| 莆田市| 玛纳斯县| 罗源县|