Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 29, 2022 10:57 pm GMT

Getting Started With Skaffold

Whether you're testing local deployments to ensure that a containerized application is working as expected or you're looking to deploy a containerized workflow to production, you need an automatic way to complete these tasks.

Not to mention, you probably need a way to automatically build container images.

That's where Skaffold can come into play.

In this blog post, you'll learn about what Skaffold is and a few different ways that you can utilize it to make deployments easier.

Prerequisites

To follow along from a hands-on perspective, you should have:

  • A Kubernetes cluster running. This can be Minikube or a production-level cluster.

What Is Skaffold?

Scaffold was originally created to make local Kubernetes development easier.

The idea for this project was for the continuous change in container images. Once code is updated, there needs to be a way to automatically update the container image. For example, if you're going from v1.1 to v1.2, you don't want to have to run docker build . on a Dockerfile manually. Instead, you want an automated way to build the new container image and have it deployed.

That's exactly what Skaffold does.

It looks at local or remote directories where code exists and if the code changes, Skaffold can automatically create a new containerized image and then deploy it to your Kubernetes cluster.

Essentially, it's like GitOps for your container images. Whereas standard GitOps will look at a Kubernetes Manifest or a Kubernetes resource being created with a client and deploy any changes made, Skaffold will look at any application changes, push those changes to the container image, and then the container image will be updated and deployed in the Kubernetes Manifest.

Docker Setup

To push a container image to a registry, like Dockerhub, youll need to log into the registry and specify your registry in your Kubernetes Manifest, which youll see in the upcoming section.

Ensure that you log into your container registry on your terminal.

For example, if youre using Dockerhub, run the following command:

docker login --username=your_dockerhub_username

The App

Because Skaffold is made for Kubernetes and containers, you will need:

  • Application code thats ready to containerize.
  • A Kubernetes Manifest to utilize the containerized app.

To keep the app simple (although this process will work with other apps as well), you can take the Go code below and store it in a directory.

package mainimport (    "fmt"    "log"    "net/http")func main() {    appRequest()}func homePage(response http.ResponseWriter, r *http.Request) {    fmt.Fprintf(response, "Welcome to the Go Web API!")    fmt.Println("Endpoint Hit: homePage")}func appRequest() {    http.HandleFunc("/", homePage)    log.Fatal(http.ListenAndServe(":8080", nil))}

In the same directory, create a new Kubernetes Manifest called goapp.yaml and store the following Manifest.

In the Manifest below, for the value of image:, youll see a container registry and a container image name. You should use your own as in the previous section you logged into your container registry, but theres an example in the Manifest below that shows my Dockerhub username (thenjdevopsguy) along with the container image name that I want to push my container image to after I build it with skaffold, which is coming up in a future section of this blog post.

apiVersion: apps/v1kind: Deploymentmetadata:  name: go-webappspec:  selector:    matchLabels:      app: gowebapp  replicas: 2  template:    metadata:      labels:        app: gowebapp    spec:      containers:      - name: gowebapp        image: thenjdevopsguy/gowebappskaffold        ports:        - containerPort: 8080

Notice how the app name is gowebapp. That piece of information will come up in the next section.

Installing Skaffold

The installation will be different for each operating system. Below is an example of installing on MacOS, but if you're on a different operating system, check out the installation methods here: https://skaffold.dev/docs/install/

The code below installs Skaffold on a MacOS machine with an M1 chip.

curl -Lo skaffold https://storage.googleapis.com/skaffold/builds/latest/skaffold-darwin-arm64 && \
sudo install skaffold /usr/local/bin/

Using Skaffold

Once Skaffold is installed, the app code is prepared, and the Kubernetes Manifest is written, you can start the process of using Skaffold.

First, ensure that youre cd into the same directory as the app code and k8s manifest.

Image description

Next, run the initialization command.

skaffold init

You'll see an output similar to the screenshot below. Notice how its using the image name that you specified in the Kubernetes Manifest.

Skaffold is building the container image via a Cloud Native Buildpack, which is a way to build container images directly from code without needing a Dockerfile.

Click y like in the screenshot below.

Image description

Youll now see a configuration similar to the screenshot below in the same directory as your app and k8s manifest. It contains the container image name, the cloud native buildpack builder, and the k8s manifest.

Image description

Next, run the following command to start building the container image which will also push the container image to your container registry and deploy it to the Kubernetes cluster thats on your current context via your Kubeconfig.

skaffold dev --default-repo=your_container_registry_username

If you come across the skaffold run command and are wondering what the difference is, here it is: skaffold run builds and deploys once whereas skaffold dev triggers the watch loop build and deploy workflows.

Youll see an output similar to the screenshots below which shows that the container image is being built for the Go app.

Image description
Image description

The whole build process should take a few minutes.

Once complete, you should see that the app deployed to Kubernetes and you should also see that the container image now exists in your container registry.


Original Link: https://dev.to/thenjdevopsguy/getting-started-with-skaffold-5120

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To