1

k8s 核心概念思维导图

图片描述

集群搭建

  • 单节点
https://my.oschina.net/u/731542/blog/2966427 (单机版)
补充:
1 init之前设置hosts 关闭swap
disable swap swapoff -a && sysctl -p
kubeadm init  --kubernetes-version=1.12.3 --pod-network-cidr=10.244.0.0/16

2 报错: [ERROR KubeletVersion]: the kubelet version is higher than the control plane version.
yum -y remove kubelet
yum -y install kubelet-1.12.3 kubeadm-1.12.3
  • 多节点
https://www.kubernetes.org.cn/4387.html

init_mini_k8s.sh

#!/bin/bash

## 准备工作
# 修改主机名 请手动重启一下
# hostnamectl set-hostname  k8s-master
# disable swap
# swapoff -a && sysctl -p
# 重启系统
# reboot

## 安装docker
yum -y install policycoreutils-python*
wget http://mirror.centos.org/centos/7/extras/x86_64/Packages/container-selinux-2.68-1.el7.noarch.rpm
rpm -ivh container-selinux-2.68-1.el7.noarch.rpm
yum install -y libltdl.so*
yum install -y pigz*
wget https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/stable/Packages/docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm
rpm -ivh docker-ce-18.03.1.ce-1.el7.centos.x86_64.rpm

## 切换 k8s yum 源
touch /etc/yum.repos.d/kubernetes.repo
cat >> /etc/yum.repos.d/kubernetes.repo <<EOF
[kuberneten]
name=Kubernetes
baseurl=http://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64
enabled=1
gpgcheck=0
repo_gpgcheck=0
gpgkey=http://mirrors.aliyun.com/kubernetes/yum/doc/yum-key.gpg
        http://mirrors.aliyun.com/kubernetes/yum/doc/rpm-package-key.gpg
EOF

yum makecache
sysctl net.bridge.bridge-nf-call-iptables=1

# 镜像加速 添加私有仓库
mkdir -p /etc/docker
tee /etc/docker/daemon.json <<-'EOF'
{
  "registry-mirrors": ["https://9cpn8tt6.mirror.aliyuncs.com"],
  "insecure-registries":["10.9.2.62:5000"]
}
EOF

# 这里指定版本 不然会报错
yum -y install kubelet-1.12.3 kubeadm-1.12.3 kubectl kubernetes-cni-0.6.0
kubectl version


systemctl enable docker && systemctl start docker
systemctl enable kubelet && systemctl start kubelet

kubeadm config images list

docker pull docker.io/mirrorgooglecontainers/kube-apiserver-amd64:v1.12.3
docker tag docker.io/mirrorgooglecontainers/kube-apiserver-amd64:v1.12.3 k8s.gcr.io/kube-apiserver:v1.12.3

docker pull docker.io/mirrorgooglecontainers/kube-controller-manager-amd64:v1.12.3
docker tag docker.io/mirrorgooglecontainers/kube-controller-manager-amd64:v1.12.3 k8s.gcr.io/kube-controller-manager:v1.12.3

docker pull docker.io/mirrorgooglecontainers/kube-scheduler-amd64:v1.12.3
docker tag docker.io/mirrorgooglecontainers/kube-scheduler-amd64:v1.12.3 k8s.gcr.io/kube-scheduler:v1.12.3

docker pull docker.io/mirrorgooglecontainers/kube-proxy-amd64:v1.12.3
docker tag docker.io/mirrorgooglecontainers/kube-proxy-amd64:v1.12.3 k8s.gcr.io/kube-proxy:v1.12.3

docker pull docker.io/mirrorgooglecontainers/pause-amd64:3.1
docker tag docker.io/mirrorgooglecontainers/pause-amd64:3.1 k8s.gcr.io/pause:3.1

docker pull docker.io/mirrorgooglecontainers/etcd-amd64:3.2.24
docker tag docker.io/mirrorgooglecontainers/etcd-amd64:3.2.24 k8s.gcr.io/etcd:3.2.24

docker pull docker.io/coredns/coredns:1.2.2
docker tag docker.io/coredns/coredns:1.2.2 k8s.gcr.io/coredns:1.2.2

docker images

kubeadm config images list

kubeadm init  --kubernetes-version=1.12.3 --pod-network-cidr=10.244.0.0/16

if [ ! -d "$HOME/.kube" ]; then
  mkdir -p $HOME/.kube
fi
if [ ! -f "$HOME/.kube/config" ]; then
  rm -f $HOME/.kube/config
fi
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config

# all-in-one
kubectl taint nodes --all node-role.kubernetes.io/master-

# 安装 cni 网络插件
docker pull quay.io/coreos/flannel:v0.10.0-amd64 
mkdir -p /etc/cni/net.d/

tee /etc/cni/net.d/10-flannel.conf <<-'EOF'
{"name":"cbr0","type":"flannel","delegate": {"isDefaultGateway": true}}
EOF

mkdir /usr/share/oci-umount/oci-umount.d -p
mkdir /run/flannel/
kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/v0.9.1/Documentation/kube-flannel.yml

kubectl get pods -n kube-system

kubectl get nodes

# 安装 Weave scope
kubectl apply -f "https://cloud.weave.works/k8s/scope.yaml?k8s-version=$(kubectl version | base64 | tr -d '\n')&k8s-service-type=NodePort"
kubectl -n weave get service weave-scope-app

入门操作

  • 创建查看集群

    kubectl get nodes -o wide  # 查看集群节点信息
    kubectl cluster-info  # Display cluster info 显示集群信息
  • 部署应用

    kubectl run my-ngx --image=nginx:latest --port=80  # 部署应用
    
    old: kubectl run -i --tty load-generator --image=busybox /bin/sh
    new: kubectl run --generator=run-pod/v1 -i --tty load-generator --image=busybox /bin/sh
  • 发布应用

    kubectl expose deployment my-ngx --type='NodePort' --port=80  # 发布应用
    kubectl get services/my-ngx    # 查看my-ngx服务
    kubectl describe services/my-ngx  # 查看详细信息
    curl 10.9.2.42:19999    #  curl NodeIP:NodePort
  • 扩容缩容

    kubectl scale deployment/my-ngx --replicas=2  # 扩容
    kubectl get pods -o wide
    kubectl scale deployment/my-ngx --replicas=1  # 缩容
  • 更新回滚

    kubectl set image deployment/my-ngx my-ngx=nginx:1.9  # 滚动更新
    kubectl rollout undo deployment/my-ngx   # 回滚
  • 删除

    kubectl get pods      # 查看pods
    kubectl get deployment  # 查看deployment服务
    kubectl delete pods my-ngx-59497d7745-qzwqz  # 删除pods
    kubectl delete deployment my-ngx  # 删除deployment
  • 查看错误信息

    journalctl -f -u kubelet

参考

<<Kubenetes指南  倪鹏飞>>
https://www.kubernetes.org.cn/4387.html  # 安装集群

EngineerLeo
598 声望38 粉丝

专注于云原生、AI等相关技术