使用kind快速部署k8s【测试用】
使用kind快速部署k8s【测试用】
·
安装kind
先安装go环境,然后使用go install安装
go install sigs.k8s.io/kind@v0.23.0
安装完成之后使用kind version命令验证,会看到以下内容
root@ecs-aliyun:~# kind version
kind v0.20.0 go1.20.3 linux/amd64
创建k8s集群
安装完kind之后,使用如下命令,一键创建k8s集群
root@ecs-aliyun:~# kind create cluster
Creating cluster "kind" ...
✓ Ensuring node image (kindest/node:v1.27.3) 🖼
✓ Preparing nodes 📦
✓ Writing configuration 📜
✓ Starting control-plane 🕹️
✓ Installing CNI 🔌
✓ Installing StorageClass 💾
Set kubectl context to "kind-kind"
You can now use your cluster with:
kubectl cluster-info --context kind-kind
创建完成之后,使用如下命令验证
root@ecs-aliyun:~# kind get clusters
kind
执行kubectl命令
先进入kind Docker容器
docker exec -it kind-control-plane bash
容器里面的环境就相当于安装了k8s的环境,可以使用kubectl命令
root@ecs-aliyun:~# docker exec -it kind-control-plane bash
root@kind-control-plane:/# kubectl get node
NAME STATUS ROLES AGE VERSION
kind-control-plane Ready control-plane 5h3m v1.27.3
容器外安装kubectl
如果想要在容器外使用kubectl命令,只要安装即可;
但是要注意,访问service必须先进入容器,外面的网络是不通的
安装最新版本
curl -LO "https://dl.k8s.io/release/$(curl -L -s https://dl.k8s.io/release/stable.txt)/bin/linux/amd64/kubectl"
安装指定版本
curl -LO https://dl.k8s.io/release/v1.30.0/bin/linux/amd64/kubectl
sudo install -o root -g root -m 0755 kubectl /usr/local/bin/kubectl
安装完成之后,使用如下命令验证
root@kind-control-plane:/# kubectl get node
NAME STATUS ROLES AGE VERSION
kind-control-plane Ready control-plane 5h3m v1.27.3
创建一个Golang Demo工程
main函数
package main
import (
"common-go/ginx"
"github.com/gin-gonic/gin"
"os"
)
func main() {
r := gin.Default()
r.GET("/", ginx.Hello())
port := os.Getenv("port")
if port == "" {
port = "8080"
}
panic(r.Run(":" + port))
}
func Hello() gin.HandlerFunc {
return func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{
"message": "Hello World",
})
}
}
Dockerfile
# 使用一个小型的基础镜像来运行编译后的应用程序
FROM alpine:3.20
LABEL authors="yimin"
# 设置工作目录
WORKDIR /app
COPY main myapp
RUN chmod +x myapp
ENV port=8088
# 暴露端口(如果应用程序需要)
EXPOSE ${port}
# 运行应用程序
CMD ["./myapp"]
这里只是测试,直接在windows平台使用交叉编译得到linux可执行文件,而没有使用golang镜像build
构建Docker镜像
将编译得到的可执行文件和Dockerfile上传到服务器,构建Docker镜像
docker build -t ginx .
可以使用如下命令测试
root@ecs-aliyun:~/ginx# docker run --rm --name=ginx -p 8088:8088 ginx
[GIN-debug] [WARNING] Creating an Engine instance with the Logger and Recovery middleware already attached.
[GIN-debug] [WARNING] Running in "debug" mode. Switch to "release" mode in production.
- using env: export GIN_MODE=release
- using code: gin.SetMode(gin.ReleaseMode)
[GIN-debug] GET / --> common-go/ginx.Hello.func1 (3 handlers)
[GIN-debug] [WARNING] You trusted all proxies, this is NOT safe. We recommend you to set a value.
Please check https://pkg.go.dev/github.com/gin-gonic/gin#readme-don-t-trust-all-proxies for details.
[GIN-debug] Listening and serving HTTP on :8088
部署到K8S
如果docker run没问题,就可以准备部署到k8s了
yaml模板
apiVersion: apps/v1
kind: Deployment
metadata:
name: ginx
spec:
replicas: 1
selector:
matchLabels:
app: ginx
template:
metadata:
labels:
app: ginx
spec:
containers:
- name: ginx
image: ginx
imagePullPolicy: IfNotPresent
ports:
- containerPort: 8088
---
apiVersion: v1
kind: Service
metadata:
name: ginx-service
spec:
selector:
app: ginx
ports:
- protocol: TCP
port: 8088
targetPort: 8088
type: NodePort
执行
kubectl apply -f deployment.yml
进入kind容器,访问service
root@ecs-aliyun:~# docker exec -it kind-control-plane bash
root@kind-control-plane:/# kubectl get svc
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
ginx-service NodePort 10.96.33.18 <none> 8088:32448/TCP 3h10m
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 5h9m
root@kind-control-plane:/# curl 10.96.33.18:8088
{"message":"Hello World"}
参考
更多推荐
已为社区贡献1条内容
所有评论(0)