k8s学习笔记(一)跑通k8s再说

网络上教程很多是旧版本的镜像,拉取不到。需要按照自己的情况去搭建。本文使用的kubernete版本是1.12.1

一、环境安装

  • ubuntu == 20.04
  • docker == 20.10.7
  • k8s == 1.21.1
    教程如下:https://blog.csdn.net/M82_A1/article/details/95635705/
$ apt-get install -y kubelet=1.21.1-00 kubeadm=1.21.1-00 kubectl=1.21.1-00
# 卸载方法
$ kubeadm reset 
$ apt-get remove kubelet kubeadm kubectl 

禁用swap:参考 https://zhuanlan.zhihu.com/p/138554103

$ sudo swapoff -a
$ sudo vim /etc/fstab #修改/etc/fstab,注释掉swap那行,持久化生效

注册aliyun账号并且,docker login: 参考: https://help.aliyun.com/document_detail/60743.html
拉取相关的image,其中有一个bug,需要重新打tag:

# 查询需要下载的镜像
$ kubeadm config images list --config kubeadm.yaml
# 查询已经有的镜像
$ docker images
# 注意coredns会出现问题
$ docker pull registry.aliyuncs.com/google_containers/coredns:1.8.0
# 重命名
$ docker tag registry.aliyuncs.com/google_containers/coredns:1.8.0 registry.aliyuncs.com/google_containers/coredns/coredns:v1.8.0
# 删除原有镜像
$ docker rmi registry.aliyuncs.com/google_containers/coredns:1.8.0

二、k8s集群组建

目的:一个master node,一个worker node.
执行命令:

$ kubeadm init --image-repository registry.aliyuncs.com/google_containers --pod-network-cidr=10.244.0.0/16 --apiserver-advertise-address=10.112.122.9 

其中10.112.122.9是master node host ip,执行结果:

Your Kubernetes control-plane has initialized successfully!

To start using your cluster, you need to run the following as a regular user:

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config

Alternatively, if you are the root user, you can run:

  export KUBECONFIG=/etc/kubernetes/admin.conf

You should now deploy a pod network to the cluster.
Run "kubectl apply -f [podnetwork].yaml" with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/

Then you can join any number of worker nodes by running the following on each as root:

kubeadm join 10.112.122.9:6443 --token u4nw4p.3fxtty1sbpkyi6n0 \
        --discovery-token-ca-cert-hash sha256:c1ea97710f4e5ed7722b0e293ca9f7f2db66fac1cde17d65c22a724d08c5a1af

完成初始化操作:

$ mkdir -p $HOME/.kube
$ sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
$ sudo chown $(id -u):$(id -g) $HOME/.kube/config

一个误操作和复原方法,记录

$ kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml
$ kubectl delete -f https://docs.projectcalico.org/manifests/calico.yaml

此时网络还没跑起来:

$ kubectl get pods -n kube-system
NAME                                                       READY   STATUS    RESTARTS   AGE
coredns-545d6fc579-lxzt9                                   0/1     Pending   0          2m6s
coredns-545d6fc579-tx6sj                                   0/1     Pending   0          2m6s
etcd-hyongtaonew-precision-tower-5810                      1/1     Running   0          2m21s
kube-apiserver-hyongtaonew-precision-tower-5810            1/1     Running   0          2m16s
kube-controller-manager-hyongtaonew-precision-tower-5810   1/1     Running   0          2m16s
kube-proxy-d5b69                                           1/1     Running   0          2m6s
kube-scheduler-hyongtaonew-precision-tower-5810            1/1     Running   0          2m16s

CNI网络部署

# 这个yaml文件有700多行 不复制了
$ wget https://docs.projectcalico.org/v3.10/manifests/calico.yaml
$ kubectl apply -f calico.yaml

稍等片刻后,coredns等服务都处于running状态了:

$ kubectl get pods -n kube-system
NAME                                                       READY   STATUS    RESTARTS   AGE
calico-kube-controllers-7c5dd46f7d-bm5m5                   1/1     Running   0          77s
calico-node-pl2cr                                          0/1     Running   0          77s
coredns-545d6fc579-lxzt9                                   1/1     Running   0          7m23s
coredns-545d6fc579-tx6sj                                   1/1     Running   0          7m23s
etcd-hyongtaonew-precision-tower-5810                      1/1     Running   0          7m38s
kube-apiserver-hyongtaonew-precision-tower-5810            1/1     Running   0          7m33s
kube-controller-manager-hyongtaonew-precision-tower-5810   1/1     Running   0          7m33s
kube-proxy-d5b69                                           1/1     Running   0          7m23s
kube-scheduler-hyongtaonew-precision-tower-5810            1/1     Running   0          7m33s

构建Worker Node

# 在worker node执行下面的命令
$ kubeadm join 10.112.122.9:6443 --token u4nw4p.3fxtty1sbpkyi6n0         --discovery-token-ca-cert-hash sha256:c1ea97710f4e5ed7722b0e293ca9f7f2db66fac1cde17d65c22a724d08c5a1af
# 在master node执行
$ kubectl get nodes
NAME                               STATUS   ROLES                  AGE   VERSION
amdlattice-precision-tower-5810    Ready                     2m    v1.21.1
hyongtaonew-precision-tower-5810   Ready    control-plane,master   14m   v1.21.1

启动一个Nginx服务

$ kubectl create deployment nginx --image=nginx
deployment.apps/nginx created
$  kubectl expose deployment nginx --port=80 --type=NodePort # 暴露80端口
service/nginx exposed
$ kubectl get pod,svc # 查看对外端口
NAME                         READY   STATUS    RESTARTS   AGE
pod/nginx-6799fc88d8-fzhnv   1/1     Running   0          29s

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)          AGE
service/flaskapp-1   NodePort    10.99.22.77            5000:30005/TCP   3m15s
service/kubernetes   ClusterIP   10.96.0.1              443/TCP          19m
service/nginx        NodePort    10.109.12.14           80:32101/TCP     16s

最后访问http://10.117.169.29:32101/的结果图:
10.117.169.29为worker node的真实ip。

nginx.png

三、几个相关的yaml文件

kubeadm.yaml的内容,没有用到,留作参考:

apiVersion: kubeadm.k8s.io/v1beta2
kind: ClusterConfiguration
controllerManager:
  extraArgs:
    horizontal-pod-autoscaler-use-rest-clients: "true"
    horizontal-pod-autoscaler-sync-period: "10s"
    node-monitor-grace-period: "10s"
apiServer:
  extraArgs:
    runtime-config: "api/all=true"
kubernetesVersion: v1.21.1
imageRepository: registry.aliyuncs.com/google_containers

你可能感兴趣的:(k8s学习笔记(一)跑通k8s再说)