K8S使用kubeadm部署安装master节点node节点和deployment调度器的使用(Ubuntu18.04)

Kubernetes安装(Ubuntu18.04)

Ubuntu18阿里源
–备份/etc/apt/sources.list

cp /etc/apt/sources.list /etc/apt/sources.list.bak

–修改国内源
将/etc/apt/sources.list替换成如下内容

deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse

–更新

sudo apt-get update

一 、docker安装

1、更新系统软件

sudo apt-get update

2、安装docker依赖以及vim net-tools openssh-server

sudo apt-get install -y vim net-tools openssh-server apt-transport-https ca-certificates curl gnupg-agent software-properties-common

3、添加docker官方秘钥

sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

4、验证指纹

sudo apt-key fingerprint 0EBFCD88

5、添加仓库

sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable"

6、更新apt包索引

sudo apt-get update

7、安装对应版本docker

sudo apt-get install -y docker-ce=5:20.10.6~3-0~ubuntu-bionic docker-ce-cli=5:20.10.6~3-0~ubuntu-bionic containerd.io

–安装指定版本(dpkg -l | grep docker 查看其它服务器上的docker版本)
(1)在仓库中列出可用版本

apt-cache madison docker-ce

(2)安装指定版本第二列字符串是版本号,替换下面命令中的VERSION_STRING

sudo apt-get install docker-ce=<VERSION_STRING> docker-ce-cli=<VERSION_STRING> containerd.io

二、安装kubernetes

1、创建配置文件

sudo touch /etc/apt/sources.list.d/kubernetes.list

2、添加写权限

sudo chmod 666 /etc/apt/sources.list.d/kubernetes.list 

3、在上面文件中添加一下内容

deb http://mirrors.ustc.edu.cn/kubernetes/apt kubernetes-xenial main

或者使用

echo "deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" | sudo tee -a /etc/apt/sources.list.d/kubernetes.list

4、更新操作系统源

sudo apt update

会输出签名认证失败
The following signatures couldn’t be verified
because the public key is not available: NO_PUBKEY
6A030B21BA07F4FB

5、记住上面的 NO_PUBKEY后8位,添加认证Key,keys后面跟NO_PUBKEY后8位(BA07F4FB)

gpg --keyserver keyserver.ubuntu.com --recv-keys BA07F4FB

6、输入一下命令,OK说明成功,注意对应NO_PUBKEY后8位

gpg --export --armor BA07F4FB | sudo apt-key add -

7、再次更新系统下载源

sudo apt update

8、禁用防火墙

sudo ufw disable

9、关闭swap交换分区
(1)关闭

sudo swapoff -a

(2)修改配置文件永久关闭

sudo sed -i 's/.*swap.*/#&/' /etc/fstab

10、禁止selinux
(1)安装操控selinux的命令

sudo apt install -y selinux-utils

(2)禁止selinux

setenforce 0

(3)重启操作系统(阿里云可能需要重启实例)

shutdown -r now

(4)查看selinux是否已经关闭

sudo getenforce

11、查看swap交换分区是否关闭

free -m

11、安装kubenetes

sudo apt-get install -y kubelet=1.20.0-00 kubeadm=1.20.0-00 kubectl=1.20.0-00 kubernetes-cni=0.8.7-00

12、设置开机自启

sudo systemctl enable kubelet && systemctl start kubelet

13、重启(阿里云重启实例)

sudo shutdown -r now

14、验证安装,尝试使用kubectl命令

kubectl get nodes

会提示The connection to the server localhost:8080 was refused - did you specify the right host or port?(说明安装成功)

15、查看当前kubernetes版本

kubectl version

三、master节点的初始化

1、显示kubeadm默认初始化文件,打印出来,写入kubeadm-config.conf文件。

kubeadm config print init-defaults > kubeadm-config.conf

2、修改kubeadm-config.conf文件

	advertiseAddress 改为本主节点地址(192.168.x.x)
	imageRepository 修改镜像源(registry.aliyuncs.com/google_containers)
	kubernetesVersion修改为对应版本
	添加子网网络
	networking:
 		dnsDomain: cluster.local
 		podSubnet: 10.244.0.0/16
		serviceSubnet: 10.96.0.0/12
	scheduler: {}

这里的10.244.0.0/16 和 10.96.0.0/12分别是k8s内部pods和services的子网网络,最好使用这个地址,后续flannel网络需要用到。

3、查看一下都需要哪些镜像文件需要拉取

kubeadm config images list --config kubeadm-config.conf

4、拉取镜像

kubeadm config images pull --config ./kubeadm-config.conf

这里可以拉取国内镜像然后改名
5、初始化并且启动

sudo kubeadm init --config ./kubeadm-config.conf

6、保存输出内容,写入文件,node节点加入需要用到

Your Kubernetes master 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
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/clusteradministration/addons/
You can now join any number of machines by running
the following on each node
as root:
 kubeadm join 192.168.x.x:6443 --token
xxxxxx.xxxxxxxxxxxxxx --discovery-token-ca-certhash
sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

7、复制admin.conf文件并更改属主

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

8、验证

kubectl get nodes

此时NotReady
9、下载flannel

wget https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml

下载不了直接复制

---
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
  # Users and groups
  runAsUser:
    rule: RunAsAny
  supplementalGroups:
    rule: RunAsAny
  fsGroup:
    rule: RunAsAny
  # Privilege Escalation
  allowPrivilegeEscalation: false
  defaultAllowPrivilegeEscalation: false
  # Capabilities
  allowedCapabilities: ['NET_ADMIN', 'NET_RAW']
  defaultAddCapabilities: []
  requiredDropCapabilities: []
  # Host namespaces
  hostPID: false
  hostIPC: false
  hostNetwork: true
  hostPorts:
  - min: 0
    max: 65535
  # SELinux
  seLinux:
    # SELinux is unused in CaaSP
    rule: 'RunAsAny'
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
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/v1beta1
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
        image: quay.io/coreos/flannel:v0.13.0-rc2
        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: quay.io/coreos/flannel:v0.13.0-rc2
        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
        volumeMounts:
        - name: run
          mountPath: /run/flannel
        - name: flannel-cfg
          mountPath: /etc/kube-flannel/
      volumes:
      - name: run
        hostPath:
          path: /run/flannel
      - name: cni
        hostPath:
          path: /etc/cni/net.d
      - name: flannel-cfg
        configMap:
          name: kube-flannel-cfg

10、编辑这个文件,确保flannel网络是对的,找到net-conf.json标记
的内容是否正确。

 net-conf.json: |
 {
 "Network": "10.244.0.0/16",
 "Backend": {
 "Type": "vxlan"
 }

这个"10.244.0.0/16"和 ./kubeadm-config.conf中的podsubnet的地址
要一致。

注意:aws需要在安全组配置允许10.244.0.0/16的所有TCP和UDP。

11、应用当前flannel配置文件

kubectl apply -f kube-flannel.yml

12、设置开机自启并启动kubelet

sudo systemctl enable kubelet
sudo systemctl start kubelet

此时是再次kubectl get nodes 会显示master节点Ready

四、node节点(初始化)加入已存在的k8s集群

1、将master节点的/etc/kubernetes/admin.conf文件发送到当前node主机

scp /etc/kubernetes/admin.conf 节点1用户名@192.168.x.x:/home/xxx

2、创建kube配置文件环境(这里的admin.conf使用master传过来的)

mkdir -p $HOME/.kube
sudo cp -i $HOME/admin.conf $HOME/.kube/config(admin.conf是master传过来的)
sudo chown $(id -u):$(id -g) $HOME/.kube/config

3、加入集群

这里用的是kubeadm join指令也就是之前初始化master节点时的最后输出的命令

sudo kubeadm join 192.168.x.x:6443 --token xxxxxxxxxxxxxxx --discovery-token-ca-cert-hash  sha256:xxxxxxxxxxxxxxxxxxxxxx

注意这里使用的hash应该是master主机 kubeadm init成功之后生成的hash码。

例如:

kubeadm join 192.168.64.131:6443 --token xxxxx.xxxxxxxxxx --discovery-token-ca-cert-hash sha256:xxxxxxxxxxxxxxxxxxxxxxxxxxxx

可能出现如下错误(亚马逊云)

error execution phase preflight: couldn't validate the identity of the API Server: Get "https://192.168.xxx.xx:6443/api/v1/namespaces/kube-public/configmaps/cluster-info?timeout=10s": net/http: request canceled while waiting for connection (Client.Timeout exceeded while awaiting headers)
To see the stack trace of this error execute with --v=5 or higher

解决:设置亚马逊云安全组允许所有内网的TCP和UDP

token有效时间为24小时,如超过24小时需要master重新生成token以及hash码,也可生成无限期。

-生成token

kubeadm token create

列如:

	[kubeadm] WARNING: starting in 1.8, tokens expire after 24 hours by default (if you require a non-expiring token use --ttl 0)
xxxxxx.xxxxxxxxxxxx

-生成hash码(hash值一般不会变,生成的和原来一样)

openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'

列如:

0fd95a9bc67a7bf0ef42da968a0d55d92e52898ec37asdfasfdadfa

查看token

kubeadm token list

或使用一下命令从新生成并输出token

kubeadm token create --print-join-command

4、如果主机名相同会报错,修改主机名,/etc/hostname文件

5、可能会遇到如下错误,删除掉 /etc/kubernetes/pki/ca.crt再加入master

[ERROR FileAvailable--etc-kubernetes-pki-ca.crt]: /etc/kubernetes/pki/ca.crt already exists
rm /etc/kubernetes/pki/ca.crt

6、node主机应用flannel网络
(1)将master节点中的kube-flannel.yml发送到当前node主机

sudo scp kube-flannel.yml 节点1用户名@节点1ip:/home/xxx

(2)node主机启用flannel网络

kubectl apply -f kube-flannel.yml

-如果node节点一直NotReady可能是镜像没有拉取,将master节点的kubeadm-config.conf传到node节点,手动拉取镜像

kubeadm config images pull --config ./kubeadm-config.conf

五、pod手动驱离(deployment调度器创建的pod)

(1)将pod所在节点设置为不可调度状态
驱动node节点上的Pod(先设置node为cordon不可调度状态,然后驱逐Pod)

kubectl drain <node name>

(2)删除节点上的pod(此时pod会被deployment重新创建到别的节点上,如没有可用节点将会导致pod处于Pending状态)

kubectl delete pod <pod name>

–强制删除pod

kubectl delete pod <pod-name> --force --grace-period=0

(3)删除节点

kubectl delete node <node name>

–恢复节点为可调度状态

kubectl uncordon <node name>

此时会将处于Pending状态的pod调度到该节点

六、kubectl创建deployment调度器以及kubectl基本操作

1、创建yaml文件

apiVersion: apps/v1
kind: Deployment
metadata:
 name: mydeployment
 labels:
  app: myapp
spec:
 replicas: 1
 selector:
  matchLabels:
   app: app
 template:
  metadata:
   labels:
    app: app
  spec:
   hostNetwork: true
   dnsPolicy: ClusterFirstWithHostNet
   containers:
   - name: app1
     image: myapp:v1
     imagePullPolicy: IfNotPresent
     ports:
     - containerPort: 8880
   - name: app2
     image: myapp:v2
     imagePullPolicy: IfNotPresent
     ports:
     - containerPort: 8881

replicas: 1期望维持的pod副本数量。

这里使用hostNetwork: true使pod使用节点网络,此时节点不能创建期望值的pod副本(端口冲突)。

image: myapp:v2 使用的镜像及版本。

imagePullPolicy: IfNotPresent表示如果当前pod所在节点有则使用本地镜像,没有则从远程仓库获
ports:
- containerPort: 8880容器暴露的端口,由于使用hostNetwork: true此设置不写也一样。

2、使用yaml文件创建deployment调度器

kubectl apply -f xxxx.yaml --record

3、查看node节点

kubectl get node -o wide

4、查看所有pod

kubectl get pod -o wide

5、查看指定pod

kubectl  get pod <pod name>

6、以JSON格式输出POD信息

kubectl get pod <pod name> --output json

7、以yaml格式输出POD信息

kubectl get pod <pod name> --output yaml

8、删除pod

kubectl delete pod <pod name>

删除调度器管理的pod会被调度器再次创建,以维持期望值的pod数量

9、查看deployment调度器

kubectl get deploy <deployment name> -o wide

10、删除deployment调度器

kubectl delete deploy <deployment name>

此时所有由该调度器管理的pod都会陆续退出

11、查看node节点

kubectl get node -o wide

12、删除node节点

kubectl delete node <node name>

七、deployment滚动更新

1、应用升级(修改容器镜像)

kubectl set image deployment <deployment name> <containers name>=<image name>:<tags>

2、查看升级状态

kubectl rollout status deployment <deployment name>

3、查看历史版本

kubectl rollout history deployment <deployment name>

4、回滚到上一个版本

kubectl rollout undo deployment <deployment name>

5、回滚到指定版本

kubectl rollout undo deployment <deployment name> --to-revision=历史版本号

6、弹性伸缩(在线扩容)

kubectl scale deployment <deployment name> --replicas=期望副本数量

八、连接阿里云私有仓库

1、查看secret

kubectl get secret [-n 名称空间默认default]

默认只有一个

root@master:~# kubectl get secret
NAME                  TYPE                                  DATA   AGE
default-token-cx7df   kubernetes.io/service-account-token   3      17d

2、创建一个secret

kubectl create secret docker-registry ali-secret --namespace=default --docker-server=registry.cn-beijing.aliyuncs.com --docker-username=username --docker-password=password --docker-email=dockerEmail
 参数说明:
 ali-secret:secret名字
 --docker-serve:私有仓库地址
 --docker-username:私有仓库登录用户名
 --docker-password:私有仓库登录密码
 --docker-email:登录邮箱(可选)
 --namespace:指定命名空间 (可选) 

3、再查看多一个

root@master:~# kubectl get secret
NAME                  TYPE                                  DATA   AGE
ali-secret            kubernetes.io/dockerconfigjson        1      3m12s
default-token-cx7df   kubernetes.io/service-account-token   3      17d

4、以yaml形式查看

kubectl get secret ali-secret -n default -o yaml

5、Deployment使用

apiVersion: apps/v1
kind: Deployment
metadata:
 name: mydeployment
 labels:
  app: myapp
spec:
 replicas: 1
 selector:
  matchLabels:
   app: app
 template:
  metadata:
   labels:
    app: app
  spec:
   imagePullSecrets:
   - name: ali-secret (使用刚才创建的secret)
   hostNetwork: true
   dnsPolicy: ClusterFirstWithHostNet
   containers:
   - name: app1
     image: registry.cn-beijing.aliyuncs.com/xxx/xxx:[镜像版本号]
     imagePullPolicy: IfNotPresent
     ports:
     - containerPort: 8880
注意
指定使用的secret
   imagePullSecrets:
   - name: ali-secret (使用刚才创建的secret)
指定使用镜像的全名称:版本号
image: registry.cn-beijing.aliyuncs.com/xxx/xxx:[镜像版本号]

6、进入容器

kubectl exec <pod名> -c <容器名> -it /bin/bash

7、查看pod详细信息

kubectl describe pod [pod名]

九、解决pod内部无法解析DNS

sudo vim /etc/resolv.conf

将nameserver临时修改为114.114.114.114 ,这是暂时的办法,这也是第一步

第二步

kubectl edit deployment coredns -n kube-system

将replicates改为0,从而停止已启动的coredns pod

第三步

kubectl edit deployment coredns -n kube-system

再将replicates改为2,触发coredns重新读取系统配置

kubectl get pods -n kube-system

检查服务状态为Running

你可能感兴趣的:(kubernetes,kubernetes,docker,linux,ubuntu,运维)