-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from google/add_minikube
Add minikube examples
- Loading branch information
Showing
9 changed files
with
282 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
version: 2 | ||
updates: | ||
- package-ecosystem: "github-actions" | ||
directory: "/" | ||
schedule: | ||
interval: "monthly" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
name: build | ||
on: | ||
workflow_dispatch: | ||
push: | ||
pull_request: | ||
schedule: | ||
# every day at 6am & 6pm pacific | ||
- cron: "0 1,13 * * *" | ||
jobs: | ||
build: | ||
strategy: | ||
matrix: | ||
os: [ubuntu-latest] | ||
runs-on: ${{ matrix.os }} | ||
steps: | ||
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab | ||
- uses: azure/setup-kubectl@901a10e89ea615cf61f57ac05cecdf23e7de06d8 | ||
- uses: medyagh/setup-minikube@latest | ||
- name: install tools for mac os | ||
if: matrix.os == 'macos-12' | ||
shell: bash | ||
run: | | ||
brew install docker-machine docker | ||
sudo docker --version | ||
sysctl hw.physicalcpu hw.logicalcpu | ||
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate off | ||
sudo /usr/libexec/ApplicationFirewall/socketfilterfw -k | ||
- name: Build Docker images inside minikube | ||
run: | | ||
eval $(minikube docker-env) | ||
cd minikube | ||
docker build -t local/devex:v1 . | ||
- name: Deploy to to Kubernetes | ||
run: | | ||
kubectl apply -f minikube/deploy/k8s.yaml | ||
- name: Verify Deployment | ||
run: | | ||
minikube service list | ||
minikube service local-devex-svc --url --wait=10 | ||
kubectl get pods -A | ||
kubectl wait --for=condition=ready pod -l app=local-devex | ||
curl -vvv $(minikube service local-devex-svc --url) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
local-dev-example-with-minikube | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
FROM golang:alpine | ||
|
||
# Set the Current Working Directory inside the container | ||
WORKDIR $GOPATH/src/github.com/medyagh/local-dev-example-with-minikube/ | ||
|
||
# Copy everything from the current directory to the PWD (Present Working Directory) inside the container | ||
COPY . . | ||
|
||
# Install the package | ||
RUN go install -v ./... | ||
|
||
# This container exposes port 8080 to the outside world | ||
EXPOSE 8080 | ||
|
||
# Run the executable | ||
CMD ["local-dev-example-with-minikube"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
# local-dev-example-with-minikube | ||
|
||
|
||
This repo demos a simple go app being deployed to a Kubernetes cluster using minikube, and demonstrates changing the code and re-deploying a new version. | ||
|
||
|
||
## Requirements | ||
- Clone this repo! | ||
- Install [minikube](https://minikube.sigs.k8s.io/docs/start/) | ||
- Install [Docker CLI](https://minikube.sigs.k8s.io/docs/tutorials/docker_desktop_replacement/) (Docker Desktop is not required, Docker CLI is sufficent) | ||
|
||
|
||
## Build and deploy the app to minikube for the first time! | ||
|
||
1. Start minikube | ||
```console | ||
minikube start | ||
``` | ||
|
||
2. Point your terminal to minikube's Docker using `minikube docker-env` | ||
```console | ||
# on Mac or Linux | ||
eval $(minikube docker-env) | ||
``` | ||
|
||
```console | ||
# on Windows PowerShell | ||
& minikube docker-env | Invoke-Expression | ||
``` | ||
|
||
Tip 1: if you close your terminal you will have to re-point your docker-env. | ||
|
||
Tip 2: to verify that your terminal is pointing to minikube's Docker you can run `minikube status` and it will show "docker-env: in-use" | ||
|
||
4. Build Docker image inside minikube | ||
|
||
```console | ||
docker build -t local/devex:v1 . | ||
``` | ||
4. Deploy to Kubernetes | ||
```console | ||
kubectl apply -f deploy/k8s.yaml | ||
``` | ||
5. Accees the deployed service in your browser | ||
```console | ||
minikube service local-devex-svc | ||
``` | ||
Tip: you can try `$ minikube service list` to see all exposed serivces. | ||
|
||
|
||
## Iterative development (how to redeploy after a code change) | ||
|
||
1. Make a change in the code (for example bump the version in `main.go`) | ||
2. Ensure you're still pointing to minikube's Docker using `minikube docker-env`) | ||
3. Delete the deployment and the image | ||
```console | ||
kubectl delete -f deploy/k8s.yaml | ||
docker rmi local/devex:v1 | ||
``` | ||
4. Rebuild the image and re-deploy to Kubernetes | ||
```console | ||
docker build -t local/devex:v1 . | ||
kubectl apply -f deploy/k8s.yaml | ||
``` | ||
5. Access the deployed service in your browser | ||
```console | ||
minikube service local-devex-svc | ||
``` | ||
|
||
(for faster development you can combine steps 3 and 4 the commands in one) | ||
``` | ||
kubectl delete -f deploy/k8s.yaml;docker rmi local/devex:v1;docker build -t local/devex:v1 .;kubectl apply -f deploy/k8s.yaml | ||
``` | ||
### Mount Files to minikube (persistent storage example) | ||
- let's create a example file on our workstation (laptop) to share with our deployment in minikube | ||
```console | ||
mkdir -p ~/Desktop/local-devex | ||
echo "Hello from laptop" > ~/Desktop/local-devex/hello-world.text | ||
``` | ||
|
||
in a separate window run: | ||
```console | ||
minikube mount ~/Desktop/local-devex:/data/ | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: local-devex-deploy | ||
labels: | ||
app: local-devex | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: local-devex | ||
template: | ||
metadata: | ||
labels: | ||
app: local-devex | ||
spec: | ||
containers: | ||
- name: app | ||
image: local/devex:v1 | ||
ports: | ||
- containerPort: 8080 | ||
volumeMounts: | ||
- mountPath: /data | ||
name: test-volume | ||
volumes: | ||
- name: test-volume | ||
hostPath: | ||
# directory location on host | ||
path: /data | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: local-devex-svc | ||
spec: | ||
type: NodePort | ||
selector: | ||
app: local-devex | ||
ports: | ||
- name: local-devex-port | ||
protocol: TCP | ||
port: 8080 | ||
targetPort: 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module local-dev-example-with-minikube | ||
|
||
go 1.19 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
apiVersion: apps/v1 | ||
kind: Deployment | ||
metadata: | ||
name: local-devex-deploy | ||
labels: | ||
app: local-devex | ||
spec: | ||
replicas: 1 | ||
selector: | ||
matchLabels: | ||
app: local-devex | ||
template: | ||
metadata: | ||
labels: | ||
app: local-devex | ||
spec: | ||
containers: | ||
- name: app | ||
image: local/devex:v1 | ||
ports: | ||
- containerPort: 8080 | ||
volumeMounts: | ||
- mountPath: /data | ||
name: test-volume | ||
volumes: | ||
- name: test-volume | ||
hostPath: | ||
# directory location on host | ||
path: /data | ||
--- | ||
apiVersion: v1 | ||
kind: Service | ||
metadata: | ||
name: local-devex-svc | ||
spec: | ||
type: NodePort | ||
selector: | ||
app: local-devex | ||
ports: | ||
- name: local-devex-port | ||
protocol: TCP | ||
port: 8080 | ||
targetPort: 8080 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"os" | ||
"time" | ||
) | ||
|
||
var version = "0.0.2" | ||
|
||
func indexHandler(w http.ResponseWriter, req *http.Request) { | ||
localFile, err := os.ReadFile("/data/hello-world.txt") | ||
if err != nil { | ||
fmt.Printf("couldn't read file %v\n", err) | ||
|
||
} | ||
fmt.Fprintf(w, "<h1>hello world :) </h1> \n Version %s\n File Content:%s", version, localFile) | ||
} | ||
|
||
func headersHandler(w http.ResponseWriter, req *http.Request) { | ||
|
||
for name, headers := range req.Header { | ||
for _, h := range headers { | ||
fmt.Fprintf(w, "%v: %v\n", name, h) | ||
} | ||
} | ||
} | ||
|
||
func main() { | ||
|
||
http.HandleFunc("/", indexHandler) | ||
http.HandleFunc("/headers", headersHandler) | ||
|
||
welcomeText := fmt.Sprintf("%s Starting example version %s, Listening on port 8080 ...", time.Now().String(), version) | ||
fmt.Println(welcomeText) | ||
http.ListenAndServe(":8080", nil) | ||
} |