kubernetes部署一个hello-world

主要内容

  • 通过实际部署一个Kubernetes服务来帮助更好的理解它

如何部署一个简单的"Hello World"服务

  • 我们在Go中编写了一个简单的服务器,用“Hello World”响应http请求。代码非常简单:
$ touch main.go
package main
import (
"fmt"
"log"
"net/http"
"os"
)
func GetEnvDefault(key, defVal string) string {
	val, ex := os.LookupEnv(key)
	if !ex {
		return defVal
	}
	return val
}

func handler(w http.ResponseWriter, r *http.Request) {
    log.Print("Hello world received a request.")
    version := GetEnvDefault("VERSION", "v1")
    log.Println(version)
    fmt.Fprintf(w, "Hello world %s\n",version)
}

func main() {
    log.Print("Hello world sample started.")
    http.HandleFunc("/hello", handler)
    port := GetEnvDefault("PORT", "80")
    log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
} 

运行它的第一步是将其构建到Docker容器中。为此,我们从基础Go镜像开始构建Dockerfile。

$ touch Dockerfile
  • Dockerfile运用了分布式构建用来减少镜像的体积
# Use the official Golang image to create a build artifact.
# https://hub.docker.com/_/golang
FROM golang:1.12.5 as builder

# Copy local code to the container image.
WORKDIR /go/src/app
COPY . .

RUN GOOS=linux GOARCH=amd64 CGO_ENABLED=0 go build -o start main.go

# Use a Docker multi-stage build to create a lean production image.
# https://docs.docker.com/develop/develop-images/multistage-build/#use-multi-stage-builds
FROM alpine:3.9.4

COPY --from=builder /go/src/app/start /start

RUN chmod 775 /start

# Run the web service on container startup.
CMD ["/start"]
  • 编译
$ docker build -t hello:v1 .
$ docker images
REPOSITORY                                TAG                 IMAGE ID            CREATED             SIZE
hello                                     v1                  a56967e07da1        About an hour ago   12.9MB

Golang拥有得天独厚的优势,其镜像体积小到令人发指,水平Pod自动伸缩会更快,遇到突发情况,生产环境可以极速伸缩出无数的Pod来减轻压力。

kubernetes yaml 文件

$ touch hello-kubernetes.yaml
  • 内容
# hello-kubernetes.yaml

apiVersion: v1
kind: Service
metadata:
  name: hello-kubernetes
spec:
  type: ClusterIP
  ports:
  - port: 80
    targetPort: 80
  selector:
    app: hello-kubernetes
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: hello-kubernetes
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-kubernetes
  template:
    metadata:
      labels:
        app: hello-kubernetes
    spec:
      containers:
      - name: helloworld-kubernetes
        image: hello:v1
        imagePullPolicy: Never
        ports:
        - containerPort: 80

注意:如果是个集群,对应slave节点没有docker image的话,会导致deployment失败。

  • 开调
# 目录
$ ls
Dockerfile  hello.yaml  main.go

# 执行部署
$ kubectl apply -f hello-kubernetes.yaml

# 获取Pod
$ kubectl get po
NAME                                                     READY   STATUS    RESTARTS   AGE
hello-kubernetes-5d75df9896-m5fzn                        1/1     Running   0          1s

# 获取service
$ kubectl get svc
NAME                            TYPE        CLUSTER-IP       EXTERNAL-IP   PORT(S)                 AGE
hello-kubernetes                ClusterIP   10.97.89.95      <none>        80/TCP                  1s

# 进入Pod
$ kubectl exec -it hello-kubernetes-5d75df9896-m5fzn sh
# curl hello-kubernetes/hello
Hello world v1

# 查看日志
$ kubectl logs hello-kubernetes-5d75df9896-m5fzn
2020/01/26 06:57:29 Hello world sample started.
2020/01/26 06:57:44 Hello world received a request.
2020/01/26 06:57:44 v1

# 删除部署
$ kubectl delete -f hello-kubernetes.yaml

# Enjoy it, happy coding!

你可能感兴趣的:(Kubernetes)