原创

k8s 安装笔记

温馨提示:
本文最后更新于 2024年06月10日 ,已超过 313 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

官方帮助和安装文档

Kubernetes 文档

一、准备工作(1.24以后的版本,不需要安装docker)

修改内核参数


cat <<EOF > /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward=1
vm.max_map_count=262144
EOF
modprobe br_netfilter 
sysctl -p /etc/sysctl.d/k8s.conf

基础的修改(所有的机器)

# 禁用交换分区(在旧版的 k8s 中 kubelet 都要求关闭 swapoff ,但最新版的 kubelet 其实已经支持 swap ,因此这一步其实可以不做。)
swapoff -a
# 永久禁用,打开/etc/fstab注释掉swap那一行。
sudo vim /etc/fstab

# 修改内核参数(首先确认你的系统已经加载了 br_netfilter 模块,默认是没有该模块的,需要你先安装 bridge-utils)
install -y bridge-utils
modprobe br_netfilter
lsmod | grep br_netfilter

# 更新到最新版本
yum update

时间同步

# 安装时间同步插件
rpm -ivh https://mirrors.wlnmp.com/centos/wlnmp-release-centos.noarch.rpm
yum install ntpdate -y

配置主机上的host

cat >> /etc/hosts <<'EOF'
172.30.10.201 master01
172.30.10.202 node01
172.30.10.204 node02
EOF

更换阿里云的镜像地址

curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
sed -i '/aliyuncs/d' /etc/yum.repos.d/*.repo
yum clean all && yum makecache fast

安装需要的容器(所有的机器)

yum -y install containerd 
systemctl start containerd
systemctl enable containerd

# 修改配置文件
containerd config default>/etc/containerd/config.toml
vi /etc/containerd/config.toml
# 1.修改SystemdCgroup参数为true,默认为false

[plugins."io.containerd.grpc.v1.cri".containerd.runtimes.runc.options]
SystemdCgroup=true

# 2.修改sandbox_image参数,其值一定要和
kubeadm config images list
# 命令输出的pause版本和tag保持一致,否则Node节点的Pod一直CrashLoopBackOff而且查询 kubectl logs 时也没有任何错误,此处我的版本为阿里云3.6

sandbox_image="registry.aliyuncs.com/google_containers/pause:3.9"

[plugins]
    [plugins."io.containerd.grpc.v1.cri".registry]
      ...
      [plugins."io.containerd.grpc.v1.cri".registry.mirrors]
      [plugins."io.containerd.grpc.v1.cri".registry.mirrors."docker.io"]
        endpoint = ["https://85so2jpo.mirror.aliyuncs.com"]
      [plugins."io.containerd.grpc.v1.cri".registry.mirrors."k8s.gcr.io"]
        endpoint = ["registry.aliyuncs.com/google_containers"]

首先安装的是kubeadm工具(更换了阿里云的yum源)(所有的机器)

cat <<EOF > /etc/yum.repos.d/kubernetes.repo
[kubernetes]
name=kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=0
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

#setenforce 0
#yum install -y kubelet kubeadm kubectl
#systemctl enable kubelet && systemctl start kubelet
# sudo yum install -y kubelet kubeadm kubectl --disableexcludes=kubernetes
# 将 SELinux 设置为 permissive 模式(相当于将其禁用)
# yum --showduplicates list available kernel-devel
# yum remove kubelet kubeadm kubectl
sudo setenforce 0
sudo sed -i 's/^SELINUX=enforcing$/SELINUX=permissive/' /etc/selinux/config
# sudo yum install -y kubelet-1.23.17-0 kubeadm-1.23.17-0 kubectl-1.23.17-0 --disableexcludes=kubernetes # 1.24开始k8s取消了docker的默认,使用了 containerd 
yum --enablerepo=kubernetes install kubeadm kubectl kubelet # 1.24开始k8s取消了docker的默认,使用了 containerd 
sudo systemctl enable --now kubelet

配置containerd 配置接口 配置容器ENDPOINT并生效(所有的机器)

crictl config runtime-endpoint unix:///run/containerd/containerd.sock
crictl config image-endpoint unix:///run/containerd/containerd.sock
systemctl restart containerd

配置命令补全(后面可以在执行kubelet)(所有的机器)

# 操作节点: k8s-master
yum install bash-completion -y
source /usr/share/bash-completion/bash_completion
source <(kubectl completion bash)
echo "source <(kubectl completion bash)">> ~/.bashrc

$$创建一个主节点 (参考命令,也可以使用官方的步骤)

# 可以拆分之行
kubeadm config images pull --v=5 --image-repository registry.aliyuncs.com/google_containers
kubeadm init  \
--apiserver-advertise-address=172.30.10.201  \
--image-repository registry.aliyuncs.com/google_containers  \
--kubernetes-version v1.27.3  \
--service-cidr=10.1.0.0/16  \
--pod-network-cidr=10.2.0.0/16  \
--v=5


## 成功后的内容
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 172.30.10.201:6443 --token eiwjrz.zqrcuzuvxzg8e1gx \
        --discovery-token-ca-cert-hash sha256:dc4ebcad2a3313067891ec9c109e9072a7d82dde8dabe132b33b4399263ecbfe

安装网络插件,只需要主节点安装即可

GitHub - cloudzun/K8SKB
参考了02章节里面的配置文件,其他互联网找到的资料都是不可用的。

# kubectl apply -f https://raw.githubusercontent.com/projectcalico/calico/v3.26.1/manifests/calicoctl-etcd.yaml
# 查看所有的pod 
kubectl get pods --all-namespaces
# 查看所有的节点
kubectl get nodes -o wide
kubectl get pod -A -o wide | grep calico
# 修改容器的数量
kubectl patch deployments.apps -n devops redis -p '{"spec":{"replicas":1}}'

kubectl logs coredns-7d89d9b6b8-ddjjp -n kube-system
kubectl config set-context $(kubectl config current-context) --namespace=kube-system
kubectl config set-context $(kubectl config current-context) --namespace=defalut
# 暴露接口并创建一个可以访问的服务。
kubectl expose deployment -n devops redis --port=6379 --target-port=6379 --type=ClusterIP
# 设置私有化的拉取验证
kubectl create secret docker-registry docker_reg_secret --docker-server=XXX --docker-username=XXX --docker-password=XXX
kubectl create secret docker-registry registry-secret --docker-server=registry-vpc.cn-shenzhen.aliyuncs.com --docker-username=xxx@xxx.com --docker-password=xxxxx --docker-email=xxx@xxx.com -n default

创建 ServiceAccount 拉取私有镜像

apiVersion: v1
imagePullSecrets:
- name: registry-secret
kind: ServiceAccount
metadata:
  name: cloud-service-account
  namespace: devops
secrets:
- name: registry-secret

Calico插件

calico 插件的安装(必须成功安装,否则coredns服务无法启动)

参考地址
Quickstart for Calico on Kubernetes
使用kubeadm安装K8s集群-主节点

配置pod外网访问

# 参考配置(可以配置 ingress-nginx、k8s-gateway )也可以配置外围的Nginx 访问集群的service nodeport节点
https://juejin.cn/post/7209262585006309433#heading-2

k8s从入门到实战(八):k8s通过service访问pod - 掘金

部署Java应用

apiVersion: apps/v1
kind: Deployment
metadata:
  creationTimestamp: null
  labels:
    app: cloud-demo
  name: cloud-demo
spec:
  replicas: 1
  selector:
    matchLabels:
      app: cloud-demo
  strategy: {}
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: cloud-demo
    spec:
      containers:
      - image: aliyun.com/cloud-demo
        name: cloud-demo
        resources: {}
            serviceAccountName: xx
status: {}
正文到此结束
本文目录