Kubernetes - 使用Rancher部署K8S集群


Kubernetes - 使用Rancher部署K8S集群

准備Linux主機

首先配置三台節點,均在內網,可以互相通信

Node Address Specs
k8s-1 172.0.102.230 8 GB/4 CPUs/150 GB
k8s-2 172.0.102.231 8 GB/4 CPUs/150 GB
k8s-3 172.0.102.232 8 GB/4 CPUs/150 GB

初始化工作,在每台節點上執行:

sudo apt-get -y update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
sudo apt-get -y update
sudo apt-get -y install docker-ce

安裝管理節點

安裝只需要一條命令,在k8s-1節點上執行:

fix:2020年12月28日,新版本修改了啟動命令,需要加上--privileged參數,否則會報錯ERROR: Rancher must be ran with the --privileged flag when running outside of Kubernetes

sudo docker run -d --restart=unless-stopped --privileged -p 80:80 -p 443:443 rancher/rancher

登錄 Rancher 界面並配置初始設置

您需要先登錄 Rancher,然后再開始使用 Rancher。登錄以后,您需要完成一些一次性的配置。

  1. 打開瀏覽器,輸入主機的 IP 地址:https://<SERVER_IP>

    請使用真實的主機 IP 地址替換 <SERVER_IP>

  2. 首次登錄時,請按照頁面提示設置登錄密碼。

  3. 設置 Rancher Server URL。URL 既可以是一個 IP 地址,也可以是一個主機名稱。請確保您在集群內添加的每個節點都可以連接到這個 URL。如果您使用的是主機名稱,請保證主機名稱可以被節點的 DNS 服務器成功解析。

結果:完成 Rancher 管理員用戶的密碼設置和訪問地址設置。下次使用 Rancher 時,可以輸入 IP 地址或主機地址訪問 Rancher 界面,然后輸入管理員用戶名admin和您設置的密碼登錄 Rancher 界面。

這里我們輸入的是k8s-1的內網地址http://172.0.102.230

設置管理員密碼 設置管理地址

按照提示,我們依次在另外兩個節點上執行:

sudo docker run -d --privileged \
	--restart=unless-stopped \
	--net=host \
	-v /etc/kubernetes:/etc/kubernetes \
	-v /var/run:/var/run rancher/rancher-agent:v2.4.8 \
	--server https://172.0.102.230 \
	--token 987tvlx76d4s58x2md7rwsxjxzspjgrwjgwdn8shlmhc26mxskrknk \
	--ca-checksum 53ff4ee58b36a39bde2ff421736a2946ddd26ea37e2a6ec22f54743e69a9b4b4 \
	--etcd \
	--controlplane \
	--worker

現在我們有一個兩節點的集群了,我們可以在集群管理找到Kubeconfig文件,配置到本地~/.kube/config就可以愉快的開始使用kubectl了。

$ kubectl cluster-info
Kubernetes master is running at https://172.0.102.230/k8s/clusters/c-jzfnr
CoreDNS is running at https://172.0.102.230/k8s/clusters/c-jzfnr/api/v1/namespaces/kube-system/services/kube-dns:dns/proxy

To further debug and diagnose cluster problems, use 'kubectl cluster-info dump'.
$ kubectl get all
NAME                 TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)   AGE
service/kubernetes   ClusterIP   10.43.0.1    <none>        443/TCP   5h10m

部署一個springboot項目

新建一個springboot項目:

$ tree
.
├── deployment.yml
├── Dockerfile
├── hello-k8s.iml
├── HELP.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    └── main
        ├── java
        │   └── com
        │       └── example
        │           └── hellok8s
        │               ├── HelloController.java
        │               └── HelloK8sApplication.java
        └── resources
            ├── application.properties
            ├── static
            └── templates

Dockerfile

FROM openjdk:11

WORKDIR /code
COPY target/hello-k8s-0.0.1-SNAPSHOT.jar .
EXPOSE 8080
CMD ["java", "-jar", "hello-k8s-0.0.1-SNAPSHOT.jar"]

編譯發布鏡像:

./mvnw clean package
docker build -t hirosyu/hello-k8s .
docker push hirosyu/hello-k8s

YAML文件:

apiVersion: apps/v1
kind: Deployment
metadata:
  labels:
    app: hello-k8s
  name: hello-k8s
spec:
  replicas: 1
  selector:
    matchLabels:
      app: hello-k8s
  strategy: {}
  template:
    metadata:
      labels:
        app: hello-k8s
    spec:
      containers:
      - image: hirosyu/hello-k8s
        name: hello-k8s
        resources: {}
---
apiVersion: v1
kind: Service
metadata:
  labels:
    app: hello-k8s
  name: hello-k8s
spec:
  ports:
  - name: 8080-8080
    port: 8080
    protocol: TCP
    targetPort: 8080
  selector:
    app: hello-k8s
  type: ClusterIP

創建pod

kubectl apply -f deployment.yml
# 為了查看效果,我們使用那個kubectl port-forward將端口轉發到本地
kubectl port-forward svc/hello-k8s 8080:8080

現在打開瀏覽器http://127.0.0.1:8080/version可查看到返回結果

{"hostname":"hello-k8s-7bcdb86dfb-zkspl","version":"v0.1"}

代碼可以在 https://github.com/hiro93/hello-k8s 找到

Traefik v2

Træfɪk 是一個為了讓部署微服務更加便捷而誕生的現代HTTP反向代理、負載均衡工具。 它支持多種后台 (Docker, Swarm, Kubernetes, Marathon, Mesos, Consul, Etcd, Zookeeper, BoltDB, Rest API, file…) 來自動化、動態的應用它的配置文件設置。

使用helm安裝,默認的倉庫是v1.7的,這里使用traefik自己的源:

helm repo add traefik https://containous.github.io/traefik-helm-chart
helm repo update
kubectl create ns traefik-v2
helm install --namespace=traefik-v2 traefik traefik/traefik

使用端口轉發查看dashboard:

kubectl port-forward $(kubectl get pods --namespace=traefik-v2 --selector "app.kubernetes.io/name=traefik" --output=name) 9000:9000

打開瀏覽器打開 http://127.0.0.1:9000/dashboard/ 可以看到管理面板

image-20200918182536383

image-20200918182423211


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM