初始化控制平面节点
1.控制平面节点是运行控制平面组件的机器,包括etcd 集群数据库和API Server(kubelet与之通讯)
2.选择一个Pod网络插件,如未安装可以参考 https://v1-23.docs.kubernetes.io/zh/docs/concepts/cluster-administration/networking/#how-to-implement-the-kubernetes-networking-model
支持多种网络插件
步骤
1.获取默认的初始化yaml信息
kubeadm config print init-defaults > init.yaml
2.编辑yaml文件,vim init.yaml
3.初始化
root@k8s-master:kubeadm init --config init.yaml | tee k8s.txt
root@k8s-master:~# ls
calico.yaml custom-resources.yaml init.yaml k8s.txt snap
4.要使用非root用户可以运行kubectl,执行以下命令
mkdir -p $HOME/.kube
sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
sudo chown $(id -u):$(id -g) $HOME/.kube/config
5.node节点加入master
在node节点输入:kubeadm join 1.1.0.10:6443 --token abcdef.0123456789abcdef
--discovery-token-ca-cert-hash sha256:6359a0906666a3eae007a359421dce2959c974749a5bbd0cf0a1266eca2f4140
安装Pod网络
k8s的网络模型有很多,如下
Calico
Calico 是一个开源的联网及网络安全方案, 用于基于容器、虚拟机和本地主机的工作负载。 Calico 支持多个数据面,包括:纯 Linux eBPF 的数据面、标准的 Linux 联网数据面 以及 Windows HNS 数据面。Calico 在提供完整的联网堆栈的同时,还可与 云驱动 CNIs 联合使用,以保证网络策略实施。
安装步骤
1.选择并进入Calico页面
2.选择安装的类型
3.安装
每个集群只能安装一个Pod 网络
4.查看集群的状态
image.png
运行Deployment
步骤
1.创建一个Deployment
root@k8s-master:~# kubectl create deployment maydep --image=httpd:2.2
deployment.apps/maydep created
2.查看Deployment
root@k8s-master:~# kubectl get deployment
NAME READY UP-TO-DATE AVAILABLE AGE
maydep 1/1 1 1 41s
3.弹性扩展
root@k8s-master:~# kubectl scale deployment maydep --replicas=3
deployment.apps/maydep scaled
root@k8s-master:~# kubectl get pod
NAME READY STATUS RESTARTS AGE
maydep-54c745d8db-dl4kc 1/1 Running 0 11s
maydep-54c745d8db-jrt7f 1/1 Running 0 101s
maydep-54c745d8db-q85z6 1/1 Running 0 11s
4.查看pod IP地址列表
root@k8s-master:~# kubectl get pods -l app=mynginx -o go-template='{{range .items}}{{.status.podIP}}{{"\n"}}{{end}}'
192.168.169.155
192.168.36.87
192.168.169.154
192.168.169.151
192.168.36.83
修改Deployment,实现版本升级
步骤
1.edit deployment
root@k8s-master:~# kubectl edit deployment maydep
2.查看deployment
root@k8s-master:~# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
maydep 3/3 3 3 4m54s httpd httpd:2.4 app=maydep
3.查看deployment更新事件信息
root@k8s-master:~# kubectl describe deployment maydep
4.查看deployment 更新记录
root@k8s-master:~# kubectl rollout history deployment maydep
deployment.apps/maydep
REVISION CHANGE-CAUSE
1 <none>
2 <none>
5.查看历史版本1或2的信息
root@k8s-master:~# kubectl rollout history deployment maydep --revision=1
deployment.apps/maydep with revision #1
Pod Template:
Labels: app=maydep
pod-template-hash=54c745d8db
Containers:
httpd:
Image: httpd:2.2
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
root@k8s-master:~# kubectl rollout history deployment maydep --revision=2
deployment.apps/maydep with revision #2
Pod Template:
Labels: app=maydep
pod-template-hash=7585487c48
Containers:
httpd:
Image: httpd:2.4
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
6.回滚到之前的历史版本1
root@k8s-master:~# kubectl rollout undo deployment maydep --to-revision=1
deployment.apps/maydep rolled back
7.查看当前的历史版本
root@k8s-master:~# kubectl get deployment -o wide
NAME READY UP-TO-DATE AVAILABLE AGE CONTAINERS IMAGES SELECTOR
maydep 3/3 3 3 19m httpd httpd:2.2 app=maydep
8.删除deployment
root@k8s-master:~# k delete deployment mydep
deployment.apps "mydep" deleted