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.
To work through this recipe, you will need the following:
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
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.
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
$ 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.
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.
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.
service
resource documentation: https://docs.chef.io/resource_service.htmlmetadata.rb
resource documentation: https://docs.chef.io/config_rb_metadata.htmlservice
resource documentation: https://docs.puppet.com/puppet/4.8/types/service.html3.12.166.131