K3S安装
curl -sfL https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh | INSTALL_K3S_VERSION=v1.25.3+k3s1 INSTALL_K3S_MIRROR=cn sh -
避坑
最开始我安装的时候没指定INSTALL_K3S_VERSION 版本,默认是stable版本,然后这个stable版本具体是哪个版本忘记看了,总之就是安装完了 kubectl get all总会在最后报一个错。
然后我改成了1.24.1,这个版本更坑,kubectl get nodes里面竟然是空的。
最后选用了这个1.25.3的版本,是当前最新版本了,一切正常。
本文的其他内容都是基于此版本上操作的,如果版本不同,部分配置可能有差异
Ingress-Nginx安装
#1.下载yaml wget https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.5.1/deploy/static/provider/cloud/deploy.yaml #2 因为网络问题,需要单独下载yaml中的两个镜像,最简单的方法是本地通过代理下载下来然后传到国内镜像仓库再拉下来打tag #2.1 下载 docker pull registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20220916-gd32f8c343 docker pull registry.k8s.io/ingress-nginx/controller:v1.5.1 #2.2 上传,可以通过docker命令上传,我是直接打成压缩包然后通过华为云的压缩包上传的 docker save -o D:/webhook.tar registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20220916-gd32f8c343 docker save -o D:/controller.tar registry.k8s.io/ingress-nginx/controller:v1.5.1 #然后在页面中处理上传 #3 下载镜像到k3s服务器并打tag,这是我自己打包的镜像,可以直接用 docker pull swr.cn-north-1.myhuaweicloud.com/rivenpub/kube-webhook-certgen:v20220916-gd32f8c343 docker pull swr.cn-north-1.myhuaweicloud.com/rivenpub/ingress-nginx-controller:v1.5.1 docker tag swr.cn-north-1.myhuaweicloud.com/rivenpub/kube-webhook-certgen:v20220916-gd32f8c343 registry.k8s.io/ingress-nginx/kube-webhook-certgen:v20220916-gd32f8c343 docker tag swr.cn-north-1.myhuaweicloud.com/rivenpub/ingress-nginx-controller:v1.5.1 registry.k8s.io/ingress-nginx/controller:v1.5.1 #4 修改deploy.yaml中的镜像后缀,(image地址中的"@"及后面的字符全部删掉) #5 安装 kubectl apply -f deploy.yaml
查看安装结果
[root@ecs-153442 nginx]# kubectl get all -n ingress-nginx NAME READY STATUS RESTARTS AGE pod/ingress-nginx-admission-patch-z8wrx 0/1 Completed 0 12h pod/ingress-nginx-admission-create-dj8cq 0/1 Completed 0 12h pod/ingress-nginx-controller-6964fbdd77-49dbm 1/1 Running 0 31m NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE service/ingress-nginx-controller LoadBalancer 10.43.57.207 <pending> 80:30302/TCP,443:30074/TCP 12h service/ingress-nginx-controller-admission ClusterIP 10.43.15.241 <none> 443/TCP 12h NAME READY UP-TO-DATE AVAILABLE AGE deployment.apps/ingress-nginx-controller 1/1 1 1 12h NAME DESIRED CURRENT READY AGE replicaset.apps/ingress-nginx-controller-6964fbdd77 1 1 1 31m replicaset.apps/ingress-nginx-controller-6944ff8885 0 0 0 12h NAME COMPLETIONS DURATION AGE job.batch/ingress-nginx-admission-patch 1/1 5s 12h job.batch/ingress-nginx-admission-create 1/1 5s 12h
Ingress-Nginx使用
直接替换调ingress yaml文件中的className为nginx即可,类似于下面
apiVersion: networking.k8s.io/v1 kind: Ingress metadata: name: netcoretest spec: ingressClassName: nginx #这里写成nginx rules: - host: netcoretest.zhangsanfeng.com http: paths: - pathType: Prefix path: / backend: service: name: netcoretest #指定你创建的svc的名称 port: number: 5000
替换ingress默认的80端口
这是我安装nginx的主要原因,因为k3s所在服务器不支持80端口直接访问,所以要改掉
输入命令
kubectl edit deploy ingress-nginx-controller -n ingress-nginx
在containers的args中追加一行
- --http-port=8080
改完后类似下面这样
template:
metadata:
creationTimestamp: null
labels:
app.kubernetes.io/component: controller
app.kubernetes.io/instance: ingress-nginx
app.kubernetes.io/name: ingress-nginx
spec:
containers:
- args:
- /nginx-ingress-controller
- --publish-service=$(POD_NAMESPACE)/ingress-nginx-controller
- --election-id=ingress-nginx-leader
- --controller-class=k8s.io/ingress-nginx
- --ingress-class=nginx
- --configmap=$(POD_NAMESPACE)/ingress-nginx-controller
- --validating-webhook=:8443
- --validating-webhook-certificate=/usr/local/certificates/cert
- --validating-webhook-key=/usr/local/certificates/key
- --http-port=8080 #这一行是追加的
然后等待k8s自己更新就好了
查看效果可以通过访问我们之前的服务来测试
比如我上面建的哪个ingress原访问地址是 http://netcoretest.zhangsanfeng.com
那么现在的访问地址就是 http://netcoretest.zhangsanfeng.com:8080
K3S使用Docker
方法一:安装时指定参数
#1.在安装时通过环境变量INSTALL_K3S_EXEC curl -sfL https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh | INSTALL_K3S_VERSION=v1.25.3+k3s1 INSTALL_K3S_EXEC="server --docker" INSTALL_K3S_MIRROR=cn sh - #2.或者直接传参 curl -sfL https://rancher-mirror.oss-cn-beijing.aliyuncs.com/k3s/k3s-install.sh | sh -s - server --docker
方法二:安装后修改配置文件
#1.打开文件 vim /etc/systemd/system/multi-user.target.wants/k3s.service #2.修改ExecStart那几行的值为: /usr/local/bin/k3s server --docker --no-deploy traefik #3.重启服务 systemctl daemon-reload service k3s restart
kubectl 客户端连接K3S
将k3s主机点上的 /etc/rancher/k3s/k3s.yaml 文件拷贝到 kubectl所在机器的 ~/.kube/config
需要注意修改 yaml文件中的ip地址为 kubectl客户端可以访问到的地址