一、准备工作
0. 基本概念
- 节点(
Node
):一个节点是一个运行 Kubernetes 中的主机。 - 容器组(
Pod
):一个 Pod 对应于由若干容器组成的一个容器组,同个组内的容器共享一个存储卷(volume)。 - 容器组生命周期(
pos-states
):包含所有容器状态集合,包括容器组状态类型,容器组生命周期,事件,重启策略,以及 replication controllers。 - Replication Controllers:主要负责指定数量的 pod 在同一时间一起运行。
- 服务(
services
):一个 Kubernetes 服务是容器组逻辑的高级抽象,同时也对外提供访问容器组的策略。 - 卷(
volumes
):一个卷就是一个目录,容器对其有访问权限。 - 标签(
labels
):标签是用来连接一组对象的,比如容器组。标签可以被用来组织和选择子对象。 - 接口权限(
accessing_the_api
):端口,IP 地址和代理的防火墙规则。 - web 界面(
ux
):用户可以通过 web 界面操作 Kubernetes。 - 命令行操作(
cli
):kubecfg
命令。
1. 制作一个镜像
用go写一个简单的httpsvr,代码如下:
package main
import (
"fmt"
"log"
"net/http"
)
func homePage(writer http.ResponseWriter, request *http.Request) {
err := request.ParseForm()
fmt.Fprint(writer, "<html><body>")
if err != nil {
fmt.Fprintf(writer, "%s", err)
}
content, found := request.Form["name"]
if found {
fmt.Fprintf(writer, "%s", content)
}
fmt.Fprint(writer, "</body></html>")
}
func main() {
http.HandleFunc("/", homePage)
err := http.ListenAndServe(":9001", nil)
if err != nil {
log.Fatal("failed to start server", err)
}
}
撰写Dockerfile,这里的scratch表示为基础镜像的话,意味着你不以任何镜像为基础,接下来所写的指令将作为镜像第一层开始存在。
# Dockerfile References: https://docs.docker.com/engine/reference/builder/
# Start from golang v1.11 base image
FROM golang:1.11
# Add Maintainer Info
LABEL maintainer="yachang.wang@gmail.com"
# Set the Current Working Directory inside the container
WORKDIR $GOPATH/src/github.com/callicoder/go-docker
# Copy everything from the current directory to the PWD(Present Working Directory) inside the container
COPY . .
# Download all the dependencies
# https://stackoverflow.com/questions/28031603/what-do-three-dots-mean-in-go-command-line-invocations
RUN go get -d -v ./...
# Install the package
RUN go install -v ./...
# This container exposes port 8080 to the outside world
EXPOSE 9001
# Run the executable
ENTRYPOINT ["./http_demo"]
#FROM scratch
FROM golang:1.11
# Copy everything from the current directory to the PWD(Present Working Directory) inside the container
COPY . .
#start
EXPOSE 9001
# Run the executable
ENTRYPOINT ["./http_demo"]
$ docker build -t http_demo:v1 .
Sending build context to Docker daemon 6.572MB
Step 1/3 : FROM scratch
--->
Step 2/3 : EXPOSE 9001
---> Running in e79fcce5b314
Removing intermediate container e79fcce5b314
---> e4318ba62c4f
Step 3/3 : CMD ["./http_demo"]
---> Running in 0b01a5a221f0
Removing intermediate container 0b01a5a221f0
---> 73ba3f4115ec
Successfully built 73ba3f4115ec
Successfully tagged http_demo:v1
build成功后,可以查看已创建。
docker image ls
REPOSITORY