四. yaml文件
需要提前创建好yaml文件,并创建好好pod运行所需要的namespace、yaml文件等资源
- apiVersion - 创建该对象所使用的 Kubernetes API 的
版本 - kind - 想要
创建的对象的类型 - metadata - 帮助识别对象
唯一性的数据 ,包括一个 name 名称 可选的 namespace - spec 定义容器信息
启动几个pod
spec:
replicas: 1
- status(Pod创建完成后k8s
自动生成status状态 )
yaml文件及必需字段 每个API对象都有3大类属性:元数据metadata、规范spec和状态status。
spec和status的区别: spec是期望状态 status是实际状态
1. 创建namespace.yaml文件
pwd /opt/k8s-data/yaml/
mkdir namespaces linux36
cd namespaces
cat linux36.yaml
apiVersion: v1 #API版本
kind: Namespace #类型为namespac
metadata: #定义元数据
name: linux36 #namespace名称
可以使用kubectl explain namespace 查看版本信息
创建并验证namespace
kubectl apply -f linux36.yaml
namespace/linux36 created
kubectl get namespaces
NAME STATUS AGE d
efault Active 21d
kube-public Active 21d
kube-system Active 21d
linuxqcq Active 45s
再web界面查看在etcd 中查看到
2. yaml与json:
yaml和json对比, 在线yaml与json编辑器:http://www.bejson.com/validators/yaml_editor/
json格式
{ '人员名单':
{ '张三': { '年龄': 18, '职业': 'Linux运维工程师', '爱好': [ '看书', '学习', '加班' ] },
'李四': { '年龄': 20, '职业': 'Java开发工程师', '爱好': [ '开源技术', '微服务', '分布式存 储' ] } }
}
json特点: json 不能注释 json 可读性较差 json 语法很严格 比较适用于API 返回值 ,也可用于配置文件
yaml格式
yaml文件主要特性:
k8s中的yaml文件以及其他场景的yaml文件,大部分都是以下类型
3. yaml文件详解
`
- 表示列表 ,可以有多个
分隔符
---
实例
1.k8s中创建Nginx pod
创建Nginx pod 并测试通过node port访问
Nginx yaml文件:
root@k8s-master1:/opt/k8s-data/yaml/linux36/nginx# cat nginx.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
labels:
app: linux36-nginx-deployment-label
name: linux36-nginx-deployment
namespace: linux36
spec:
replicas: 1
selector:
matchLabels:
app: linux36-nginx-selector
template:
metadata:
labels:
app: linux36-nginx-selector
spec:
containers:
- name: linux36-nginx-container
image: harbor.magedu.net/linux36/nginx-web1:v1
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 80
protocol: TCP
name: http
- containerPort: 443
protocol: TCP
name: https
env:
- name: "password"
value: "123456"
- name: "age"
value: "18"
resources:
limits:
cpu: 2
memory: 2Gi
requests:
cpu: 500m
memory: 1Gi
---
kind: Service
apiVersion: v1
metadata:
labels:
app: linux36-nginx-service-label
name: linux36-nginx-service
namespace: linux36
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 30002
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 30443
selector:
app: linux36-nginx-selector
创建Nginx pod:
kubectl apply -f nginx.yaml
deployment.extensions/linux36-nginx-deployment created
service/linux36-nginx-spec created
测试访问Nginx web界面
2.运行tomcat
基于基础的centos镜像,制作公司内部基础镜像–jdk镜像–tomcat基础镜像–tomcat业务镜像:
JDK基础镜像制作
参考
Dockerfile文件 打镜像脚本
验证JDK镜像启动为容器后的java环境
tomcat基础镜像制作
测试访问tomcat基础镜像启动为容器
3. 在k8s环境运行tomcat
创建tomcat业务pod
pwd
/opt/k8s-data/yaml/linux36/tomcat-app1
kubectl apply -f tomcat-app1.yaml
验证pod启动成功
4. k8s中nginx+tomcat实现动静分离
实现一个通用的nginx+tomcat动静分离web架构,即用户访问的静态页面和图片在由nginx直接响应,而动态请求则基于location转发至tomcat 。
Nginx基于tomcat的service name转发用户请求到tomcat业务app
nginx配置文件
重新构建nginx业务镜像
bash build-command.sh
镜像启动为容容器并验证配置文件 重新创建业务nginx pod
删除并重新创建nginx业务镜像
更新nginx业务镜像版本号
重新构建新版本镜像,然后打一个新的tag号,然后通过指定镜像的方式对pod进行更新
准备新版本nginx业务镜像 获取当前deployment
执行更新nginx业务镜像版本
web访问测试------通过nginx访问到tomcat的app项目
五. 基于NFS实现动静分离
环境
NFS客户端挂载并测试写入文件
nginx 业务容器yaml
cat nginx.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
labels:
app: linux36-nginx-deployment-label
name: linux36-nginx-deployment
namespace: linux36
spec:
replicas: 1
selector:
matchLabels:
app: linux36-nginx-selector
template:
metadata:
labels:
app: linux36-nginx-selector
spec:
containers:
- name: linux36-nginx-container
image: harbor.magedu.net/linux36/nginx-web1:v1
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 80
protocol: TCP
name: http
- containerPort: 443
protocol: TCP
name: https
env:
- name: "password"
value: "123456"
- name: "age"
value: "18"
resources:
limits:
cpu: 2
memory: 2Gi
requests:
cpu: 500m
memory: 1Gi
volumeMounts:
- name: linux36-images
mountPath: /usr/local/nginx/html/webapp/images
readOnly: false
- name: linux36-static
mountPath: /usr/local/nginx/html/webapp/static
readOnly: false
volumes: #kubectl explain Deployment.spec.template.spec.volumes
- name: linux36-images
nfs:
server: 192.168.7.108
path: /data/linux36/images
- name: linux36-static
nfs:
server: 192.168.7.108
path: /data/linux36/static -
--
kind: Service
apiVersion: v1
metadata:
labels:
app: linux36-nginx-service-label
name: linux36-nginx-service
namespace: linux36
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 80
nodePort: 30002
- name: https
port: 443
protocol: TCP
targetPort: 443
nodePort: 30443
selector:
app: linux36-nginx-selector
执行更新yaml文件 pod中验证NFS挂载
tomcat业务pod更新挂载
pwd
/opt/k8s-data/yaml/linux36/tomcat-app1
cat tomcat-app1.yaml
kind: Deployment
apiVersion: extensions/v1beta1
metadata:
labels:
app: linux36-tomcat-app1-deployment-label
name: linux36-tomcat-app1-deployment
namespace: linux36
spec:
replicas: 1
selector:
matchLabels:
app: linux36-tomcat-app1-selector
template:
metadata:
labels:
app: linux36-tomcat-app1-selector
spec:
containers:
- name: linux36-tomcat-app1-container
image: harbor.magedu.net/linux36/tomcat-app1:2019-08-02_11_02_30
#command: ["/apps/tomcat/bin/run_tomcat.sh"]
#imagePullPolicy: IfNotPresent
imagePullPolicy: Always
ports:
- containerPort: 8080
protocol: TCP
name: http
#env:
#- name: "password"
# value: "123456"
#- name: "age"
# value: "18"
#resources:
# limits:
# cpu: 4
# memory: 4Gi
# requests:
# cpu: 2
# memory: 4Gi
volumeMounts:
- name: linux36-images
mountPath: /data/tomcat/webapps/myapp/images
readOnly: false
- name: linux36-static
mountPath: /data/tomcat/webapps/myapp/static
readOnly: false
volumes:
- name: linux36-images
nfs:
server: 192.168.7.108
path: /data/linux36/images
- name: linux36-static
nfs:
server: 192.168.7.108
path: /data/linux36/static
---
kind: Service
apiVersion: v1
metadata:
labels:
app: linux36-tomcat-app1-service-label
name: linux36-tomcat-app1-service
namespace: linux36
spec:
type: NodePort
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
nodePort: 30003
selector:
app: linux36-tomcat-app1-selector
执行更新tomcat app1业务容器yaml
访问nginx 业务pod
上传数据
http://192.168.7.110:30002/myapp/images/1.jpg
|