用docker自带的k8s部署了一个go项目,有点儿意思,今天把它记录下来。
客户端->slb->ingress->service->bff->microservice
一、k8s mac下安装
mac 下安装docker
brew install --cask docker
设置docker景象仓库服务地址:
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"features": {
"buildkit": true
},
"insecure-registries": [
"127.0.0.1:5000"
],
"registry-mirrors": [
"https://{阿里云的镜像}.mirror.aliyuncs.com",
"https://hub-mirror.c.163.com/",
"https://reg-mirror.qiniu.com",
"https://docker.mirrors.ustc.edu.cn",
"https://dockerhub.azk8s.cn",
"https://registry.docker-cn.com"
]
}
然后在设置中启用k8s
二、k8s基本命令的使用
1、查看集群所有节点
kubectl get nodes
2、根据配置文件,给集群创建资源
kubectl apply -f xxxx.yaml
3、查看集群部署了哪些应用?
docker ps === kubectl get pods -A
4、运行中的应用在docker里面叫容器,在k8s里面叫Pod
kubectl get pods -A
5、创建命名空间
kubectl create ns kkkk
6、删除命名空间
kubectl delete ns kkkk
7、查看命名空间
kubectl get ns
8、新增副本
kubectl create deployment nginx-deployment --image=nginx --replicas=3
或
kubectl apply -f deployment.yaml
9、删除副本
kubectl delete deploy nginx-deployment
11、查看创建的部署
kubectl get deployments
12、要查看 Deployment 上线状态
kubectl rollout status deployment/nginx-deployment
13、扩/缩放 Deployment
kubectl scale deployment/nginx-deployment --replicas=1
14、更新 Deployment
kubectl set image deployment/nginx-deployment nginx=nginx:1.16.1
或
kubectl set image deployment.v1.apps/nginx-deployment nginx=nginx:1.16.1
15、查看service信息
kubectl get svc nginx-deployment
16、查看服务的详情信息
kubectl describe service nginx-deployment
17、删除部署
kubectl delete deploy demo
18、删除服务代理
kubectl delete svc demo
三、部署go项目到k8s
1、开发一个应用
package main
import (
"fmt"
"log"
"net/http"
)
func say(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, "hello..."+r.RemoteAddr)
}
func main() {
http.HandleFunc("/say", say)
log.Fatal(http.ListenAndServe(":8090", nil))
}
2、打包镜像
(1) 先写docker文件
FROM golang:alpine AS builder
WORKDIR /build
COPY hello.go .
RUN go build -o hello hello.go
FROM alpine
WORKDIR /build
COPY --from=builder /build/hello .
EXPOSE 8090
CMD ["./hello"]
(2) 构建docker镜像
docker build -t echomusic/hello:v1.0 .
(3) 推送docker镜像
docker push echomusic/hello:v1.0
3、创建部署 (hello.yaml)
kind: Deployment
apiVersion: apps/v1
metadata:
name: demo-deploy
namespace: default
spec:
replicas: 10
selector:
matchLabels:
app: demo
template:
metadata:
labels:
app: demo
spec:
hostNetwork: true
containers:
- name: hello
image: echomusic/hello:v1.1
ports:
- containerPort: 8090
protocol: TCP
4、创建 service 代理服务 (hello.yaml)
kind: Service
apiVersion: v1
metadata:
name: demo-service
namespace: default
spec:
ports:
- protocol: TCP
port: 8090
targetPort: 8090
selector:
app: demo
type: ClusterIP
5、创建 ingress 网关 (hello.yaml)
kind: Ingress
apiVersion: networking.k8s.io/v1
metadata:
name: demo-localhost
namespace: default
annotations:
nginx.ingress.kubernetes.io/enable-access-log: "true"
nginx.ingress.kubernetes.io/configuration-snippet: |
access_log /var/log/nginx/demo.localdev.me.access.log upstreaminfo if=$loggable;
error_log /var/log/nginx/demo.localdev.me.error.log;
spec:
ingressClassName: nginx
rules:
- host: demo.localdev.me
http:
paths:
- path: /
pathType: Prefix
backend:
service:
name: demo-service
port:
number: 8090
访问地址: http://demo.localdev.me/say 可以看到会打印不同的端口号
kubectl apply -f hello.yaml
或
kubectl create deployment demo --image=echomusic/hello:v1.1 --port=8090
kubectl expose deployment demo
kubectl create ingress demo-localhost --class=nginx \
--rule="demo.localdev.me/*=demo:8090"
资料
1、k8s搭建 点击查看
2、安装ingress-nginx 点击查看