声明
- 此教程为个人收藏,其中省去了很多细节和bug点,仅供我个人进行参考。如果执行有问题,概不负责。如有疑问也可以私信我进行了解。
- 此教程将持续更新,以后的版本会越来越详细。
配置Ubuntu阿里云镜像源
apt-get -y install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable"
apt-get -y update
apt-get -y install docker-ce
配置docker国内镜像源
vim /etc/docker/daemon.json
{
"registry-mirrors": [
"https://registry.docker-cn.com"
]
}
systemctl restart docker #重启docker服务
docker info #查看是否配置成功
#
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
gpg --keyserver keyserver.ubuntu.com --recv-keys BA07F4FB
gpg --export --armor BA07F4FB | sudo apt-key add -
apt-get update
# 安装kubelet kubeadm kubectl
apt-get install -y kubelet kubeadm kubectl
初始化 kubernetes
kubeadm config print init-defaults --kubeconfig ClusterConfiguration > kubeadm.yml
# 修改kubeadm安装程序配置文件
# 文件是上一步命令保存到文件夹目录下的
vim kubeadm.yml
# 找到并修改这两项配置
# 这里podSubnet是配置kubenates的网络范围,因为我们用的是calico,所以是配置的如下设置
imageRepository: registry.aliyuncs.com/goole_containers
podSubnet: "192.168.0.0/16"
# 执行拉去镜像和安装kubenates
kubeadm config images list --config kubeadm.yml
kubeadm config images pull --config kubeadm.yml
kubeadm init --config=kubeadm.yml --experimental-upload-certs | tee kubeadm-init.log
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
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.122.132:6443 --token abcdef.0123456789abcdef \
--discovery-token-ca-cert-hash sha256:6dbb2c64e08aab0e03317d1c774b6fe35c868e71a99045c0aebf8d6bffc86305
结点退出集群
$ kubeadm reset
说明:
- token
- 可以通过安装 master 时的日志查看 token 信息
- 可以通过
kubeadm token list
命令打印出 token 信息 - 如果
token
过期,可以使用kubeadm token create
命令创建出新的token
-
discovery-token-ca-cert-hash
- 可以通过安装master 时的日志查看sha256信息
- 可以通过
openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2>/dev/null | openssl dgst -sha256 -hex | sed 's/^.* //'
命令查看sha256信息
查看pod状态
kubectl get pod -n kube-system -o wide
重写网络状态
#ImagePullBackOff错误
vim /etc/netplan/50-cloud-init.yml
network:
ethernets:
ens33:
addresses: [192.168.122.134/24]
gateway4: 192.168.122.2
nameservers:
addresses: [192.168.122.2]
version: 2
$ netplan apply
vim /etc/resolv.conf
nameserver 114.114.114.114
systemctl stop systemd-resolved #关闭重写网络dns地址重写
检查组件的运行状态
kubectl get cs
检查Master状态
kubectl cluster-info
检查Nodes状态
kubectl get nodes
查看全部Pods的状态
kubectl get pods
运行第一个容器实例
kubectl run nginx --image=nginx --replicas=2 --port=80
查看全部的pods的状态
kubectl get pods
查看已部署的服务
kubectl get deployment
映射服务,让用户可以访问
kubectl expose deployment nginx --port=80 --type=LoadBalancer # 以负载均衡的方式将容器暴露出来
查看已经发布的服务
kubectl get services
查看服务详情
kubectl describe service nginx
停止服务
kubectl delete deployment nginx
删除服务
kubectl delete service nginx
删除结点
-
在master结点上执行
kubectl delete nodes <nodename>
-
在node结点上执行
kubeadm reset
网友评论