k8s环境搭建(一主两从)

1. 环境准备

kubeadm是一个kubernetes官方提供的快速安装和初始化拥有最佳实践(best practice)的kubernetes集群的工具,提供kubeadm init和kubeadm join,用于快速部署Kubernetes集群。
https://kubernetes.io/docs/reference/setup-tools/kubeadm/kubeadm/

作用 ip 操作系统 配置 组件
master 10.0.0.80 centos7.9 2c2g docker/kubeadm/kubelet/kubectl
node1 10.0.0.81 centos7.9 2c2g docker/kubeadm/kubelet/kubectl
node2 10.0.0.82 centos7.9 2c2g docker/kubeadm/kubelet/kubectl

1.1 关闭防火墙

systemctl stop firewalld
systemctl disable firewalld

1.2. 关闭selinue

sed -i 's/enforcing/disabled/' /etc/selinux/config
setenforce 0

1.3. 关闭swap

swapoff -a # 临时关闭
sed -ri 's/.*swap.*/#&/' /etc/fstab # 永久关闭

# 编辑分区配置文件/etc/fstab,注释掉swap分区一行
# 注意修改完毕之后需要重启linux服务

#/dev/mapper/centos##-swap swap                    swap    defaults        0 0

1.4.添加主机名与IP对应关系

cat >> /etc/hosts << EOF
10.0.0.81 master
10.0.0.82 node1
10.0.0.83 node2
EOF

1.5 修改操作系统参数

# 修改linux的内核参数,添加网桥过滤和地址转发功能
# 编辑/etc/sysctl.d/kubernetes.conf文件,添加如下配置:
cat >> /etc/sysctl.d/kubernetes.conf << EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

# 重新加载配置
sysctl -p

# 加载网桥过滤模块
modprobe br_netfilter

# 查看网桥过滤模块是否加载成功
lsmod | grep br_netfilter

1.6 时间同步

yum install ntpdate -y
ntpdate time.windows.com

2. 安装docker

## 下载阿里源repo文件
yum install -y wget && wget https://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo -O /etc/yum.repos.d/docker-ce.repo && wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
## yum安装
yum -y install docker-ce-18.06.1.ce-3.el7
## 查看源中可用版本
yum list docker-ce --showduplicates | sort -r

## 配置源加速
## https://cr.console.aliyun.com/cn-hangzhou/instances/mirrors
mkdir -p /etc/docker
cat >> /etc/docker/daemon.json << EOF
{
  "registry-mirrors" : [
    "https://wi17kahy.mirror.aliyuncs.com"
  ]
}
EOF


## 设置开机自启
systemctl enable docker
systemctl daemon-reload

## 启动docker
systemctl start docker 

## 查看docker信息
docker info

3. 安装kubernetes组件

# 由于kubernetes的镜像源在国外,速度比较慢,这里切换成国内的镜像源
cat > /etc/yum.repos.d/kubernetes.repo << EOF
[kubernetes]
name=Kubernetes
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repogpgcheck=0
gpgkey=https://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg https://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

#安装kubeadm,kubelet和kubectl
#kubelet:运行在集群所有节点上,用于启动Pod和容器等对象的工具
#kubeadm:用于初始化集群,启动集群的命令工具
#kubectl:用于和集群通信的命令行,通过kubectl可以部署和管理应用,查看各种资源,创建、删除和更新各种组件
yum install -y kubelet-1.18.0 kubeadm-1.18.0 kubectl-1.18.0
#添加到开机自启
#注意:kubelet加入开机启动之后不手动启动,要不然会报错,初始化集群之后集群会自动启动kubelet服务
systemctl enable kubelet && systemctl start kubelet

4. 部署Kubernetes Master

kubeadm init \
  --apiserver-advertise-address=10.0.0.81 \
  --image-repository registry.aliyuncs.com/google_containers \
  --kubernetes-version v1.18.0 \
  --service-cidr=10.96.0.0/12 \
  --pod-network-cidr=10.244.0.0/16

出现:Your Kubernetes control-plane has initialized successfully!为初始化完成
参数解析:

  • apiserver-advertise-address 指明用 Master 的哪个 interface 与 Cluster 的其他节点通信。
  • image-repository 由于默认拉取镜像地址k8s.gcr.io国内无法访问,这里指定阿里云镜像仓库地址
  • kubernetes-version K8s版本,与上面安装的一致
  • service-cidr 集群内部虚拟网络,Pod统一访问入口
  • pod-network-cidr Pod网络,与下面部署的CNI网络组件yaml中保持一致
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
# 通过以下命令可以看到节点信息
# 集群处于NotReady状态是因为没有安装网络组件
kubectl get nodes

image

5. 创建永久token

由于默认生成的token只有24小时有效期,避免到期后不可用,提前创建永久token
本命令为创建token, --ttl 0 表示永不过期
命令执行后会输出最后一行e59mta.4d1tsekp5k0g2ack就是生成的token
image

#查看token
kubeadm token list
#创建永久token
kubeadm token create --ttl 0
#有了 Token 还不够,Worker 节点加入命令中还需要 --discovery-token-ca-cert-hash CA 证书 Hash 值:
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'

6.加入Kubernetes Node

在node执行
kubeadm join --token :6443 --discovery-token-ca-cert-hash sha256:

  • 将上面得到的token值替换如下
  • 将上面得到的hash值替换如下
  • 将master节点ip值替换如下
kubeadm join 10.0.0.81:6443 --token e59mta.4d1tsekp5k0g2ack --discovery-token-ca-cert-hash sha256:a96eb5c71056e5a8c54545c34af6fa866b3dbde86112a4afe5e4d97c41f4f3bb

image
master:
image

7. 安装网络插件Calico

https://blog.csdn.net/inthat/article/details/103921026
执行以下命令,下载yaml文件
calico下载地址:https://docs.projectcalico.org/v3.15/manifests/calico.yaml
flannel下载地址:https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

wget https://docs.projectcalico.org/v3.15/manifests/calico.yaml
#安装calico
kubectl apply -f calico.yaml

kube-flannel.yml

---
kind: Namespace
apiVersion: v1
metadata:
  name: kube-flannel
  labels:
    k8s-app: flannel
    pod-security.kubernetes.io/enforce: privileged
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  labels:
    k8s-app: flannel
  name: flannel
rules:
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - get
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes/status
  verbs:
  - patch
- apiGroups:
  - networking.k8s.io
  resources:
  - clustercidrs
  verbs:
  - list
  - watch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  labels:
    k8s-app: flannel
  name: flannel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: flannel
subjects:
- kind: ServiceAccount
  name: flannel
  namespace: kube-flannel
---
apiVersion: v1
kind: ServiceAccount
metadata:
  labels:
    k8s-app: flannel
  name: flannel
  namespace: kube-flannel
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-flannel
  labels:
    tier: node
    k8s-app: flannel
    app: flannel
data:
  cni-conf.json: |
    {
      "name": "cbr0",
      "cniVersion": "0.3.1",
      "plugins": [
        {
          "type": "flannel",
          "delegate": {
            "hairpinMode": true,
            "isDefaultGateway": true
          }
        },
        {
          "type": "portmap",
          "capabilities": {
            "portMappings": true
          }
        }
      ]
    }
  net-conf.json: |
    {
      "Network": "10.244.0.0/16",
      "Backend": {
        "Type": "vxlan"
      }
    }
---
apiVersion: apps/v1
kind: DaemonSet
metadata:
  name: kube-flannel-ds
  namespace: kube-flannel
  labels:
    tier: node
    app: flannel
    k8s-app: flannel
spec:
  selector:
    matchLabels:
      app: flannel
  template:
    metadata:
      labels:
        tier: node
        app: flannel
    spec:
      affinity:
        nodeAffinity:
          requiredDuringSchedulingIgnoredDuringExecution:
            nodeSelectorTerms:
            - matchExpressions:
              - key: kubernetes.io/os
                operator: In
                values:
                - linux
      hostNetwork: true
      priorityClassName: system-node-critical
      tolerations:
      - operator: Exists
        effect: NoSchedule
      serviceAccountName: flannel
      initContainers:
      - name: install-cni-plugin
        image: docker.io/flannel/flannel-cni-plugin:v1.1.2
       #image: docker.io/rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.2
        command:
        - cp
        args:
        - -f
        - /flannel
        - /opt/cni/bin/flannel
        volumeMounts:
        - name: cni-plugin
          mountPath: /opt/cni/bin
      - name: install-cni
        image: docker.io/flannel/flannel:v0.21.4
       #image: docker.io/rancher/mirrored-flannelcni-flannel:v0.21.4
        command:
        - cp
        args:
        - -f
        - /etc/kube-flannel/cni-conf.json
        - /etc/cni/net.d/10-flannel.conflist
        volumeMounts:
        - name: cni
          mountPath: /etc/cni/net.d
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      containers:
      - name: kube-flannel
        image: docker.io/flannel/flannel:v0.21.4
       #image: docker.io/rancher/mirrored-flannelcni-flannel:v0.21.4
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
        securityContext:
          privileged: false
          capabilities:
            add: ["NET_ADMIN", "NET_RAW"]
        env:
        - name: POD_NAME
          valueFrom:
            fieldRef:
              fieldPath: metadata.name
        - name: POD_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: EVENT_QUEUE_DEPTH
          value: "5000"
        volumeMounts:
        - name: run
          mountPath: /run/flannel
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
        - name: xtables-lock
          mountPath: /run/xtables.lock
      volumes:
      - name: run
        hostPath:
          path: /run/flannel
      - name: cni-plugin
        hostPath:
          path: /opt/cni/bin
      - name: cni
        hostPath:
          path: /etc/cni/net.d
      - name: flannel-cfg
        configMap:
          name: kube-flannel-cfg
      - name: xtables-lock
        hostPath:
          path: /run/xtables.lock
          type: FileOrCreate

8.查看集群状态

#查看node状态
kubectl get nodes
#查看pod转态
kubectl get pods -A

image
image

posted @ 2023-03-23 16:10  XIN-0808  阅读(200)  评论(0编辑  收藏  举报