# 容器化部署K8S-Mac单机版
# Kubernetes 本地快速启动
- 操作系统
- 桌面 Docker
> docker -v
Docker version 20.10.20
- 安装 K8S
> kubectl get ns
NAME STATUS AGE
default Active 11d
kube-node-lease Active 11d
kube-public Active 11d
kube-system Active 11d
注:如果长时间安装不上,需要科学上网,自行搜索即可
// yaml 启动安装
> kubectl apply -f https://raw.githubusercontent.com/kubernetes/dashboard/v2.7.0/aio/deploy/recommended.yaml
或下载到本地
> kubectl apply -f config/kubernetes-dashboard.yaml
// 启动代理端口监听
// 默认 8001 端口,若有需要可通过 --port=8080 进行指定
> kubectl proxy
新增配置文件 dashboard-adminuser.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: admin-user
namespace: kubernetes-dashboard
// 生成 Token 复制到登录页面即可
> kubectl apply -f dashboard-adminuser.yaml
> kubectl -n kubernetes-dashboard create token admin-user
最终效果下
# Kubernetes 部署应用程序
# 创建 Deployment
创建 nginx-deployment.yaml
文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
labels:
app: nginx
spec:
replicas: 2
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.18.0
应用 nginx-deployment.yaml
文件
> kubectl apply -f nginx-deployment.yaml
deployment.apps/nginx-deployment created
# 查看运行状态
查看 Pod 运行情况
> kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-deployment-dcb989799-bm625 1/1 Running 0 109s
nginx-deployment-dcb989799-kq9gs 1/1 Running 0 109s
查看 Deployment 部署情况
> kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-deployment 2/2 2 2 2m26s
我们也可以通过 describe 命令进行查看
> kubectl describe pod nginx-deployment-dcb989799-bm625
# 查看 Dashboard
# 创建 Nginx Service
通过 Kubernetes 的 NodePort 的方式对外提供访问。
生产中不推荐,Node节点都会监听端口号nodePort: 30001
,较危险
创建 nginx-service.yaml
文件
apiVersion: v1
kind: Service
metadata:
name: nginx-service
labels:
app: nginx
spec:
selector:
app: nginx
ports:
- name: nginx-port
protocol: TCP
port: 80
nodePort: 30001
targetPort: 80
type: NodePort
应用 nginx-service.yaml
文件
> kubectl apply -f nginx-service.yaml
> kubectl get services -o wide
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 11d <none>
nginx-service NodePort 10.108.246.231 <none> 80:30001/TCP 21m app=nginx
访问
curl http://127.0.0.1:30001
<!DOCTYPE html>
<html>
<head>
<title>Welcome to nginx!</title>
# 部署 Go 程序
- 编写和编译 Dockerfile
FROM golang:latest
ENV GOPROXY https://goproxy.cn,direct
WORKDIR $GOPATH/k8s
COPY . $GOPATH/k8s
RUN go build .
EXPOSE 8000
ENTRYPOINT ["./k8s"]
- 编译并打标签
docker build -t k8s:v0.0.1 .
- 验证打包进 Docker 中的程序是否正常运行
docker run -p 10001:8000 k8s:v0.0.1
[GIN-debug] GET /ping --> main.main.func1 (3 handlers)
[GIN-debug] Listening and serving HTTP on :8000
[GIN] 2023/01/04 - 02:43:12 | 200 | 2.229125ms | 172.17.0.1 | GET "/ping"
- 上传到本地的 docker hub
# 本地运行镜像 registry 具体参考
https://ni-ning.cn/k8s/docker.html#核心要素及常用操作详解 -> 7.推送本地镜像到镜像仓库中
# 镜像仓库给外部访问,不能通过localhost,尝试使用内网地址192.168.1.4:5000/nginx:alpine
> docker tag k8s:v0.0.1 192.168.1.4:5000/k8s:v0.0.1
> docker push 192.168.1.4:5000/k8s:v0.0.1
The push refers to repository [192.168.1.4:5000/k8s]
21eab65d6dd7: Pushed
bbcf3aecf686: Pushed
0b95ab180b3d: Pushed
d41140bbc3ba: Pushed
acd2b47d5b2a: Pushed
3ffe05c5d95f: Pushed
ffd50287b468: Pushed
cba7a92f211b: Pushed
fe09b9981fd2: Pushed
dd5b48ca5196: Pushed
v0.0.1: digest: sha256:21e9f5282a52774a78e3e979480a072e5ba2c92b3b2de321637a061880aa2e46 size: 2423
# 编写 Kubernetes 配置
- 编写
go-deployment.yaml
文件
apiVersion: apps/v1
kind: Deployment
metadata:
name: go-k8s
labels:
app: go-k8s
spec:
replicas: 2
selector:
matchLabels:
app: go-k8s
template:
metadata:
labels:
app: go-k8s
spec:
containers:
- name: go-k8s
image: 192.168.1.4:5000/k8s:v0.0.1
- 创建 Service,编写 go-service.yaml
apiVersion: v1
kind: Service
metadata:
name: go-k8s-svc
labels:
app: go-k8s
spec:
ports:
- port: 8000
type: ClusterIP
selector:
app: go-k8s
- 创建应用
kubectl apply -f go-deployment.yaml
kubectl apply -f go-service.yaml
# 部署 Ingress
- Ingress Controller 我们采用 Docker for Mac 特定提供的 Ingress Controller 部署脚本 quick-start (opens new window)
> kubectl apply -f https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/cloud/deploy.yaml
或保存到本地,命名为install-ingress-nginx.yml
// 行510 新增配置 hostNetwork: true
> kubectl apply -f install-ingress-nginx.yml
// 查看运行情况
> kubectl -n ingress-nginx get pods -owide
> kubectl -n ingress-nginx exec ingress-nginx-controller-5757bc8586-clrnl -it /bin/bash
- Nginx Ingress
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
name: test-ingress
annotations:
nginx.ingress.kubernetes.io/use-regex: "true"
spec:
rules:
- host: "bar.foo.com"
http:
paths:
- pathType: Prefix
path: /
backend:
service:
name: go-k8s-svc
port:
number: 8000
- 外部访问
kubectl get ingress
# Go程序调用 Kubernetes API
# 查看 Kubernetes API
kube-apiserver 架构图
访问 API 和查看列表
> kubectl proxy
> curl curl http://127.0.0.1:8001/api/v1/pods
# Go 程序调用 Kubernetes API
Kubernetes 官方提供了 Go 语言的 Client SDK
https://github.com/kubernetes/client-go (opens new window)
# Prometheus 快速入门
- 一是指的 Prometheus 自身,是一个时序数据库
- 一种是指 Prometheus 生态圈,指的是是整体的监控报警的生态圈和解决方案(Prometheus+Grafana+Alertmanager)
# Prometheus 整体流程图
# 安装 Prometheus
tar xvfz prometheus-*.tar.gz
- 就可以看到下述目录
prometheus
├── LICENSE
├── NOTICE
├── console_libraries
├── consoles
├── data
├── prometheus
├── prometheus.yml
├── prometheus.yml.default
├── promtool
├── rules
└── tsdb
- 启动 Prometheus
./prometheus
- 默认监听 9090 端口