文章目录
1. deployment资源
有rc在滚动升级之后,会造成服务访问中断,于是k8s引入了deployment资源
创建deployment
cd k8s_yaml/
mkdir deploy
cd deploy/
[root@k8s-master deploy]# cat k8s_delpoy.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
[root@k8s-master deploy]# kubectl create -f k8s_delpoy.yaml
[root@k8s-master deploy]# kubectl get deployment
NAME DESIRED CURRENT UP-TO-DATE AVAILABLE AGE
nginx-deployment 3 3 3 3 8m
[root@k8s-master deploy]# kubectl get pod
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 3 4d
nginx-5mf4r 1/1 Running 1 3d
nginx-deployment-3014407781-1msh5 1/1 Running 0 4m
nginx-deployment-3014407781-67f4s 1/1 Running 0 4m
nginx-deployment-3014407781-tj854 1/1 Running 0 4m
[root@k8s-master deploy]# kubectl expose deployment nginx-deployment --type=NodePort --port=80
[root@k8s-master deploy]# kubectl get svc
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes 10.254.0.1 <none> 443/TCP 4d
myweb 10.254.173.22 <nodes> 80:30000/TCP 3d
nginx 10.254.22.101 <nodes> 80:4336/TCP 3d
nginx-deployment 10.254.87.197 <nodes> 80:40510/TCP 8m
[root@k8s-master deploy]# curl -I 10.0.0.12:40510
HTTP/1.1 200 OK
Server: nginx/1.13.12
Date: Mon, 16 Sep 2019 02:10:10 GMT
Content-Type: text/html
Content-Length: 612
Last-Modified: Tue, 02 Oct 2018 14:49:27 GMT
Connection: keep-alive
ETag: "5bb38577-264"
Accept-Ranges: bytes
#修改配置文件中的此行改为nginx 1.15
[root@k8s-master deploy]# kubectl edit deployment nginx-deployment
- image: 10.0.0.11:5000/nginx:1.15
[root@k8s-master deploy]# kubectl rollout history deployment nginx-deployment
deployments "nginx-deployment"
REVISION CHANGE-CAUSE
5 <none>
6 <none>
#回滚的命令
[root@k8s-master deploy]# kubectl rollout undo deployment nginx-deployment
deployment "nginx-deployment" rolled back
[root@k8s-master deploy]# curl -I 10.0.0.12:40510
HTTP/1.1 200 OK
Server: nginx/1.13.12
#再执行回滚命令
[root@k8s-master deploy]# kubectl rollout undo deployment nginx-deployment
deployment "nginx-deployment" rolled back
[root@k8s-master deploy]# curl -I 10.0.0.12:40510
HTTP/1.1 200 OK
Server: nginx/1.15.5
#再run一个资源
[root@k8s-master deploy]# kubectl run lcx --image=10.0.0.11:5000/nginx:1.13 --replicas=3
deployment "lcx" created
[root@k8s-master deploy]# kubectl rollout history deployment lcx
deployments "lcx"
REVISION CHANGE-CAUSE
1 <none>
#删除资源
[root@k8s-master deploy]# kubectl delete deployment lcx
deployment "lcx" deleted
#run一个资源
[root@k8s-master deploy]# kubectl run lcx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
deployment "lcx" created
[root@k8s-master deploy]# kubectl rollout history deployment lcx
deployments "lcx"
REVISION CHANGE-CAUSE
1 kubectl run lcx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
#修改配置为nginx:1.15
[root@k8s-master deploy]# kubectl edit deployment lcx
spec:
containers:
- image: 10.0.0.11:5000/nginx:1.15
[root@k8s-master deploy]# kubectl rollout history deployment lcx
deployments "lcx"
REVISION CHANGE-CAUSE
1 kubectl run lcx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
2 kubectl edit deployment lcx #将执行的命令记录了下来
#再次更新版本为1.16
[root@k8s-master deploy]# kubectl edit deployment lcx
spec:
containers:
- image: 10.0.0.11:5000/nginx:1.16
#查看还是不显示版本
[root@k8s-master deploy]# kubectl rollout history deployment lcx
deployments "lcx"
REVISION CHANGE-CAUSE
1 kubectl run lcx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
2 kubectl edit deployment lcx
3 kubectl edit deployment lcx
#因为不显示版本,所以要引用一条新的命令
[root@k8s-master deploy]# kubectl set image deploy lcx lcx=10.0.0.11:5000/nginx:1.15
deployment "lcx" image updated
#第二条执行的命令已经回滚为nginx:1.15了
[root@k8s-master deploy]# kubectl rollout history deployment lcx
deployments "lcx"
REVISION CHANGE-CAUSE
1 kubectl run lcx --image=10.0.0.11:5000/nginx:1.13 --replicas=3 --record
3 kubectl edit deployment lcx
4 kubectl set image deploy lcx lcx=10.0.0.11:5000/nginx:1.15
2. tomcat+mysql练习
在k8s中容器之间相互访问,通过VIP地址!
2.1搭建过程(截图较多)
2.2 扩展—实现wordpress
version: '3'
services:
db:
image: mysql:5.7
volumes:
- /data/db_data:/var/lib/mysql
restart: always
environment:
MYSQL_ROOT_PASSWORD: somewordpress
MYSQL_DATABASE: wordpress
MYSQL_USER: wordpress
MYSQL_PASSWORD: wordpress
wordpress:
depends_on:
- db
image: wordpress:latest
volumes:
- /data/web_data:/var/www/html
ports:
- "80:80"
restart: always
environment:
WORDPRESS_DB_HOST: db
WORDPRESS_DB_USER: wordpress
WORDPRESS_DB_PASSWORD: wordpress
3 .k8s的附加组件
3.1 dns服务()
作用:把svc的名字解析成VIP的地址
kubectl get all -n kube-system -o wide
#1:下载dns_docker镜像包
wget http://192.168.12.202/docker_image/docker_k8s_dns.tar.gz
#2:导入dns_docker镜像包(node1节点)
#3:修改skydns-rc.yaml, 在node1 创建dns服务
spec:
nodeSelector:
kubernetes.io/hostname: 10.0.0.12
containers:
#4:创建dns服务
kubectl create -f skydns-deploy.yaml
kubectl create -f skydns-svc.yaml
#5:检查
kubectl get all --namespace=kube-system
#6:修改所有node节点kubelet的配置文件
vim /etc/kubernetes/kubelet
KUBELET_ARGS="--cluster_dns=10.254.230.254 --cluster_domain=cluster.local"
systemctl restart kubelet
3.2 namespace命令空间
namespace做资源隔离
[root@k8s-master wordpress_demo]# kubectl get namespace
NAME STATUS AGE
default Active 5d
kube-system Active 5d
#增
kubectl create namespace lcx
#删
kubectl delete namespace lcx
测试
[root@k8s-master wordpress_demo]# pwd
/root/k8s_yaml/wordpress_demo
#创建wordpress的空间
[root@k8s-master wordpress_demo]# kubectl create namespace wordpress
namespace "wordpress" created
#删除当前的环境
[root@k8s-master wordpress_demo]# kubectl delete -f .
#修改所以配置文件添加namespace空间
[root@k8s-master wordpress_demo]# ls
mysql-rc.yml mysql-svc.yml wordpress-rc.yml wordpress-svc.yml
[root@k8s-master wordpress_demo]# sed -i '3a \ \ namespace: wordpress' *
#创建新环境
[root@k8s-master wordpress_demo]# kubectl create -f .
replicationcontroller "wordpress-db" created
service "wordpress-db" created
replicationcontroller "wordpress-web" created
service "wordpress-web" created
#查看wordpress的空间
[root@k8s-master wordpress_demo]# kubectl get all -n wordpress
NAME DESIRED CURRENT READY AGE
rc/wordpress-db 1 1 1 1m
rc/wordpress-web 1 1 1 1m
NAME CLUSTER-IP EXTERNAL-IP PORT(S) AGE
svc/wordpress-db 10.254.47.172 <none> 3306/TCP 1m
svc/wordpress-web 10.254.226.90 <nodes> 80:30009/TCP 1m
NAME READY STATUS RESTARTS AGE
po/wordpress-db-dv5f4 1/1 Running 0 1m
po/wordpress-web-v3bqd 1/1 Running 0 1m
访问一下 10.0.0.12:30009
yum install dos2unix.x86_64
dos2unix <文件名> 可以修复排版问题