环境介绍
香港轻量 1核1G,此配置无法开启 ingress-controller
,请禁用 tls
,和ingress-controller
即无法使用 ApisixTls 配置证书
设置虚拟内存
mkdir swap && cd swap
# bs 为块的大小,count 创建多少个块
sudo dd if=/dev/zero of=swapfile bs=1M count=2048
# 修改权限
sudo chmod 0600 swapfile
#把生成的文件转换成 Swap 文件
sudo mkswap swapfile
# 激活文件
sudo swapon swapfile
AI 代码解读
不设置容易死机,这里我们设置
2G
安装 k3s
- 安装目录
mkdir -p ~/softwares/k3s && cd ~/softwares/k3s && touch install_k3s.sh
AI 代码解读
主节点
#!/bin/sh
# 本地网卡设备号
export MY_ETH_DEV=eth0
# 自动获取本机IP
export MY_NODE_IP=`ip -o -4 addr list | grep $MY_ETH_DEV | awk '{print $4}' | cut -d/ -f1`
# k3s主服务器内网IP
# export MY_SERVER_IP=172.21.0.11
export MY_SERVER_IP=`ip -o -4 addr list | grep $MY_ETH_DEV | awk '{print $4}' | cut -d/ -f1`
curl -sfL https://rancher-mirror.rancher.cn/k3s/k3s-install.sh | INSTALL_K3S_MIRROR=cn sh -s - server \
--disable traefik \
--cluster-init \
--node-ip $MY_NODE_IP \
--node-external-ip $MY_SERVER_IP \
--advertise-address $MY_SERVER_IP \
--flannel-iface $MY_ETH_DEV \
--write-kubeconfig ~/.kube/config \
--write-kubeconfig-mode 644 \
--node-label asrole=worker
AI 代码解读
- 可选参数
--disable traefik
禁用默认网关 - 可选参数
--cluster-init
初始化集群,为平行节点做准备 - 可选参数
--write-kubeconfig ~/.kube/config
配置路径
查看服务:cat /etc/systemd/system/k3s.service
参数请查看 https://rancher.com/docs/k3s/latest/en/installation/install-options/server-config/
基础命令
# 查看所有节点
kubectl get node
# 查看所有应用
kubectl get pod -A -o wide
# 查看所有服务
kubectl get service -A
# 查看所有入口
kubectl get ingress -A
# 查看节点运行的容器
crictl ps
AI 代码解读
安装 helm
脚本安装
mkdir -p $HOME/softwares/heml_tool && cd $HOME/softwares/heml_tool
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3
bash get_helm.sh
AI 代码解读
二进制安装
- 下载 需要的版本
- 上传
scp helm-v3.9.0-linux-amd64.tar.gz root@127.0.0.1:/tmp/helm-v3.9.0-linux-amd64.tar.gz
- 解压(tar -zxvf helm-v3.9.0-linux-amd64.tar.gz )
- 在解压目中找到
helm
程序,移动到需要的目录中(mv linux-amd64/helm /usr/local/bin/helm
)
查看结果
helm repo list
安装 apisix
安装主程序
mkdir -p $HOME/softwares/apisix_gateway && cd $HOME/softwares/apisix_gateway
helm repo add apisix https://charts.apiseven.com
helm repo add bitnami https://charts.bitnami.com/bitnami
helm repo update
kubectl create ns ingress-apisix
helm install apisix apisix/apisix \
--set gateway.type=LoadBalancer \
--set gateway.tls.enabled=true \
--set ingress-controller.enabled=true \
--set ingress-controller.config.apisix.serviceNamespace=ingress-apisix \
--namespace ingress-apisix \
--kubeconfig /etc/rancher/k3s/k3s.yaml
AI 代码解读
--set gateway.type=LoadBalancer
设置端口类型- 可选参数
--set gateway.tls.enabled=true
激活tls
- 可选参数
--set ingress-controller.enabled=true
激活ingress-controller
- 可选参数
--set ingress-controller.config.apisix.serviceNamespace=ingress-apisix
配置ingress-controller
查看资源
kubectl -n ingress-apisix get pods
安装面板
helm repo add apisix https://charts.apiseven.com
helm repo update
helm install apisix-dashboard apisix/apisix-dashboard --namespace ingress-apisix
AI 代码解读
- 编辑服务
kubectl --namespace ingress-apisix edit service apisix-dashboard
AI 代码解读
- 修改如下
ports:
- name: http
port: 9000
protocol: TCP
targetPort: http
selector:
app.kubernetes.io/instance: apisix-dashboard
app.kubernetes.io/name: apisix-dashboard
sessionAffinity: None
type: LoadBalancer
AI 代码解读
- 修改用户名密码
kubectl -n ingress-apisix edit cm apisix-dashboard
AI 代码解读
- 配置如下
authentication:
secert: secert
expire_time: 3600
users:
- username: admin
password: admin
AI 代码解读
- 重启生效
kubectl get pods -l app.kubernetes.io/name=apisix-dashboard -o name -n ingress-apisix -o yaml | kubectl replace --force -f -
AI 代码解读
本地证书
- eg:
www.baidu.com
- 目录如下
├── certs
│ ├── www.baidu.com.key
│ └── www.baidu.com.pem
├── create-tls-secret.sh
└── tls-www.baidu.com.yaml
1 directory, 4 files
AI 代码解读
- 脚本
cat > create-tls-secret.sh <<-EOF
#!/usr/bin/env bash
kubectl create secret tls tls-www.baidu.com -n ingress-apisix \
--cert=certs/www.baidu.com.pem \
--key=certs/www.baidu.com.key
EOF
bash create-tls-secret.sh
AI 代码解读
- 查看
kubectl get secret -n ingress-apisix
AI 代码解读
- 创建
tls-www.baidu.com.yaml
# 定义 ApisixTls 对象
apiVersion: apisix.apache.org/v1
kind: ApisixTls
metadata:
name: tls-www.baidu.com
namespace: ingress-apisix
spec:
hosts:
- www.baidu.com
secret:
name: tls-www.baidu.com
namespace: ingress-apisix
AI 代码解读
k3s文档 https://docs.rancher.cn/docs/k3s/quick-start/_index/
apisix文档 https://apisix.apache.org/zh/docs/ingress-controller/getting-started/
设置虚拟内存 https://blog.csdn.net/ChenZhC/article/details/118603442