文章记录minikube运行一个简单demo的流程,总结体会使用k8s的整体部署流程。
准备一个docker image
简单的http服务
package main
import (
"fmt"
"io"
"log"
"net/http"
)
func HelloServer(w http.ResponseWriter, req *http.Request) {
io.WriteString(w, "hello, world!\n")
}
func main() {
http.HandleFunc("/hello", HelloServer)
fmt.Println("will listen on 12345")
log.Fatal(http.ListenAndServe(":12345", nil))
}
Dockfile
FROM golang:1.11.5-alpine3.8 AS build
RUN mkdir -p $GOPATH/src/hello && mkdir -p /root/myapp
COPY . $GOPATH/src/hello
WORKDIR $GOPATH/src/hello
RUN go build -o /root/myapp/hello
CMD /root/myapp/hello
生成镜像并推送到docker hub
生成镜像
1 |
|
为镜像打tag, 事先需要在docker hub上创建kbtest的项目。
1 |
|
将镜像推送到docker hub的kbtest项目下
1 |
|
setup minikube
1.创建minikube cluster
1 |
|
2.打开minikube dashboard
1 |
|
3.创建deployment
1 |
|
4.查看deployment
1 |
|
输出:
1
2NAME READY UP-TO-DATE AVAILABLE AGE myapp 1/1 1 1 24h
5.查看pod
1 |
|
输出:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47Name: myapp-579c5846dc-drmj5 Namespace: default Priority: 0 PriorityClassName: <none> Node: minikube/10.0.2.15 Start Time: Thu, 21 Feb 2019 17:18:58 +0800 Labels: app=myapp pod-template-hash=579c5846dc Annotations: <none> Status: Running IP: 172.17.0.2 Controlled By: ReplicaSet/myapp-579c5846dc Containers: kbtest: Container ID: docker://0a725a3f5cf57c1eb23fb6b967e24e313593e5cc9034f3c85e4ec263692db283 Image: guijiedocker/kbtest:myapp-v1 Image ID: docker-pullable://guijiedocker/kbtest@sha256:79a4855a6fc532b9b194cbcccbaa55963dd13466e53b0d027eb44931b71a75e6 Port: <none> Host Port: <none> State: Running Started: Fri, 22 Feb 2019 16:31:15 +0800 Last State: Terminated Reason: Error Exit Code: 255 Started: Thu, 21 Feb 2019 17:18:59 +0800 Finished: Fri, 22 Feb 2019 16:30:49 +0800 Ready: True Restart Count: 1 Environment: <none> Mounts: /var/run/secrets/kubernetes.io/serviceaccount from default-token-hw6bk (ro) Conditions: Type Status Initialized True Ready True ContainersReady True PodScheduled True Volumes: default-token-hw6bk: Type: Secret (a volume populated by a Secret) SecretName: default-token-hw6bk Optional: false QoS Class: BestEffort Node-Selectors: <none> Tolerations: node.kubernetes.io/not-ready:NoExecute for 300s node.kubernetes.io/unreachable:NoExecute for 300s Events: <none>
可以看出: 启动的pod的IP是172.17.0.2。(或者执行kubectl get pods -o wide
) 但这个IP目前无法直接访问。它是集群内部的一个IP,需要进入到kubernate 节点中进行访问。
可运行:
1 |
|
进入kubernates Node之后,ping 172.17.0.2
,测试到pod的连通性。然后执行:
1 |
|
6.创建service
外部如何访问到myapp服务?kubernetes默认方案是在deployment基础之上又抽象出了service
这个组件。执行:
1 |
|
以上命令中的port要跟我们服务的监听端口一致。
7.查看外部访问端口
1 |
|
输出:
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
myapp LoadBalancer 10.100.146.178
或者,更直观一点:
1 |
|
输出: http://192.168.99.100:30173
测试请求:
1 |
|