当前位置:网站首页>Using rancher to build kubernetes cluster
Using rancher to build kubernetes cluster
2022-06-29 10:07:00 【Jack_ Chen】
Use Rancher build Kubernetes colony
Rancher build Kubernetes colony
kubernetes There are many ways to deploy , The main known are kind、minikube、kubeadm、 Binary package 、Rancher
Official website :https://kubernetes.io/zh/
The Chinese community : https://www.kubernetes.org.cn/
Rancher summary
Rancher It is an open source enterprise container management platform . Use Rancher, There is no need to build a container service platform from scratch .
Rancher Provides management for use in production environments Docker and Kubernetes Full stack container deployment and management platform .
Rancher yes K8S Visual management tools ,Rancher Built in K8S, No additional installation is required , Use it directly .

Rancher Installation
Pull the mirror image
docker pull rancher/rancher:v2.5.12
Start the container
docker run -p 80:80 -p 443:443 --name rancher --restart=unless-stopped --privileged -d rancher/rancher:v2.5.12
Be careful :Rancher 2.5.x And later , Need to add –privileged Flag variable , Enable privileged mode installation
Rancher Initial configuration and overview of
visit Rancher The home page of , The first time you need to set up an administrator (admin) Account and password

Set up Rancher Of Server URL, other Node Can be accessed to the address 
Get into Rancher home page , Installed by default k3s The cluster of 
Click the cluster name to view the cluster status information 
Click the dashboard button , View the various features of the cluster Dashboard
Rancher Application deployment
Mode one


Fill in the form information for deployment 
Import YAML File deployment 

Mode two
1. establish Deployment object
find Deployments->Create-> Edit as YAML

Fill in deployment.yaml Information 
2. establish Service Expose port information 

Fill in service.yaml Information 
Rancher Deploy MySQL application
establish Deployment
# API edition
apiVersion: apps/v1
# API object type
kind: Deployment
metadata:
# Appoint Deployment The name of
name: mysql-deployment
# Appoint Deployment Space , Otherwise, it cannot be created
namespace: default
# Appoint Deployment The label of
labels:
app: mysql
spec:
# Specify the created Pod Copy number
replicas: 1
# Define how to find the Pod
selector:
# Manage tags app by mysql Of Pod
matchLabels:
app: mysql
# Specify create Pod The template of
template:
metadata:
# to Pod In the play app:mysql label
labels:
app: mysql
# Pod Template specification for
spec:
containers:
- name: mysql
# Specify the container image
image: mysql:5.7
# Specify the open port
ports:
- containerPort: 3306
# Set the environment variable
env:
- name: MYSQL_ROOT_PASSWORD
value: root123456
# Using a storage volume
volumeMounts:
# Mount the storage volume to the internal path of the container
- mountPath: /var/log/mysql
name: log-volume
- mountPath: /var/lib/mysql
name: data-volume
- mountPath: /etc/mysql
name: conf-volume
# Define storage volumes
volumes:
- name: log-volume
# hostPath Type the path of the storage volume on the host
hostPath:
path: /usr/local/program/mysql/log
# Create when directory doesn't exist
type: DirectoryOrCreate
- name: data-volume
hostPath:
path: /usr/local/program/mysql/data
type: DirectoryOrCreate
- name: conf-volume
hostPath:
path: /usr/local/program/mysql/conf
type: DirectoryOrCreate


establish Service
apiVersion: v1
kind: Service
metadata:
# Defining space
namespace: default
# Define service name , other Pod It can be accessed as a domain name through the service name
name: mysql-service
spec:
# Specify service type , adopt Node Static port exposure service on
type: NodePort
# Manage tags app by mysql Of Pod
selector:
app: mysql
ports:
- name: http
protocol: TCP
port: 3307
targetPort: 3306
# Node Static port on
nodePort: 30303
Access test
obtain Rancher Containers IP Address
[[email protected] ~]# docker inspect rancher |grep IPAddress
"SecondaryIPAddresses": null,
"IPAddress": "172.17.0.2",
"IPAddress": "172.17.0.2",
Connect to database
[[email protected] ~]# mysql -h 172.17.0.2 -P 30303 -uroot -proot123456
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.7.37 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
Rancher Deploy SpringBoot application
establish Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
namespace: default
name: jar-deployment
labels:
app: jar-test
spec:
replicas: 1
selector:
matchLabels:
app: jar-test
template:
metadata:
labels:
app: jar-test
spec:
containers:
- name: jar-name
# Appoint Docker Hub Image address in
image: IP/jar-test:0.0.1-SNAPSHOT
ports:
- containerPort: 8080
env:
# Specify the database connection address
- name: spring.datasource.url
value: jdbc:mysql://mysql-service:3307/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
- name: logging.path
value: /var/logs
volumeMounts:
- mountPath: /var/logs
name: log-volume
volumes:
- name: log-volume
hostPath:
path: /usr/local/program/app/logs
type: DirectoryOrCreate
establish Service
apiVersion: v1
kind: Service
metadata:
namespace: default
name: jar-service
spec:
type: NodePort
selector:
app: jar-test
ports:
- name: http
protocol: TCP
port: 8080
targetPort: 8080
# Node Static port on
nodePort: 30001
Access test
curl http://172.17.0.2:30001index.html
Add clusters



stay master The node runs the command import rancher

Kubernetes Basic operation
Application deployment
Create a name nginx-test Of Deployment, Also specify the application image
kubectl create deployment nginx-test --image=nginx
View all Deployment:kubectl get deployments
NAME READY UP-TO-DATE AVAILABLE AGE
nginx-test 1/1 1 1 18s
Delete deployment Application
kubectl delete deployment nginx-test
View application information
View all Pod The state of :kubectl get pods
NAME READY STATUS RESTARTS AGE
nginx-test-84b478f9c5-vz7bw 1/1 Running 0 29s
see Pod Detailed state :kubectl describe pods
Name: nginx-test-84b478f9c5-vz7bw
Namespace: default
Priority: 0
Node: minikube/192.168.49.2
Start Time: Tue, 22 Mar 2022 09:03:09 +0800
Labels: app=nginx-test
pod-template-hash=84b478f9c5
Annotations: <none>
Status: Running
IP: 172.17.0.3
IPs:
IP: 172.17.0.3
Controlled By: ReplicaSet/nginx-test-84b478f9c5
Containers:
nginx:
Container ID: docker://8f20af263a8c7dce564fa6d49943fbef4fe151aaaef24e3564e57e13787c7213
Image: nginx
Image ID: docker-pullable://[email protected]:e1211ac17b29b585ed1aee166a17fad63d344bc973bc63849d74c6452d549b3e
Port: <none>
Host Port: <none>
State: Running
Started: Tue, 22 Mar 2022 09:03:13 +0800
Ready: True
Restart Count: 0
Environment: <none>
Mounts:
/var/run/secrets/kubernetes.io/serviceaccount from kube-api-access-2hldl (ro)
Conditions:
Type Status
Initialized True
Ready True
ContainersReady True
PodScheduled True
Volumes:
kube-api-access-2hldl:
Type: Projected (a volume that contains injected data from multiple sources)
TokenExpirationSeconds: 3607
ConfigMapName: kube-root-ca.crt
ConfigMapOptional: <nil>
DownwardAPI: true
QoS Class: BestEffort
Node-Selectors: <none>
Tolerations: node.kubernetes.io/not-ready:NoExecute op=Exists for 300s
node.kubernetes.io/unreachable:NoExecute op=Exists for 300s
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal Scheduled 39s default-scheduler Successfully assigned default/nginx-test-84b478f9c5-vz7bw to minikube
Normal Pulling 39s kubelet Pulling image "nginx"
Normal Pulled 36s kubelet Successfully pulled image "nginx" in 2.952573727s
Normal Created 35s kubelet Created container nginx
Normal Started 35s kubelet Started container nginx
take Pod Set the name to the environment variable , Easy to use $POD_NAME To apply Pod The name of
export NGINX_POD=nginx-test-84b478f9c5-vz7bw
see Pod Printed logs :kubectl logs $NGINX_POD
/docker-entrypoint.sh: /docker-entrypoint.d/ is not empty, will attempt to perform configuration
/docker-entrypoint.sh: Looking for shell scripts in /docker-entrypoint.d/
/docker-entrypoint.sh: Launching /docker-entrypoint.d/10-listen-on-ipv6-by-default.sh
10-listen-on-ipv6-by-default.sh: info: Getting the checksum of /etc/nginx/conf.d/default.conf
10-listen-on-ipv6-by-default.sh: info: Enabled listen on IPv6 in /etc/nginx/conf.d/default.conf
/docker-entrypoint.sh: Launching /docker-entrypoint.d/20-envsubst-on-templates.sh
/docker-entrypoint.sh: Launching /docker-entrypoint.d/30-tune-worker-processes.sh
/docker-entrypoint.sh: Configuration complete; ready for start up
2022/03/22 01:03:13 [notice] 1#1: using the "epoll" event method
2022/03/22 01:03:13 [notice] 1#1: nginx/1.21.6
2022/03/22 01:03:13 [notice] 1#1: built by gcc 10.2.1 20210110 (Debian 10.2.1-6)
2022/03/22 01:03:13 [notice] 1#1: OS: Linux 3.10.0-1160.59.1.el7.x86_64
2022/03/22 01:03:13 [notice] 1#1: getrlimit(RLIMIT_NOFILE): 1048576:1048576
2022/03/22 01:03:13 [notice] 1#1: start worker processes
2022/03/22 01:03:13 [notice] 1#1: start worker process 32
2022/03/22 01:03:13 [notice] 1#1: start worker process 33
Use exec stay Pod Execute commands in the container of
kubectl exec nginx-test-84b478f9c5-vz7bw -- echo hello world
Go inside the container and execute bash command , Exit container exit command
kubectl exec -it nginx-test-84b478f9c5-vz7bw -- bash
Public exposure applications
Default Pod Cannot be accessed outside the cluster , Need to create Service And expose the port to be accessed externally .
establish Service expose nginx-test This Deployment, adopt NodePort Property gets exposed to the external port
kubectl expose deployment nginx-test --type=NodePort --port 80
View all Service The state of :kubectl get services
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16h
nginx-test NodePort 10.101.176.18 <none> 80:32299/TCP 77s
see Service Details of
kubectl describe services nginx-test
Access the service :IP:32299
Name: nginx-test
Namespace: default
Labels: app=nginx-test
Annotations: <none>
Selector: app=nginx-test
Type: NodePort
IP Family Policy: SingleStack
IP Families: IPv4
IP: 10.101.176.18
IPs: 10.101.176.18
Port: <unset> 80/TCP
TargetPort: 80/TCP
NodePort: <unset> 32299/TCP
Endpoints: 172.17.0.3:80
Session Affinity: None
External Traffic Policy: Cluster
Events: <none>
Use of labels
By adding Label, It's easy to manage resources
see Deployment Included in Label:kubectl describe deployment
Name: nginx-test
Namespace: default
CreationTimestamp: Tue, 22 Mar 2022 09:03:09 +0800
Labels: app=nginx-test
Annotations: deployment.kubernetes.io/revision: 1
Selector: app=nginx-test
Replicas: 1 desired | 1 updated | 1 total | 1 available | 0 unavailable
StrategyType: RollingUpdate
MinReadySeconds: 0
RollingUpdateStrategy: 25% max unavailable, 25% max surge
Pod Template:
Labels: app=nginx-test
Containers:
nginx:
Image: nginx
Port: <none>
Host Port: <none>
Environment: <none>
Mounts: <none>
Volumes: <none>
Conditions:
Type Status Reason
---- ------ ------
Available True MinimumReplicasAvailable
Progressing True NewReplicaSetAvailable
OldReplicaSets: <none>
NewReplicaSet: nginx-test-84b478f9c5 (1/1 replicas created)
Events:
Type Reason Age From Message
---- ------ ---- ---- -------
Normal ScalingReplicaSet 32m deployment-controller Scaled up replica set nginx-test-84b478f9c5 to 1
adopt Label Inquire about Pod:kubectl get pods -l app=nginx-test
NAME READY STATUS RESTARTS AGE
nginx-test-84b478f9c5-vz7bw 1/1 Running 0 34m
adopt Label Inquire about Service:kubectl get services -l app=nginx-test
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
nginx-test NodePort 10.101.176.18 <none> 80:32299/TCP 16m
to Pod add to Label:kubectl label pod nginx-test-84b478f9c5-vz7bw env_role=dev
see Pod Details of :kubectl describe pods nginx-test-84b478f9c5-vz7bw
Name: nginx-test-84b478f9c5-vz7bw
Namespace: default
Priority: 0
Node: minikube/192.168.49.2
Start Time: Tue, 22 Mar 2022 09:03:09 +0800
Labels: app=nginx-test
env_role=dev
adopt Label Delete service :kubectl delete service -l app=nginx-test
service "nginx-test" deleted
kubectl get service
NAME TYPE CLUSTER-IP EXTERNAL-IP PORT(S) AGE
kubernetes ClusterIP 10.96.0.1 <none> 443/TCP 16h
边栏推荐
- Flutter 基础组件之 Container
- FreeRTOS(九)——队列
- Mysql5.7 installation tutorial in centos7 under Linux
- Constructing SQL statements by sprintf() function in C language
- Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages
- gcc与makefile
- Application of decorator mode, packaging ServletRequest and adding addparameter method
- ImageView picture fill problem
- Rikka with Cake(线段树+线段树)
- 我想知道如何免费网上注册股票开户?另外,手机开户安全么?
猜你喜欢
随机推荐
Sublime Text3 set to run your own makefile
Wechat applet realizes store function
2019-11-10训练总结
Minorgc, majorgc, fullgc
2019.10.23训练总结
详细分析PBot挖矿病毒家族行为和所利用漏洞原理,提供蓝军详细防护建议
2020-09-23左右值 右值引用 std::move()
【51nod 1215】数组的宽度
JVM instructions for four call methods
Wechat applet implements the data listener watch, including the watch that destroys the watch and sub attributes
Gross Tumor Volume Segmentation for Head and Neck Cancer Radiotherapy using Deep Dense Multi-modalit
JVM之虚拟机栈之动态链接
RecyclerView 通用适配器封装
点在多边形内外的判断
C # judge whether the array contains any items of another array
PHP内存马技术研究与查杀方法总结
sympy的dsolve函数
Zabbix4.4 configure the indicators of the monitoring server and solve the garbled graphics pages
HDU 6778 Car (分组枚举-->状压 dp)
聊聊你理解的线程与并发









