K8s入门手记

这篇博客详细介绍了Kubernetes的基本概念,如节点、Pod、Replication Controllers和Service,并通过实例展示了创建镜像、创建Pod、开启服务对外开放、扩容与部署、使用ConfigMap以及Helm管理的过程,适合K8s初学者入门。
摘要由CSDN通过智能技术生成

一、准备工作

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  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值