笔记 使用minikube部署golang服务示例

创建目录

mkdir minikube
cd minikube
touch server.go
touch Dockerfile

build 镜像

1 server.go
package main

import (
    "fmt"
    "net/http"
)

func main() {
    http.HandleFunc("/", myHandler)
    fmt.Println("start begin...")
    err := http.ListenAndServe(":8088", nil)
    if err != nil {
        fmt.Printf("start failed! err :%v \n", err)
    }

}
func myHandler(w http.ResponseWriter, r *http.Request) {
    _, _ = fmt.Fprintf(w, "Hello there!\n")
}
2 Dockerfile
FROM golang:1.13
EXPOSE 8088
COPY server.go .
CMD go run server.go
3 推往dockerhub,这里用的是我自己的harbor仓库
docker build -t docker.yishi.tech/test/goserver:v1.0 .
docker push docker.yishi.tech/test/goserver:v1.0
4 根据文档minikube安装
5 注意如果不安装Hypervisor 需要使用--vm-driver=none 参数启动
minikube start --vm-driver=none
  minikube v1.4.0 on Ubuntu 18.04
  Running on localhost (CPUs=2, Memory=3944MB, Disk=80599MB) ...
ℹ️   OS release is Ubuntu 18.04.3 LTS
  Preparing Kubernetes v1.16.0 on Docker 19.03.4 ...
    ▪ kubelet.resolv-conf=/run/systemd/resolve/resolv.conf

  Downloading kubeadm v1.16.0
  Downloading kubelet v1.16.0
  Pulling images ...
  Launching Kubernetes ... 
  Configuring local host environment ...

⚠️  The 'none' driver provides limited isolation and may reduce system security and reliability.
⚠️  For more information, see:
  https://minikube.sigs.k8s.io/docs/reference/drivers/none/

⚠️  kubectl and minikube configuration will be stored in /root
⚠️  To use kubectl or minikube commands as your own user, you may need to relocate them. For example, to overwrite your own settings, run:

    ▪ sudo mv /root/.kube /root/.minikube $HOME
    ▪ sudo chown -R $USER $HOME/.kube $HOME/.minikube

  This can also be done automatically by setting the env var CHANGE_MINIKUBE_NONE_USER=true
⌛  Waiting for: apiserver proxy etcd
 scheduler controller dns
  Done! kubectl is now configured to use "minikube"
  For best results, install kubectl: https://kubernetes.io/docs/tasks/tools/install-kubectl/

6 安装kubectl 安装文档
->  kubectl version
Client Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.2", GitCommit:"c97fe5036ef3df2967d086711e6c0c405941e14b", GitTreeState:"clean", BuildDate:"2019-10-15T19:18:23Z", GoVersion:"go1.12.10", Compiler:"gc", Platform:"linux/amd64"}
Server Version: version.Info{Major:"1", Minor:"16", GitVersion:"v1.16.0", GitCommit:"2bd9643cee5b3b3a5ecbd3af49d09018f0773c77", GitTreeState:"clean", BuildDate:"2019-09-18T14:27:17Z", GoVersion:"go1.12.9", Compiler:"gc", Platform:"linux/amd64"}
7 部署goserver
  • 根据image 创建一个 Kubernetes Deployment
$ kubectl create deployment goserver --image=docker.yishi.tech/test/goserver:v1.0
deployment.apps/goserver created
  • 暴露端口
$ kubectl expose deployment goserver --type=NodePort --port=8088
service/goserver exposed

--type=NodePort 的说明:

nodePort
 外部机器可访问的端口。
比如一个Web应用需要被其他用户访问,那么需要配置type=NodePort,而且配置nodePort=30001,那么其他机器就可以通过浏览器访问scheme://node:30001访问到该服务,例如http://node:30001。
 例如MySQL数据库可能不需要被外界访问,只需被内部服务访问,那么不必设置NodePort
targetPort
 容器的端口(最根本的端口入口),与制作容器时暴露的端口一致(DockerFile中EXPOSE),例如docker.io官方的nginx暴露的是80端口。
 docker.io官方的nginx容器的DockerFile参考https://github.com/nginxinc/docker-nginx
port
 kubernetes中的服务之间访问的端口,尽管mysql容器暴露了3306端口(参考https://github.com/docker-library/mysql/的DockerFile),但是集群内其他容器需要通过33306端口访问该服务,外部机器不能访问mysql服务,因为他没有配置NodePort类型

  • 查看Pod
$ kubectl get pod
NAME                              READY   STATUS    RESTARTS   AGE
goserver-696cc845bf-twm84         1/1     Running   0          7m36s
  • 获取服务的url
$ minikube service goserver --url      
* http://108.61.223.211:30016
  • 调用测试 这里go的服务是输出一个hello there! ,证明一切顺利
$ curl http://108.61.223.211:30016
Hello there!
  • 其他基础命令
kubectl delete services hello-minikube
kubectl delete deployment hello-minikube
minikube stop
  • 查看minikube 控制台页面
$ minikube dashboard --url
* Verifying dashboard health ...
* Launching proxy ...
* Verifying proxy health ...
http://127.0.0.1:38535/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
  • 如果是ubuntu server 没有浏览器弹出,可以设置代理然后通过远程查看
$ kubectl proxy --address='0.0.0.0' --port='8002'  --accept-hosts='^.*' 
Starting to serve on [::]:8002
  • 访问
http://ip:8002/api/v1/namespaces/kubernetes-dashboard/services/http:kubernetes-dashboard:/proxy/
  • 界面


    image.png

你可能感兴趣的:(笔记 使用minikube部署golang服务示例)