1.k8s常用的资源
k8s yaml的主要组成
apiVersion: v1 api版本
kind: pod 资源类型
metadata: 属性
spec: 详细
k8s_pod.yaml
apiVersion: v1
kind: Pod
metadata:
name: nginx
labels:
app: web
spec:
containers:
- name: nginx
image: 10.0.0.11:5000/nginx:1.13
ports:
- containerPort: 80
1.构建命令
kubectl create -f k8s_pod.yaml
2.查看状态
kubectl get pod nginx -o wide
3.描述pod状态
kubectl describe pod nginx
4.删除pod
kubectl delete pod nginx 或者kubectl delete -f xxx.yaml
5.编辑pod的配置
kubectl edit pod nginx
这里需要注意的是构建会失败,因为从节点下载pod-infrastructure镜像是从官网,需要红帽证书,所以我们用下载好的上传到本地自己的镜像仓库。
改变两个从节点
vim /etc/kubernetes/kubelet
# pod infrastructure container
KUBELET_POD_INFRA_CONTAINER="--pod-infra-container-i
mage=10.0.0.11:5000/pod-infrastructure:latest"
systemctl restart kubelet
pod资源:至少由两个容器组成,pod基础容器和业务容器组成
pod配置文件2:
apiVersion: v1
kind: Pod
metadata:
name: test
labels:
app: web
spec:
containers:
- name: nginx
image: 10.0.0.11:5000/nginx:1.13
ports:
- containerPort: 80
- name: busybox
image: 10.0.0.11:5000/busybox:latest
command: ["sleep","10000"]
pod是k8s最小的资源单位
2.ReplicationController资源
rc:保证指定数量的pod始终存活,rc通过标签选择器来关联pod
创建一个rc.yaml文件
apiVersion: v1
kind: ReplicationController
metadata:
name: nginx
spec:
replicas: 5
selector:
app: myweb
template:
metadata:
labels:
app: myweb
spec:
containers:
- name: myweb
image: 10.0.0.11:5000/nginx:1.13
ports:
- containerPort: 80
rc的滚动升级 新建一个nginx-rc1.15.yaml
image.png
升级
kubectl rolling-update nginx -f nginx-rc1.15.yaml --update-period=10s
回滚
kubectl rolling-update nginx2 -f nginx-rc.yaml --update-period=1s
3. service资源
image.pngservice帮助pod暴露端口
创建一个service
apiVersion: v1
kind: Service
metadata:
name: myweb
spec:
type: NodePort #ClusterIP
ports:
- port: 80 #clusterIP
nodePort: 30000 #nodeport
targetPort: 80 #podport
selector:
app: myweb
#生成svc
kubectl create -f k8s_svc.yaml
#查看svc的两种方法
kubectl get svc
kubectl get service
#查看所有资源类型
kubectl get all -o wide
#确保标签一样,修改为myweb
kubectl edit svc myweb
app: myweb
标签需要确保一样,必须,很重要!!!
#查看端口是否暴露成功
kubectl describe svc myweb
如何验证它的负载均衡呢?
方法一:
#修改副本数
kubectl scale rc nginx --replicas=2
方法二:
#进入容器
kubectl exec -it pod_name /bin/bash
#k8s进入容器的方法
[root@k8s-master svc]# kubectl exec -it nginx-5mf4r /bin/bash
root@nginx-5mf4r:/# echo '11111' >/usr/share/nginx/html/index.html
root@nginx-5mf4r:/# exit
[root@k8s-master svc]# kubectl exec -it nginx-ppjb3 /bin/bash
root@nginx-ppjb3:/# echo '2222' >/usr/share/nginx/html/index.html
root@nginx-ppjb3:/# exit
添加随机端口
[root@k8s-master svc]# vim /etc/kubernetes/apiserver
KUBE_API_ARGS="--service-node-port-range=3000-50000"
[root@k8s-master svc]# systemctl restart kube-apiserver.service
service默认使用iptables来实现负载均衡, k8s 1.8新版本中推荐使用lvs(四层负载均衡)
4.deployment资源
有rc在滚动升级之后,会造成服务访问中断,于是k8s引入了deployment资源
创建deployment
1、编辑deploy的yaml文件
cat k8s_deploy.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: nginx-deployment
spec:
replicas: 3
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: 10.0.0.11:5000/nginx:1.13
ports:
- containerPort: 80
resources:
limits:
cpu: 100m
requests:
cpu: 100m
2.编辑实现负载的yaml文件
cat k8s_svc.yaml
apiVersion: v1
kind: Service
metadata:
name: nginx-deployment
spec:
type: NodePort #ClusterIP
ports:
- port: 80 #clusterIP
nodePort: 30001 #nodeport
targetPort: 80 #podport
selector:
app: nginx
方法一:
deployment升级和回滚
命令行创建deployment
kubectl run nginx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
命令行升级版本
kubectl set image deploy nginx nginx=10.0.0.11:5000/nginx:1.15
查看deployment所有历史版本
kubectl rollout history deployment nginx
deployment回滚到上一个版本
kubectl rollout undo deployment nginx
deployment回滚到指定版本
kubectl rollout undo deployment nginx --to-revision=2
方法二:
#修改配置文件中的此行改为nginx 1.15
[root@k8s-master deploy]# kubectl edit deployment nginx-deployment
- image: 10.0.0.11:5000/nginx:1.15
5.tomcat+mysql练习
在k8s中容器之间相互访问,通过VIP地址!
1.上传配置
[root@k8s-master tomcat_demo]# ll
total 16
-rw-r--r-- 1 root root 416 Jan 13 22:46 mysql-rc.yml
-rw-r--r-- 1 root root 145 Jan 13 22:29 mysql-svc.yml
-rw-r--r-- 1 root root 492 Jan 13 23:37 tomcat-rc.yml
-rw-r--r-- 1 root root 162 Jan 13 22:17 tomcat-svc.yml
2.创建
先创建mysql
kubectl create -f mysql-rc.yml
kubectl create -f mysql-svc.yml
kubectl get all -o wide查看mysql虚拟ip
vim tomcat-rc.yml
- name: MYSQL_SERVICE_HOST
value: '10.254.146.149'
更改ip
kubectl create -f tomcat-rc.yml
kubectl create -f tomcat-svc.yml
kubectl get all -o wide
启动成功后登陆
image.png
image.png
网友评论