Ansible - Deploying a Go app to Minikube
bogotobogo.com site search:
Ansible 2.0
Introduction
Before we use Ansible to deploy our Go app to minikube, we may want to how can that be done manually from Kubernetes Q and A - II #1.
In this post, our playbook will start a minikube if it's not running, build the Hello Go app image if it's not available at local, and then deploy the app to our minikube cluster. Then, it will expose the pod via a load balancer service and will access the app. Here are the files we are using in this post:
$ tree . ├── Dockerfile ├── hello.go ├── hosts ├── main.yaml
The files are available from Ansible-Minikube-GoApp.
Dockerfile:
FROM golang:1-alpine as build WORKDIR /app COPY hello.go /app RUN go build hello.go FROM alpine:latest WORKDIR /app COPY --from=build /app /app EXPOSE 8180 ENTRYPOINT ["./hello"]
hello.go:
package main import ( "fmt" "log" "net/http" ) //Hello Server responds to requests with the given URL path. func HelloServer(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Hello, you requested: %s", r.URL.Path) log.Printf("Received request for path: %s", r.URL.Path) } func main() { var addr string = ":8181" handler := http.HandlerFunc(HelloServer) if err := http.ListenAndServe(addr, handler); err != nil { log.Fatalf("Could not listen on port %s %v", addr, err) } }
hosts:
[host] localhost ansible_connection=local
main.yaml:
--- - name: go minikube hosts: localhost gather_facts: False vars: ansible_python_interpreter: '{{ ansible_playbook_python }}' image_name: hello-go image_tag: 1.0 replicas: 4 pre_tasks: - name: check the status of minikube. command: minikube status register: minikube_status changed_when: false ignore_errors: true - name: start minikube if it's not running. command: minikube start --vm = true when: "not minikube_status.stdout or 'Running' not in minikube_status.stdout" tasks: - name: get existing image hash. shell: | eval $(minikube docker-env) docker images -q {{ image_name }} register: image_hash changed_when: false - name: build image if it's not already built. shell: | eval $(minikube docker-env) docker build -t {{ image_name }}:{{ image_tag }} . when: not image_hash.stdout - name: create a deployment for Hello Go. k8s: state: present definition: apiVersion: apps/v1 kind: Deployment metadata: name: hello-go namespace: default spec: replicas: "{{ replicas }}" selector: matchLabels: app: hello-go template: metadata: labels: app: hello-go spec: containers: - name: hello-go image: "{{ image_name }}:{{ image_tag }}" imagePullPolicy: IfNotPresent ports: - containerPort: 8181 - name: create a Service for Hello Go. k8s: state: present definition: apiVersion: v1 kind: Service metadata: name: hello-go namespace: default spec: type: LoadBalancer ports: - port: 8181 targetPort: 8181 selector: app: hello-go post_tasks: - name: exposing Hello Go on the host via Minikube. command: minikube service hello-go --url changed_when: false register: minikube_service - debug: msg: "Hello Go URL: {{ minikube_service['stdout_lines'][0] }}" - name: verify Hello Go is responding. uri: url: "{{ minikube_service['stdout_lines'][0] }}/test" return_content: true register: hello_go_response failed_when: "'/test' not in hello_go_response.content" - debug: msg: "Testing URL Hello GO Requested: {{hello_go_response.content}} Status: {{hello_go_response.status}}"
Let's run the playbook and we get tbe following output:
$ ansible-playbook -i hosts main.yaml PLAY [go minikube] ********************************************************************************************************** TASK [check the status of minikube.] **************************************************************************************** fatal: [localhost]: FAILED! => {"changed": false, "cmd": ["minikube", "status"], "delta": "0:00:00.351411", "end": "2021-04-08 17:56:41.581405", "msg": "non-zero return code", "rc": 85, "start": "2021-04-08 17:56:41.229994", "stderr": "", "stderr_lines": [], "stdout": "* There is no local cluster named \"minikube\"\n - To fix this, run: \"minikube start\"", "stdout_lines": ["* There is no local cluster named \"minikube\"", " - To fix this, run: \"minikube start\""]} ...ignoring TASK [start minikube if it's not running.] ********************************************************************************** changed: [localhost] TASK [get existing image hash.] ********************************************************************************************* ok: [localhost] TASK [build image if it's not already built.] ******************************************************************************* changed: [localhost] TASK [create a deployment for Hello Go.] ************************************************************************************ changed: [localhost] TASK [create a Service for Hello Go.] *************************************************************************************** changed: [localhost] TASK [exposing Hello Go on the host via Minikube.] ************************************************************************** ok: [localhost] TASK [debug] **************************************************************************************************************** ok: [localhost] => { "msg": "Hello Go URL: http://192.168.64.15:32129" } TASK [verify Hello Go is responding.] *************************************************************************************** ok: [localhost] TASK [debug] **************************************************************************************************************** ok: [localhost] => { "msg": "Testing URL Hello GO Requested: Hello, you requested: /test Status: 200" } PLAY RECAP ****************************************************************************************************************** localhost : ok=10 changed=4 unreachable=0 failed=0 skipped=0 rescued=0 ignored=1
$ kubectl get all NAME READY STATUS RESTARTS AGE pod/hello-go-68b787b6f9-9gcgl 1/1 Running 0 91m pod/hello-go-68b787b6f9-kpjbr 1/1 Running 0 91m pod/hello-go-68b787b6f9-tmcdr 1/1 Running 0 91m pod/hello-go-68b787b6f9-z6gld 1/1 Running 0 91m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/hello-go LoadBalancer 10.109.184.76 <pending> 8181:32129/TCP 91m service/kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 92m NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/hello-go 4/4 4 4 91m NAME DESIRED CURRENT READY AGE replicaset.apps/hello-go-68b787b6f9 4 4 4 91m
Refs
Ansible 2.0
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization