Deploy a Static Site on Linode Kubernetes Engine
Traducciones al EspañolEstamos traduciendo nuestros guías y tutoriales al Español. Es posible que usted esté viendo una traducción generada automáticamente. Estamos trabajando con traductores profesionales para verificar las traducciones de nuestro sitio web. Este proyecto es un trabajo en curso.
Linode Kubernetes Engine (LKE) allows you to easily create, scale, and manage Kubernetes clusters to meet your application’s demands, reducing the often complicated cluster set-up process to just a few clicks. Linode manages your Kubernetes master node, and you select how many Linodes you want to add as worker nodes to your cluster.
Deploying a static site using an LKE cluster is a great example to follow when learning Kubernetes. A container image for a static site can be written in less than ten lines, and only one container image is needed. Therefore, it’s often less complicated to deploy a static site on Kubernetes than some other applications that require multiple components.
In this Guide
This guide shows you how to:
- On your workstation, create a site with Hugo, a static site generator (SSG).
- Containerize the static site using Docker.
- Deploy the container to your LKE cluster.
Before You Begin
You should have a working knowledge of Kubernetes’ key concepts, including master and worker nodes, Pods, Deployments, and Services. For more information on Kubernetes, see our Beginner’s Guide to Kubernetes series.
You also need to prepare your workstation with some prerequisite software:
- Install kubectl (your client’s version should be at least 1.13)
- Install Git
- Install Docker
- Sign up for a Docker Hub Account
- Install Hugo
Finally, you need to create a cluster on LKE, if you do not already have one:
To create a cluster in the Linode Cloud Manager, review the Deploy a Cluster with Linode Kubernetes Engine guide.
Note Specifically, follow the Create an LKE Cluster and Connect to your LKE Cluster with kubectl sections.To create a cluster from the Linode API, review the Deploy and Manage a Cluster with Linode Kubernetes Engine and the Linode API tutorial.
Note Specifically, follow the Create an LKE Cluster section.
Install kubectl
You should have kubectl
installed on your local workstation. kubectl
is the command line interface for Kubernetes, and allows you to remotely connect to your Kubernetes cluster to perform tasks.
macOS:
Install via Homebrew:
brew install kubectl
If you don’t have Homebrew installed, visit the Homebrew home page for instructions. Alternatively, you can manually install the binary; visit the Kubernetes documentation for instructions.
Linux:
Download the latest kubectl release:
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
Make the downloaded file executable:
chmod +x ./kubectl
Move the command into your PATH:
sudo mv ./kubectl /usr/local/bin/kubectl
Windows:
Visit the Kubernetes documentation for a link to the most recent Windows release.
Install Git
To perform some of the commands in this guide you need to have Git installed on your workstation. Git is a version control system that allows you to save your codebase in various states to ease development and deployment. Follow our How to Install Git on Linux, Mac or Windows guide for instructions on how to install Git.
Install Docker
To install Docker CE (Community Edition), follow the instructions within one of the guides below:
For complete instructions on even more Linux distributions, reference the Install Docker Engine section of Docker’s official documentation.
Sign up for a Docker Hub Account
You use Docker Hub to store your Docker image. If you don’t already have a Docker Hub account, create one now.
Install Hugo
A static site generator (SSG) is usually a command line tool that takes text files written in a markup language like Markdown, applies a stylized template to the content, and produces valid HTML, CSS, and JavaScript files. Static sites are prized for their simplicity and speed, as they do not generally have to interact with a database.
The Linode documentation website, and this guide, employ Hugo. Hugo is a powerful and fast SSG written in the Go programming language, but you can choose one that best suits your needs by reading our How to Choose a Static Site Generator guide.
The steps in this guide are generally the same across SSGs: install a static site generator, create some content in a text file, and then generate your site’s HTML through a build process.
To download and install Hugo, you can use a package manager.
For Debian and Ubuntu:
sudo apt-get install hugo
For Red Hat, Fedora, and CentOS:
sudo dnf install hugo
For macOS, use Homebrew:
brew install hugo
For Windows, use Chocolatey:
choco install hugo
For more information on downloading Hugo, you can visit the official Hugo website.
Create a Static Site Using Hugo
In this section you creates a static site on your workstation using Hugo.
Use Hugo to scaffold a new site. This command creates a new directory with the name you provide, and inside that directory it creates the default Hugo directory structure and configuration files:
hugo new site lke-example
Move into the new directory:
cd lke-example
Initialize the directory as a Git repository. This allows you to track changes to your website and save it in version control.
git init
Hugo allows for custom themes. For the sake of this example, you install the Ananke theme as a Git submodule.
git submodule add https://github.com/budparr/gohugo-theme-ananke.git themes/ananke
Note Git submodules allow you to include one Git repository within another, each maintaining their own version history. To view a collection of Hugo themes, visit the Hugo theme collection.In the text editor of your choice, open the
config.toml
file and add the following line to the end:1
theme = "ananke"
This line instructs Hugo to search for a folder named
ananke
in thethemes
directory and applies the templating it finds to the static site.Add an example first post to your Hugo site:
hugo new posts/first_post.md
This creates a Markdown file in the
content/posts/
directory with the namefirst_post.md
. You see output like the following:/Users/linode/k8s/lke/lke-example/content/posts/first_post.md created
Open the
first_post.md
file in the text editor of your choosing. You see a few lines of front matter, a format Hugo uses for extensible metadata, at the top of the file:- File: lke-example/content/posts/first_post.md
1 2 3 4 5
--- title: "First_post" date: 2019-07-29T14:22:04-04:00 draft: false ---
Change the
title
to your desired value, and changedraft
tofalse
. Then, add some example Markdown text to the bottom of the file, like the example below:- File: lke-example/content/posts/first_post.md
1 2 3 4 5 6 7 8 9 10 11 12 13
--- title: "First Post About LKE Clusters" date: 2019-07-29T14:22:04-04:00 draft: false --- ## LKE Clusters Linode Kubernetes Engine (LKE) clusters are: - Fast - Affordable - Scalable
You can preview your changes by starting the local Hugo server:
hugo server
You should see output like the following:
. | EN +------------------+----+ Pages | 8 Paginator pages | 0 Non-page files | 0 Static files | 3 Processed images | 0 Aliases | 0 Sitemaps | 1 Cleaned | 0 Total in 6 ms Watching for changes in /Users/linode/k8s/lke/lke-example/{content,data,layouts,static,themes} Watching for config changes in /Users/linode/k8s/lke/lke-example/config.toml Serving pages from memory Running in Fast Render Mode. For full rebuilds on change: hugo server --disableFastRender Web Server is available at http://localhost:1313/ (bind address 127.0.0.1) Press Ctrl+C to stop
Visit the URL that Hugo is running on. In the above example, the URL is
http://localhost:1313
. This server automatically updates whenever you make a change to a file in the Hugo site directory. To stop this server, enter CTRL-C on your keyboard in your terminal.When you are satisfied with your static site, you can generate the HTML, CSS, and JavaScript for your site by building the site:
hugo -v
Hugo creates the site’s files in the
public/
directory. View the files by listing them:ls public
You can build the site at any time from your source Markdown content files, so it’s common practice to keep built files out of a Git repository. This practice keeps the size of the repository to a minimum.
You can instruct Git to ignore certain files within a repository by adding them to a
.gitignore
file. Add thepublic/
directory to your.gitignore
file to exclude these files from the repository:echo 'public/' >> .gitignore
Add and commit the source files to the Git repository:
git add . git commit -m "Initial commit. Includes all of the source files, configuration, and first post."
You are now ready to create a Docker image from the static site you’ve just created.
Create a Docker Image
In this section you create a Docker container for your static site, which you then run on your LKE cluster. Before deploying it on your cluster, you test its functionality on your workstation.
In your Hugo static site folder, create a new text file named
Dockerfile
and open it in the text editor of your choosing. A Dockerfile tells Docker how to create the container.Add the following contents to the
Dockerfile
. Each command has accompanying comments that describe their function:- File: lke-example/Dockerfile
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
# Install the latest Debian operating system. FROM alpine:3.12.0 as HUGO # Install Hugo. RUN apk update && apk add hugo # Copy the contents of the current working directory to the # static-site directory. COPY . /static-site # Command Hugo to build the static site from the source files, # setting the destination to the public directory. RUN hugo -v --source=/static-site --destination=/static-site/public # Install NGINX, remove the default NGINX index.html file, and # copy the built static site files to the NGINX html directory. FROM nginx:stable-alpine RUN mv /usr/share/nginx/html/index.html /usr/share/nginx/html/old-index.html COPY --from=HUGO /static-site/public/ /usr/share/nginx/html/ # Instruct the container to listen for requests on port 80 (HTTP). EXPOSE 80
Save the Dockerfile and return to the command prompt.
Create a new text file named
.dockerignore
in your Hugo static site folder and add the following lines:- File: lke-example/.dockerignore
1 2 3 4
public/ .git/ .gitmodules/ .gitignore
Note This file, similar to the.gitignore
file you created in the previous section, allows you to ignore certain files within the working directory that you want to leave out of the container. Because you want the container to be the smallest size possible, the.dockerignore
file includes thepublic/
folder and some hidden folders that Git creates.Run the Docker
build
command. Replacemydockerhubusername
with your Docker Hub username. The period at the end of the command tells Docker to use the current directory as its build context.docker build -t mydockerhubusername/lke-example:v1 .
Note In the example below, the container image is namedlke-example
and has been given a version tag ofv1
. Feel free to change these values.Docker downloads the required Debian and NGINX images, as well as install Hugo into the image. Once complete, you should see output similar to the following:
Successfully built 320ae416c940 Successfully tagged mydockerhubusername/lke-example:v1
You can view the image by listing all local images:
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE mydockerhubusername/lke-example v1 320ae416c940 About an hour ago 20.8MB
Test the Docker Image
You can test your new image by creating a container with it locally. To do so, enter the following
run
command:docker run -p 8080:80 -d mydockerhubusername/lke-example:v1
The
-p
flag instructs Docker to forward port8080
on localhost to port80
on the container. The-d
flag instructs Docker to run in detached mode so that you are returned to the command prompt once the container initializes.Once the container has started, open your browser and navigate to
localhost:8080
. You should see your static site.You can stop the running container by finding the ID of the container and issuing the
stop
command. To find the ID of the container, use theps
command:docker ps
You should see a list of actively running containers, similar to the following:
b4a7b959a6c7 mydockerhubusername/lke-example:v1 "nginx -g 'daemon of…" 5 hours ago Up 5 hours 0.0.0.0:8080->80/tcp romantic_mahavira
Note the random string of numbers and letters next to the image name. In the above example, the string is
b4a7b959a6c7
. Issue thestop
command, supplying the string of numbers and letters:docker stop b4a7b959a6c7
Upload the Image to Docker Hub
Now that you have a working container image, you can push that image to Docker Hub. First, log in to Docker Hub from your workstation’s terminal:
docker login
Next, push the image, with version tag, to Docker Hub, using the
push
command:docker push mydockerhubusername/lke-example:v1
You can now view your image on Docker Hub as a repository. To view all of your repositories, navigate to the Docker Hub repository listing page.
Lastly, add the
Dockerfile
and.dockerignore
file to your Git repository:git add . git commit -m "Add Dockerfile and .dockerignore."
You are now ready to deploy the container to your LKE cluster.
Deploying the Container to LKE
In this section, you create a Deployment from the container you created in the previous section, and a Service to load balance the deployment.
Begin by navigating to a location outside of your static site directory. You do not need your static site directory for the remainder of this guide.
cd ..
Create a new directory to house your Kubernetes manifests, and move into that directory:
mkdir manifests && cd manifests
Create a Deployment
In the text editor of your choice, create a new YAML manifest file for your Deployment. Name the file
static-site-deployment.yaml
, save it to yourmanifests
directory, and enter the contents of this snippet:- File: manifests/static-site-deployment.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
apiVersion: apps/v1 kind: Deployment metadata: name: static-site-deployment labels: app: static-site spec: replicas: 3 selector: matchLabels: app: static-site template: metadata: labels: app: static-site spec: containers: - name: static-site image: mydockerhubusername/lke-example:v1 imagePullPolicy: Always ports: - containerPort: 80
- In this example the number of replica Pods is set to
3
on line 8. This value can be changed to meet the needs of your website. - The
spec.containers.image
field on line 19 should be changed to match the name of the container image you pushed to Docker Hub. Be sure to include the proper version tag at the end of the container name. imagePullPolicy: Always
on line 20 ensures that each time a Pod is created, the most recent version of the container image will be pulled from Docker Hub.
Once you have a Deployment manifest, you can apply the deployment to the LKE cluster with
kubectl
:kubectl apply -f static-site-deployment.yaml
You can check on the progress of your Deployment by listing the available pods:
kubectl get pods
If your Deployment was successful, you should see output like the following:
NAME READY STATUS RESTARTS AGE static-site-deployment-cdb88b5bb-7pbjc 1/1 Running 0 1h static-site-deployment-cdb88b5bb-gx9h5 1/1 Running 0 1h static-site-deployment-cdb88b5bb-lzdvh 1/1 Running 0 1h
Create a Service
Create a Service manifest file to provide load balancing for the deployment. Load balancing ensures that traffic is balanced efficiently across multiple backend nodes, improving site performance and ensuring that your static site is accessible should a node go down.
Specifically, the Service manifest that is used in this guide triggers the creation of a Linode NodeBalancer.
Note The NodeBalancer’s creation is controlled through the Linode Cloud Controller Manager (CCM). The CCM provides a number of settings, calledannotations
, that allow you to control the functionality of the NodeBalancer. To learn more about the CCM, read our Installing the Linode CCM on an Unmanaged Kubernetes Cluster guide.Name the file
static-site-service.yaml
, save it to yourmanifests
directory, and enter the contents of this snippet:- File: manifests/static-site-service.yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
apiVersion: v1 kind: Service metadata: name: static-site-service annotations: service.beta.kubernetes.io/linode-loadbalancer-throttle: "4" labels: app: static-site spec: type: LoadBalancer ports: - name: http port: 80 protocol: TCP targetPort: 80 selector: app: static-site sessionAffinity: None
Once you’ve created your Service manifest file, you can apply it to the LKE cluster:
kubectl apply -f static-site-service.yaml
You can check on the status of your Service by listing the Services currently running on your server:
kubectl get services
You should see output similar to the following:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE kubernetes ClusterIP 10.128.0.1 <none> 443/TCP 20h static-site-service LoadBalancer 10.128.99.240 192.0.2.1 80:32648/TCP 100m
Note the external IP address of the Service you created. This is the IP address of the NodeBalancer, and you can use it to view your static site.
In the above example, the IP address is
192.0.2.1
. Navigate to the external IP address in the browser of your choice to view your static site. You should see the same content as when you tested your Docker image on your workstation.
General Network and Firewall Information
In an LKE cluster, some entities and services are only accessible from within that cluster while others are publicly accessible (reachable from the internet).
Private (accessible only within the cluster)
- Pod IPs, which use a per-cluster virtual network in the range 10.2.0.0/16
- ClusterIP Services, which use a per-cluster virtual network in the range 10.128.0.0/16
Public (accessible over the internet)
- NodePort Services, which listen on all Nodes with ports in the range 30000-32768.
- LoadBalancer Services, which automatically deploy and configure a NodeBalancer.
- Any manifest which uses
hostNetwork
: true and specifies a port. - Most manifests which use
hostPort
and specify a port.
Exposing workloads to the public internet through the above methods can be convenient, but this can also carry a security risk. You may wish to manually install firewall rules on your cluster nodes. The following policies are needed to allow communication between the node pools and the control plane and block unwanted traffic:
- Allow kubelet health checks: TCP port 10250 from 192.168.128.0/17 Accept
- Allow Wireguard tunneling for kubectl proxy: UDP port 51820 from 192.168.128.0/17 Accept
- Allow Calico BGP traffic: TCP port 179 from 192.168.128.0/17 Accept
- Allow NodePorts for workload services: TCP/UDP port 30000 - 32767 192.168.128.0/17 Accept
- Block all other TCP traffic: TCP All Ports All IPv4/All IPv6 Drop
- Block all other UDP traffic: UDP All Ports All IPv4/All IPv6 Drop
- Block all ICMP traffic: ICMP All Ports All IPv4/All IPv6 Drop
- IPENCAP for IP ranges 192.168.128.0/17 for internal communication between node pools and control plane.
For additional information, please see this community post. Future LKE release may allow greater flexibility for the network endpoints of these types of workloads.
Please note, at this time, nodes should be removed from the Cloud Firewall configuration before removing/recycling of node pools within the Kubernetes configuration. Also, when adding node pools to the Kubernetes cluster, Cloud Firewall must be updated with the new node pool(s). Failure to add the new nodes creates a security risk.
Kubernetes
in the default
namespace designed to ease interactions with the control plane. This is a standard service for LKE clusters.Next Steps
If you’d like to continue using the static site that you created in this guide, you may want to assign a domain to it. Review the DNS Records: An Introduction and DNS Manager guides for help with setting up DNS. When setting up your DNS record, use the external IP address that you noted at the end of the previous section.
If you would rather not continue using the cluster you just created, review the tear-down section to remove the billable Linode resources that were generated.
Tear Down your LKE Cluster and NodeBalancer
To remove the NodeBalancer you created, all you need to do is delete the underlying Service. From your workstation:
kubectl delete service static-site-service
Alternatively, you can use the manifest file you created to delete the Service. From your workstation:
kubectl delete -f static-site-service.yaml
To remove the LKE Cluster and the associated nodes from your account, navigate to the Linode Cloud Manager:
Click on the Kubernetes link in the sidebar. A new page with a table which lists your clusters appears.
Click on the more options elipsis next to the cluster you would like to delete, and select Delete.
You are prompted to enter the name of the cluster to confirm the action. Enter the cluster name and click Delete.
Lastly, remove the
KUBECONFIG
line you added to your Bash profile to remove the LKE cluster from your available contexts.
More Information
You may wish to consult the following resources for additional information on this topic. While these are provided in the hope that they will be useful, please note that we cannot vouch for the accuracy or timeliness of externally hosted materials.
This page was originally published on