Rancherv2.6.3 部署与 k8s 集群搭建
1. 部署 rancher
启动可能很慢,等两分钟
docker run -itd --restart=unless-stopped -p 8888:80 -p 8443:443 --privileged=true rancher/rancher:v2.6.3
[root@harbor ~]#docker ps -a | grep rancher
e49f56dd82f2 rancher/rancher:v2.6.3 "entrypoint.sh" 13 seconds ago Up 11 seconds 0.0.0.0:8888->80/tcp, :::8888->80/tcp, 0.0.0.0:8443->443/tcp, :::8443->443/tcp happy_beaver
查找引导信息:登录时要用
[root@harbor ~]#docker logs `docker ps -a | grep rancher | awk '{print $1}'` 2>&1 | grep "Bootstrap Password:"
2022/08/23 01:51:29 [INFO] Bootstrap Password: gfh8f2f625sl4vh85lmgrsv7tpvz5vh5dmzl6hg8sv8pnkzs4vf2wr
设置中文
2. 使用 rancher 创建 k8s 集群
点击创建
我们选择自定义
在 "Advanced Options" 中进行高级设置,比如:选择是否启用 Nginx Ingress。是否开启请根据您的需求来定。这个设置项后续可以点击 rancher 界面中此集群的升级按钮进行重新选择。
点击 "Next" 后,进入添加主机命令页面
我们用两台机器做 k8s 机器,一台 master,一台 worker
docker 安装:curl http://49.232.8.65/shell/docker/docker.sh | bash
选择 etcd,Control Plan 两个选项,然后复制下方的命令,到 master节点执行
(我这里将 master 也作为 worker 节点)
选择 Worker 选项,然后复制下方的命令,到 node 节点执行
下面显示注册信息
当集群前面的状态变成 Active 时,就表示集群部署完成,可以点击集群的名称,进入集群详情页。
这个过程耗时和网络相关,需要进行下载镜像等操作。
3. 使用 kubectl 控制集群
(1) 安装 kubectl 工具
cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=1
repo_gpgcheck=1
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF
yum install -y kubectl-1.21.14
(2) 配置 config 文件
集群详情页下载 KubeConfig
在 master 创建 /root/.kube 文件夹,并在其中创建文件 config,并将下载的配置文件中的内容写入 config 文件。然后就可以通过 kubectl 操作集群
[root@master ~]#cd .kube/
[root@master ~/.kube]#ls
cache config
[root@master ~]#kubectl get nodes
NAME STATUS ROLES AGE VERSION
master Ready controlplane,etcd,worker 3h53m v1.21.14
worker Ready worker 3h47m v1.21.14
[root@master ~]#kubectl get pods -A
NAMESPACE NAME READY STATUS RESTARTS AGE
cattle-system cattle-cluster-agent-65975f94c8-kgjhb 1/1 Running 0 3h47m
cattle-system cattle-cluster-agent-65975f94c8-lq9fx 1/1 Running 1 3h51m
cattle-system cattle-node-agent-6zz92 1/1 Running 0 3h51m
cattle-system cattle-node-agent-lfn8r 1/1 Running 0 3h47m
cattle-system kube-api-auth-sh4bc 1/1 Running 0 3h51m
kube-system calico-kube-controllers-6c977d77bc-thhr5 1/1 Running 0 3h52m
kube-system canal-mqt64 2/2 Running 0 3h47m
kube-system canal-ps4sw 2/2 Running 0 3h52m
kube-system coredns-685d6d555d-l5ltn 1/1 Running 0 3h47m
kube-system coredns-685d6d555d-wvrs7 1/1 Running 0 3h52m
kube-system coredns-autoscaler-96789f8f7-xrdkq 1/1 Running 0 3h52m
kube-system metrics-server-7bf4b68b78-v4t28 1/1 Running 0 3h51m
kube-system rke-coredns-addon-deploy-job-g4z44 0/1 Completed 0 3h52m
kube-system rke-metrics-addon-deploy-job-rkwqp 0/1 Completed 0 3h52m
kube-system rke-network-plugin-deploy-job-xdmhh 0/1 Completed 0 3h53m
4. 运行示例服务
kubectl apply -f http://49.232.8.65/yaml/openresty/open_test.yaml
[root@master ~]#kubectl get pods,svc,deploy -o wide
NAME READY STATUS RESTARTS AGE IP NODE NOMINATED NODE READINESS GATES
pod/open-headless-6f87f56c8f-d6l7m 1/1 Running 0 17s 10.42.0.8 master <none> <none>
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE SELECTOR
service/kubernetes ClusterIP 10.43.0.1 <none> 443/TCP 4h40m <none>
service/open-headless ClusterIP None <none> 80/TCP 17s app=open-headless
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
deployment.apps/open-headless 1/1 1 1 17s open-headless openresty:1.21.4.1 app=open-headless