svc的优缺点:
优点:
(1)基于标签动态发现后端Pod服务的变化,为服务提供统一的访问入口;
(2)底层基于kube-proxy实现负载均衡;
缺点:
(1)只能识别四层的协议,而无法解析七层协议。
- 传输层: IP
- 应用层: 协议: http,https
(2)对于K8S外部客户端访问时,需要指定NodePort类型,每个worker都要监听端口,费端口;
ingress控制器概述:
ingress:
k8s中的抽象资源,给管理员提供暴露服务的入口定义方法,换句话说,就是编写规则。
Ingress Server:
根据ingress生成具体路由规则,并借助svc实现Pod的负载均衡。
部署traefik第三方组件:
[root@k8s151.oldboyedu.com traefik]# cat 01-rabc.yaml
apiVersion: v1
kind: ServiceAccount
metadata:
name: traefik-ingress-controller
namespace: kube-system
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: traefik-ingress-controller
rules:
- apiGroups:
- ""
resources:
- services
- endpoints
- secrets
verbs:
- get
- list
- watch
- apiGroups:
- extensions
resources:
- ingresses
verbs:
- get
- list
- watch
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
name: traefik-ingress-controller
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: traefik-ingress-controller
subjects:
- kind: ServiceAccount
name: traefik-ingress-controller
namespace: kube-system
[root@k8s151.oldboyedu.com traefik]#
[root@k8s151.oldboyedu.com traefik]# cat 02-traefik.yaml
kind: DaemonSet
apiVersion: extensions/v1beta1
metadata:
name: traefik-ingress-controller
namespace: kube-system
labels:
k8s-app: traefik-ingress-lb
spec:
selector:
matchLabels:
k8s-app: traefik-ingress-lb
template:
metadata:
labels:
k8s-app: traefik-ingress-lb
name: traefik-ingress-lb
spec:
tolerations:
- operator: Exists
serviceAccountName: traefik-ingress-controller
hostNetwork: true
containers:
- image: k8s151.oldboyedu.com:5000/ingress-server/traefik:v1.7.2
imagePullPolicy: IfNotPresent
name: traefik-ingress-lb
ports:
- name: http
containerPort: 80
- name: admin
containerPort: 8080
args:
- --api
- --kubernetes
- --logLevel=INFO
[root@k8s151.oldboyedu.com traefik]#
配置Ingress规则暴露tomcat服务:
[root@k8s151.oldboyedu.com tomcat]# cat 01-deploy-tomcat.yaml
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: mysql
spec:
replicas: 1
template:
metadata:
labels:
app: oldboyedu-mysql
spec:
tolerations:
- operator: Exists
containers:
- name: mysql
image: k8s151.oldboyedu.com:5000/oldboyedu-db/mysql:5.7
ports:
- containerPort: 3306
env:
- name: MYSQL_ROOT_PASSWORD
value: '123456'
---
apiVersion: v1
kind: Service
metadata:
name: oldboyedu-mysql
spec:
selector:
app: oldboyedu-mysql
ports:
- port: 3306
targetPort: 3306
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: oldboyedu-tomcat-app
spec:
replicas: 3
template:
metadata:
labels:
app: oldboyedu-tomcat-app
spec:
tolerations:
- operator: Exists
containers:
- name: myweb
# image: jasonyin2020/tomcat-app:v1
image: k8s151.oldboyedu.com:5000/oldboyedu-tomcat/tomcat-app:v1
resources:
limits:
cpu: "100m"
requests:
cpu: "100m"
ports:
- containerPort: 8080
env:
- name: MYSQL_SERVICE_HOST
value: oldboyedu-mysql
- name: MYSQL_SERVICE_PORT
value: '3306'
---
apiVersion: v1
kind: Service
metadata:
name: oldboyedu-tomcat-app
spec:
selector:
app: oldboyedu-tomcat-app
ports:
- port: 8080
targetPort: 8080
[root@k8s151.oldboyedu.com tomcat]#
[root@k8s151.oldboyedu.com tomcat]#
[root@k8s151.oldboyedu.com tomcat]# cat 02-ing-tomcat.yaml
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: oldboyedu-linux82-tomcat
spec:
# 定义访问规则
rules:
# 定义访问的主机名
- host: tomcat.oldboyedu.com
# 指定Ingress后端的服务,即指定svc相关信息
backend:
# svc的名称
serviceName: oldboyedu-tomcat-app
# svc的端口
servicePort: 8080
[root@k8s151.oldboyedu.com tomcat]#
解决WordPress无法正常登录的情况:
kubectl cp oldboyedu-linux82-wordpress-786c8cb647-pqgdg:/var/www/html/wp-includes/pluggable.php ./pluggable.php # 连接的是WordPress的Pod!
vim pluggable.php
...
# 大概是在"1003"行!
/*setcookie( $auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true );*/
setcookie( $auth_cookie_name, $auth_cookie, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, $secure, true );
kubectl cp pluggable.php oldboyedu-linux82-wordpress-786c8cb647-pqgdg:/var/www/html/wp-includes/pluggable.php
traefik ingress https配置:
配置文件:
见资源清单,此处略。
测试数据:
cd /usr/share/nginx/html/
echo "<h1 style='color:red;'>oldboyedu linux82<h1>" > index.html
echo "<h1 style='color:green;'>oldboyedu linux82<h1>" > index.html
echo "<h1 style='color:pink;'>oldboyedu linux82<h1>" > index.html
什么是helm:
helm是k8s资源清单的管理工具,它就像Linux下的包管理器,比如centos的yum,ubuntu的apt。
helm有以下几个术语:
helm:
命令行工具,主要用于k8s的chart的创建,打包,发布和管理。
chart:
应用描述,一系列用于描述k8s资源相关文件的集合。
release:
基于chart的部署实体,一个chart被helm运行后会生成一个release实体。
这个release实体会在k8s集群中创建对应的资源对象。
部署helm组件:
(1)解压软件包
tar xf helm-v3.9.4-linux-amd64.tar.gz
(2)将软件包拷贝到PATH
mv linux-amd64/helm /usr/local/sbin/
(3)清理软件包
rm -rf linux-amd64/
(4)验证helm是否安装成功,如上图所示。
helm -h
(5)可用命令(Available Commands)概述
completion:
生成命令补全的功能。使用"source <(helm completion bash)"
create:
创建一个chart并指定名称。
dependency:
管理chart依赖关系。
env:
查看当前客户端的helm环境变量信息。
get:
下载指定版本的扩展信息。
help:
查看帮助信息。
history:
获取发布历史记录。
install:
安装chart。
lint:
检查chart中可能出现的问题。
list:
列出releases信息。
package:
将chart目录打包到chart存档文件中。
plugin:
安装、列出或卸载Helm插件。
pull:
从存储库下载chart并将其解包到本地目录。
repo:
添加、列出、删除、更新和索引chart存储库。
rollback:
将版本回滚到以前的版本。
search:
在chart中搜索关键字。
show:
显示chart详细信息。
status:
显示已有的"RELEASE_NAME"状态。
template:
本地渲染模板。
test:
运行版本测试。
uninstall:
卸载版本。
upgrade:
升级版本。
verify:
验证给定路径上的chart是否已签名且有效
version:
查看客户端版本。
管理Chart生命周期初体验:
1.创建一个名为"oldboyedu-linux82"的Chart
helm create oldboyedu-linux82
2.将名为"oldboyedu-linux82"的Chart部署为一个名为"oldboyedu-linux82-web01"的Release实体,会在K8创建相应的资源。
helm install oldboyedu-linux82-web01 oldboyedu-linux82
3.查看所有的Release版本
helm list
4.卸载名为"oldboyedu-linux82-web01"的Release实体卸载,会在K8S删除相应的资源。
helm uninstall oldboyedu-linux82-web01
5.升级案例
推送测试镜像:
docker tag huangjinkuanggong:v0.2 k8s151.oldboyedu.com:5000/oldboyedu-helm-games/huangjinkuanggong-80:v0.1
docker push k8s151.oldboyedu.com:5000/oldboyedu-helm-games/huangjinkuanggong-80:v0.1
基于命令行的方式进行升级
helm upgrade --set replicas=3,oldboyedu.wordpress.image=k8s151.oldboyedu.com:5000/oldboyedu-helm-games/huangjinkuanggong-80,oldboyedu.wordpress.tag=v0.1 oldboyedu-wordpress oldboyedu-linux82
基于文件的方式进行升级(强烈推荐使用!)
[root@k8s151.oldboyedu.com helm]# cat oldboyedu-linux82/values.yaml
# replicas: 3
replicas: 10
oldboyedu:
wordpress:
# image: k8s151.oldboyedu.com:5000/oldboyedu-wordpress/wordpress
image: k8s151.oldboyedu.com:5000/oldboyedu-web/nginx
# tag: latest
tag: 1.18
db:
image: k8s151.oldboyedu.com:5000/oldboyedu-db/mysql
tag: "5.7"
replicas: 1
[root@k8s151.oldboyedu.com helm]#
[root@k8s151.oldboyedu.com helm]# helm upgrade -f oldboyedu-linux82/values.yaml oldboyedu-wordpress oldboyedu-linux82
5.回滚案例
查看某个Release的历史发布版本信息
helm history oldboyedu-wordpress
回滚到上一个版本:
helm rollback oldboyedu-wordpress
回滚到指定的版本:
helm rollback oldboyedu-wordpress 1
helm的公有仓库添加:
(1)查看现有的仓库信息,默认情况下是没有任何仓库地址的
helm repo list
(2)添加第三方仓库
helm repo add azure http://mirror.azure.cn/kubernetes/charts/
helm repo add oldboyedu-aliyun https://kubernetes.oss-cn-hangzhou.aliyuncs.com/charts
(3)我们也可以更新仓库信息哟~
helm repo update
(4)搜索我们关心的镜像
helm search repo mysql
helm search repo mysql
helm search repo elasticsearch
helm search repo nginx
helm search repo redis
(5)下载第三方的Chart
helm pull azure/mysql
helm的私有仓库
推荐阅读:
https://github.com/helm/chartmuseum
https://hub.docker.com/r/chartmuseum/chartmuseum
今日内容回顾:
- ingress *****
- http
- https
- helm ***
- chart
- upgrade
- rollback