整体配置:
1台master;2台node ,并配置3台机器作为外部ectd集群,另1台机器作为跳板机操作集群;
安装基本步骤
ubuntu对应镜像源:
https://developer.aliyun.com/mirror/ubuntu?spm=a2c6h.13651102.0.0.45c91b11NiYpzP
# 备份
sudo cp /etc/apt/sources.list /etc/apt/sources.list_backup
#修改为指定镜像源
sudo vim /etc/apt/sources.list
# 更新
sudo apt-get update
# step 1: 安装必要的一些系统工具
sudo apt-get update
sudo apt-get -y install apt-transport-https ca-certificates curl software-properties-common
# step 2: 安装GPG证书
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
# Step 3: 写入软件源信息--
sudo add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu bionic stable"
#查找可安装Docker-CE的版本:
apt-cache madison docker-ce
# Step 2: 安装指定版本的Docker-CE:
sudo apt-get -y install docker-ce=5:19.03.11~3-0~ubuntu-bionic
# 锁定版本
sudo apt-mark hold docker-ce=5:19.03.11~3-0~ubuntu-bionic
# 相关命令
# 启动docker,设置开机自启动
sudo systemctl enable docker && sudo systemctl start docker
# 配置docker 镜像源
vi /etc/docker/daemon.json
{
"registry-mirrors": ["https://g2djyyu3.mirror.aliyuncs.com"],
"exec-opts": [ "native.cgroupdriver=systemd" ]
}
# 重启docker
sudo systemctl restart docker
# 查看docker状态
systemctl status docker
# 详细日志
journalctl -u docker.server
# 将当前登录用户user_name加入docker用户组中,便于用户使用
$ sudo usermod -aG docker user_name
apt-get update && apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update
# 查看可安装版本
apt-cache madison kubelet
# 安装指定版本
apt-get install -y kubelet=1.18.3-00 kubeadm=1.18.3-00 kubectl=1.18.3-00
# 配置自启动
sudo systemctl enable kubelet && sudo systemctl start kubelet
参考文档:https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/setup-ha-etcd-with-kubeadm/
主要就是按照步骤生成3台集群上所用到的证书,而后分发到各机器上。
参考文档:https://kubernetes.io/zh/docs/setup/production-environment/tools/kubeadm/high-availability/#%E5%A4%96%E9%83%A8-etcd-%E8%8A%82%E7%82%B9
# 查看所需镜像
$ kubeadm config images list --kubernetes-version=v1.18.3
k8s.gcr.io/kube-apiserver:v1.18.3
k8s.gcr.io/kube-controller-manager:v1.18.3
k8s.gcr.io/kube-scheduler:v1.18.3
k8s.gcr.io/kube-proxy:v1.18.3
k8s.gcr.io/pause:3.2
k8s.gcr.io/etcd:3.4.3-0
k8s.gcr.io/coredns:1.6.7
# 从新的地址下载所需镜像
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.3
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.3
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.3
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.3
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0
sudo docker pull registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7
# 将镜像重新打回k8s.gcr.io的镜像tag
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-apiserver:v1.18.3 k8s.gcr.io/kube-apiserver:v1.18.3
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-controller-manager:v1.18.3 k8s.gcr.io/kube-controller-manager:v1.18.3
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-scheduler:v1.18.3 k8s.gcr.io/kube-scheduler:v1.18.3
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/kube-proxy:v1.18.3 k8s.gcr.io/kube-proxy:v1.18.3
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/pause:3.2 k8s.gcr.io/pause:3.2
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/etcd:3.4.3-0 k8s.gcr.io/etcd:3.4.3-0
sudo docker tag registry.cn-hangzhou.aliyuncs.com/google_containers/coredns:1.6.7 k8s.gcr.io/coredns:1.6.7
sudo kubeadm init --config kubeadm-config.yaml --upload-certs --v=5
kubectl apply -f "https://cloud.weave.works/k8s/net?k8s-version=$(kubectl version | base64 | tr -d '\n')"
根据master生成的命令进行配置
kubeadm join **:6443 --token ** --discovery-token-ca-cert-hash sha256:**
参考文档:https://kubernetes.io/zh/docs/tasks/tools/install-kubectl/#%E9%85%8D%E7%BD%AE-kubectl
# 直接安装kubectl
sudo apt-get update && sudo apt-get install -y apt-transport-https
curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | apt-key add -
cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
EOF
apt-get update
sudo apt-get update
sudo apt-get install -y kubectl=1.18.3-00
# 将master节点上/etc/kubernetes/admin.conf 拷贝至目标服务器$HOME/.kube/目录
# 注意:目标服务器不能内网访问maste节点时,需要在/etc/hosts中配置master节点的真实ip
scp /etc/kubernetes/admin.conf usename@host:$HOME/.kube/
# 验证kubectl是否被正确配置:kubectl cluster-info,URL 被返回,那么 kubectl 已经被正确配置
kubectl cluster-info
给其他用户添加k8s访问权限
mkdir -p $HOME/.kube
sudo cp -i /home/**/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
# 查看docker容器内镜像
docker ps
# docker内启动容器
docker container ls
# 进入docker某容器中
docker exec -it 338cd4b675ab bash
# docker日志
docker logs --since 30m 840ce29c2b99
#单机k8s init
sudo kubeadm init --apiserver-advertise-address=172.19.17.27 --kubernetes-version=v1.18.3
# 查看k8s节点
kubectl get nodes
# 查看指定namespace 下的pod
kubectl get pods --namespace=test
# 查看namespace:
kubectl get namespace
# 创建名为test的namespace
kubectl create namespace test
# 设置命名空间首选项
kubectl config set-context --current --namespace=test
# 在已创建的命名空间中创建资源
kubectl apply -f pod.yaml --namespace=test
# 进入k8s启动pod
kubectl exec -it admin-frontend-server-74497cb64f-8fxk8 --bash
#查看各组件状态
kubectl get componentstatuses
# master 节点也作为node节点
kubectl taint nodes --all node-role.kubernetes.io/master-
# 其他命令
kubectl get pods -n kube-system
kubectl get pod --all-namespaces
kubectl get csr
kubectl get deployments
kubectl get pods -n kube-system -o wide --watch
kubectl describe pods weave-net-87t7g -n kube-system
# 创建pod
kubectl create -f deployment.yaml
# 创建server文件
kubectl create -f services.yaml
# 查看文件创建情况
kubectl discribe service **
kubectl discribe deployment **
# 删除 secret, deployment
kubectl delete secret **
kubectl delete deployment **
# 匹配特定服务;
kubectl get po | grep display
kubectl get svc | grep data
# 查看服务日志
kubectl logs ** -f —tail=20
kubectl logs ** --since=1h
# 卸载集群
# 想要撤销kubeadm做的事,首先要排除节点,并确保在关闭节点之前要清空节点。
# 在主节点上运行:
kubectl drain <node name> --delete-local-data --force --ignore-daemonsets
kubectl delete node <node name>
# 然后在需要移除的节点上,重置kubeadm的安装状态:
kubeadm reset
# 重置Kubernetes
# 参考https://www.jianshu.com/p/31f7dda9ccf7
sudo kubeadm reset
# 重置后删除网卡等信息
rm -rf /etc/cni/net.d
# 重置iptables
iptables -F && iptables -t nat -F && iptables -t mangle -F && iptables -X
sysctl net.bridge.bridge-nf-call-iptables=1
# 清楚网卡
sudo ip link del cni0
sudo ip link del flannel.1
重置Kubernetes;