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的版本,是當前最新版本了,一切正常。
本文的其他內容都是基於此版本上操作的,如果版本不同,部分設定可能有差異
#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 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
這是我安裝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
#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
將k3s主機點上的 /etc/rancher/k3s/k3s.yaml 檔案拷貝到 kubectl所在機器的 ~/.kube/config
需要注意修改 yaml檔案中的ip地址為 kubectl使用者端可以存取到的地址