Managing services

We've seen how to install system packages using the package resource. In this section, you'll discover how to manage system services, using a resource named service. We'll continue to build the LAMP server we started in the previous section by managing the Apache HTTP and MariaDB services right from Chef. This way we'll be able to manage any available service.

Getting ready

To work through this recipe, you will need the following:

  • A working Chef DK installation on the workstation
  • A working Chef client configuration on the remote host
  • The Chef code from the previous recipe

How to do it…

The structure of the service resource is very similar to the package resource. We want to do two actions with our services: enable them at boot and start them right away. This translates into a simple Chef resource with an array of actions:

service "service_name" do
  action [:enable, :start]
end

Enabling and starting Apache service

Add this service resource to the apache/recipes/default.rb file, just after the package resource:

service "httpd" do
  action [:enable, :start]
end

Bump the cookbook version number on the apache/metatada.rb file:

version '0.2.0'

This way, the Chef server will always keep version 0.1.0 with only the package installation, and a new version 0.2.0, with the service support. We'll also be able to easily rollback to a previously running version.

Note

When the Chef client runs, it always downloads and applies the latest version by default. It's advised to fix (or pin) versions where appropriate—especially in production.

Upload the new cookbook version:

$ knife cookbook upload apache

Now we have both versions of the cookbook available on the Chef server:

$ knife cookbook show apache
apache   0.2.0  0.1.0

Apply on the remote host:

$ sudo chef-client

Verify the Apache service is indeed running:

$ systemctl status httpd

You can also navigate to the site's IP address in HTTP to see the default page displayed.

Enabling and starting the MariaDB service

Do exactly the same for MariaDB's mariadb service in mariadb/recipes/default.rb:

service "mariadb" do
  action [:enable, :start]
end

Don't forget to also bump the cookbook version in mariadb/metadata.rb:

version '0.2.0'

Send the updated cookbook to the Chef server:

$ knife cookbook upload mariadb

Apply the new cookbook:

$ sudo chef-client

Confirm the MariaDB service is now running:

$ systemctl status mariadb

Confirm we can access the MariaDB server from the node:

$ mysql -e "show databases;"
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
+--------------------+

We've just covered how to handle a system service using Chef, so you now know how to easily and repeatedly deploy packages and manage the corresponding service.

There's more…

Using Puppet, services are also managed with a dedicated resource directive. Using the previous example, we now need to ensure that the corresponding services are running.

This resource needs to be added on both Apache and MariaDB modules. The new manifest for the Apache module is:

class apache {
        package {'apache2':
           ensure => present,
        }

        service {'apache2':
           ensure => running,
           enable => true
        }
}

The new manifest for the MariaDB module is:

class mariadb {
        package {'mariadb-server':
           ensure => present,
        }

        service {'mysql':
           ensure => running,
           enable => true
        }
}

The ensure=>running property is used to check the service is running (and will start it if needed), and enable=>true is used to start the service at boot.

Note

The name of the service used in the service resource is the same as used in a root shell to stop/start/reload the service.

See also

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.14.144.229