Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update rust language-specific guide #18521

Merged
merged 3 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions content/language/rust/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ toc_max: 2

The Rust language-specific guide teaches you how to create a containerized Rust application using Docker. In this guide, you'll learn how to:

* Create a sample Rust application
* Create a new Dockerfile which contains instructions required to build a Rust image
* Containerize a Rust application
* Build an image and run the newly built image as a container
* Set up volumes and networking
* Orchestrate containers using Compose
* Use containers for development
* Configure a CI/CD pipeline for your application using GitHub Actions
* Deploy your application to the cloud
* Deploy your containerized Rust application locally to Kubernetes to test and debug your deployment

After completing the Rust modules, you should be able to containerize your own Rust application based on the examples and instructions provided in this guide.

Expand Down
122 changes: 117 additions & 5 deletions content/language/rust/configure-ci-cd.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,128 @@ keywords: rust, CI/CD, local, development
description: Learn how to Configure CI/CD for your application
---

## Get started with GitHub Actions
## Prerequisites

{{< include "gha-tutorial.md" >}}
Complete the previous sections of this guide, starting with [Develop your Rust application](develop.md). You must have a [GitHub](https://github.com/signup) account and a [Docker](https://hub.docker.com/signup) account to complete this section.

## Overview

In this section, you'll learn how to set up and use GitHub Actions to build and push your Docker image to Docker Hub. You will complete the following steps:

1. Create a new repository on GitHub.
2. Define the GitHub Actions workflow.
3. Run the workflow.

## Step one: Create the repository

Create a GitHub repository, configure the Docker Hub secrets, and push your source code.

1. [Create a new repository](https://github.com/new) on GitHub.

2. Open the repository **Settings**, and go to **Secrets and variables** >
**Actions**.

3. Create a new secret named `DOCKER_USERNAME` and your Docker ID as value.

4. Create a new [Personal Access Token (PAT)](../../security/for-developers/access-tokens.md/#create-an-access-token) for Docker Hub. You can name this token `rust-docker`.

5. Add the PAT as a second secret in your GitHub repository, with the name
`DOCKERHUB_TOKEN`.

6. In your local repository on your machine, run the following command to change
the origin to the repository you just created. Make sure you change
`your-username` to your GitHub username and `your-repository` to the name of
the repository you created.

```console
$ git remote set-url origin https://github.com/your-username/your-repository.git
```

7. Run the following commands to stage, commit, and push your local repository to GitHub.

```console
$ git add -A
$ git commit -m "my commit"
$ git push -u origin main
```

## Step two: Set up the workflow

Set up your GitHub Actions workflow for building, testing, and pushing the image
to Docker Hub.

1. Go to your repository on GitHub and then select the **Actions** tab.

2. Select **set up a workflow yourself**.

This takes you to a page for creating a new GitHub actions workflow file in
your repository, under `.github/workflows/main.yml` by default.

3. In the editor window, copy and paste the following YAML configuration.

```yaml
name: ci

on:
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
steps:
-
name: Checkout
uses: actions/checkout@v4
-
name: Login to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
-
name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ secrets.DOCKER_USERNAME }}/${{ github.event.repository.name }}:latest
```

For more information about the YAML syntax used here, see [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions).

## Step three: Run the workflow

Save the workflow file and run the job.

1. Select **Commit changes...** and push the changes to the `main` branch.

After pushing the commit, the workflow starts automatically.

2. Go to the **Actions** tab. It displays the workflow.

Selecting the workflow shows you the breakdown of all the steps.

3. When the workflow is complete, go to your
[repositories on Docker Hub](https://hub.docker.com/repositories).

If you see the new repository in that list, it means the GitHub Actions
successfully pushed the image to Docker Hub.

## Summary

In this section, you have learned how to set up GitHub Actions workflow to an existing Docker project, optimize your workflow to improve build times and reduce the number of pull requests, and finally, you learned how to push only specific versions to Docker Hub. You can also set up nightly tests against the latest tag, test each PR, or do something more elegant with the tags you are using and make use of the Git tag for the same tag in your image.
In this section, you learned how to set up a GitHub Actions workflow for your Rust application.

Related information:
- [Introduction to GitHub Actions](../../build/ci/github-actions/index.md)
- [Workflow syntax for GitHub Actions](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions)

## Next steps

Next, learn how you can deploy your application.
Next, learn how you can locally test and debug your workloads on Kubernetes before deploying.

{{< button text="Deploy your app" url="deploy.md" >}}
{{< button text="Test your deployment" url="./deploy.md" >}}
228 changes: 224 additions & 4 deletions content/language/rust/deploy.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,227 @@
---
title: Deploy your Rust app
keywords: deploy, ACI, ECS, rust, local, development
description: Learn how to deploy your application
title: Test your Rust deployment
keywords: deploy, kubernetes, rust
description: Learn how to test your Rust deployment locally using Kubernetes
---

{{< include "deploy.md" >}}
## Prerequisites

- Complete the previous sections of this guide, starting with [Develop your Rust application](develop.md).
- [Turn on Kubernetes](/desktop/kubernetes/#turn-on-kubernetes) in Docker Desktop.

## Overview

In this section, you'll learn how to use Docker Desktop to deploy your application to a fully-featured Kubernetes environment on your development machine. This lets you to test and debug your workloads on Kubernetes locally before deploying.

## Create a Kubernetes YAML file

In your `docker-rust-postgres` directory, create a file named
`docker-rust-kubernetes.yaml`. Open the file in an IDE or text editor and add
the following contents. Replace `DOCKER_USERNAME/REPO_NAME` with your Docker
username and the name of the repository that you created in [Configure CI/CD for
your Rust application](configure-ci-cd.md).

```yaml
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
service: server
name: server
namespace: default
spec:
replicas: 1
selector:
matchLabels:
service: server
strategy: {}
template:
metadata:
labels:
service: server
spec:
initContainers:
- name: wait-for-db
image: busybox:1.28
command: ['sh', '-c', 'until nc -zv db 5432; do echo "waiting for db"; sleep 2; done;']
containers:
- image: DOCKER_USERNAME/REPO_NAME
name: server
imagePullPolicy: Always
ports:
- containerPort: 8000
hostPort: 5000
protocol: TCP
env:
- name: ADDRESS
value: 0.0.0.0:8000
- name: PG_DBNAME
value: example
- name: PG_HOST
value: db
- name: PG_PASSWORD
value: mysecretpassword
- name: PG_USER
value: postgres
- name: RUST_LOG
value: debug
resources: {}
restartPolicy: Always
status: {}
---
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
service: db
name: db
namespace: default
spec:
replicas: 1
selector:
matchLabels:
service: db
strategy:
type: Recreate
template:
metadata:
labels:
service: db
spec:
containers:
- env:
- name: POSTGRES_DB
value: example
- name: POSTGRES_PASSWORD
value: mysecretpassword
- name: POSTGRES_USER
value: postgres
image: postgres
name: db
ports:
- containerPort: 5432
protocol: TCP
resources: {}
restartPolicy: Always
status: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
service: server
name: server
namespace: default
spec:
type: NodePort
ports:
- name: "5000"
port: 5000
targetPort: 8000
nodePort: 30001
selector:
service: server
status:
loadBalancer: {}
---
apiVersion: v1
kind: Service
metadata:
labels:
service: db
name: db
namespace: default
spec:
ports:
- name: "5432"
port: 5432
targetPort: 5432
selector:
service: db
status:
loadBalancer: {}
```

In this Kubernetes YAML file, there are four objects, separated by the `---`. In addition to a Service and Deployment for the database, the other two objects are:

- A Deployment, describing a scalable group of identical pods. In this case,
you'll get just one replica, or copy of your pod. That pod, which is
described under `template`, has just one container in it. The container is
created from the image built by GitHub Actions in [Configure CI/CD for your
Rust application](configure-ci-cd.md).
- A NodePort service, which will route traffic from port 30001 on your host to
port 5000 inside the pods it routes to, allowing you to reach your app
from the network.

To learn more about Kubernetes objects, see the [Kubernetes documentation](https://kubernetes.io/docs/home/).

## Deploy and check your application

1. In a terminal, navigate to `docker-rust-postgres` and deploy your application
to Kubernetes.

```console
$ kubectl apply -f docker-rust-kubernetes.yaml
```

You should see output that looks like the following, indicating your Kubernetes objects were created successfully.

```shell
deployment.apps/server created
deployment.apps/db created
service/server created
service/db created
```

2. Make sure everything worked by listing your deployments.

```console
$ kubectl get deployments
```

Your deployment should be listed as follows:

```shell
NAME READY UP-TO-DATE AVAILABLE AGE
db 1/1 1 1 2m21s
server 1/1 1 1 2m21s
```

This indicates all of the pods you asked for in your YAML are up and running. Do the same check for your services.

```console
$ kubectl get services
```

You should get output like the following.

```shell
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
db ClusterIP 10.105.167.81 <none> 5432/TCP 109s
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 9d
server NodePort 10.101.235.213 <none> 5000:30001/TCP 109s
```

In addition to the default `kubernetes` service, you can see your `service-entrypoint` service, accepting traffic on port 30001/TCP.

3. In a terminal, curl the service.

```console
$ curl http://localhost:30001/users
[{"id":1,"login":"root"}]
```

4. Run the following command to tear down your application.

```console
$ kubectl delete -f docker-rust-kubernetes.yaml
```

## Summary

In this section, you learned how to use Docker Desktop to deploy your application to a fully-featured Kubernetes environment on your development machine.

Related information:
- [Kubernetes documentation](https://kubernetes.io/docs/home/)
- [Deploy on Kubernetes with Docker Desktop](../../desktop/kubernetes.md)
- [Swarm mode overview](../../engine/swarm/_index.md)
2 changes: 1 addition & 1 deletion data/toc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ Guides:
path: /language/rust/develop/
- title: "Configure CI/CD"
path: /language/rust/configure-ci-cd/
- title: "Deploy your app"
- title: "Test your deployment"
path: /language/rust/deploy/
- sectiontitle: Develop with Docker
section:
Expand Down