首页 > 系统相关 >Centos7 yum安装k8s 1.23.0

Centos7 yum安装k8s 1.23.0

时间:2023-04-21 16:13:48浏览次数:41  
标签:kube name kubernetes Centos7 etc yum k8s cni flannel

本次部署有3个节点,一个master,2个node。其中maser是192.168.18.11,node分别是192.168.18.12、192.168.18.12。

 

一、在master:192.168.18.11上

1、前提条件安装docker,并修改/etc/docker/daemon.json

{
  "registry-mirrors": ["https://qtlj897j.mirror.aliyuncs.com"],   #添加的
  "insecure-registries": [
    "http://192.168.18.11"
  ],
  "exec-opts": ["native.cgroupdriver=systemd"]  #添加的
}

2、关闭selinux

setenforce 0
sed -i 's/SELINUX=permissive/SELINUX=disabled/' /etc/sysconfig/selinux
sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config

3、配置内核参数

vim /etc/sysctl.d/k8s.conf

net.ipv4.ip_forward=1 # 其值为0,说明禁止进行IP转发;如果是1,则说明IP转发功能已经打开。 net.bridge.bridge-nf-call-ip6tables = 1 # 是否在ip6tables链中过滤IPv6包   net.bridge.bridge-nf-call-iptables = 1     # 二层的网桥在转发包时也会被iptables的FORWARD规则所过滤,这样有时会出现L3层的iptables rules去过滤L2的帧的问题
sysctl -p

4、修改hostname,hosts文件

192.168.18.11 tjgjc-ai11
192.168.18.12 tjgjc-ai12
192.168.18.13 tjgjc-ai13

5、禁用交换分区,否则会在k8s初始化时报错,[kubelet-check] The HTTP call equal to 'curl -sSL http://localhost:10248/healthz' failed with error: Get "http://localhost:10248/healthz": dial tcp [::1]:10248: connect: connection refused.

# 禁用交换分区
swapoff -a
# 永久禁用,打开/etc/fstab注释掉swap那一行。
sed -i 's/.*swap.*/#&/' /etc/fstab

 

6、配置k8s阿里云源

vim /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

7、安装kubeadm、kubectl、kubelet

yum install -y kubectl-1.23.0-0 kubeadm-1.23.0-0 kubelet-1.23.0-0

8、启动kubelet服务

systemctl enable kubelet && systemctl start kubelet

9、初始化k8s,这条命令执行时会卡在[preflight] You can also perform this action in beforehand using ''kubeadm config images pull,大概需要2分钟,请耐心等待。

kubeadm init --image-repository registry.aliyuncs.com/google_containers --kubernetes-version v1.23.0 --apiserver-advertise-address 192.168.18.11 --pod-network-cidr=10.244.0.0/16 --token-ttl 0

 

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 192.168.18.11:6443 --token xybjgk.dnutwxyfpdxnratp \
    --discovery-token-ca-cert-hash sha256:21d14e3ebe35ca81caef9653270c5c5a76d841ed4ec18393b3fbdc4fc1deff09

10、看到上面,初始化成功了,上面kubeadm init执行成功后会返回给你node节点加入集群的命令,等会要在node节点上执行,需要保存下来,如果忘记了,可以使用如下命令获取。

kubeadm token create --print-join-command

 11、根据提示: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

 

二、在node节点192.168.18.11、192.168.18.11上

1、前提条件安装docker

2、做maser的2~8步骤的操作

3、加入集群,运行在master初始化k8s时获得的加入集群的命令

kubeadm join 192.168.18.11:6443 --token xybjgk.dnutwxyfpdxnratp \
    --discovery-token-ca-cert-hash sha256:21d14e3ebe35ca81caef9653270c5c5a76d841ed4ec18393b3fbdc4fc1deff09

 

三、在master上安装flannel网络插件

1、从网上找到flannel配置文件,注意配置文件中的"Network"字段要和初始化k8s命令的参数--pod-network-cidr=10.244.0.0/16指定的网段一致。

---
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
  name: psp.flannel.unprivileged
  annotations:
    seccomp.security.alpha.kubernetes.io/allowedProfileNames: docker/default
    seccomp.security.alpha.kubernetes.io/defaultProfileName: docker/default
    apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
    apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
spec:
  privileged: false
  volumes:
  - configMap
  - secret
  - emptyDir
  - hostPath
  allowedHostPaths:
  - pathPrefix: "/etc/cni/net.d"
  - pathPrefix: "/etc/kube-flannel"
  - pathPrefix: "/run/flannel"
  readOnlyRootFilesystem: false
  runAsUser:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny
  allowPrivilegeEscalation: false
  defaultAllowPrivilegeEscalation: false
  allowedCapabilities: ['NET_ADMIN', 'NET_RAW']
  defaultAddCapabilities: []
  requiredDropCapabilities: []
  hostPID: false
  hostIPC: false
  hostNetwork: true
  hostPorts:
  - min: 0
    max: 65535
  seLinux:
    rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
rules:
- apiGroups: ['extensions']
  resources: ['podsecuritypolicies']
  verbs: ['use']
  resourceNames: ['psp.flannel.unprivileged']
- apiGroups:
  - ""
  resources:
  - pods
  verbs:
  - get
- apiGroups:
  - ""
  resources:
  - nodes
  verbs:
  - list
  - watch
- apiGroups:
  - ""
  resources:
  - nodes/status
  verbs:
  - patch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: flannel
roleRef:
  apiGroup: rbac.authorization.k8s.io
  kind: ClusterRole
  name: flannel
subjects:
- kind: ServiceAccount
  name: flannel
  namespace: kube-system
---
apiVersion: v1
kind: ServiceAccount
metadata:
  name: flannel
  namespace: kube-system
---
kind: ConfigMap
apiVersion: v1
metadata:
  name: kube-flannel-cfg
  namespace: kube-system
  labels:
    tier: node
    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-system
  labels:
    tier: node
    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: rancher/mirrored-flannelcni-flannel-cni-plugin:v1.1.0
        command:
        - cp
        args:
        - -f
        - /flannel
        - /opt/cni/bin/flannel
        volumeMounts:
        - name: cni-plugin
          mountPath: /opt/cni/bin
      - name: install-cni
        image: rancher/mirrored-flannelcni-flannel:v0.18.1
        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: rancher/mirrored-flannelcni-flannel:v0.18.1
        command:
        - /opt/bin/flanneld
        args:
        - --ip-masq
        - --kube-subnet-mgr
        resources:
          requests:
            cpu: "100m"
            memory: "50Mi"
          limits:
            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

2、查看k8s集群的状态,Ready说明成功了

 

  

 

 

 

标签:kube,name,kubernetes,Centos7,etc,yum,k8s,cni,flannel
From: https://www.cnblogs.com/lfxx/p/17334177.html

相关文章

  • yum常用命令
    一、查看源 [root@hlccfs_rpms]#yumrepolist仓库标识仓库名称hl95hl95yumks10-adv-osKylinLinuxAdvancedServer10-Osk......
  • k8s将pod指定到某个节点
    1、查看节点标签kubectlgetnodes--show-labels2、给节点打标签kubectllabelnode10.64.39.219node=bmdkubectllabelnode10.64.39.186node=bmd3、指定程序的运行pod[root@apiserverk8s]#catselec.ymlapiVersion:extensions/v1beta1kind:Deployment......
  • k8s 1.23部署ingress
    1.安装部署ingress-nginx前言:坑比较多,pod启动running成功也并不代表ingress就能使用,需要进去pod内查看logs还有pod的描述也要看看是否有报错。文件地址:https://raw.githubusercontent.com/kubernetes/ingress-nginx/controller-v1.1.1/deploy/static/provider/cloud/deploy.y......
  • CentOS7.9 安装 RabbitMQ
    官方安装文档https://www.rabbitmq.com/install-rpm.html一、rpm包下载1.下载erlanghttps://packagecloud.io/rabbitmq/erlang/packages/el/7/erlang-23.3.4.11-1.el7.x86_64.rpm?distro_version_id=140wget--content-disposition"https://packagecloud.io/rabbitmq/erl......
  • Centos7 mysql 5.7 安装
    一.卸载原有数据库1.通过命令rpm-qa|grepmysql,rpm-qa|grep mariadb 查看原有数据库[root@ganluren-wketc]#rpm-qa|grepmysql[root@ganluren-wketc]#rpm-qa|grep mariadb2.[root@ganluren-wketc]#rpm-e--nodepsmariadb-libs-5.5.68-1.el7.x86_64卸载......
  • Linux——yum&rpm
    RPMRMP是LINUX下的一种软件的可执行程序,你只要安装它就可以了。这种软件安装包通常是一个RPM包(RedhatLinuxPacketManager,就是Redhat的包管理器),后缀是.rpm。RPM是RedHat公司随RedhatLinux推出了一个软件包管理器,通过它能够更加轻松容易地实现软件的安装。 1.安装软件:......
  • k8s编写cronjob
    1.k8s编写cronjob的知识点:通过sed对cronjob的yaml文件中的image属性进行替换:seds/old/new/gfile当old和new均为变量的时候,书写方式如下:sed-i's#'''$old_image'''#'''$new_image'''#g'quota-cronjob.yaml 2.cron......
  • CentOS7 升级openssl版本(升级至3.0.3)
    首先查看本地openssl版本opensslversion创建一个放源码的目录(按自己习惯)#这里是我的习惯cd/usr/local/src#去官网获取你想要版本的tar压缩包如果没有wget命令,自行百度wget--no-check-certificatehttps://www.openssl.org/source/old/3.0/openssl-3.0.3.tar......
  • k3s 基础 —— 配置 traefik dashboard & k8s dashboard 域名访问
    配置k8sdashboard域名访问安装略①kubectl代理(只支持localhost或http):kubectlproxy--address='0.0.0.0'--accept-hosts='^*$'②若k8s在虚拟机中部署,局域网中临时访问,可使用端口转发:kubectlport-forward-nkubernetes-dashboardservice/kubernetes-dashboard......
  • CentOS7使用systemctl添加自定义服务
    一、简介Centos7开机第一个程序从init完全换成了systemd这种启动方式,同centos56已经是实质差别。systemd是靠管理unit的方式来控制开机服务,开机级别等功能。在/usr/lib/systemd/system目录下包含了各种unit文件,有service后缀的服务unit,有target后缀的开机级别unit等,这里介绍关......