韩先超简介:
1、把开发写好的代码跑在k8s里具体步骤:
1)编写Dockerfile:使用Dockerfile构建一个Docker镜像,其中包含应用程序和相关的依赖项。
2)构建Docker镜像:使用docker build命令构建Docker镜像。例如,docker build -t my-image:1.0 .将构建一个名为my-image的镜像,并将版本号设置为1.0
3)将Docker镜像推送到镜像仓库:可以使用docker push命令将Docker镜像推送到Docker镜像仓库中,以便在Kubernetes集群中使用。例如,docker push my-image:1.0将镜像推送到Docker Hub中。
4)编写Kubernetes清单文件:编写Kubernetes清单文件,其中包含Deployment、Service、ConfigMap等资源的定义,以便将应用程序部署到Kubernetes集群中。在清单文件中,需要指定镜像名称和版本,容器端口等信息。
5)应用Kubernetes清单文件:使用kubectl apply命令将Kubernetes清单文件应用到Kubernetes集群中。例如,kubectl apply -f my-app.yaml将应用名为my-app的清单文件。
6)查看应用状态:可以使用kubectl命令查看应用程序在Kubernetes中的状态。例如,kubectl get pods将列出所有正在运行的Pod。
7)访问应用程序:使用Service的IP地址和端口号可以访问应用程序。根据Service类型的不同,访问方式也有所不同。
2、基于go代码写一个猜字小游戏
1)写代码
[root@xianchaomaster1~]# mkdir caizi
[root@xianchaomaster1~]# cd caizi/
[root@xianchaomaster1caizi]# cat main.go
package main
import (
"fmt"
"math/rand"
"time"
)
const maxGuesses = 10
func main() {
rand.Seed(time.Now().UnixNano())
number := rand.Intn(100) + 1
fmt.Println("I'm thinking of a number between 1 and 100.")
fmt.Printf("You have %d guesses.\n", maxGuesses)
for guesses := 1; guesses <= maxGuesses; guesses++ {
fmt.Printf("Guess #%d: ", guesses)
var guess int
_, err := fmt.Scanln(&guess)
if err != nil {
fmt.Println("Invalid input. Please enter an integer.")
continue
}
if guess < 1 || guess > 100 {
fmt.Println("Invalid input. Please enter a number between 1 and 100.")
continue
}
if guess < number {
fmt.Println("Too low.")
} else if guess > number {
fmt.Println("Too high.")
} else {
fmt.Printf("Correct! You guessed the number in %d guesses.\n", guesses)
return
}
}
fmt.Printf("Sorry, you did not guess the number. It was %d.\n", number)
}
[root@xianchaomaster1caizi]# go mod init test
[root@xianchaomaster1caizi]# go env -w GOPROXY=https://goproxy.cn,direct
[root@xianchaomaster1caizi]# go mod tidy
[root@xianchaomaster1caizi]# CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -o k8s-demo main.go
FROM alpine
WORKDIR /app
COPY k8s-demo /app
CMD ["/bin/sh","-c","./k8s-demo"]
[root@xianchaomaster1 caizi]# docker build -t docker.io/library/k8sgame:v1 .
[root@xianchaomaster1 caizi]# docker save -o k8sgame.tar.gz docker.io/library/k8sgame:v1
[root@xianchaomaster1 caizi]# scp k8sgame.tar.gz xianchaonode1:/root/
[root@xianchaomaster1 caizi]# scp k8sgame.tar.gz xianchaonode2:/root/
[root@xianchaonode1 ~]# ctr -n=k8s.io images import k8sgame.tar.gz
[root@xianchaonode2 ~]# ctr -n=k8s.io images import k8sgame.tar.gz
apiVersion: apps/v1
kind: Deployment
metadata:
name: guess-game
spec:
replicas: 1
selector:
matchLabels:
app: guess-game
template:
metadata:
labels:
app: guess-game
spec:
containers:
- name: guess-game
image: docker.io/library/k8sgame:v1
imagePullPolicy: IfNotPresent
command: ["/bin/sh","-c","sleep 3600"]
[root@xianchaomaster1 caizi]# kubectl apply -f
deployment.yaml
[root@xianchaomaster1 caizi]# kubectl get pods -owide
NAME READY STATUS RESTARTS IP
guess-game-6c9b4df786-892ds 1/1 Running 0 10.244.121.35
[root@xianchaomaster1 caizi]# kubectl exec -it guess-game-6c9b4df786-892ds
-- /bin/sh
/app # ./k8s-demo
I'm thinking of a number between 1 and 100.
You have 10 guesses.
Guess #1:
本周精彩文章推荐
作者微信:luckylucky421302
加微信,可以进学习交流群。