[
199
]
The
swap
file for your server is declared in the
/etc/fstab
file (we'll discuss the
/etc/
fstab
file in more detail in
Chapter 9
,
Managing Storage Volumes
). In most cases, you
would've had a
swap
file created for you during installation. You could, of course,
add a swap partition later if for some reason you don't have one. In the case of some
cloud instance providers, you may not get a
swap
file by default. In that situation,
you would create a
swap
file yourself (we'll discuss the process later in this section)
and then use the
swapon
command to activate it:
sudo swapon -a
When run, the
swapon -a
command will find your swap partition in
/etc/fstab
(if
it's mentioned there), mount it, and activate it for use. The inverse of this command
is
swapoff -a
, which deactivates your
swap
file. It's rare that you'd need to disable
swap, unless, of course, you were planning on deleting your
swap
file in order to
create a larger one. If you find out that your server has an inadequate swap partition
size, that may be a course of action you would take.
While having swap is generally a good idea, there are actually some applications
that prefer that the server doesn't have it at all. This is rare, but Kubernetes is a good
example of this. If you're running Kubernetes, the installation of it will complain if
you do have a swap partition. This is somewhat of a rare occurrence; not very many
applications run better if the server has no
swap
file. In the case of a Kubernetes
cluster, the individual servers within such a cluster would be a special case anyway,
each dedicated to the task of running containers (which is what Kubernetes does;
more on that in
Chapter 18
,
Container Orchestration
).
When you check your free memory (hint: execute
free -m
), you'll see
swap
listed
whether you have it or not, but when swap is deactivated, you will see all zeros for
the size totals.
So, how do you actually create a
swap
file? To do so, you'll first create the actual
file to be used as swap. This can be stored anywhere, but
/swapfile
is typically
ideal. You can use the
fallocate
command to create the actual file. The
fallocate
command will force a file to be a particular size:
sudo fallocate -l 4G /swapfile
Here, I'm creating a 4 GB
swap
file, but feel free to make yours whatever size you
want in order to fit your needs. Next, we need to prepare this file to be used as swap.
First, we'll need to fix the permissions as we need this file to be a bit more restrictive
than most:
sudo chmod 0600 /swapfile
Monitoring System Resources
Do'stlaringiz bilan baham: |