582337768。群是一堆牛人,你有问题一般不过分,很多人都会解答一二。添加群的时候,请说明来自于 汉克博客园

汉克书

http://hankbook.cn

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

制作最简单的http服务

go代码

package main

import (
	"fmt"
	"net/http"
)

func helloHello(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "Hello world!")
}

func main() {
	http.HandleFunc("/", helloHello)
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		fmt.Printf("http server failed, err:%v\n", err)
		return
	}
}

编译代码

dockerfile的另外一种写法,此方案使用多个FROM 方便很多编译的做法, 例如在A镜像中编译vue的代码,然后把生成后的镜像文件直接拷贝到新的容器当中,这样能保证镜像的最小化

# stage 1: build src code to binary
FROM golang:1.13-alpine3.10 as builder

COPY *.go /app/

RUN cd /app && go build -o helloworld .

# stage 2: use alpine as base image
FROM alpine:3.10

RUN apk update && \
    apk --no-cache add tzdata ca-certificates && \
    cp -f /usr/share/zoneinfo/Asia/Shanghai /etc/localtime && \
    apk del tzdata && \
    rm -rf /var/cache/apk/*
# 使用--from的参数做到拷贝使用
COPY --from=builder /app/helloworld /helloworld

CMD ["/helloworld"]
docker build -t hank997/go-helloworld:v1 .
docker run -it --rm -p 9090:9090 hank997/go-helloworld:v1

编写yaml

kind: Pod
apiVersion: v1
metadata:
  name: test-go-http-pod
spec:
  containers:
  - name: test-go-http-pod
    image: hank997/go-helloworld:v1
    ports:
    - containerPort: 9090

---
# service
apiVersion: v1
kind: Service
metadata:
  name: test-go-http-service
  labels:
    name: go
spec:
  ports:
  - port: 9090
    protocol: TCP
    targetPort: 9090
    name: go-http
    nodePort: 31221
  type: NodePort
  selector:
    name: test-go-http-pod

制作kubectl命令工具

先出门左拐到官方地址去下载自己所需要的kubectl对应版本的二进制

Dockerfile制作

FROM alpine
COPY kubectl /usr/bin
# 我仓库里面暂时只有1.15.9的镜像, 推荐二进制等命令都到官方下载,防止被人串改
docker build -t hank997/kubectl:1.15.9 .

kube的 secret 制作

# key应该跟下面的subpath一致
kubectl -n devops-ns create secret generic kube-config --from-file=config=/root/.kube/config

测试demo yaml,此步骤是为了自己测试使用

apiVersion: v1
kind: Pod
metadata:
  name: kubectl
  namespace: devops-ns
  labels:
    env: test
spec:
  containers:
  - name: kubectl
    image: hank997/kubectl:1.15.9
    imagePullPolicy: IfNotPresent
    command: ["sleep", “3600”]
    volumeMounts:
    - mountPath: /root/.kube
      name: kube-config
  volumes:
  - name: kube-config
    secret:
      secretName: kube-config

测试kubectl get pod命令

kubectl -n devops-ns exec -it  kubectl kubectl get pod

pipeline

pipeline语法 https://www.w3cschool.cn/jenkins/jenkins-jg9528pb.html

插件地址: https://github.com/jenkinsci/kubernetes-plugin

此处使用了 docker in docker的模式操作,所以需要挂载一个socket进行使用

如果使用到了nvidia的话,请加上environment指令

def label = "worker-${UUID.randomUUID().toString()}"

podTemplate(label: label, containers: [
  containerTemplate(name: 'docker', image: 'docker', command: 'cat', ttyEnabled: true),
  containerTemplate(name: 'kubectl', image: 'hank997/kubectl:1.15.9', command: 'cat', ttyEnabled: true)
],
volumes: [
  hostPathVolume(mountPath: '/var/run/docker.sock', hostPath: '/var/run/docker.sock'),
  secretVolume(secretName: 'kube-config',mountPath: "/root/.kube", subPath:"config")
]) {
  node(label) {
    stage("check out"){git  credentialsId: '3c210def-c000-5d2a-9b2d-838986a6b1sd', url: 'https://github.com/AgoCan/go-helloworld.git'}注释 //s't
    stage('Create Docker images') {
      container('docker') {
          sh """
            docker login -u admin --password Harbor12345  
            docker build -t hank997/hello-go:v2 .
            docker push hank997/hello-go:v2
            """

      }
    }
    stage("kubectl"){
        container('kubectl') {
            sh """
            kubectl set images  pod/test-go-http-pod test-go-http-pod=hank997/hello-go:v2
            """
        }
            //sh "kubectl set images pod/test-go-http-pod test-go-http-pod=hank997/hello-go:v2"
    }
  }
}

参考文档: https://kubernetes.hankbook.cn/practice/cicd/example.html

posted on 2020-03-09 18:48  汉克书  阅读(237)  评论(0)    收藏  举报